def get_last_and_penultimate_bm(self): last = BabyMonitorService(BabyMonitorSend).last_record() if last: penultimate = BabyMonitorService( BabyMonitorSend).penultimate_record() return last, penultimate return None, None
def check_user_confirm(): data_send = BabyMonitorService(BabyMonitorSend).last_record() data_receive = BabyMonitorService(BabyMonitorReceive).last_record() if data_receive: if data_receive["id_notification"] == data_send["id"]: return True return False
def publish_confirmation(self): confirmation = {"type": "confirmation", "msg": "Notificaiton confirmed!"} self.channel.basic_publish( exchange=exchange, routing_key=bm_msg, properties=pika.BasicProperties(delivery_mode=2,), body=json.dumps(confirmation), ) last_record = BabyMonitorService(BabyMonitorSend).last_record() user_confirm = {"id_notification": last_record["id"], "type": "confirm"} BabyMonitorService(BabyMonitorReceive).insert_data(user_confirm) print("(Publish) Notification Confirmed!")
def data_from_baby(type: str): data = {} if type == "fine": data = { "breathing": True, "sleeping": True, "crying": False, "time_no_breathing": 0, } elif type == "new": data["breathing"] = random.choices([True, False], [0.75, 0.25], k=1)[0] data["crying"] = ( False if not data["breathing"] else random.choices([True, False], [0.6, 0.4], k=1)[0] ) data["sleeping"] = ( False if data["crying"] else random.choices([True, False], [0.8, 0.2], k=1)[0] ) data["time_no_breathing"] = 0 elif type == "repeat": value = 0 data = BabyMonitorService(BabyMonitorSend).last_record() if not data["breathing"]: value = datetime.utcnow() - data["time"] data["time_no_breathing"] += int(value.total_seconds()) return data
def get_data(self): data_send = BabyMonitorService(BabyMonitorSend).last_record() data_receive = BabyMonitorService(BabyMonitorReceive).last_record() if not data_send: return data_from_baby("new_status") if data_send["type"] == "notification" and not data_receive: return data_from_baby("repeat_status") if (data_send["type"] == "notification" and data_receive["id_notification"] != data_send["id"]): return data_from_baby("repeat_status") if (data_send["type"] == "notification" and data_receive["id_notification"] == data_send["id"]): return data_from_baby("force_fine") return data_from_baby("new_status")
def generate_status(self): data = self.get_data() if data["time_no_breathing"] > 5 or data["crying"]: data["type"] = "notification" else: data["type"] = "status" data = clean_dict_baby_monitor(data) last_record = clean_dict_baby_monitor( BabyMonitorService(BabyMonitorSend).last_record()) if data != last_record: BabyMonitorService(BabyMonitorSend).insert_data(data) return data if last_record and data["breathing"] is False: BabyMonitorService(BabyMonitorSend).update_data(data) return data return data
def forward_message(self): last_record = BabyMonitorService(BabyMonitorSend).last_record() notification = self.format_notification(last_record) self.channel.basic_publish( exchange=exchange, routing_key=st_msg, properties=pika.BasicProperties(delivery_mode=2,), body=json.dumps(notification), ) print("(Publish) Forwarding notification to TV.")
def babymonitor_connect(): global bm_on bm_on = True data = data_from_baby("force_fine") data["type"] = "status" BabyMonitorService(BabyMonitorSend).insert_data(data) subscriber = BabyMonitorSubscriber() subscriber.start() while True: sleep(1) if bm_on: publisher = BabyMonitorPublisher() publisher.start() else: subscriber.stop() break
def babymonitor_connect(): global bm_on bm_on = True data = data_from_baby("fine") data['type'] = 'status' BabyMonitorService(BabyMonitorSend).insert_data(data) info = {"info": "Baby Monitor Start"} socketio.emit("BabyMonitorInformation", info) subscriber = BabyMonitorSubscriber() subscriber.start() while True: sleep(1) if bm_on: publisher = BabyMonitorPublisher() publisher.start() else: subscriber.stop() break
def data_from_baby(flag: str): data = {} if flag == "force_fine": data["crying"] = False data["sleeping"] = random.choices([True, False], [0.1, 0.0], k=1)[0] data["breathing"] = True data["time_no_breathing"] = 0 elif flag == "new_status": data["crying"] = random.choices([True, False], [0, 1.0], k=1)[0] if data["crying"]: data["sleeping"] = False data["breathing"] = True data["time_no_breathing"] = 0 else: # Mudar o data["breathing"] data["sleeping"] = random.choices([True, False], [0.75, 0.25], k=1)[0] data["breathing"] = random.choices([True, False], [0.25, 0.75], k=1)[0] data["time_no_breathing"] = 0 if not data["breathing"]: data["sleeping"] = True if data["sleeping"]: data["crying"] = False elif flag == "repeat_status": value = 0 data = BabyMonitorService(BabyMonitorSend).last_record() if not data["breathing"]: value = datetime.utcnow() - data["time"] data["time_no_breathing"] += int(value.total_seconds()) return data
def user_confirm(): last_send = BabyMonitorService(BabyMonitorSend).last_record() id_notification = last_send["id"] BabyMonitorService(BabyMonitorReceive).insert_data( dict(id_notification=id_notification)) SmartphonePublisher("confirmation").start()
def baby_is_normal(self): last = BabyMonitorService(BabyMonitorSend).last_record() return last["type"] == "status"