Files
Promag400-Modbus/python-driver/promagmbs.py
2018-01-16 10:19:25 -06:00

92 lines
3.1 KiB
Python

"""Driver for promagmbs."""
import threading
from device_base import deviceBase
from utilities import get_public_ip_address
import time
import logging
import sys
from logging.handlers import RotatingFileHandler
log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(funcName)s(%(lineno)d) %(message)s')
logFile = './promagmbs.log'
my_handler = RotatingFileHandler(logFile, mode='a', maxBytes=500*1024, backupCount=2, encoding=None, delay=0)
my_handler.setFormatter(log_formatter)
my_handler.setLevel(logging.INFO)
logger = logging.getLogger('promagmbs')
logger.setLevel(logging.INFO)
logger.addHandler(my_handler)
console_out = logging.StreamHandler(sys.stdout)
console_out.setFormatter(log_formatter)
logger.addHandler(console_out)
_ = None
class start(threading.Thread, deviceBase):
"""Start class required by Meshify."""
def __init__(self, name=None, number=None, mac=None, Q=None, mcu=None, companyId=None, offset=None, mqtt=None, Nodes=None):
"""Initialize the driver."""
threading.Thread.__init__(self)
deviceBase.__init__(self, name=name, number=number, mac=mac, Q=Q, mcu=mcu, companyId=companyId, offset=offset, mqtt=mqtt, Nodes=Nodes)
self.daemon = True
self.version = "4"
self.finished = threading.Event()
self.forceSend = False
threading.Thread.start(self)
# this is a required function for all drivers, its goal is to upload some piece of data
# about your device so it can be seen on the web
def register(self):
"""Register the driver."""
# self.sendtodb("log", "BOOM! Booted.", 0)
pass
def run(self):
# pass
"""Actually run the driver."""
global persist
wait_sec = 60
for i in range(0, wait_sec):
logger.info("promagmbs driver will start in {} seconds".format(wait_sec - i))
time.sleep(1)
logger.warning("BOOM! Starting promagmbs driver...")
public_ip_address = get_public_ip_address()
self.sendtodbDev(1, 'public_ip_address', public_ip_address, 0, 'promagmbs')
send_loops = 0
watchdog_loops = 0
watchdog_check_after = 5000
while True:
if self.forceSend:
logger.warning("FORCE SEND: TRUE")
logger.info("promagmbs driver still alive...")
if self.forceSend:
if send_loops > 2:
logger.warning("Turning off forceSend")
self.forceSend = False
send_loops = 0
else:
send_loops += 1
watchdog_loops += 1
if (watchdog_loops >= watchdog_check_after):
test_public_ip = get_public_ip_address()
if not test_public_ip == public_ip_address:
self.sendtodbDev(1, 'public_ip_address', test_public_ip, 0, 'promagmbs')
public_ip_address = test_public_ip
watchdog_loops = 0
time.sleep(10)
def promagmbs_sync(self, name, value):
"""Sync all data from the driver."""
self.forceSend = True
self.sendtodb("log", "synced", 0)
return True