-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
1288 lines (1101 loc) · 52.5 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# app.py
from flask import Flask, render_template, request, jsonify, redirect, url_for, flash, session
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
from flask_bcrypt import Bcrypt
import pyotp
import uuid
import yaml
from datetime import datetime, timedelta
import base64
import paho.mqtt.client as mqtt
import json
import requests
import time
import threading
import os
import logging
import qrcode
import io
import secrets
import pytz
import socket # For SIA TCP communication
import struct
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes # For SIA encryption
from cryptography.hazmat.backends import default_backend
from binascii import hexlify, unhexlify
# Record the system's start time
system_start_time = time.time()
# ------------------------------------------------------------------------------
# 1. Update Logging Configuration to Use a Relative Path (./logs) by Default
# ------------------------------------------------------------------------------
LOG_DIR = os.getenv("LOG_DIR", "./logs") # Use environment variable if set, otherwise ./logs
os.makedirs(LOG_DIR, exist_ok=True)
log_file = os.path.join(LOG_DIR, "application.log")
# Configure logging to write to both file and console
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[
logging.FileHandler(log_file),
logging.StreamHandler()
]
)
logger = logging.getLogger()
# ------------------------------------------------------------------------------
# Initialize Flask app
# ------------------------------------------------------------------------------
app = Flask(__name__)
app.secret_key = secrets.token_hex(32) # Generate a secure, random 32-byte hex key
# ------------------------------------------------------------------------------
# Global variables and configurations
# ------------------------------------------------------------------------------
mqtt_client_instance = None # Global variable to store the YoLink MQTT client instance
monitor_mqtt_client = None # Global variable to store the MQTT client instance for monitor.industrialcamera.com
temp_user_data = {} # Holds temporary data for users not yet verified
system_status = {'armed': False} # Track system status (armed/disarmed)
config_file = "config.yaml"
devices_file = "devices.yaml"
mappings_file = "mappings.yaml"
# Global config and users data
config_data = {}
users_db = {}
bcrypt = Bcrypt(app)
login_manager = LoginManager(app)
login_manager.login_view = 'login' # Redirect to login page if not logged in
# Global MQTT connection status variables
yolink_mqtt_status = {'connected': False}
monitor_mqtt_status = {'connected': False}
# ------------------------------------------------------------------------------
# Define a User class for Flask-Login
# ------------------------------------------------------------------------------
class User(UserMixin):
def __init__(self, username):
self.id = username # Flask-Login requires that the `id` property be set
# ------------------------------------------------------------------------------
# Implement the user_loader function
# ------------------------------------------------------------------------------
@login_manager.user_loader
def load_user(username):
if username in users_db:
return User(username)
return None
# ------------------------------------------------------------------------------
# Load & Save Configuration
# ------------------------------------------------------------------------------
def load_config():
global config_data, users_db
if os.path.exists(config_file):
with open(config_file, 'r') as file:
config_data = yaml.safe_load(file)
else:
config_data = {}
# Provide default MQTT configuration if not present
if 'mqtt' not in config_data:
config_data['mqtt'] = {
'url': 'mqtt://api.yosmart.com',
'port': 8003,
'topic': 'yl-home/${Home ID}/+/report'
}
# Provide default monitor MQTT configuration if not present
if 'mqtt_monitor' not in config_data:
config_data['mqtt_monitor'] = {
'url': os.getenv('MONITOR_MQTT_URL', 'mqtt://monitor.industrialcamera.com'),
'port': int(os.getenv('MONITOR_MQTT_PORT', 1883)),
'username': os.getenv('MONITOR_MQTT_USERNAME', ''),
'password': os.getenv('MONITOR_MQTT_PASSWORD', ''),
'client_id': os.getenv('MONITOR_MQTT_CLIENT_ID', 'monitor_client_id')
}
# Load users from config.yaml (if present)
users_db = config_data.get('users', {})
return config_data
def save_config(data):
global config_data
config_data.update(data)
with open(config_file, 'w') as file:
yaml.dump(config_data, file)
def load_yaml(file_path):
if os.path.exists(file_path):
with open(file_path, 'r') as yaml_file:
return yaml.safe_load(yaml_file)
return {}
def save_to_yaml(file_path, data):
with open(file_path, 'w') as yaml_file:
yaml.dump(data, yaml_file)
# ------------------------------------------------------------------------------
# Helper function to get the monitor API key (deprecated if using MQTT)
# ------------------------------------------------------------------------------
def get_monitor_api_key():
return os.getenv('MONITOR_API_KEY') or config_data.get('monitor', {}).get('api_key')
monitor_api_key = get_monitor_api_key()
def send_data_to_monitor(data):
# Deprecated if using MQTT
pass
# ------------------------------------------------------------------------------
# Example function to send Home info via MQTT
# ------------------------------------------------------------------------------
def send_home_info_via_mqtt():
global mqtt_client_instance
home_id = config_data.get("home_id")
uaid = config_data.get("yolink", {}).get("uaid")
secret_key = config_data.get("yolink", {}).get("secret_key")
if not home_id or not uaid or not secret_key:
logger.debug("Skipping send_home_info_via_mqtt(): missing home_id or YoLink credentials.")
return
payload = {
"home_id": home_id,
"uaid": uaid,
"secret_key": secret_key
}
topic = f"homes/{home_id}/info"
try:
mqtt_client_instance.publish(topic, json.dumps(payload), retain=True)
logger.info(f"Sent home info to topic {topic}")
except Exception as e:
logger.error(f"Error sending home info via MQTT: {str(e)}")
# ------------------------------------------------------------------------------
# YoLink Token Management
# ------------------------------------------------------------------------------
def is_token_expired():
yolink_data = config_data.get('yolink', {})
expiry_time = yolink_data.get('token_expiry', 0)
current_time = time.time()
return current_time >= expiry_time
def generate_yolink_token(uaid, secret_key):
url = "https://api.yosmart.com/open/yolink/token"
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
data = {"grant_type": "client_credentials", "client_id": uaid, "client_secret": secret_key}
try:
response = requests.post(url, headers=headers, data=data)
if response.status_code == 200:
token_data = response.json()
token = token_data.get("access_token")
expires_in = token_data.get("expires_in")
if token:
expiry_time = time.time() + expires_in - 60 # Subtract 60 seconds for early refresh
config_data['yolink']['token'] = token
config_data['yolink']['token_expiry'] = expiry_time
save_config(config_data)
return token
else:
logger.error("Failed to obtain YoLink token. Check UAID and Secret Key.")
else:
logger.error(f"Failed to generate YoLink token. Status code: {response.status_code}, Response: {response.text}")
except Exception as e:
logger.error(f"Error generating YoLink token: {str(e)}")
return None
def handle_token_expiry():
token = generate_yolink_token(config_data['yolink']['uaid'], config_data['yolink']['secret_key'])
if token:
config_data['yolink']['token'] = token
save_config(config_data)
return token
else:
logger.error("Failed to generate a new YoLink token.")
return None
def force_generate_token_and_client():
config = load_config()
if is_token_expired():
token = generate_yolink_token(config['yolink']['uaid'], config['yolink']['secret_key'])
if not token:
logger.error("Failed to obtain a valid YoLink token. MQTT client will not start.")
return None, None
else:
token = config['yolink']['token']
client_id = str(uuid.uuid4())
return token, client_id
# ------------------------------------------------------------------------------
# Timezone Conversion
# ------------------------------------------------------------------------------
def convert_to_timezone(timestamp):
timezone_name = config_data.get('timezone', 'UTC')
try:
target_timezone = pytz.timezone(timezone_name)
except pytz.UnknownTimeZoneError:
logger.error(f"Unknown timezone '{timezone_name}' specified in config. Defaulting to UTC.")
target_timezone = pytz.UTC
utc_time = datetime.fromtimestamp(timestamp, pytz.UTC)
return utc_time.astimezone(target_timezone)
# ------------------------------------------------------------------------------
# Device Data Updates
# ------------------------------------------------------------------------------
def update_device_data(device_id, payload):
logger.info(f"Updating device data for Device ID: {device_id}")
timezone_name = config_data.get('timezone', 'UTC')
devices_data = load_yaml(devices_file) or {'devices': []}
now = datetime.now(pytz.timezone(timezone_name)).strftime('%Y-%m-%dT%H:%M:%S')
device_found = False
for device in devices_data.get('devices', []):
if device['deviceId'] == device_id:
device_found = True
device['state'] = payload['data'].get('state', device.get('state', 'unknown'))
device['battery'] = payload['data'].get('battery', device.get('battery', 'unknown'))
# Temperature / Humidity
if 'temperature' in payload['data']:
temperature = payload['data'].get('temperature')
mode = payload['data'].get('mode', 'c')
if temperature is not None:
if mode == 'c':
device['temperature'] = celsius_to_fahrenheit(temperature)
else:
device['temperature'] = temperature
else:
device['temperature'] = 'unknown'
if 'humidity' in payload['data']:
device['humidity'] = payload['data'].get('humidity', device.get('humidity', 'unknown'))
else:
device['humidity'] = 'unknown'
device['tempLimit'] = payload['data'].get('tempLimit', device.get('tempLimit', {'max': None, 'min': None}))
device['humidityLimit'] = payload['data'].get('humidityLimit', device.get('humidityLimit', {'max': None, 'min': None}))
# Alarms
if 'alarm' in payload['data']:
alarm_data = payload['data'].get('alarm', {})
device['alarm'] = {
'lowBattery': alarm_data.get('lowBattery', False),
'lowTemp': alarm_data.get('lowTemp', False),
'highTemp': alarm_data.get('highTemp', False),
'lowHumidity': alarm_data.get('lowHumidity', False),
'highHumidity': alarm_data.get('highHumidity', False),
}
else:
device['alarm'] = {
'lowBattery': False,
'lowTemp': False,
'highTemp': False,
'lowHumidity': False,
'highHumidity': False,
}
# Signal Strength
lora_info = payload['data'].get('loraInfo', {})
if 'signal' in lora_info:
device['signal'] = lora_info.get('signal')
else:
device['signal'] = 'unknown'
# Last seen
device['last_seen'] = now
# Prepare the data to send to the monitoring server
device_data = {
"home_id": config_data.get('home_id', 'UNKNOWN_HOME_ID'),
"device_id": device_id,
"sensor_data": {
"temperature": device.get('temperature'),
"humidity": device.get('humidity'),
"state": device.get('state'),
"last_seen": device['last_seen'],
"signal": device.get('signal'),
},
"configuration": config_data,
"devices": devices_data,
}
# Publish device data to monitor MQTT broker
trigger_monitor_event(device_id, "Device Data Updated", device_data)
break
if not device_found:
logger.warning(f"Device {device_id} not found in devices.yaml.")
save_to_yaml(devices_file, devices_data)
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
# ------------------------------------------------------------------------------
# YoLink API Class
# ------------------------------------------------------------------------------
class YoLinkAPI:
def __init__(self, token):
self.base_url = "https://api.yosmart.com/open/yolink/v2/api"
self.token = token
def get_home_info(self):
url = self.base_url
headers = {
'Content-Type': 'application/json',
'Authorization': f"Bearer {self.token}"
}
data = {
"method": "Home.getGeneralInfo",
"time": int(time.time() * 1000)
}
try:
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
return response.json()
else:
logger.error(f"Failed to get home info. Status code: {response.status_code}, Response: {response.text}")
return None
except Exception as e:
logger.error(f"Error retrieving home info: {str(e)}")
return None
def get_device_list(self):
url = self.base_url
headers = {
'Content-Type': 'application/json',
'Authorization': f"Bearer {self.token}"
}
data = {
"method": "Home.getDeviceList",
"time": int(time.time() * 1000)
}
try:
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
return response.json()
else:
logger.error(f"Failed to get device list. Status code: {response.status_code}, Response: {response.text}")
return None
except Exception as e:
logger.error(f"Error retrieving device list: {str(e)}")
return None
# ------------------------------------------------------------------------------
# Authentication Routes
# ------------------------------------------------------------------------------
@app.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
logger.info("User is already authenticated; redirecting to index.")
return redirect(url_for('index'))
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
totp_code = request.form.get('totp_code')
# TOTP step
if session.get('password_verified') == username:
user = users_db.get(username)
if user and 'totp_secret' in user:
totp = pyotp.TOTP(user['totp_secret'])
if totp.verify(totp_code):
login_user(User(username), remember=True)
session.pop('password_verified', None)
next_page = request.args.get('next')
return redirect(next_page or url_for('index'))
else:
flash('Invalid TOTP code. Please try again.')
return render_template('login.html', totp_required=True, username=username)
# Initial login with username/password
if username and username in users_db:
user = users_db[username]
if bcrypt.check_password_hash(user['password'], password):
session['password_verified'] = username
return render_template('login.html', totp_required=True, username=username)
else:
flash('Invalid username or password.')
else:
flash('User does not exist. Please create a new user.')
return render_template('login.html', totp_required=False)
@app.route('/logout')
@login_required
def logout():
logout_user()
session.pop('password_verified', None)
return redirect(url_for('login'))
@app.route('/setup_totp/<username>', methods=['GET', 'POST'])
def setup_totp(username):
if request.method == 'POST':
totp_code = request.form['totp_code']
user = temp_user_data.get(username)
if user:
totp = pyotp.TOTP(user['totp_secret'])
if totp.verify(totp_code):
users_db[username] = user
config_data['users'] = users_db
save_config(config_data)
temp_user_data.pop(username, None)
flash('TOTP setup complete. You can now log in.')
return redirect(url_for('login'))
else:
flash('Invalid TOTP code. Please try again.')
return redirect(url_for('setup_totp', username=username))
# Generate the QR code for TOTP
if username not in users_db and username in temp_user_data:
totp_secret = temp_user_data[username]['totp_secret']
otp_uri = pyotp.TOTP(totp_secret).provisioning_uri(username, issuer_name="YoLink-Monitor")
qr = qrcode.make(otp_uri)
img_io = io.BytesIO()
qr.save(img_io, 'PNG')
img_io.seek(0)
qr_base64 = base64.b64encode(img_io.getvalue()).decode('utf-8')
return render_template('setup_totp.html', qr_code=qr_base64, totp_secret=totp_secret, username=username)
flash('User not found or already configured.')
return redirect(url_for('login'))
@app.route('/create_user', methods=['GET', 'POST'])
def create_user():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
if username in users_db or username in temp_user_data:
flash('User already exists. Please log in.')
return redirect(url_for('login'))
hashed_password = bcrypt.generate_password_hash(password).decode('utf-8')
totp_secret = pyotp.random_base32()
temp_user_data[username] = {
'password': hashed_password,
'totp_secret': totp_secret
}
return redirect(url_for('setup_totp', username=username))
return render_template('create_user.html')
# ------------------------------------------------------------------------------
# Configuration Routes
# ------------------------------------------------------------------------------
@app.route('/config')
@login_required
def config():
devices_data = load_devices()
mappings_data = load_mappings()
devices = devices_data.get('devices', [])
mappings = mappings_data.get('mappings', []) if mappings_data else []
device_mappings = {m['yolink_device_id']: m for m in mappings}
config_data = load_config()
return render_template('config.html', devices=devices, mappings=device_mappings, config=config_data)
@app.route('/save_config', methods=['POST'])
def save_config_route():
try:
new_config_data = request.get_json()
if not new_config_data:
return jsonify({"status": "error", "message": "Invalid or empty configuration data."}), 400
logger.debug(f"Received configuration data: {new_config_data}")
current_config = load_config()
current_config.update(new_config_data)
save_config(current_config)
return jsonify({"status": "success", "message": "Configuration saved successfully."})
except Exception as e:
logger.error(f"Error saving configuration: {str(e)}", exc_info=True)
return jsonify({"status": "error", "message": "Internal Server Error"}), 500
@app.route('/save_mapping', methods=['POST'])
def save_mapping():
try:
new_mappings = request.get_json()
if not new_mappings or 'mappings' not in new_mappings:
logger.error(f"Invalid or empty mappings received: {new_mappings}")
return jsonify({"status": "error", "message": "Invalid mappings data."}), 400
logger.debug(f"Received new mappings: {new_mappings}")
existing_mappings = load_yaml(mappings_file) or {'mappings': []}
for new_mapping in new_mappings['mappings']:
device_id = new_mapping.get('yolink_device_id')
existing_mapping = next((m for m in existing_mappings['mappings'] if m['yolink_device_id'] == device_id), None)
if existing_mapping:
existing_mapping.update(new_mapping)
else:
existing_mappings['mappings'].append(new_mapping)
save_to_yaml(mappings_file, existing_mappings)
logger.debug(f"Updated mappings: {existing_mappings}")
return jsonify({"status": "success", "message": "Mapping saved successfully."})
except Exception as e:
logger.error(f"Error in save_mapping: {str(e)}", exc_info=True)
return jsonify({"status": "error", "message": "Internal Server Error"}), 500
@app.route('/refresh_yolink_devices', methods=['GET'])
def refresh_yolink_devices():
config = load_config()
token = config['yolink'].get('token')
if not token:
return jsonify({"status": "error", "message": "No token available. Please generate a token first."})
yolink_api = YoLinkAPI(token)
home_info = yolink_api.get_home_info()
if not home_info or home_info.get("code") != "000000":
return jsonify({"status": "error", "message": f"Failed to retrieve home info: {home_info.get('desc', 'Unknown error')}"})
# ---------------------------------------------------------
# 1. Grab the home_id from the YoLink API response
# ---------------------------------------------------------
home_id = home_info["data"]["id"]
config['home_id'] = home_id # Store in config at top-level
save_config(config) # Persist to config.yaml
# Get the list of devices from YoLink
devices = yolink_api.get_device_list()
if not devices or devices.get("code") != "000000":
return jsonify({"status": "error", "message": f"Failed to retrieve devices: {devices.get('desc', 'Unknown error')}"})
existing_devices_data = load_yaml(devices_file) or {}
existing_devices = {d['deviceId']: d for d in existing_devices_data.get('devices', [])}
mappings_data = load_yaml(mappings_file) or {'mappings': []}
mappings_dict = {m['yolink_device_id']: m for m in mappings_data.get('mappings', [])}
new_devices = []
for device in devices["data"]["devices"]:
device_id = device["deviceId"]
device_name = device.get('name', f"Device {device_id[-4:]}")
signal_strength = device.get('loraInfo', {}).get('signal', 'unknown')
device_data = {
'deviceId': device_id,
'name': device_name,
'state': 'unknown',
'battery': 'unknown',
'temperature': 'unknown',
'humidity': 'unknown',
'tempLimit': {'max': None, 'min': None},
'humidityLimit': {'max': None, 'min': None},
'alarm': {
'lowBattery': False,
'lowTemp': False,
'highTemp': False,
'lowHumidity': False,
'highHumidity': False
},
'signal': signal_strength,
'last_seen': 'never'
}
# Merge existing data if we already have this device in devices.yaml
if device_id in existing_devices:
existing_device = existing_devices[device_id]
device_data.update({
'state': existing_device.get('state', 'unknown'),
'battery': existing_device.get('battery', 'unknown'),
'temperature': existing_device.get('temperature', 'unknown'),
'humidity': existing_device.get('humidity', 'unknown'),
'tempLimit': existing_device.get('tempLimit', {'max': None, 'min': None}),
'humidityLimit': existing_device.get('humidityLimit', {'max': None, 'min': None}),
'alarm': existing_device.get('alarm', device_data['alarm']),
'signal': existing_device.get('signal', signal_strength),
'last_seen': existing_device.get('last_seen', 'never')
})
# Update or create a mapping entry if needed
if device_id in mappings_dict:
mapping = mappings_dict[device_id]
mapping['sia_zone_description'] = device_name
mapping['sia_signal_strength'] = signal_strength
else:
mappings_data['mappings'].append({
'yolink_device_id': device_id,
'sia_zone': '',
'sia_zone_description': device_name,
'sia_signal_strength': signal_strength,
'chekt_zone': 'N/A'
})
# Also store the CHEKT zone for UI
device_data['chekt_zone'] = mappings_dict.get(device_id, {}).get('chekt_zone', 'N/A')
new_devices.append(device_data)
# Save the updated devices list (with home_id in "homes" block)
data_to_save = {"homes": {"id": home_id}, "devices": new_devices}
save_to_yaml(devices_file, data_to_save)
save_to_yaml(mappings_file, mappings_data)
# ---------------------------------------------------------
# Restart YoLink MQTT Client with the updated home_id
# ---------------------------------------------------------
if mqtt_client_instance:
mqtt_client_instance.disconnect()
mqtt_client_instance.loop_stop()
mqtt_thread = threading.Thread(target=run_mqtt_client)
mqtt_thread.daemon = True
mqtt_thread.start()
# ---------------------------------------------------------
# 2. After success, call send_home_info_via_mqtt()
# so it uses the newly updated home_id, uaid, secret_key.
# ---------------------------------------------------------
send_home_info_via_mqtt()
return jsonify({"status": "success", "message": "YoLink devices refreshed, MQTT client restarted, and home info published."})
@app.route('/get_logs', methods=['GET'])
def get_logs():
try:
with open(log_file, 'r') as lf:
logs = lf.read()
return jsonify({"status": "success", "logs": logs})
except FileNotFoundError:
return jsonify({"status": "error", "message": "Log file not found."})
@app.route('/get_sensor_data', methods=['GET'])
def get_sensor_data():
devices_data = load_devices()
mappings_data = load_mappings()
devices = devices_data.get('devices', [])
device_mappings = {m['yolink_device_id']: m for m in mappings_data.get('mappings', [])}
all_sensors = []
for sensor in devices:
device_id = sensor.get('deviceId')
mapping = device_mappings.get(device_id, {})
chekt_zone = mapping.get('chekt_zone', 'N/A')
last_seen = sensor.get('last_seen', 'Unknown')
all_sensors.append({
'deviceId': device_id,
'name': sensor.get('name', 'Unknown'),
'state': sensor.get('state', 'Unknown'),
'battery': sensor.get('battery', 'Unknown'),
'temperature': sensor.get('temperature', 'Unknown'),
'humidity': sensor.get('humidity', 'Unknown'),
'signal': sensor.get('signal', 'Unknown'),
'last_seen': last_seen,
'chekt_zone': chekt_zone
})
return jsonify({'devices': all_sensors})
def load_devices():
try:
with open(devices_file, 'r') as file:
return yaml.safe_load(file) or {}
except FileNotFoundError:
return {'devices': []}
def load_mappings():
try:
with open(mappings_file, 'r') as yaml_file:
mappings_data = yaml.safe_load(yaml_file)
logger.info(f"Loaded mappings data: {mappings_data}")
return mappings_data
except Exception as e:
logger.error(f"Error loading mappings: {str(e)}")
return {"mappings": [], "chekt_mappings": [], "sia_mappings": []}
@app.route('/config.html')
@login_required
def config_html():
devices_data = load_devices()
mappings_data = load_mappings()
devices = devices_data.get('devices', [])
device_mappings = {m['yolink_device_id']: m for m in mappings_data.get('mappings', [])}
config_data = load_config()
return render_template('config.html', devices=devices, mappings=device_mappings, config=config_data)
# ------------------------------------------------------------------------------
# Status Checks Routes
# ------------------------------------------------------------------------------
@app.route('/check_all_mqtt_status', methods=['GET'])
def check_all_mqtt_status():
"""
Returns JSON object with the connectivity status of both YoLink MQTT and Monitor MQTT.
Example response:
{
"yolink": "connected",
"monitor": "disconnected"
}
"""
return jsonify({
'yolink': 'connected' if yolink_mqtt_status['connected'] else 'disconnected',
'monitor': 'connected' if monitor_mqtt_status['connected'] else 'disconnected'
})
@app.route('/check_monitor_mqtt_status', methods=['GET'])
def check_monitor_mqtt_status():
global monitor_mqtt_client
try:
if monitor_mqtt_client and monitor_mqtt_client.is_connected():
return jsonify({"status": "success", "message": "Monitor MQTT connection is active."})
else:
return jsonify({"status": "error", "message": "Monitor MQTT connection is inactive."})
except Exception as e:
logger.error(f"Error checking monitor MQTT status: {str(e)}")
return jsonify({"status": "error", "message": "Error checking monitor MQTT status."})
@app.route('/check_mqtt_status', methods=['GET'])
def check_mqtt_status():
global mqtt_client_instance
try:
if mqtt_client_instance and mqtt_client_instance.is_connected():
return jsonify({"status": "success", "message": "MQTT connection is active."})
else:
return jsonify({"status": "error", "message": "MQTT connection is inactive."})
except Exception as e:
logger.error(f"Error checking MQTT status: {str(e)}")
return jsonify({"status": "error", "message": "Error checking MQTT status."})
@app.route('/system_uptime', methods=['GET'])
def system_uptime():
uptime_seconds = time.time() - system_start_time
return jsonify({"uptime_seconds": uptime_seconds})
# ------------------------------------------------------------------------------
# MQTT Configuration and Callbacks for YoLink
# ------------------------------------------------------------------------------
def on_connect(client, userdata, flags, rc):
if rc == 0:
logger.info(f"Successfully connected to YoLink MQTT broker. Subscribing to topic: {userdata['topic']}")
client.subscribe(userdata['topic'])
else:
logger.error(f"Failed to connect to YoLink MQTT broker. Return code: {rc}")
def on_disconnect(client, userdata, rc):
# This will be triggered whenever the MQTT client disconnects for any reason.
yolink_mqtt_status['connected'] = False
logger.warning("YoLink MQTT client disconnected from the broker.")
def on_message(client, userdata, msg):
logger.info(f"Received message on topic {msg.topic}")
try:
payload = json.loads(msg.payload.decode("utf-8"))
device_id = payload.get('deviceId')
state = payload['data'].get('state', 'Unknown state')
event_type = payload.get('event', 'Unknown event').lower()
if device_id:
logger.info(f"Device ID: {device_id}, State: {state}, Event Type: {event_type}")
update_device_data(device_id, payload)
if "alert" in event_type or state in ['open', 'closed', 'alert']:
device_type = parse_device_type(event_type, payload)
if device_type and should_trigger_event(state, device_type):
receiver_type = config_data.get("receiver_type", "CHEKT").upper()
if receiver_type not in ["CHEKT", "SIA"]:
receiver_type = "CHEKT"
mappings_data = load_mappings()
mapping = next((m for m in mappings_data.get('mappings', []) if m['yolink_device_id'] == device_id), None)
if mapping:
zone = mapping.get('chekt_zone' if receiver_type == "CHEKT" else 'sia_zone')
if zone and zone.strip():
logger.info(f"Triggering {receiver_type} alert in zone {zone} for device {device_id}")
trigger_alert(device_id, state, device_type)
else:
logger.warning(f"No valid zone for device {device_id} with receiver {receiver_type}. Skipping trigger.")
else:
logger.warning(f"No mapping found for device {device_id}")
else:
logger.info(f"No triggering event for device {device_id}")
else:
logger.debug(f"Non-alert event received for device {device_id}. State updated only.")
else:
logger.warning("Message received without device ID.")
except Exception as e:
logger.error(f"Error processing message: {str(e)}")
def parse_device_type(event_type, payload):
if "motionsensor" in event_type.lower():
return 'motion'
elif "doorsensor" in event_type.lower():
return 'door_contact'
elif "leaksensor" in event_type.lower():
return 'leak_sensor'
return None
def should_trigger_event(state, device_type):
if device_type == 'door_contact' and state in ['open', 'closed']:
return True
elif device_type == 'motion' and state == 'alert':
return True
elif device_type == 'leak_sensor' and state == 'alert':
return True
return False
def map_state_to_event(state, device_type):
if device_type == 'door_contact':
if state == 'open':
return "Door Opened"
elif state == 'closed':
return "Door Closed"
elif device_type == 'motion':
if state == 'alert':
return "Motion Detected"
elif device_type == 'leak_sensor':
if state == 'alert':
return "Water Leak Detected"
return "Unknown Event"
def get_zone(device_id):
mappings_data = load_yaml(mappings_file)
mappings = mappings_data.get('mappings', [])
mapping = next((m for m in mappings if m['yolink_device_id'] == device_id), None)
if mapping:
return mapping.get('chekt_zone') or mapping.get('sia_zone')
return None
def trigger_alert(device_id, state, device_type):
event_description = map_state_to_event(state, device_type)
receiver_type = config_data.get("receiver_type", "CHEKT").upper()
if receiver_type not in ["CHEKT", "SIA"]:
logger.error(f"Invalid receiver type in config: {receiver_type}. Defaulting to CHEKT.")
receiver_type = "CHEKT"
mappings_data = load_mappings()
mapping = next((m for m in mappings_data.get('mappings', []) if m['yolink_device_id'] == device_id), None)
if not mapping:
logger.warning(f"No mapping found for device {device_id}")
return
if receiver_type == "CHEKT":
chekt_zone = mapping.get('chekt_zone')
if chekt_zone and chekt_zone.strip() and chekt_zone != 'N/A':
logger.info(f"Triggering CHEKT event in zone {chekt_zone} for device {device_id}")
trigger_chekt_event(device_id, event_description, chekt_zone)
else:
logger.warning(f"No valid CHEKT zone found for device {device_id}. Mapping details: {mapping}")
elif receiver_type == "SIA":
sia_zone = mapping.get('sia_zone')
sia_config = config_data.get('sia', {})
if sia_zone and sia_zone.strip() and sia_zone != 'N/A':
logger.info(f"Sending SIA event in zone {sia_zone} for device {device_id}")
send_sia_message(device_id, event_description, sia_zone, sia_config)
else:
logger.warning(f"No valid SIA zone found for device {device_id}. Mapping details: {mapping}")
else:
logger.error(f"Unknown receiver type: {receiver_type}")
def trigger_chekt_event(device_id, event_description, chekt_zone, mapping=None):
"""
Triggers an event on the CHEKT server for a specific zone.
Parameters:
- device_id (str): The ID of the device triggering the event.
- event_description (str): Description of the event.
- chekt_zone (str): The default zone to trigger the event in.
- mapping (dict, optional): Mapping information for the device.
"""
try:
# Load CHEKT configuration
chekt_config = config_data.get('chekt', {})
ip = chekt_config.get('ip')
port = chekt_config.get('port')
api_token = chekt_config.get('api_token')
# Validate CHEKT configuration
if not ip or not port:
logger.error("CHEKT IP or Port not configured or missing in config.yaml.")
return
if not api_token:
logger.error("CHEKT API token missing from configuration. Cannot trigger CHEKT event.")
return
# Determine the appropriate CHEKT zone
if mapping and mapping.get('chekt_zone') and mapping['chekt_zone'] != 'N/A':
chekt_zone = mapping['chekt_zone']
logger.debug(f"Using mapped CHEKT zone: {chekt_zone} for device {device_id}")
else:
logger.debug(f"Using default CHEKT zone: {chekt_zone} for device {device_id}")
# Construct the API URL using the determined chekt_zone
chekt_api_url = f"http://{ip}:{port}/api/v1/zones/{chekt_zone}/events"
# Basic authentication setup
auth_string = f"apikey:{api_token}"
auth_header = base64.b64encode(auth_string.encode()).decode()
headers = {
"Authorization": f"Basic {auth_header}",
"Content-Type": "application/json"
}
# Prepare the payload
chekt_payload = {
"event_description": event_description
}
logger.info(f"Attempting to post event to CHEKT at URL: {chekt_api_url} with payload: {chekt_payload}")
# Send the POST request to CHEKT
response = requests.post(chekt_api_url, headers=headers, json=chekt_payload, timeout=10)
# Check if the response is JSON
content_type = response.headers.get("Content-Type", "")
if "application/json" in content_type:
response_data = response.json()
if response.status_code in [200, 202]:
logger.info(f"Success: Event triggered on zone '{chekt_zone}' for device '{device_id}'. Response: {response_data}")
else:
logger.error(f"Failed to trigger event on zone '{chekt_zone}' for device '{device_id}'. "
f"Status code: {response.status_code}, Response: {response_data}")
else:
if response.status_code in [200, 202]:
logger.info(f"Event triggered on zone '{chekt_zone}' for device '{device_id}', but response is not JSON. Response: {response.text}")
else:
logger.error(f"Failed to trigger event on zone '{chekt_zone}' for device '{device_id}'. "
f"Status code: {response.status_code}, Response text: {response.text}")
except requests.exceptions.RequestException as e:
logger.error(f"Request error while triggering CHEKT event for device '{device_id}': {str(e)}")
except json.JSONDecodeError as e:
logger.error(f"JSON decode error while parsing CHEKT event response for device '{device_id}': {str(e)}")
except Exception as e:
logger.error(f"Unexpected error in trigger_chekt_event for device '{device_id}': {str(e)}")
def send_sia_message(device_id, event_description, zone, sia_config, event_type="BA"):
"""
Send a SIA DC-09 compliant message to the central monitoring station.
Args:
device_id (str): ID of the YoLink device.
event_description (str): Description of the event.