"""Driver for connecting ABB Flowmeter to Meshify.""" import threading import time from device_base import deviceBase from utilities import get_public_ip_address WAIT_FOR_CONNECTION_SECONDS = 60 IP_CHECK_PERIOD = 60 class start(threading.Thread, deviceBase): """Start class required for driver.""" def __init__(self, name=None, number=None, mac=None, Q=None, mcu=None, companyId=None, offset=None, mqtt=None, Nodes=None): """Initalize 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 = "9" self.finished = threading.Event() self.public_ip_address = "" self.public_ip_address_last_checked = 0 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.channels["status"]["last_value"] = "" def run(self): """Run the driver.""" for i in range(0, WAIT_FOR_CONNECTION_SECONDS): print("abbflow driver will start in {} seconds".format(WAIT_FOR_CONNECTION_SECONDS - i)) time.sleep(1) while True: if (time.time() - self.public_ip_address_last_checked) > IP_CHECK_PERIOD: self._check_ip_address() def _check_ip_address(self): """Check the public IP address and send to Meshify if changed.""" print("Checking IP Address...") self.public_ip_address_last_checked = time.time() test_public_ip = get_public_ip_address() print("Got {} for IP Address".format(test_public_ip)) if not test_public_ip == self.public_ip_address: self.sendtodbDev(1, 'public_ip_address', test_public_ip, 0, 'abbflow') self.public_ip_address = test_public_ip