85 lines
2.8 KiB
Python
85 lines
2.8 KiB
Python
"""Driver for prostarsolar."""
|
|
|
|
import threading
|
|
from device_base import deviceBase
|
|
import persistence
|
|
from utilities import get_public_ip_address
|
|
import time
|
|
|
|
|
|
_ = None
|
|
|
|
|
|
# GLOBAL VARIABLES
|
|
WATCHDOG_SEND_PERIOD = 3600 # Seconds, the longest amount of time before sending the watchdog status
|
|
PLC_IP_ADDRESS = "192.168.1.10"
|
|
|
|
# PERSISTENCE FILE
|
|
persist = persistence.load()
|
|
|
|
|
|
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 = "3"
|
|
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):
|
|
"""Actually run the driver."""
|
|
global persist
|
|
wait_sec = 60
|
|
for i in range(0, wait_sec):
|
|
print("prostarsolar driver will start in {} seconds".format(wait_sec - i))
|
|
time.sleep(1)
|
|
print("BOOM! Starting prostarsolar driver...")
|
|
self.nodes["prostarsolar_0199"] = self
|
|
|
|
public_ip_address = get_public_ip_address()
|
|
self.sendtodbDev(1, 'public_ip_address', public_ip_address, 0, 'prostarsolar')
|
|
|
|
send_loops = 0
|
|
watchdog_loops = 0
|
|
watchdog_check_after = 5000
|
|
while True:
|
|
if self.forceSend:
|
|
print "FORCE SEND: TRUE"
|
|
|
|
print("prostarsolar driver still alive...")
|
|
if self.forceSend:
|
|
if send_loops > 2:
|
|
print("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, 'prostarsolar')
|
|
public_ip_address = test_public_ip
|
|
watchdog_loops = 0
|
|
time.sleep(10)
|
|
|
|
def prostarsolar_sync(self, name, value):
|
|
"""Sync all data from the driver."""
|
|
self.forceSend = True
|
|
# self.sendtodb("log", "synced", 0)
|
|
return True
|