BeoGateway/main.py

119 lines
5.3 KiB
Python
Raw Normal View History

2021-11-23 15:13:55 +00:00
import asyncore
import logging
import json
import time
from datetime import datetime
import Resources.MLCONFIG as MLCONFIG
import Resources.MLGW_CLIENT as MLGW
import Resources.MLCLI_CLIENT as MLCLI
import Resources.BLHIP_CLIENT as BLHIP
import Resources.MLtn_CLIENT as MLtn
if __name__ == '__main__':
2021-11-25 19:17:50 +00:00
# Define callback function for JSON data dump from B&O Gateway
2021-11-23 15:13:55 +00:00
def cb(name, header, payload, message):
2021-11-25 19:17:50 +00:00
# Pretty formatting - convert to JSON format then remove braces
2021-11-23 15:13:55 +00:00
message = json.dumps(message, indent=4)
for r in (('"', ''), (',', ''), ('{', ''), ('}', '')):
message = str(message).replace(*r)
2021-11-25 19:17:50 +00:00
# Print message data
if len(payload) + 9 < 73:
2021-11-23 15:13:55 +00:00
logging.info("\n\t----------------------------------------------------------------------------" +
"\n\t" + name + ": <--Data-received!-<< " +
2021-11-25 19:17:50 +00:00
datetime.now().strftime("on %d/%m/%y at %H:%M:%S") +
2021-11-23 15:13:55 +00:00
"\n\t----------------------------------------------------------------------------" +
2021-11-25 19:17:50 +00:00
"\n\tHeader: " + header +
2021-11-23 15:13:55 +00:00
"\n\tPayload: " + payload +
"\n\t============================================================================" +
message)
2021-11-25 19:17:50 +00:00
elif 73 < len(payload) + 9 < 132:
2021-11-23 15:13:55 +00:00
logging.info("\n\t----------------------------------------------------------------------------" +
"\n\t" + name + ": <--Data-received!-<< " +
2021-11-25 19:17:50 +00:00
datetime.now().strftime("on %d/%m/%y at %H:%M:%S") +
2021-11-23 15:13:55 +00:00
"\n\t----------------------------------------------------------------------------" +
"\n\tHeader: " + header +
"\n\tPayload: " + payload[:66] + "\n\t\t" + payload[66:132] +
"\n\t============================================================================" +
message)
else:
logging.info("\n\t----------------------------------------------------------------------------" +
"\n\t" + name + ": <--Data-received!-<< " +
2021-11-25 19:17:50 +00:00
datetime.now().strftime("on %d/%m/%y at %H:%M:%S") +
2021-11-23 15:13:55 +00:00
"\n\t----------------------------------------------------------------------------" +
"\n\tHeader: " + header +
"\n\tPayload: " + payload[:66] + "\n\t\t" + payload[66:132] + "\n\t\t" + payload[132:] +
"\n\t============================================================================" +
message)
def check_connection(self):
last = round(time.time() - self.last_received_at, 2)
logging.debug(self.name + " Protocol Client last received " + str(last) + " seconds ago")
2021-11-25 19:17:50 +00:00
# Recconect if socket has disconnected, or if no response received to last ping
2021-11-23 15:13:55 +00:00
if not self.is_connected or last > 60:
2021-11-25 19:17:50 +00:00
logging.info("\t" + self.name + ": Reconnecting!")
2021-11-23 15:13:55 +00:00
self.handle_close()
time.sleep(0.5)
self.client_connect()
# ########################################################################################
# Main program loop
host = 'blgw.local'
2021-11-25 19:17:50 +00:00
port = [9000, 9100, 23]
2021-11-23 15:13:55 +00:00
user = 'admin'
2021-11-25 19:17:50 +00:00
pwd = 's0198247'
2021-11-23 15:13:55 +00:00
logging.basicConfig(level=logging.INFO)
config = MLCONFIG.MLConfig(host, user, pwd)
2021-11-25 19:17:50 +00:00
# Create MLGW Protocol and ML_CLI Protocol clients (basic command listening)
2021-11-23 15:13:55 +00:00
logging.info('Creating MLGW Protocol Client...')
2021-11-25 19:17:50 +00:00
mlgw = MLGW.MLGWClient(host, port[0], user, pwd, 'MLGW protocol', cb)
2021-11-23 15:13:55 +00:00
asyncore.loop(count=10, timeout=0.2)
logging.info('Creating ML Command Line Protocol Client...')
2021-11-25 19:17:50 +00:00
mlcli = MLCLI.MLCLIClient(host, port[2], user, pwd, 'ML command line interface', cb)
# Log onto the MLCLI client and ascertain the gateway model
2021-11-23 15:13:55 +00:00
asyncore.loop(count=10, timeout=0.2)
2021-11-25 19:17:50 +00:00
# Now MLGW and MasterLink Command Line Client are set up, retrieve MasterLink IDs of products
2021-11-23 15:13:55 +00:00
config.get_masterlink_id(mlgw, mlcli)
# If the gateway is a BLGW use the BLHIP protocol, else use the legacy MLHIP protocol
if mlcli.isBLGW:
logging.info('Creating BLGW Home Integration Protocol Client...')
2021-11-25 19:17:50 +00:00
blgw = BLHIP.BLHIPClient(host, port[1], user, pwd, 'BLGW Home Integration Protocol', cb)
mltn = None
2021-11-23 15:13:55 +00:00
else:
logging.info('Creating MLGW Home Integration Protocol Client...')
2021-11-25 19:17:50 +00:00
mltn = MLtn.MLtnClient(host, port[2], user, pwd, 'ML telnet client', cb)
blgw = None
2021-11-23 15:13:55 +00:00
2021-11-25 19:17:50 +00:00
# Main program loop
2021-11-23 15:13:55 +00:00
while True:
2021-11-25 19:17:50 +00:00
# Ping all connections every 10 minutes to prompt messages on the network
2021-11-23 15:13:55 +00:00
asyncore.loop(count=595, timeout=1)
2021-11-25 19:17:50 +00:00
if mlgw.is_connected:
mlgw.ping()
if mlcli.is_connected:
mlcli.ping()
2021-11-23 15:13:55 +00:00
if mlcli.isBLGW:
2021-11-25 19:17:50 +00:00
if blgw.is_connected:
blgw.ping()
2021-11-23 15:13:55 +00:00
else:
2021-11-25 19:17:50 +00:00
if mltn.is_connected:
mltn.ping()
2021-11-23 15:13:55 +00:00
2021-11-25 19:17:50 +00:00
# Check the connections approximately every 10 minutes to keep sockets open
2021-11-23 15:13:55 +00:00
asyncore.loop(count=5, timeout=1)
logging.debug("LOOP: %d enqueued, waiting to finish!" % len(asyncore.socket_map))
check_connection(mlgw)
check_connection(mlcli)
if mlcli.isBLGW:
check_connection(blgw)
else:
check_connection(mltn)