diff --git a/src/app.py b/src/app.py index 9021f8c..d900310 100644 --- a/src/app.py +++ b/src/app.py @@ -22,7 +22,7 @@ from datetime import datetime, timezone import traceback from http.server import HTTPServer, BaseHTTPRequestHandler import threading - +import copy import time import logging @@ -30,8 +30,8 @@ import uuid client = None # Initialize to None, not a string connected = False - - +humidity_offset = 34 +temperature_offset = -10 try: from prometheus_client import generate_latest, start_http_server, CONTENT_TYPE_LATEST, Gauge, Counter, REGISTRY prometheus_available = True @@ -520,7 +520,14 @@ def process_messages(connection, db_conn): data_type, parsed_data = process_message_data(cursor, data) if data_type and parsed_data: #logger.info(f"Successfully processed as {data_type}") - MQSend("/"+ids2MAC[parsed_data["device_id"]], json.dumps(parsed_data)) + + #For mqtt needs to be calibrated! + parsed_data_mqtt = copy.deepcopy(parsed_data) + if "temperature" in parsed_data: + parsed_data_mqtt["temperature"] = parsed_data["temperature"] + temperature_offset + parsed_data_mqtt["humidity"] = parsed_data["humidity"] + humidity_offset + + MQSend("/"+ids2MAC[parsed_data["device_id"]], json.dumps(parsed_data_mqtt)) if data_type in data_batches: data_batches[data_type].append(parsed_data) batch_size = len(data_batches[data_type]) @@ -894,7 +901,10 @@ def GetLastDetected(device_id, field_name, threshold_value): with get_db_connection() as conn: with conn.cursor() as cur: cur.execute(query) - last_detected = cur.fetchone()[0].timestamp() + record = cur.fetchone() + if record != None: + last_detected = cur.fetchone()[0].timestamp() + return last_detected @@ -909,9 +919,27 @@ def UpdateLastSeen(new_message_dict): threshold_details = GetRedisString('radar_threshold'+device_id_s) try: - radar_threshold_list = ast.literal_eval(threshold_details) - radar_threshold_signal = radar_threshold_list[0] - radar_threshold_value = radar_threshold_list[1] + #radar_threshold_list = ast.literal_eval(threshold_details) + #radar_threshold_signal = radar_threshold_list[0] + #radar_threshold_value = radar_threshold_list[1] + + if threshold_details != None: + + if type(threshold_details) == str: + if "," in threshold_details == False: + threshold_details = f'["s3_max",{int(threshold_details)}]' + threshold_details_list = ast.literal_eval(threshold_details) + elif type(threshold_details) == int: + threshold_details = f'["s3_max",{threshold_details}]' + threshold_details_list = ast.literal_eval(threshold_details) + elif type(threshold_details) == list: + threshold_details_list = ast.literal_eval(threshold_details) + + + radar_threshold_signal = threshold_details_list[0] + radar_threshold_value = threshold_details_list[1] + + except: #key not found so read from DB, and store to key sql = f""" @@ -928,7 +956,17 @@ def UpdateLastSeen(new_message_dict): if threshold_details != None: - threshold_details_list = ast.literal_eval(threshold_details) + if type(threshold_details) == str: + if "," in threshold_details == False: + threshold_details = f'["s3_max",{int(threshold_details)}]' + threshold_details_list = ast.literal_eval(threshold_details) + elif type(threshold_details) == int: + threshold_details = f'["s3_max",{threshold_details}]' + threshold_details_list = ast.literal_eval(threshold_details) + elif type(threshold_details) == list: + threshold_details_list = ast.literal_eval(threshold_details) + + radar_threshold_signal = threshold_details_list[0] radar_threshold_value = threshold_details_list[1] @@ -956,11 +994,13 @@ def UpdateLastSeen(new_message_dict): last_seen = GetRedisFloat('lastseen_'+device_id_s) if last_seen == None: last_seen = GetLastDetected(device_id_s, radar_threshold_signal, radar_threshold_value) - r.set('lastseen_'+device_id_s, last_seen) + if last_seen != None: + r.set('lastseen_'+device_id_s, last_seen) except Exception as e: - print(f"UpdateLastSeen failed: {e}") + logger.error(f"Error: {traceback.format_exc()}") + logger.error(f"UpdateLastSeen failed: {e}") def process_message_data(cursor, body): @@ -1027,7 +1067,6 @@ def process_message_data(cursor, body): MACS2id[MAC] = device_id ids2MAC[device_id] = MAC - if device_id > 0: @@ -1043,6 +1082,10 @@ def process_message_data(cursor, body): if message_type > 0: st = time.time() if message_type == 16: #Radar message + + if MAC == "10061C15C330": + print("Stop") + m_type = "radar" #counter, no targets, moving targets, stationary targets, both, 9 moving gates energy #counter, 9 stationary gates energy @@ -1118,6 +1161,9 @@ def process_message_data(cursor, body): pointer = pointer + lenn_1 lenn = 4 P = struct.unpack(' 100: + P = 100 new_message_dict["pressure"] = P pointer = pointer + lenn elif message_type == 2: #temperature event