"""Driver for connecting Pond Level to Meshify.""" import threading from device_base import deviceBase import calibration_db as db import time import json import persistence _ = None pond_level = { 'value': 15.0, 'timestamp': 0 } send_delta = 0.5 send_time = 300 temp_pl = pond_level['value'] persist_data = persistence.load() def scale_to_eu(inp_measurement, raw_max, raw_min, eu_max, eu_min): """Scale the inp_measurement to engineering units.""" m = (eu_max - eu_min) / (raw_max - raw_min) b = eu_max - raw_max * m scaled = inp_measurement * m + b return scaled 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 = "1" self.finished = threading.Event() self.forceSend = False threading.Thread.start(self) self.PL_RAWMAX = 20.0 self.PL_RAWMIN = 4.0 self.PL_EUMAX = 23.068 self.PL_EUMIN = 0.0 # 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) def run(self): """Actually run the driver.""" global pond_level, temp_pl, send_delta, send_time try: self.PL_RAWMAX = persist_data['raw_max'] self.PL_RAWMIN = persist_data['raw_min'] self.PL_EUMAX = persist_data['eu_max'] self.PL_EUMIN = persist_data['eu_min'] except KeyError: print("No persistence data for scaling parameters.") except TypeError: print("No persistence data for scaling parameters.") wait_sec = 30 for i in range(0, wait_sec): print("pondlevel driver will start in {} seconds".format(wait_sec - i)) time.sleep(1) print("BOOM! Starting pondlevel driver...") self.sendtodb("setrawmax", self.PL_RAWMAX, 0) self.sendtodb("setrawmin", self.PL_RAWMIN, 0) self.sendtodb("seteumax", self.PL_EUMAX, 0) self.sendtodb("seteumin", self.PL_EUMIN, 0) send_loops = 0 while True: send_now = False if self.forceSend: print "FORCE SEND: TRUE" send_now = True mcu_status = self.mcu.getDict() # Gets a dictionary of the IO states cloop_val = float(mcu_status['cloop']) temp_pl = scale_to_eu(cloop_val, self.PL_RAWMAX, self.PL_RAWMIN, self.PL_EUMAX, self.PL_EUMIN) if abs(temp_pl - pond_level['value']) > send_delta: print("Sending {} due to value change".format(temp_pl)) send_now = True elif (time.time() - pond_level['timestamp']) > send_time: print("Sending {} due to time limit".format(temp_pl)) send_now = True if send_now: self.sendtodb('pond_level', temp_pl, 0) pond_level['value'] = temp_pl pond_level['timestamp'] = time.time() print("pondlevel driver still alive...") if self.forceSend: if send_loops > 2: print("Turning off forceSend") self.forceSend = False send_loops = 0 else: send_loops += 1 time.sleep(10) def send_calibration_points(self): """Send calibration data from database to Meshify.""" cal_data = db.get_calibration_data() self.sendtodb('pondlevel', json.dumps(cal_data), 0) def pondlevel_sync(self, name, value): """Sync all data from the driver.""" self.forceSend = True self.sendtodb("log", "synced", 0) return True def pondlevel_addcalibrationpoint(self, name, value): """Add a JSON calibration point to the database.""" try: new_point = json.loads(value) db.insert_calibration_data(new_point['height'], new_point['volume']) self.send_calibration_points() return True except KeyError: return "Misformed JSON" def store_scaling_data(self): """Store scaling data in the persist file.""" scale_obj = { 'raw_max': self.PL_RAWMAX, 'raw_min': self.PL_RAWMIN, 'eu_max': self.PL_EUMAX, 'eu_min': self.PL_EUMIN } persistence.store(scale_obj) def pondlevel_setrawmin(self, name, value): """Set the raw min scaling value.""" self.PL_RAWMIN = float(value) self.sendtodb("setrawmin", self.PL_RAWMIN, 0) self.store_scaling_data() return(True) def pondlevel_setrawmax(self, name, value): """Set the raw max scaling value.""" self.PL_RAWMAX = float(value) self.sendtodb("setrawmax", self.PL_RAWMAX, 0) self.store_scaling_data() return(True) def pondlevel_seteumin(self, name, value): """Set the gpm min scaling value.""" self.PL_EUMIN = float(value) self.sendtodb("seteumin", self.PL_EUMIN, 0) self.store_scaling_data() return(True) def pondlevel_seteumax(self, name, value): """Set the gpm max scaling value.""" self.PL_EUMAX = float(value) self.sendtodb("seteumax", self.PL_EUMAX, 0) self.store_scaling_data() return(True)