def onChange(self, sensor, timeStamp, value): try: #self.cbLog("debug", "onChange. sensor: " + self.idToName[sensor] + ", value: " + str(value) + ", time: " + nicetime(timeStamp) + ", kettleOn: " + str(self.kettleOn)) if not timeCorrect(): self.cbLog("info", "Data not processed as time is not correct") return if sensor == self.power: if value > config["threshold"] and not self.kettleOn: if timeStamp - self.kettleOffTime > config["ignore_time"]: self.sensorOnTimes[sensor] = timeStamp self.kettleOn = True self.cbLog("debug", "kettle on") values = { "name": self.bridge_id + "/kettle", "points": [[int(timeStamp*1000), 1]] } self.storeValues(values) self.counts["kettlesInDay"] += 1 self.cbLog("debug", "kettlesInDay: " + str(self.counts["kettlesInDay"])) elif value < config["threshold"] and self.kettleOn: self.kettleOn = False self.triggered = False self.kettleOffTime = timeStamp self.cbLog("debug", "kettle off") elif sensor in self.binary and value == "on": self.sensorOnTimes[sensor] = timeStamp now = time.time() trigger = True #self.cbLog("debug", "onChange, sensorOnTimes: " + str(self.sensorOnTimes)) for t in self.sensorOnTimes: if now - self.sensorOnTimes[t] > config["window"]: trigger = False if trigger and not self.triggered: self.cbLog("debug", "triggered") self.triggered = True self.counts["drinksInDay"] += 1 self.cbLog("debug", "drinksInDay: " + str(self.counts["drinksInDay"])) if config["alert"]: msg = {"m": "alert", "a": "Hot drinks being made by " + config["name"] + " at " + nicetime(now), "t": now } self.client.send(msg) self.cbLog("debug", "msg send to client: " + str(json.dumps(msg, indent=4))) values = { "name": self.bridge_id + "/hot_drinks", "points": [[int(now*1000), 1]] } self.storeValues(values) except Exception as ex: self.cbLog("warning", "HotDrinks onChange encountered problems. Exception: " + str(type(ex)) + str(ex.args))
def onAdaptorData(self, message): #self.cbLog("debug", "onAdaptorData. message: " + str(message)) if message["id"] in self.sensorsID: if message["characteristic"] == "buttons": if message["data"]["rightButton"] == 1: self.enableState.enable(True) elif message["data"]["leftButton"] == 1: self.enableState.enable(False) self.cbLog("debug", "onAdaptorData. alarm: " + str(self.enableState.isEnabled())) elif message["characteristic"] == "number_buttons": for m in message["data"].keys(): if m == "1": self.enableState.enable(True) elif m == "3": self.enableState.enable(False) self.cbLog("debug", "onAdaptorData. alarm: " + str(self.enableState.isEnabled())) elif message["characteristic"] == "binary_sensor": if self.enableState.isEnabled() and message["data"] == "on": if not message["id"] in self.onSensors: now = time.time() self.lastTrigger = now self.onSensors.append(message["id"]) active = [] for a in self.onSensors: active.append(self.idToName[a]) msg = {"m": "alert", "a": "Intruder detected by " + str(", ".join(active)) + " at " + nicetime(now), "t": now } self.client.send(msg)
def monitor(self): # Called every 5 seconds #self.cbLog("debug", "monitor. taken: " + str(self.taken)) try: if self.taken: self.taken = False inSlot = False for s in config["time_slots"]: if betweenTimes(self.lastTime, s["start"], s["end"]): inSlot = True break joinedName = self.name.replace(" ", "_") if inSlot: values = { "name": self.bridge_id + "/" + joinedName + "/" + "in_slot", "points": [[int(self.lastTime * 1000), 1]] } else: values = { "name": self.bridge_id + "/" + joinedName + "/" + "out_slot", "points": [[int(self.lastTime * 1000), 1]] } self.storeValues(values) except Exception as ex: self.cbLog( "warning", "medicine monitor encountered problems in taken. Exception: " + str(type(ex)) + str(ex.args)) try: if config["reminders"]: now = time.time() if now - self.lastReminderTime > config["reminder_time"] + 10: for s in config["time_slots"]: if betweenTimes(now, s["start"], s["end"]): if not betweenTimes(self.lastTime, s["start"], s["end"]): epochEnd = hourMin2Epoch(s["end"]) if epochEnd - now < config["reminder_time"]: self.cbLog( "debug", "monitor. start: " + str(s["start"]) + ", end: " + str(s["end"])) msg = { "m": "alert", "a": "Remember to take your " + self.name + " by " + nicetime(epochEnd)[:5], "t": now } self.client.send(msg) self.cbLog( "debug", "msg send to client: " + str(json.dumps(msg, indent=4))) self.lastReminderTime = now break except Exception as ex: self.cbLog( "warning", "medicine monitor encountered problems in reminders. Exception: " + str(type(ex)) + str(ex.args)) reactor.callLater(5, self.monitor)
def reportAlert(self, timeStamp): msg = {"m": "alert", "a": "Night wandering detected by " + str(", ".join(self.activatedSensors)) + " at " + nicetime(timeStamp), "t": timeStamp } self.client.send(msg) self.cbLog("debug", "msg send to client: " + str(json.dumps(msg, indent=4)))
def monitor(self): # Called every 5 seconds #self.cbLog("debug", "monitor. taken: " + str(self.taken)) try: if self.taken: self.taken = False inSlot = False for s in config["time_slots"]: if betweenTimes(self.lastTime, s["start"], s["end"]): inSlot = True break joinedName = self.name.replace(" ", "_") if inSlot: values = { "name": self.bridge_id + "/" + joinedName + "/" + "in_slot", "points": [[int(self.lastTime*1000), 1]] } else: values = { "name": self.bridge_id + "/" + joinedName + "/" + "out_slot", "points": [[int(self.lastTime*1000), 1]] } self.storeValues(values) except Exception as ex: self.cbLog("warning", "medicine monitor encountered problems in taken. Exception: " + str(type(ex)) + str(ex.args)) try: if config["reminders"]: now = time.time() if now - self.lastReminderTime > config["reminder_time"] + 10: for s in config["time_slots"]: if betweenTimes(now, s["start"], s["end"]): if not betweenTimes(self.lastTime, s["start"], s["end"]): epochEnd = hourMin2Epoch(s["end"]) if epochEnd - now < config["reminder_time"]: self.cbLog("debug", "monitor. start: " + str(s["start"]) + ", end: " + str(s["end"])) msg = {"m": "alert", "a": "Remember to take your " + self.name + " by " + nicetime(epochEnd)[:5], "t": now } self.client.send(msg) self.cbLog("debug", "msg send to client: " + str(json.dumps(msg, indent=4))) self.lastReminderTime = now break except Exception as ex: self.cbLog("warning", "medicine monitor encountered problems in reminders. Exception: " + str(type(ex)) + str(ex.args)) reactor.callLater(5, self.monitor)