Hello,
I have written some code to detect two inputs simultaniously activated through websocket.
Here is the code
#timingclass
import websocket
import threading
import datetime
import json
import time
url = "ws://192.168.1.104:8088/ws"
class DI:
def __init__(self,circuit,value,timestamp):
self.circuit = circuit
self.value = value
self.timestamp = timestamp
self.longpress = 0
self.doubletap = 0
class homecontrol:
def __init__(self,websock_evok_url):
self.status = 0
self.digitalinput_list = [[0,0],[0,0]] # create list with timestamp inputs
self.timeout = 1000
self.websock_evok_url = websock_evok_url
#connect to websocket and spawn message thread
self.ws = websocket.WebSocketApp(url, on_message = self.on_message, on_error = self.on_error, on_close = self.on_close)
self.wst = threading.Thread(target=self.ws.run_forever())
self.wst.daemon = True
self.wst.start()
def time_stamp_inputs(self):
t = datetime.datetime.now()
t = (t.second * 1000)+(t.microsecond / 1000)
return t
def detect_if_pressed_together(self):
d1 = self.digitalinput_list[0]
d2 = self.digitalinput_list[1]
# must be two different inputs and the same values you cannot press and unpress at the same time
if d1.circuit != d2.circuit and d1.value == d2.value:
t = d1.timestamp -d2.timestamp
if t < self.timeout:
return 1
else:
return 0
def on_message(self,ws, message):
self.obj = json.loads(message)
print self.obj
self.dev = self.obj["dev"]
self.circuit = self.obj["circuit"]
self.value = self.obj['value']
if self.dev == "input" and self.value == 1 :
digitalinput = DI(self.circuit,self.value,self.time_stamp_inputs())
self.digitalinput_list.insert(0,digitalinput)
if(self.detect_if_pressed_together()):
l = (self.digitalinput_list[0].circuit,self.digitalinput_list[1].circuit)
l = sorted(l)
x = str(l[0])+str(l[1])
self.digitalinput_list[0].circuit = int(x)
x = self.digitalinput_list[0]
proc = threading.Thread(target=self.proceed_input,args=(x,))
proc.start()
def detect_long_press_of_button(self):
pass
def proceed_input(self,I):# SETUP a method to declare what to do which each function , was thinking of using codeblocks for this part ?
if I.circuit == 17:
print I.circuit
relay = 5
t = threading.Thread(target=self.switch_rel,args=(relay,))
#t = threading.Thread(target=switch_on_off_relay,args=(relay,))
t.start()
print "thread"
if I.circuit == 26:
print "26"
relay = 1
c = threading.Thread(target=self.switch_rel,args=(relay,))
c.start()
print "thread2"
def on_error(self,ws,error):
print "error"
print error
def on_close(self,ws):
print "connection closed"
def switch_rel(self,c):
relaynumber = c
self.ws.send('{"cmd":"set","dev":"relay","circuit":"'+str(relaynumber)+'","value":"0"}')
#else:
# self.ws.send('{"cmd":"set","dev":"relay","circuit":"'+str(relaynumber)+'","value":"0"}')
# self.status = 1
# SETUP a method to declare what to do which each function , was thinking of using codeblocks for this part ?
def switch_on_off_relay(c):
print c
relaynumber = c
url = "ws://192.168.1.104:8088/ws"
ws = websocket.create_connection(url)
print "swdf"
ws.send('{"cmd":"set","dev":"relay","circuit":"'+str(relaynumber)+'","value":"1"}')
ws.send('{"cmd":"set","dev":"relay","circuit":"'+str(relaynumber)+'","value":"0"}')
ws.close
pass
x = homecontrol(url)
But I have a problem that some inputs when they are to fast one after each other get forgetten.
and if I do none multithreading it does do al the steps but then one push may hold the others so not good.
Can somebody help me ?