def reportEnds(self):
     #self.cbLog("debug", "reportEnds")
     try:
         now = time.strftime("%H:%M:%S", time.localtime()).split(":")[0:2]  # Format: ["06", "30"]
         nightEnd = config["night_end"].split(":")
         #self.cbLog("debug", "reportEnds, now: " + str(now) + ", nightEnd: " + str(nightEnd))
         if int(nightEnd[0]) == int(now[0]) and int(nightEnd[1]) == int(now[1]):
             timeStamp = time.time()
             values = {
                 "name": self.bridge_id + "/Night_Wander/" + "night_start",
                 "points": [[int(timeStamp*1000), int(hourMin2Epoch(config["night_start"]))*1000]]
             }
             self.storeValues(values)
             values = {
                 "name": self.bridge_id + "/Night_Wander/" + "night_end",
                 "points": [[int(timeStamp*1000), int(hourMin2Epoch(config["night_end"]))*1000]]
             }
             self.storeValues(values)
             values = {
                 "name": self.bridge_id + "/Night_Wander/" + "wander_count",
                 "points": [[int(timeStamp*1000), self.state["wanderCount"]]]
             }
             self.storeValues(values)
             self.state["wanderCount"] = 0
     except Exception as ex:
         self.cbLog("warning", "Problem running reportEnds. Type: " + str(type(ex)) + "exception: " +  str(ex.args))
     reactor.callLater(60, self.reportEnds)
Ejemplo n.º 2
0
 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)