Commit 1a499bfa authored by Nikita Moshkalov's avatar Nikita Moshkalov
Browse files

Added: sample code for Z-5R Web

parent 770467bd
import sys
import socket
import time
import traceback
from collections import namedtuple
if sys.implementation.name == 'micropython':
print(sys.implementation.name)
import ujson as json
else:
import json
class ILCLient:
host = '192.168.88.72' # whatever
port = 9900
socket = socket.socket()
master_step = 0
master_time = time.time()
onboard = True if sys.implementation.name == 'micropython' else False
EVENT_ACTIONS = {}
def __init__(self):
addr = socket.getaddrinfo(self.host, self.port)[0][-1]
try:
self.socket.connect(addr)
except Exception as err:
print('SockConn-ERR#: ', err)
self.socket.close()
print('NOP')
sys.exit(err)
self.EVENT_ACTIONS['key_found'] = self.key_found
self.EVENT_ACTIONS['key_lost'] = self.key_lost
self.EVENT_ACTIONS['input_change'] = self.input_change
def execute(self, command):
if self.onboard:
self.socket.write(command)
else:
self.socket.send(command.encode())
def beep(self, count, duration):
for beep in range(0, count):
self.execute('{"commands":[{"cmd":"set_out","out": "intsound","value":0}]}')
time.sleep(duration / 1000)
self.execute('{"commands":[{"cmd":"set_out","out": "intsound","value":1}]}')
time.sleep(duration / 1000)
def door_open(self, arg):
time.sleep(100 / 1000)
self.execute('{"commands":[{"cmd":"set_out","out":"lock","value":0}]}')
for beep in range(0, arg * 1000 / 200):
self.execute('{"commands":[{"cmd":"set_out","out": "intsound","value":0}]}')
time.sleep(100 / 1000)
self.execute('{"commands":[{"cmd":"set_out","out": "intsound","value":1}]}')
time.sleep(100 / 1000)
self.execute('{"commands":[{"cmd":"set_out","out":"lock","value":1}]}')
def key_found(self, evt):
print('Found CARD: ' + evt.code)
if evt.code == 1:
self.door_open(3)
else:
self.beep(2, 100)
def key_lost(self, evt):
print('CARD Lost: ' + evt.code)
def input_change(self, evt):
print('Input change: ' + evt.input + "-> %d" % evt.value)
if evt.input == 'door':
if evt.value == 1:
print('Door Opened')
def loop(self):
while True:
raw_response = self.socket.recv(1024)
if raw_response:
response = raw_response.decode("utf-8").rstrip('\0')
try:
events = json.loads(response)
events = events.get('events')
for record in events:
EVT = namedtuple('Event', sorted(record.keys()))
evt = EVT(**record)
if evt.event in self.EVENT_ACTIONS.keys():
func = self.EVENT_ACTIONS[evt.event]
func(evt)
else:
print(time.strftime("%H:%M:%S", time.time()))
print(record)
except Exception as err:
print('Error: ', err)
print(raw_response)
traceback.print_exc()
print('----------')
else:
break
if __name__ == '__main__':
cli = ILCLient()
cli.loop()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment