/
spkserver.py
402 lines (377 loc) · 17.7 KB
/
spkserver.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# -*- coding: utf-8 -*-
#!/usr/bin/env python
#
# Copyright 2015 Daniel Fernández (daniel@spkautomatizacion.com), Saúl Pilatowsky (saul@spkautomatizacion.com)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Find us at: http://www.spkautomatizacion.com
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from SocketServer import ThreadingMixIn
import cgi
import json
import spkwifi
import threading
import collections
import time
from spkserial import ThreadsafeSerial,DeadSerialException
import common
PORT_NUMBER = 80
ALLOWED_CONFIG_ORIGINS=[common.URL,common.URL.replace("https", "http"),"http://www.spkautomatizacion.com"]
local_connection_token=""
class ConsoleManager():
lock= threading.RLock()
dictlock= threading.RLock()
lines=collections.OrderedDict()
lineid=0
def _get_prev_lines(self,lastlineid=None):
if lastlineid!=None:
with self.dictlock:
keys=self.lines.keys()
if lastlineid in keys:
i=keys.index(lastlineid)
if i<len(keys)-1:
return keys[-1],[self.lines[keys[i+j]] for j in range(1,len(keys)-i)]
return None
def readlines(self,lastlineid=None):
s=ThreadsafeSerial.get()
prevs=self._get_prev_lines(lastlineid)
if prevs is not None: return prevs #checamos si no hay lineas guardadas
with self.lock:#esperamos al lock de lectura
prevs=self._get_prev_lines(lastlineid) #volvemos a checar si no se guardaron lineas miesntras esperabamos el lock
if prevs is not None: return prevs
newlines=[]
time_reading=time.time()
while True:
if common.internet_thread.routine is not None: break
l =s.readline(timeout=1)
if l.endswith("\n"):
newlines.append(l[0:-1]) #leemos mas
else:
break
if s.inWaiting()==0 or time.time()-time_reading>1:
break
if len(newlines):
with self.dictlock:
for newline in newlines:
if len(self.lines)>100:
self.lines.popitem(False)
self.lineid+=1
self.lines[self.lineid]=newline
return self.lineid,newlines
else:
return lastlineid,None
def writeline(self,data):
if data == "reset":
ThreadsafeSerial.get().write("\x18\n")
else:
ThreadsafeSerial.get().write(data+"\n")
class ServerHandler(BaseHTTPRequestHandler):
console_manager=ConsoleManager()
def log_message(self, f, *args):
#BaseHTTPRequestHandler.log_message(self, f, *args)
pass
# def do_GET(self):
# if self.path=="/":
# self.path="/index.html"
#
# try:
# #Check the file extension required and
# #set the right mime type
#
# sendReply = False
# if self.path.endswith(".html"):
# mimetype='text/html'
# sendReply = True
# if self.path.endswith(".jpg"):
# mimetype='image/jpg'
# sendReply = True
# if self.path.endswith(".gif"):
# mimetype='image/gif'
# sendReply = True
# if self.path.endswith(".js"):
# mimetype='application/javascript'
# sendReply = True
# if self.path.endswith(".css"):
# mimetype='text/css'
# sendReply = True
#
# if sendReply == True:
# #Open the static file requested and send it
# f = open(curdir + sep + self.path)
# index_file = f.read() % ("Ethernet IP: %s \n WiFi IP: %s" % spkwifi.get_ip_adresses())
# f.close()
#
# self.send_response(200)
# self.send_header('Content-type',mimetype)
# self.end_headers()
# self.wfile.write(index_file)
#
#
# return
#
# except IOError:
# self.send_error(404,'File Not Found: %s' % self.path)
#
def do_POST(self):
# if self.path=="/send_web":
#
# form = cgi.FieldStorage(
# fp=self.rfile,
# headers=self.headers,
# environ={'REQUEST_METHOD':'POST',
# 'CONTENT_TYPE':self.headers['Content-Type'],
# })
# self.send_response(200)
# self.end_headers()
# textout = ["Nombre red: %s",
# "Pswd: %s\n",
# "Tu raspberry se esta conectando...",
# "Si cometiste algun error, conectate al cable de Ethernet recarga la pagina y repite los pasos.",
# ]
#
# self.wfile.write("SPKClient:. \n\n" + "\n".join(textout) % (form["ssid"].value,form["psk"].value))
#
# if not spkwifi.set_wifi_network(form["ssid"].value.strip(), form["psk"].value.strip()):
# self.wfile.write("Algo salio mal...")
#
#
# return
# if self.path=="/send_cncid":
# form = cgi.FieldStorage(
# fp=self.rfile,
# headers=self.headers,
# environ={'REQUEST_METHOD':'POST',
# 'CONTENT_TYPE':self.headers['Content-Type'],
# })
#
# cncid= form["cncid"].value
#
# try:
# if cncid.startswith("sudo"):
# cncid = cncid.split("\"")[1]
#
# f = open(main.NEWCNCIDFILENAME,"w")
# f.write(cncid)
# f.close
#
# self.send_response(200)
# self.end_headers()
#
# textout = ["ID: %s\n",
# "Recarga la pagina en unos momentos.",
# "Tu raspberry se esta reiniciando...",
# "Revisa tu nueva maquina en el taller correspondiente."
# ]
# self.wfile.write("SPKClient:. \n\n" + "\n".join(textout) % (cncid))
#
# common.restart_please()
# #system("reboot")
# #execl(sys.executable, *([sys.executable]+sys.argv))
#
# except:
# self.send_response(200)
# self.end_headers()
# self.wfile.write("Error, revisa el contenido de tu id")
#
#
# return
# if self.path=="/reset_client":
# form = cgi.FieldStorage(
# fp=self.rfile,
# headers=self.headers,
# environ={'REQUEST_METHOD':'POST',
# 'CONTENT_TYPE':self.headers['Content-Type'],
# })
#
#
# self.send_response(200)
# self.end_headers()
# self.wfile.write("El script de spkclient se esta reiniciando, ve a la pagina principal y recargala.")
# common.restart_please()
# return
# if self.path=="/reset_pi":
# form = cgi.FieldStorage(
# fp=self.rfile,
# headers=self.headers,
# environ={'REQUEST_METHOD':'POST',
# 'CONTENT_TYPE':self.headers['Content-Type'],
# })
#
#
# self.send_response(200)
# self.end_headers()
# self.wfile.write("La raspberrypi se reiniciara...")
# system("reboot")
# return
#
if self.path=="/spkwizardsetup":
data = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
action= data.getfirst("action", None)
origin= self.headers.get("Origin",None)
if not origin in ALLOWED_CONFIG_ORIGINS:
self.send_response(403)
return
self.send_response(200)
self.send_header('Content-type',"application/json")
self.send_header('Access-Control-Allow-Origin',origin)
self.end_headers()
if action=="marco":
if common.get_id()!=None:
self.wfile.write(json.dumps({"status":"already_configured"}))
else:
self.wfile.write(json.dumps({"status":"polo","current_network":spkwifi.get_current_connected_network()}))
elif action=="get_network_status":
self.wfile.write(json.dumps({"status":"success","available_networks":[n.ssid for n in spkwifi.get_available_networks()],"current_network":spkwifi.get_current_connected_network()}))
elif action=="set_wifi_network":
ssid= data.getfirst("ssid", None)
password= data.getfirst("password", None)
if spkwifi.set_wifi_network(ssid, password):
self.wfile.write(json.dumps({"status":"success","current_network":spkwifi.get_current_connected_network()}))
else:
self.wfile.write(json.dumps({"status":"error","current_network":spkwifi.get_current_connected_network()}))
elif action=="send_workshop":
config_data= json.loads(data.getfirst("config_data", "{}"))
response=common.send_config_info(config_data)
if "extra_info" in response:
self.wfile.write(json.dumps({"status":"success","extra_info":response["extra_info"]}))
else:
self.wfile.write(json.dumps({"status":"error","reason":response.get("reason","")}))
else:
self.wfile.write(json.dumps({"status":"error"}))
elif self.path=="/spkcnclocalcommandreceiver":
data = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
origin= self.headers.get("Origin",None)
local_token= data.getfirst("_local_token", None)
prev_local_token= data.getfirst("_prev_local_token", None)
if not origin in ALLOWED_CONFIG_ORIGINS or local_token==None or (local_token!=local_connection_token!=prev_local_token):
self.send_response(403)
return
self.send_response(200)
self.send_header('Content-type',"application/json")
self.send_header('Access-Control-Allow-Origin',origin)
self.end_headers()
message= json.loads(data.getfirst("message", None))
command=message.get("command",None)
if command=="checking_same_network":
self.wfile.write(json.dumps({"status":"sure :)"}))
elif command=="lifeline":
time.sleep(60)
self.wfile.write(json.dumps({"status":"sure :)"}))
elif command=="get_network_status":
self.wfile.write(json.dumps({"status":"success","available_networks":[n.ssid for n in spkwifi.get_available_networks()],"current_network":spkwifi.get_current_connected_network()}))
elif command=="get_current_network":
self.wfile.write(json.dumps({"status":"success","current_network":spkwifi.get_current_connected_network()}))
elif command=="set_wifi_network":
ssid= message.get("ssid", None)
password= message.get("password", None)
if spkwifi.set_wifi_network(ssid, password):
self.wfile.write(json.dumps({"status":"success","current_network":spkwifi.get_current_connected_network()}))
else:
self.wfile.write(json.dumps({"status":"error","current_network":spkwifi.get_current_connected_network()}))
elif command=="get_script_status_change":
while True:
if common.internet_thread.routine is None or common.internet_thread.routine.status.as_dict()!=message.get("last_status",None):
break
else:
time.sleep(.01)
if common.internet_thread.routine is not None:
self.wfile.write(json.dumps({"status":common.internet_thread.routine.status.as_dict()}))
else:
self.wfile.write(json.dumps({"status":common.internet_thread.last_job_status.as_dict() if common.internet_thread.last_job_status is not None else None}))
elif command=="send_console_line":
if common.internet_thread.routine is None:
try:
self.console_manager.writeline(message.get("line",""))
except:
pass
self.wfile.write(json.dumps({}))
elif command=="console_line_poll":
try:
lines=None
lastid=message.get("lastlineid",None)
counter=0
response={"status":"success"}
while common.internet_thread.routine is None and lines is None and counter <3:
if message.get("i_think_there_is_no_serial",False):
response["parser"]=common.get_parser()
break #para que se quite el mensaje de que no hay serial
lastid,lines=self.console_manager.readlines(lastid)
counter+=1
response["lines"]=lines
response["lastlineid"]=lastid
self.wfile.write(json.dumps(response))
except DeadSerialException:
self.wfile.write(json.dumps({"status":"error","reason":"no-serial"}))
except:
self.wfile.write(json.dumps({"status":"error"}))
elif command =="jog_pressed_key":
global current_jogging
global jogging_cancel_timer
try:
if common.internet_thread.routine is None:
key=message.get("key",None)
pressed=message.get("pressed",None)
possibles={38:"Y",40:"Y-",37:"X-",39:"X",33:"Z",34:"Z-"} #http://keycode.info/
usb=ThreadsafeSerial.get()
parser=common.get_parser()
if key in possibles and pressed is not None:
if jogging_cancel_timer is not None:
jogging_cancel_timer.cancel()
if not pressed or (current_jogging is not None and current_jogging!=key):
if parser=="tiny-g":
usb.write("!%\n")
#grbl mandar (! y ctrl x? cuando se frene?)
current_jogging=None
else:
if message.get("new_pressed",False) and current_jogging is None:
if parser=="tiny-g":
usb.write("G91 G0 "+possibles[key]+"1000\nG90\n")
#grbl mandar lo mismo?
current_jogging=key
jogging_cancel_timer=threading.Timer(0.5,lambda: usb.write("!%\n" if parser=="tiny-g" else ""))
jogging_cancel_timer.start()
usb.flushInput()
except DeadSerialException:
pass
self.wfile.write(json.dumps({}))
else:
common.internet_thread.manage_response(message)
self.wfile.write(json.dumps({}))
current_jogging=None
jogging_cancel_timer=None
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
daemon_threads=True
class ServerThread (threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.port = PORT_NUMBER
self.server = ThreadedHTTPServer(('', PORT_NUMBER), ServerHandler)
def run(self):
try:
self.server.serve_forever()
finally:
try:
self.server.socket.close()
except:
pass