forked from kinkintama/Deneyap-Kart-Web-Agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebsocket.py
347 lines (282 loc) · 12.1 KB
/
Websocket.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import asyncio
import json
import subprocess
import os
import config
from DeviceChecker import DeviceChecker
from utils import Data, downloadCore, updateIndex
from Board import Board
from multiprocessing import Queue
import logging
from websockets.exceptions import ConnectionClosedOK
import websockets
from LibraryDownloader import searchLibrary, installLibrary
class aobject(object):
"""
Inheriting this class allows you to define an async __init__.
So you can create objects by doing something like `await MyClass(params)`
credit:https://stackoverflow.com/questions/33128325/how-to-set-class-attribute-with-await-in-init
"""
async def __new__(cls, *a, **kw):
instance = super().__new__(cls)
await instance.__init__(*a, **kw)
return instance
async def __init__(self):
pass
class Websocket(aobject):
"""
For main communication between front-end and agent, for every front-end connection, one object is created
"""
async def __init__(self, websocket: websockets.legacy.server.WebSocketServerProtocol, path:str):
"""
:param websocket: websocket connection to front-end
:type websocket: websockets.legacy.server.WebSocketServerProtocol
:param path: need for websockets library to work. not used in this project
:type path: str
"""
logging.info(f"Websocket object is created")
Data.websockets.append(self)
self.websocket = websocket
self.queue = Queue()
self.deviceChecker = DeviceChecker(self.queue)
self.deviceChecker.start()
await self.mainLoop()
async def readAndSend(self, pipe: subprocess.Popen) -> None:
"""
reads subporcess.Popen live and sends it to front-end to be written to console
:param pipe: subprocess that will be readed. name is pipe, but its not pipe. my bad.
:type pipe: subprocess.Popen
"""
allText = ""
for c in iter(lambda: pipe.stdout.readline(), b''):
t = c.decode("utf-8")
allText += t
bodyToSend = {"command": "consoleLog", "log": t}
bodyToSend = json.dumps(bodyToSend)
await self.websocket.send(bodyToSend)
await asyncio.sleep(0)
if pipe.communicate()[1]:
t = pipe.communicate()[1].decode("utf-8")
allText += t
bodyToSend = {"command": "consoleLog", "log": t}
bodyToSend = json.dumps(bodyToSend)
logging.info(f"Pipe output {allText}")
await self.websocket.send(bodyToSend)
async def commandParser(self, body: dict) -> None:
"""
message send from front-end is first comes to here to redirect appropriate function
messages related to main communication comes to this class. other messages goes to SerialMonitor class.
upload message is sent both this class and SerialMonitor class. when new code is uploading serial monitor has to be closed.
:param body: data that sent from front-end. %100 has 'command' other keys are depended on command
:type body: dict
"""
command = body['command']
if command == None:
return
else:
await self.sendResponse()
if command == "upload":
fqbn = self.fixFqbn(body['board'])
await self.upload(fqbn, body['port'], body["code"], body['uploadOptions'])
elif command == "compile":
fqbn = self.fixFqbn(body['board'])
await self.compile(fqbn, body["code"], body['uploadOptions'])
elif command == "getBoards":
await self.getBoards()
elif command == "getVersion":
await self.getVersion()
elif command == "changeVersion":
await self.changeVersion(body['version'])
elif command == "getExampleNames":
await self.getExampleNames()
elif command == "getExample":
await self.getExample(body['lib'], body['example'])
elif command == "searchLibrary":
await self.searchLibrary(body['searchTerm'])
elif command == "downloadLibrary":
await self.downloadLibrary(body['libName'], body['libVersion'])
elif command == "getCoreVersion":
await self.getCoreVersion()
def fixFqbn(self, fqbn:str, prefix :str = "deneyap:esp32:") -> str:
"""
In case prefix is not in fqbn adds it.
:param fqbn: fully qualified board name
:type fqbn: str
:param prefix: prefix that going to be checked
:type prefix: str
"""
return fqbn if fqbn.startswith(prefix) else prefix+fqbn
async def downloadLibrary(self, libName:str, libVersion:str)->None:
"""
:param libName: full name of the library
:type libName: str
:param libVersion: version of the library, like 1.3.12
:type libVersion: str
"""
updateError = updateIndex()
if updateError:
logging.error("Error while updating index")
logging.error(updateError)
logging.info(f"Installling Library: {libName}:{libVersion}")
result = installLibrary(libName, libVersion)
bodyToSend = {
"command": "downloadLibraryResult",
"result": result
}
logging.info(f"Result: {result}")
bodyToSend = json.dumps(bodyToSend)
await self.websocket.send(bodyToSend)
async def searchLibrary(self, searchTerm)->None:
"""
for searching libraries using arduino-cli
:param searchTerm: string that typed to front-end. will be search using arduino-cli
:type searchTerm: str
:return: returns string but in json format, will be converted to json on front-end
:rtype: str
"""
logging.info(f"Searching Library: {searchTerm}")
libraries = searchLibrary(searchTerm)
bodyToSend = {
"command" : "searchLibraryResult",
"libraries": libraries
}
bodyToSend = json.dumps(bodyToSend)
await self.websocket.send(bodyToSend)
async def changeVersion(self, version:str)->None:
"""
for chaning deneyap core version. LIB_PATH, DENEYAP_VERSION varibles in config file will also change accordingly
:param version: version of core that will be installed
:type version: str
"""
logging.info(f"Changing version to {version}")
bodyToSend = {"command":"versionChangeStatus", "success":True}
updateError = updateIndex()
if updateError:
logging.error("Error while updating index")
logging.error(updateError)
bodyToSend["success"] = False
bodyToSend = json.dumps(bodyToSend)
await self.websocket.send(bodyToSend)
else:
error = downloadCore(version)
if error:
logging.error("Version cant be downloaded")
logging.error(error)
bodyToSend["success"] = False
else:
logging.info("version changed successfully, writing new version to config file")
Data.config['LIB_PATH'] = Data.config['LIB_PATH'].replace(Data.config['DENEYAP_VERSION'], version)
Data.config['DENEYAP_VERSION'] = version
Data.updateConfig()
bodyToSend = json.dumps(bodyToSend)
await self.websocket.send(bodyToSend)
async def sendResponse(self) -> None:
"""
send message back to front-end to say that message is received succesfully if this message is not send, front-end send message again.
"""
logging.info(f"Main Websocket sending response back")
bodyToSend = {"command": "response"}
bodyToSend = json.dumps(bodyToSend)
await self.websocket.send(bodyToSend)
async def upload(self, fqbn:str, port:str, code:str, uploadOptions:str) -> None:
"""
Compiles and uploads code to board
:param fqbn: fully qualified board name, board name that recognized by arduino-cli
dydk_mpv10 for Deneyap Kart
dym_mpv10 for Deneyap Mini
dydk1a_mpv10 for Deneyap Kart 1A
dyg_mpv10 for Deneyap Kart G
dym_mpv20 for Deneyap Mini v2
dydk1a_mpv20 for Deneyap Kart 1A v2
:type fqbn: str
:param port: port that device is on like COM4
:type port: str
:param code: code that sent by front-end
:type code: str
:param uploadOptions: upload options for board. it is board spesific and sent by front-end as parsed.
:type uploadOptions: str
"""
board = Data.boards[port]
pipe = board.uploadCode(code, fqbn, uploadOptions)
bodyToSend = {"command": "cleanConsoleLog", "log": ""}
bodyToSend = json.dumps(bodyToSend)
await self.websocket.send(bodyToSend)
await self.readAndSend(pipe)
async def getVersion(self) -> None:
"""
Sends agent version to front-end.
it notifies user that new version is exists.
"""
bodyToSend = {"command": "returnVersion", "version": Data.config["AGENT_VERSION"]}
bodyToSend = json.dumps(bodyToSend)
await self.websocket.send(bodyToSend)
async def getCoreVersion(self) -> None:
"""
Sends deneyap core version to front-end.
"""
logging.info(f"Sending core version to front-end: {Data.config['DENEYAP_VERSION']}")
bodyToSend = {"command": "returnCoreVersion", "version": Data.config["DENEYAP_VERSION"]}
bodyToSend = json.dumps(bodyToSend)
await asyncio.sleep(1)
await self.websocket.send(bodyToSend)
async def compile(self, fqbn:str, code:str, uploadOptions:str) -> None:
"""
compiles code
:param fqbn: fully qualified board name, board name that recognized by arduino-cli
dydk_mpv10 for Deneyap Kart
dym_mpv10 for Deneyap Mini
dydk1a_mpv10 for Deneyap Kart 1A
dyg_mpv10 for Deneyap Kart G
dym_mpv20 for Deneyap Mini v2
dydk1a_mpv20 for Deneyap Kart 1A v2
:type fqbn: str
:param code: code that sent by front-end
:type code: str
:param uploadOptions: upload options for board. it is board spesific and sent by front-end as parsed.
:type uploadOptions: str
"""
pipe = Board.compileCode(code, fqbn, uploadOptions)
bodyToSend = {"command": "cleanConsoleLog", "log": "Compling Code...\n"}
bodyToSend = json.dumps(bodyToSend)
await self.websocket.send(bodyToSend)
await self.readAndSend(pipe)
async def getBoards(self) -> None:
"""
Sends devices that are connected to front-end
"""
Board.refreshBoards()
await Board.sendBoardInfo(self.websocket)
def closeSocket(self) -> None:
"""
closes device checker process
"""
logging.info("Closing DeviceChecker")
self.deviceChecker.terminate()
self.deviceChecker.process.join()
logging.info("DeviceChecker Closed")
async def mainLoop(self) -> None:
"""
main loop for main communication. checks if there is a message send my front-end if there is sends it to commandParser function.
takes commands to queue then processes them.
"""
try:
while True:
body = {"command":None}
try:
message=await asyncio.wait_for(self.websocket.recv(), timeout=0.1)
logging.info(f"Main Websocket received {message}")
body = json.loads(message)
except (asyncio.TimeoutError, ConnectionRefusedError):
if not self.queue.empty():
body = self.queue.get()
except Exception:
logging.exception("Main Websocket recv error: ")
await self.websocket.close()
logging.info("Websocket is closed")
break
await self.commandParser(body)
except:
logging.exception("Websocket Mainloop: ")
finally:
self.deviceChecker.terminate()
self.deviceChecker.process.join()