diff --git a/scripts/alarm.py b/scripts/alarm.py new file mode 100644 index 0000000000000000000000000000000000000000..c778097ac05b02d018a3ee7d5a6e17f9504744c7 --- /dev/null +++ b/scripts/alarm.py @@ -0,0 +1,122 @@ +from xaal.lib import Device,Engine,tools,Message +import platform +from enum import Enum +import time + +DELAY = 50 + +ADDR = 'aa4d1cbc-92af-11e8-80cd-408d5c18c800' +PKG_NAME = 'alarm' + +DOOR = 'dbeed1b4-91a6-11e8-a717-408d5c18c8f7' +#DOOR = '42775a2e-92af-11e8-ae30-408d5c18c800' +SIREN = '9e2d91a0-905a-11e8-9cdd-02150400d000' +FLASH = '9e2d91a0-905a-11e8-9cdd-02150400d001' +BULLET = 'ef1187ce-9119-11e8-9daa-408d5c18c8f7' + +logger = tools.get_logger(PKG_NAME,'DEBUG') + + +class States(Enum): + disabled = 'disabled' + armed = 'armed' + wait_entrance = 'wait_entrance' + wait_exit = 'wait_exit' + siren = 'siren' + + +dev = None +state = States.disabled +timer = 0 + +def send(targets,action,body=None): + global dev + engine = dev.engine + engine.send_request(dev,targets,action,body) + + +def start_alert(): + logger.warning('ALARM !!!!') + send([BULLET],'notify',{'title':'Alarm !!','msg':"Intrusion en cours"}) + send([FLASH],'on') + send([SIREN],'on') + +def stop_alert(): + send([SIREN],'off') + send([FLASH],'off') + +def notify(msg): + send([BULLET],'notify',{'title':'Notif alarm','msg':msg}) + + +def update_state_attribute(): + global dev + dev.attributes['state']=state.value + +def handle_msg(msg): + global state,timer + # search for the buttons + if msg.is_notify(): + if msg.action == 'long_click_press': + # you have x seconds to exit + if state == States.disabled: + state = States.wait_exit + timer=time.time() + notify('Départ de la maison') + update_state_attribute() + return + # you have disabled the alarm + if state in [States.armed,States.wait_entrance,States.siren,States.wait_exit]: + state = States.disabled + stop_alert() + notify('Alarme désactivée') + update_state_attribute() + return + + # windows & doors + #msg = Message() + if msg.is_attributes_change(): + if msg.devtype in ['window.basic','door.basic']: + pos = msg.body.get('position',None) + if pos == True: + if state == States.armed: + if msg.source == DOOR: + state = States.wait_entrance + notify('Ouverture porte') + timer = time.time() + else: + state = States.siren + start_alert() + update_state_attribute() + +def update(): + global state,timer + now = time.time() + if state == States.wait_exit: + if now > (timer + DELAY): + state = States.armed + notify("Alarme activée") + + if state == States.wait_entrance: + if now > (timer + DELAY): + state = States.siren + start_alert() + update_state_attribute() + + +def main(): + global dev + dev = Device('alarm.jkx',ADDR) + dev.info = '%s@%s' % (PKG_NAME,platform.node()) + dev.new_attribute('state') + engine = Engine() + engine.add_device(dev) + engine.add_rx_handler(handle_msg) + engine.add_timer(update,1) + engine.run() + +if __name__ == '__main__': + try: + main() + except KeyboardInterrupt: + print('Bye bye') diff --git a/scripts/btn_relay.py b/scripts/btn_relay.py new file mode 100644 index 0000000000000000000000000000000000000000..aaf9ef139943832828a885ff2ccf44778df819ab --- /dev/null +++ b/scripts/btn_relay.py @@ -0,0 +1,51 @@ +from xaal.lib import Engine +from xaal.schemas import devices +import platform + +PKG_NAME = 'btn_relay' + +REL1 = '059111c6-7cb7-11e8-93ce-408d5c18c8f7' +REL2 = '05912044-7cb7-11e8-93ce-408d5c18c8f7' + +BTN1 = 'ec069c08-92af-11e8-80cd-408d5c18c800' +BTN2 = '6fa87ef2-9975-11e8-b1fa-82ed25e6aa00' +BTN3 = '821c6026-92ae-11e8-82af-408d5c18c800' + +dev = None + +def send(targets,action,body=None): + global dev + engine = dev.engine + engine.send_request(dev,targets,action,body) + + +def handle_msg(msg): + if not msg.is_notify(): + return + # search for the buttons + if msg.action == 'click': + if msg.source == BTN2: + send([REL1,REL2],'toggle') + if msg.source == BTN1: + send([REL1],'toggle') + if msg.source == BTN3: + send([REL2,],'toggle') + if msg.action == 'double_click': + if msg.source in [BTN1,BTN3]: + send([REL1,REL2],'off') + + +def main(): + global dev + dev = devices.basic() + dev.info = '%s@%s' % (PKG_NAME,platform.node()) + engine = Engine() + engine.add_device(dev) + engine.add_rx_handler(handle_msg) + engine.run() + +if __name__ == '__main__': + try: + main() + except KeyboardInterrupt: + print('Bye bye')