forked from varunon9/Remote-Control-PC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
151 lines (113 loc) · 3.95 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from threading import Timer
import websocket
class EspUsb:
def __init__(self, host):
self.wsUri = f"ws://{host}/d/ws/issue"
self.mywebsocket = None
self.commsup = 0
self.workqueue = []
self.work_dict = {}
self.lastitem = None
self.is_waiting_on_stations = False
##########
self.msg = 0
self.tickmessage = 0
self.lasthz = 0
self.time_since_hz = 10
def reset_variable(self):
self.work_dict = {}
self.workqueue = []
self.lastitem = None
def increment_msg(self):
self.msg = self.msg + 1
pass
def process_lastitem(self, message):
if self.lastitem is not None:
if self.lastitem.callback is not None:
self.lastitem.callback(self.lastitem, message)
self.lastitem = None
pass
def send_work_queue(self):
if len(self.workqueue) > 0:
elem = self.workqueue.pop(0)
self.work_dict.pop(elem.request)
if elem.request is not None:
self.doSend(elem.request)
self.lastitem = elem
return True
return False
def doSend(self, message):
print(f"send: {message}")
self.mywebsocket.send(message)
def send_wx(self):
self.doSend('wx')
def send_e(self):
self.doSend('e')
pass
def getlasthz(self):
self.lasthz = self.msg - self.tickmessage
self.tickmessage = self.msg
return self.lasthz
def increment_time_since_hz(self):
self.time_since_hz = self.time_since_hz + 1
pass
def StartWebSocket(self):
if self.mywebsocket is not None:
self.mywebsocket.close()
self.reset_variable()
# websocket.enableTrace(True)
self.mywebsocket = websocket.WebSocketApp(self.wsUri,
on_open=self.on_open,
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close)
self.mywebsocket.run_forever()
def on_message(self, ws, message):
espData.increment_msg()
if espData.commsup != 1:
espData.commsup = 1
print("Comm Establisheed.")
espData.process_lastitem(message)
if espData.send_work_queue():
return # return if message is send
espData.send_wx()
def on_error(self, ws, error):
espData.commsup = 0
def on_close(self, ws):
espData.commsup = 0
def on_open(self, ws):
espData.send_e()
def QueueOperation(self, command, callback):
if command in espData.work_dict:
if espData.work_dict[command] == 1:
return
espData.work_dict[command] = 1
command_obj = Command(command, callback)
espData.workqueue.append(command_obj)
def Ticker(self):
Timer(1.0, self.Ticker).start()
if self.getlasthz() == 0:
self.increment_time_since_hz()
if self.time_since_hz > 3:
if self.commsup != 0 and not self.is_waiting_on_stations:
print("Comms Lost")
self.commsup = 0
self.StartWebSocket()
else:
self.time_since_hz = 0
class Command:
def __init__(self, command, callback):
self.request = command
self.callback = callback
def command_append(cmd):
espData.QueueOperation(cmd, print)
def mesg_send_tick():
Timer(5.0, mesg_send_tick).start()
command_append("CK0\t8") # sending press "e"
command_append("CK0\t0") # releasing key
#hope I add another function to make string to "CK0\t...." converson... i will do when i need...
###########################
espData = EspUsb("192.168.4.1")
if __name__ == "__main__":
mesg_send_tick()
espData.Ticker()