import time from datetime import datetime from carrier_api import ApiConnection import json import mysql.connector username = 'bkiedinger@gmail.com' password = 'Little1!' def parse_status(status, last_status_time, table): print(status['idu']) print(status['odu']) print(status['zones']['zone'][0]) zone = status['zones']['zone'][0] status_time = datetime.strptime(status['timestamp'], '%Y-%m-%dT%H:%M:%S.%fZ') status_time = status_time.strftime('%Y-%m-%d %H:%M:%S') print(status_time) if last_status_time != status_time: last_status_time = status_time print('Add database entry') dataBase = mysql.connector.connect( host="192.168.1.25", port="3307", user="grafana_user", passwd="Little1!", database="grafana" ) # preparing a cursor object cursorObject = dataBase.cursor() with open("status.json", "w") as file: json.dump(status, file, indent=4) # insert into table sql = (f"INSERT INTO {table} (timestamp, status_timestamp, outdoor_temp, idu_cfm, \ idu_opstat, odu_opmode, odu_opstat, room_temp, room_humidity, cool_setpoint, heat_setpoint, \ current_activity, zone_conditioning, mode) \ VALUES (CURRENT_TIMESTAMP, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)") odu_opmode = status['odu'].get('opmode') val = [status_time, status['oat'], status['idu']['cfm'], status['idu']['opstat'], odu_opmode, status['odu']['opstat'], zone['rt'], zone['rh'], zone['clsp'], zone['htsp'], zone['currentActivity'], zone['zoneconditioning'], status['mode']] cursorObject.execute(sql, val) dataBase.commit() dataBase.close() return last_status_time def main(): print('Carrier Logger') # cursorObject.execute('SELECT NOW()') # for time in cursorObject: # print(time) # print() api_connection = ApiConnection(username=username, password=password) systems = api_connection.get_systems() # config = api_connection.get_config(systems[1].serial) # fid = open('config.json', 'w') # json.dump(config, fid, indent=4) # fid.close() last_status_time_upstairs = '' last_status_time_downstairs = '' serial_to_table = {"4721W204230": "CARRIER_DOWNSTAIRS_STATUS", "4221W206632": "CARRIER_UPSTAIRS_STATUS"} while True: try: api_connection.activate() for i in range(2): print('') table = serial_to_table[systems[i].serial] print('Get Log Downstairs', systems[i].serial, table) status_downstairs = api_connection.get_status(systems[i].serial) last_status_time_downstairs = parse_status(status_downstairs, last_status_time_downstairs, table) except: # print("Reconnect to server") try: print("Reconnect to server") api_connection = ApiConnection(username=username, password=password) systems = api_connection.get_systems() except: print("Server didn't respond") time.sleep(1 * 60) test = 0 # {'serial': '4221W206632', # 'name': 'Upstairs ', # 'profile': { # 'model': 'SYSTXCCWIC01-B', # 'brand': 'Carrier', # 'firmware': 'CESR131626-04.47', # 'indoor_model': '58TN0A070C141212', # 'indoor_serial': '4521A18985', # 'outdoor_model': '24VNA925A00320', # 'outdoor_serial': '2422E23423 ', # 'time_stamp': '05/23/2024, 04:49:12 CDT', # 'zone_ids': ['1']}, # 'status': { # 'outdoor_temperature': 89.0, # 'mode': 'cool', # 'temperature_unit': 'F', # 'filter_used': 10, # 'is_disconnected': False, # 'airflow_cfm': 373, # 'humidity_level': 100, # 'humidifier_on': False, # 'outdoor_unit_operational_status': 'Stage 2', # 'indoor_unit_operational_status': 'off', # 'time_stamp': '05/26/2024, 21:06:24 CDT', # 'zones': [{'id': '1', 'name': 'ZONE 1', 'current_activity': 'home', 'temperature': 72.0, 'humidity': 57, 'fan': 'off', 'hold': False, 'occupancy': False, 'hold_until': None, 'heat_set_point': 69.0, 'cool_set_point': 72.0, 'conditioning': 'active_cool'}]}, 'config': {'temperature_unit': 'F', 'static_pressure': 0.8033332824707031, 'mode': 'cool', 'limit_min': 50, 'limit_max': 90, 'time_stamp': '05/22/2024, 19:00:14 CDT', 'zones': [{'api_id': '1', 'name': 'ZONE 1', 'now': '21:11', 'current_activity': {'api_id': 'home', 'fan': 'off', 'heat_set_point': 69.0, 'cool_set_point': 72.0}, 'hold_activity': None, 'hold': False, 'hold_until': None, 'today_active_periods': ["{'$': {'id': '1'}, 'activity': 'wake', 'time': '07:00', 'enabled': 'on'}", "{'$': {'id': '2'}, 'activity': 'away', 'time': '08:00', 'enabled': 'on'}", "{'$': {'id': '3'}, 'activity': 'home', 'time': '18:45', 'enabled': 'on'}", "{'$': {'id': '4'}, 'activity': 'home', 'time': '19:45', 'enabled': 'on'}", "{'$': {'id': '5'}, 'activity': 'sleep', 'time': '22:00', 'enabled': 'on'}"], 'yesterday_active_periods': ["{'$': {'id': '1'}, 'activity': 'wake', 'time': '07:00', 'enabled': 'on'}", "{'$': {'id': '2'}, 'activity': 'away', 'time': '08:00', 'enabled': 'on'}", "{'$': {'id': '3'}, 'activity': 'home', 'time': '18:45', 'enabled': 'on'}", "{'$': {'id': '4'}, 'activity': 'home', 'time': '19:45', 'enabled': 'on'}", "{'$': {'id': '5'}, 'activity': 'sleep', 'time': '22:00', 'enabled': 'on'}"], 'activities': [{'api_id': 'away', 'fan': 'off', 'heat_set_point': 68.0, 'cool_set_point': 74.0}, {'api_id': 'home', 'fan': 'off', 'heat_set_point': 69.0, 'cool_set_point': 72.0}, {'api_id': 'manual', 'fan': 'off', 'heat_set_point': 68.0, 'cool_set_point': 72.0}, {'api_id': 'sleep', 'fan': 'off', 'heat_set_point': 70.0, 'cool_set_point': 72.0}, {'api_id': 'wake', 'fan': 'off', 'heat_set_point': 68.0, 'cool_set_point': 74.0}, {'api_id': 'vacation', 'fan': 'off', 'heat_set_point': 63.0, 'cool_set_point': 77.0}]}]}} if __name__ == "__main__": # execute only if run as a script main()