def eval_upload(self): """ Evaluate possible transmissions. To be called once per minute """ if self.tx_config is None: return transmissions = self.tx_config["cloud"] update = False current = datetime.now() for name, tx in transmissions.iteritems(): interval = tx["interval"] if interval == "1m": update = True elif interval == "5m": if current.minute % 5 == 0: update = True elif interval == "15m": if current.minute % 15 == 0: update = True elif interval == "30m": if current.minute % 30 == 0: update = True elif interval == "1h": if current.minute == 0: update = True elif interval == "12h": if current.hour % 12 == 0: update = True elif interval == "1d": if current.hour == 0: update = True if update: update = False # Fill endpoint data endpoint_values = [] for field_id in tx["endpoints"]: endp_name = tx["endpoints"][field_id] endp = network.get_endpoint(endp_name) if endp is None: raise LagartoException("Unable to find a matching endpoint for " + endp_name + " before updating " + name + " from database") elif endp.value is None: raise LagartoException(endp_name + " has an invalid value. Unable to upload " + name + " to " + tx["service"]) else: endpoint_values.append((field_id, endp.value)) update = True if update: if tx["service"] == "ThingSpeak": thinkspeak = ThingSpeakPacket(tx["apikey"], endpoint_values) thinkspeak.push()
def update_tables(self): """ Update table contents """ if self.db_config is None: return tables = self.db_config["database"] current = datetime.now() for name, table in tables.iteritems(): # Get table object from database dbtable = self.get_table(name) update = False # Get latest timestamp fromdatabase last = dbtable.get_last_timestamp() if last is None: update = True else: elapsed = current - last if not update: if table["interval"] == "1m": if elapsed >= timedelta(minutes=1): update = True elif table["interval"] == "5m": if elapsed >= timedelta(minutes=5): update = True elif table["interval"] == "15m": if elapsed >= timedelta(minutes=15): update = True elif table["interval"] == "30m": if elapsed >= timedelta(minutes=30): update = True elif table["interval"] == "1h": if elapsed >= timedelta(hours=1): update = True elif table["interval"] == "12h": if elapsed >= timedelta(hours=12): update = True elif table["interval"] == "1d": if elapsed >= timedelta(days=1): update = True elif table["interval"] == "1w": if elapsed >= timedelta(weeks=1): update = True elif table["interval"] == "2w": if elapsed >= timedelta(weeks=2): update = True elif table["interval"] == "1M": if current.month != last.month: update = True if update: # Fill endpoint data endpoint_values = [] for endp_id in table["endpoints"]: endp = network.get_endpoint(endp_id) if endp is None: raise LagartoException( "Unable to find a matching endpoint for " + endp_id + " before updating " + name + " from database") if endp.value is None: raise LagartoException( endp_id + " has an invalid value. Unable to update " + name + " from database") endpoint_values.append(endp.value) # Update table dbtable.update(endpoint_values)
def update_tables(self): """ Update table contents """ if self.db_config is None: return tables = self.db_config["database"] current = datetime.now() for name, table in tables.iteritems(): # Get table object from database dbtable = self.get_table(name) update = False # Get latest timestamp fromdatabase last = dbtable.get_last_timestamp() if last is None: update = True else: elapsed = current - last if not update: if table["interval"] == "1m": if elapsed >= timedelta(minutes=1): update = True elif table["interval"] == "5m": if elapsed >= timedelta(minutes=5): update = True elif table["interval"] == "15m": if elapsed >= timedelta(minutes=15): update = True elif table["interval"] == "30m": if elapsed >= timedelta(minutes=30): update = True elif table["interval"] == "1h": if elapsed >= timedelta(hours=1): update = True elif table["interval"] == "12h": if elapsed >= timedelta(hours=12): update = True elif table["interval"] == "1d": if elapsed >= timedelta(days=1): update = True elif table["interval"] == "1w": if elapsed >= timedelta(weeks=1): update = True elif table["interval"] == "2w": if elapsed >= timedelta(weeks=2): update = True elif table["interval"] == "1M": if current.month != last.month: update = True if update: # Fill endpoint data endpoint_values = [] for endp_id in table["endpoints"]: endp = network.get_endpoint(endp_id) if endp is None: raise LagartoException("Unable to find a matching endpoint for " + endp_id + " before updating " + name + " from database") if endp.value is None: raise LagartoException(endp_id + " has an invalid value. Unable to update " + name + " from database") endpoint_values.append(endp.value) # Update table dbtable.update(endpoint_values)