changed temperature calibration

This commit is contained in:
RZ_MINIX\rober 2025-08-14 21:52:55 -07:00
parent 60ffb1cfe6
commit d0d4c20691

View File

@ -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,8 +901,11 @@ def GetLastDetected(device_id, field_name, threshold_value):
with get_db_connection() as conn:
with conn.cursor() as cur:
cur.execute(query)
record = cur.fetchone()
if record != None:
last_detected = cur.fetchone()[0].timestamp()
return last_detected
def UpdateLastSeen(new_message_dict):
@ -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:
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)
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('<f', decrypt_data[pointer:pointer+lenn])[0]
#we are not interested at changes larger than 100!
if P > 100:
P = 100
new_message_dict["pressure"] = P
pointer = pointer + lenn
elif message_type == 2: #temperature event