def restart_services_h(): zeit = time.time() now = (strftime("%Y-%m-%d %H:%M:%S",localtime(zeit))) setting_s("Autorestart", str(now)) count = int(setting_r("NumRestart")) if count == 0: aes.new_event(description="Verbindung unterbrochen", prio=1) if ((count > 0) and (setting_r("XS1_off") == "inactive")): aes.new_event(description="XS1 nicht erreichbar", prio=2) setting_s("XS1_off", "Active") setting_s("NumRestart", str(count + 1)) exectext = "sudo killall python" os.system(exectext)
def send_abwesend(self, to, titel, text): success = True data = {'titel': titel, 'message': text} gcm_users = mdb_get_table(table.name) for user in gcm_users: if user.get('Name') <> None: if (user.get('Name') in to) and (str(setting_r(str('Notify_'+ user.get('Name')))) == "True") and constants.redundancy_.master: #if user.get('gcm_regid') in to: if str(setting_r(user.get('Name')) == "0"): response = self.gcm.json_request(registration_ids=[user.get('gcm_regid')], data=data) if response <> {}: success = False return success
def heartbeat_sup(): if selfsupervision: zeit = time.time() now = (strftime("%Y-%m-%d %H:%M:%S",localtime(zeit))) setting_s("Autorestart", str(now)) count = int(setting_r("NumRestart")) if count == 0: aes.new_event(description="Verbindung unterbrochen XS1inputs", prio=1) if ((count > 0) and (setting_r("XS1_off") == "inactive")): aes.new_event(description="XS1 nicht erreichbar", prio=2) setting_s("XS1_off", "Active") setting_s("NumRestart", str(count + 1)) exectext = "sudo killall python" print "connection lost" if ping(constants.router_IP): os.system(exectext) else: reset_wlan() os.system(exectext)
def new_event_t(self, description, prio=0, durchsage="", karenz=-1): if prio < 0: return if karenz == -1: if prio == 1: alarme = self.alarm_events_read(unacknowledged=True,prio=1, time=24*60) else: alarme = self.alarm_events_read(unacknowledged=True,prio=1, time=60) else: alarme = self.alarm_events_read(prio=int(prio), time=karenz) neuer = True for alarm in alarme: if description == alarm.get("description"): neuer = False if neuer: dicti = {} XS1DB = mdb.connect(constants.sql_.IP, constants.sql_.USER, constants.sql_.PASS, constants.sql_.DB) with XS1DB: cur = XS1DB.cursor() if prio < 1: insertstatement = 'INSERT INTO '+table.name+'(description, prio, acknowledged, Date) VALUES("' + str(description) + '", "' + str(prio) + '", CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)' else: insertstatement = 'INSERT INTO '+table.name+'(description, prio, Date) VALUES("' + str(description) + '", "' + str(prio) + '", CURRENT_TIMESTAMP)' cur.execute(insertstatement) if str(setting_r("Status")) == "Wach": anwesend = True else: anwesend = False if prio == 0: pass elif prio == 1: pass elif prio == 2: self.mes.send_zuhause(to=self.mes.alle, titel="Hinweis", text=description) elif prio == 3: self.mes.send_direkt(to=self.mes.alle, titel="Hinweis", text=description) elif prio == 4: self.mes.send_wach(to=self.mes.alle, titel="Hinweis", text=description) elif prio > 4 and prio < 5: self.mes.send_wach(to=self.mes.alle, titel="Hinweis", text=description) elif prio == 5: self.mes.send_wach(to=self.mes.alle, titel="Achtung", text=description) elif prio > 5 and prio < 6: self.mes.send_wach(to=self.mes.alle, titel="Achtung", text=description) elif prio >= 6 and prio < 7: self.mes.send_direkt(to=self.mes.alle, titel="Alarm", text=description) msg = MIMEText(description) msg["From"] = constants.mail_.receiver msg["To"] = constants.mail_.receiver msg["Subject"] = "Alarm" p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE) p.communicate(msg.as_string()) elif prio >= 6 and prio < 7: self.mes.send_direkt(to=self.mes.chris, titel="Alarm", text=description)
def check_all(self): Bewohner = self.read_mysql("Bewohner") Besucher = self.read_mysql("Besucher") Anwesenheit = 0 Besuch = 0 Urlaub = True Einbruch = True dicti = {} for name in Bewohner: ping_wert_o = name.get("Handy_State") ping_wert = self.check_handy(name.get("Handy_IP"), ping_wert_o) self.write_mysql(table="Bewohner",name=name.get("Name"),setting="Handy_State",wert=ping_wert) usb_wert = name.get("USB_State") if (ping_wert > ping_weg or usb_wert >usb_weg): #ansonsten alles aus sobald der schluessel gezogen wird and not(usb_wert < usb_wert_o): Anwesenheit += 1 if str(setting_r(name.get("Name")+"_anwesend")) == "0": aes.new_event(description=name.get("Name")+" heimgekommen", prio=0) setting_s(name.get("Name")+"_anwesend","1") else: if str(setting_r(name.get("Name")+"_anwesend")) == "1": aes.new_event(description=name.get("Name")+" weggegangen", prio=0) setting_s(name.get("Name")+"_anwesend","0") if ping_wert > ping_urlaub or usb_wert > usb_min: Urlaub = False if (ping_wert_o < ping_wert): Einbruch = False for name in Bewohner: usb_wert = name.get("USB_State") if (usb_wert >usb_weg): Anwesenheit += 1 if Urlaub: Anwesenheit = -1 if str(setting_r("Einbruch")) == "True" and not Einbruch: setting_s("Einbruch","False") dicti['Anwesenheit'] = Anwesenheit setting_s("Anwesenheit", Anwesenheit) dicti['Besuch'] = Besuch dicti['Einbruch'] = Einbruch return dicti
def set_auto_szene(): time.sleep(2) if (setting_r("AV_cmd")<>"1"): szene = mdb_marantz_r("Aktuell") if str(szene.get("Power")) == "False": pass #set_szene("auto_szene_Aus") elif (mdb_marantz_r('Aktuell').get('Source') <> mdb_marantz_r(str(setting_r("Kommando"))).get('Source')): if str(szene.get("Source")) == "11": set_szene("auto_szene_TV") setting_s("Kommando", "TV") elif str(szene.get("Source")) == "22": set_szene("auto_szene_PS3") setting_s("Kommando", "PS3") elif str(szene.get("Source")) == "99": set_szene("auto_szene_RaspBMC") setting_s("Kommando", "RaspBMC") time.sleep(5) ezcontrol.SetSwitch("RaspberryPi", str(100)) ezcontrol.SetSwitch("RaspberryPi", str(100)) elif str(szene.get("Source")) == "CC": set_szene("auto_szene_Sonos") setting_s("Kommando", "Sonos")
def on_receive(data): if not constants.run: sys.exit("Error message") global heartbeat global last_data global ldt if False: ldt.cancel() ldt = Timer(1, last_data_reset) ldt.start() if last_data == data: return count = int(setting_r("NumRestart")) if count > 0: setting_s("NumRestart", str(0)) if count > 1: aes.new_event(description="XS1 wieder erreichbar", prio=3) setting_s("XS1_off", "inactive") #Zerlegung des Empfangs value = float(data.split(" ")[-1]) name = str(data.split(" ")[-3]) #now = datetime.datetime.now().strftime("%H:%M:%S.%f") #aes.new_event(description="Empfangen: " + name + " " + str(now), prio=0) ##################### #Heartbeat & Server Steuerung ##################### if (("heartbeat" == name) and (value == 0)): heartbeat.cancel() heartbeat = Timer(constants.heartbt, heartbeat_sup) heartbeat.start() ezcontrol.SetSwitchFunction("heartbeat", "1") szns = inputs(name,value) for szene in szns: if szene <> None: scenes.threadExecute(szene, check_bedingung=False, wert = value) last_data = data
def Tuer_auf(): Anwesend_init = setting_r("Anwesenheit") if str(setting_r("Status")) in ["Schlafen", "Abwesend", "Urlaub"]: einbruch = True setting_s("Einbruch","True") set_szene("Einbruch_1") set_szene("Einbruch_Sofort") #setting_s("Status","Einbruch") else: einbruch = False #alle weggewesen i = 0 while i < 7: i = i + 1 akt_status = anw_status.check_all() if akt_status.get("Einbruch") == False: einbruch = False setting_s("Einbruch","False") if str(setting_r("Status")) in ["Abwesend", "Urlaub"]: set_szene("Heimgekommen") if str(setting_r("Status")) in ["Schlafen"]: set_szene("einer_heimgekommen") i = 60 if str(setting_r("Status")) in ["Wach", "Besuch"]: einbruch = False setting_s("Einbruch","False") i = 60 if Anwesend_init > akt_status.get("Anwesenheit"): aes.new_event(description="Jemand gegangen", prio=0) Anwesend_init = akt_status.get("Anwesenheit") #!!!!!!!!!!!!!achtung noch die passenden Szenen setzen #einer gegangen einer schlaeft check if this gets even executed then time.sleep(10) #if einbruch: # setting_s("Einbruch","True") #else: # setting_s("Einbruch","False") if str(setting_r("Einbruch")) == "True": aes.new_event(description="Einbruch", prio=3.1) Tuer_auf()
def set_device(self, player, command, text): if command in ["man", "auto"]: set_val_in_szenen(device=player, szene="Auto_Mode", value=command) player, p_uid, playerName = self.get_addr(player) if player in self.Devices: player = self.Devices.get(str(player)) playerName = self.Names.get(player) if str(command) == "Pause": self.SetPause(player) elif str(command) == "Play": self.SetPlay(player) elif str(command) == "Save": self.sonos_write_szene(player) elif str(command) == "Announce_Time": self.sonos_write_szene(player) lt = localtime() stunde = int(strftime("%H", lt)) minute = int(strftime("%M", lt)) if (minute <> 0) and (minute <> 30): text = "Es ist " + str(stunde) + " Uhr und " + str(minute) + " Minuten." laenge = downloadAudioFile(text) self.sonos_read_szene(player, mdb_read_table_entry(table.name,"TextToSonos")) time.sleep(laenge + 1) self.sonos_read_szene(player, mdb_read_table_entry(table.name,playerName)) elif str(command) == "Durchsage": self.durchsage(text) elif str(command) == "Ansage": self.ansage(text,player) elif str(command) == "Return": self.sonos_read_szene(player, mdb_read_table_entry(table.name,playerName), hergestellt = False) elif ((str(command) == "resume") ): time.sleep(60) self.sonos_read_szene(player, mdb_read_table_entry(table.name,playerName)) elif (str(command) == "lauter"): ActVol = self.GetVolume(player) increment = 8 VOLUME = ActVol + increment self.SetVolume(player, VOLUME) return elif (str(command) == "leiser"): ActVol = self.GetVolume(player) increment = 8 VOLUME = ActVol - increment self.SetVolume(player, VOLUME) return elif (str(command) == "inc_lauter"): ActVol = self.GetVolume(player) if ActVol >= 20: increment = 8 if ActVol < 20: increment = 4 if ActVol < 8: increment = 2 VOLUME = ActVol + increment self.SetVolume(player, VOLUME) elif (str(command) == "inc_leiser"): ActVol = self.GetVolume(player) if ActVol >= 20: increment = 8 if ActVol < 20: increment = 4 if ActVol < 8: increment = 2 VOLUME = ActVol - increment self.SetVolume(player, VOLUME) elif (str(command) == "WeckerAnsage"): self.SetPause(player) self.SetVolume(player, 20) setting_s("Durchsage", str(crn.next_wecker_heute_morgen())) text = setting_r("Durchsage") laenge = downloadAudioFile(text) self.sonos_read_szene(player, mdb_read_table_entry(table.name,"TextToSonos")) time.sleep(laenge + 1) self.SetPause(player) elif (str(command) == "EingangWohnzi"): self.StreamInput(player, self.WohnZiZone) elif ((str(command) <> "resume") and (str(command) <> "An") and (str(command) <> "None")): sonos_szene = mdb_read_table_entry(table.name,command) self.sonos_read_szene(player, sonos_szene) return True
def main(): asz = Timer(2, set_auto_szene) heartbeat = Timer(heartbt, restart_services_h) heartbeat.start() ezcontrol.SetSwitchFunction("heartbeat", str(1)) ezcontrol.SetSwitchFunction("NotbetrNot", str(1)) #Initialize scenes if ((setting_r("Status") == "Am Gehen 1") or (setting_r("Status") == "Am Gehen 2")): t = threading.Thread(target=set_szene, args=["Alles_aus_4"]) t.start() if ((setting_r("Status") == "Am Gehen 3") or (setting_r("Status") == "Gegangen") or (setting_r("Status") == "Urlaub")): t = threading.Thread(target=set_szene, args=["Alles_aus_4"]) t.start() if ((setting_r("Status") == "Abwesend") or (setting_r("Status") == "Urlaub")): t = threading.Thread(target=set_szene, args=["Alles_aus_5"]) t.start() if ((setting_r("Status") == "Schlafen")): t = threading.Thread(target=set_szene, args=["sz_Schlafen_stealth"]) t.start() aes.new_event(description="Outputs neugestartet", prio=0) while constants.run: (data,addr) = mySocket.recvfrom(SIZE) heartbeat.cancel() heartbeat = Timer(heartbt, restart_services_h) heartbeat.start() isdict = False #if True: try: data_ev = eval(data) if type(data_ev) is dict: if "Source" in data_ev: #threading hinzufuegen ansonsten timed es aus asz.cancel() mdb_marantz_s("Aktuell", data_ev) asz = Timer(2, set_auto_szene) #asz.start() elif "Key" in data_ev: gefunden = False aes.new_event(description="Schluessel: "+str(data_ev.get("Key")), prio=0) for typ in ["Bewohner", "Besucher"]: namen = read_mysql(typ) for name in namen: if (str(data_ev.get("Key")) == name.get("USB_ID")) and (name.get("USB_State")>-15): aes.new_event(description=str(name.get("Name"))+" Schluessel: "+str(data_ev.get("value")), prio=0) gefunden = True try: actions = read_bebe_action(BeBe=typ, Status=setting_r("Status"), Event=data_ev.get("value")) t = threading.Thread(target=set_szene, args=[actions[0].get("Szene")]) t.start() except: pass write_mysql(table=typ, name=str(name.get("Name")), setting="USB_State",wert=data_ev.get("value"), prod=data_ev.get("prod")) if not gefunden: t = threading.Thread(target=set_szene, args=["FalscherSchluessel"]) t.start() elif "Licht" in data_ev: if str(data_ev.get("Licht")) in ["Stablampe 1","Stehlampe","Monaco Lampe","Balkonlampe","LightStrips 2","BettSabina","BettChris","SchlafziFenster"]: #setting = {'hue': hue, 'bri': bri, 'sat': sat, 'an': an} mdb_hue_s("App", data_ev) hue_set_szene(str(data_ev.get("Licht")),"App") else: mdb_sideb_s("App", data_ev) set_TF_LEDs(data_ev.get("Licht"), "App") elif ("name" in data_ev) and ("value" in data_ev): XS1DB = mdb.connect(constants.sql_.IP, constants.sql_.USER, constants.sql_.PASS, constants.sql_.DB) zeit = time.time() now = (strftime("%Y-%m-%d %H:%M:%S",localtime(zeit))) with XS1DB: cur = XS1DB.cursor() insertstatement = 'INSERT INTO Actuators(Name, Value, Date) VALUES("' + str(data_ev.get("name")) + '", ' + str(data_ev.get("value")) + ', "' + str(now) + '")' cur.execute(insertstatement) XS1DB.close() szenen = tc.get_szene(sensor = data_ev.get("name"), value = data_ev.get("value")) for szene in szenen: t = threading.Thread(target=set_szene, args=[szene]) t.start() if data_ev.get("name") == "Helligkeit": setting_s("Helligkeit", str(data_ev.get("value"))) if data_ev.get("name") == "Haustuer": setting_s("Haustuer", str(data_ev.get("value"))) if data_ev.get("name") == "SchlafZiFenster": setting_s("SchlafZiFenster", str(data_ev.get("value"))) isdict = True except Exception as serr: isdict = False if (data == "heartbeat"): ezcontrol.SetSwitchFunction("heartbeat", str(1)) ezcontrol.SetSwitchFunction("NotbetrNot", str(1)) else: if (not (data in no_event_list)) and (not (isdict)): aes.new_event("UDP receive: "+data, karenz = 1) if "sz_" in data: t = threading.Thread(target=set_szene, args=[data]) t.start() if "appz_" in data: t = threading.Thread(target=set_szene, args=[data[5:]]) t.start() if "az_" in data: t = threading.Thread(target=set_szene, args=[data]) t.start() #Hauptszenen #Alles ein if (data == "Alles_ein"): t = threading.Thread(target=set_szene, args=["Alles_ein"]) t.start() if (setting_r("Status") == "Wach"): t = threading.Thread(target=set_szene, args=["sz_gemuetlich"]) t.start() elif (data == "Alles_ein_Flur"): ezcontrol.SetSwitch("Diele", str(100)) t = threading.Thread(target=set_szene, args=["Alles_ein"]) t.start() if (setting_r("Status") == "Wach"): t = threading.Thread(target=set_szene, args=["sz_gemuetlich"]) t.start() #app szenen elif "app_" in data: t = threading.Thread(target=set_szene, args=[str(app_r(data))]) t.start() #Wach elif (data == "Wach"): ezcontrol.SetSwitch("Kueche", str(100)) t = threading.Thread(target=set_szene, args=["Wach"]) t.start() elif (data == "Alles_aus_nochmal"): setting_s("Status", "Abwesend") ezcontrol.SetSwitch("Webcams", str(100)) ezcontrol.SetSwitch("Anwesend_Block", str(0)) #Schlafen elif (data == "Schlafen"): t = threading.Thread(target=set_szene, args=["Schlafen1"]) t.start() #Mediaquellen #Fernsehen$ elif (data == "Media_TV"): t = threading.Thread(target=set_szene, args=["TV"]) t.start() #Sonos elif (data == "Media_Sonos"): t = threading.Thread(target=set_szene, args=["MediaSonos"]) t.start() #Serversteuerung funzt nicht elif (data == "Neustart_services"): exectext = "sudo killall python" os.system(exectext) elif (data == "Neustart_raspberry"): os.system("sudo reboot") #temp_alar, elif (data == "alle_alarme_gesehen"): aes.acknowledge_all() #FernBett elif (data == "Bett_0_kurz"): set_szene(mdb_fern_r_neu("Fern_Bett", "Fern_Bett", "Fern_Bett_0_kurz")) elif (data == "Bett_0_lang"): set_szene(mdb_fern_r_neu("Fern_Bett", "Fern_Bett", "Fern_Bett_0_lang")) elif (data == "Bett_0_lang_lang"): set_szene(mdb_fern_r_neu("Fern_Bett", "Fern_Bett", "Fern_Bett_0_lang_lang")) elif (data == "Bett_3_kurz"): set_szene(mdb_fern_r_neu("Fern_Bett", "Fern_Bett", "Fern_Bett_3_kurz")) elif (data == "Bett_3_lang"): set_szene(mdb_fern_r_neu("Fern_Bett", "Fern_Bett", "Fern_Bett_3_lang")) elif (data == "Bett_4_kurz"): set_szene(mdb_fern_r_neu("Fern_Bett", "Fern_Bett", "Fern_Bett_4_kurz")) elif (data == "Bett_4_lang"): set_szene(mdb_fern_r_neu("Fern_Bett", "Fern_Bett", "Fern_Bett_4_lang")) elif (data == "Bett_7_kurz"): set_szene(mdb_fern_r_neu("Fern_Bett", "Fern_Bett", "Fern_Bett_7_kurz")) elif (data == "Bett_7_lang"): set_szene(mdb_fern_r_neu("Fern_Bett", "Fern_Bett", "Fern_Bett_7_lang")) if "Distance_" in data: volume = (int(data.split("_")[1]) - 124) / 15 + 5 sn.SetVolume(sn.SchlafZi, volume) sys.exit()
def send_wach(self, to, titel, text): if (setting_r("Status") != "Schlafen"): return self.send_direkt(to, titel, text)