/
spkthreads.py
325 lines (285 loc) · 12.9 KB
/
spkthreads.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
# -*- 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
import threading
import time
import json
import shutil
import traceback
import base64
import urllib2
from cStringIO import StringIO
import spynner
import sys
import zipfile
import spkwifi
import spkserver
import common
from spkserial import ThreadsafeSerial, DeadSerialException
import os.path
import spkcutter
VERSIONFILE="client_version"
class RoutineThread(threading.Thread):
def __init__(self,routine_type,job_id=None,arguments={}):
threading.Thread.__init__(self)
self.kill=False
self.type=routine_type
self.job_id=job_id
self.daemon=False
self.job=spkcutter.Routine(routine_type,arguments)
self.status=self.job.status
def run(self):
try:
self.job.run()
except (SystemExit,KeyboardInterrupt,GeneratorExit):
self.status.end_requested()
except Exception,info:
exc_type, exc_value, exc_traceback = sys.exc_info()
trace=str.decode(traceback.format_exc(),"utf-8")
fullerror=unicode.join(u"\n",trace.splitlines()[3:])
try:
line=info[1][1] #la vida...
except:
line=traceback.extract_tb(exc_traceback)[-1][1]#...es complicada
#y obtener la linea correcta... tambien
self.status.end_error({"line":line,"fullInfo":fullerror,"exception":str(exc_value)})
common.internet_thread.show_message("error","Hubo un error:",str(exc_value))
else:
self.status.end_success()
if self.type=="cut":
common.internet_thread.show_message("success","Corte finalizado","El tiempo de corte fue de "+str(self.status.ending["cut_time"])+" segundos.")
elif self.type=="home":
common.internet_thread.show_message("success","Ejes medidos","El cero está en la posición actual.")
elif self.type=="probe":
common.internet_thread.show_message("success","Herramienta medida","Cero del eje Z ajustado")
finally:
common.internet_thread.last_job_status=self.status
common.internet_thread.routine=None
common.internet_thread.required_post=True
class ChannelApiThread(threading.Thread):
def __init__(self,message_callback):
threading.Thread.__init__(self)
self.connected=False
self.connecting=False
self.kill=False
self.token=None
self.bad_token=None
self.message_callback=message_callback
self._browser=None
def _message_listener(self,a,message,b):
message=json.loads(message)
if message["state"]=="open":
self.connected=True
self.connecting=False
elif message["state"]=="message":
self.connected=True
self.connecting=False
self.message_callback(base64.b64decode(message["data"]))
elif message["state"]=="error":
error_code=message["error"]["code"]
self.connected=False
self.connecting=False
print "channel_error",message
if str(error_code).strip()=="401":
self.bad_token=self.token
self.token=None
elif message["state"]=="close":
self.connected=False
self.retry()
def run(self):
url=os.path.join(common.URL,"_cnc/channelclient")
browser=spynner.Browser()
browser.create_webview(True)
browser.load(url,load_timeout=30,tries=True)
browser.set_javascript_prompt_callback(self._message_listener)
while self.kill==False:
if self.token and not self.connected and self.connecting:
browser.runjs("openChannel('"+self.token+"')")
self.connected=True
self.connecting=False
if self.token==None and self.connected:
browser.runjs("closeSocket()")
browser.wait(1)
browser.close()
def connect(self,token):
self.token=token
self.bad_token=None
self.retry()
def retry(self):
if not self.connected and self.token:
self.connecting=True
def disconnect(self):
self.token=None
class InternetThread (threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.kill=False
self.ui = None
self.last_job_status=None
self.channel_api_token=None
self.channel_thread=None
self.required_post=True
self.routine=None
self.message=None
def channel_api_message(self,message):
self.manage_response(json.loads(message))
def run(self):
self.check_version()
self.channel_thread=ChannelApiThread(self.channel_api_message)
self.channel_thread.start()
print u"Cliente conectado con servidor"
while not self.kill:
self.required_post=False
response=self.send_prescence()
self.manage_response(response)
time.sleep(1)
for _ in range(200):
if common.get_id() is not None or self.kill: break
time.sleep(5) #cuando no hay id, esperamos 5 segundos y solo mandamos el ip paque nos configuren
while self.kill==False and self.required_post==False and (self.channel_thread.connected==True or self.channel_thread.connecting==True):
time.sleep(0.1)
pass
if self.required_post==True:
time.sleep(0.1) #para que si se marco el required post antes de que acabara de hacer cosas otro thread, las acabe
def send_prescence(self):
data={"id":common.get_id(),"local_ip":spkwifi.get_ip_adress()}
if common.get_id() is not None:
if self.channel_thread.token==None:
if self.channel_thread.bad_token!=None:
data["bad_token"]=self.channel_thread.bad_token
data["get_token"]=True
if self.routine!=None:
data["routine"]=self.routine.type
data["current_cut_job"]=self.routine.job_id
data["can_stop"]=self.routine.status.can_stop
data["stop_requested"]=self.routine.status.stop_requested
data["cut_fase"]=self.routine.status.fase
if self.last_job_status is not None:
data["last_job_status"]=self.last_job_status.as_dict()
self.last_job_status=None
if self.message is not None:
data["popup_message"]=self.message
self.message=None
try:
#print data
response_object=common.send_data(data)
#print data
#print response_object
except Exception as error:
print error
response_object={}
time.sleep(1) #durmamos 10 segundos, hubo un error con el servidor
return response_object
def manage_response(self,data):
if "local_connection_token" in data:
spkserver.local_connection_token=data["local_connection_token"]
if "channel_api_token" in data:
self.channel_thread.connect(data["channel_api_token"])
if "command" in data:
if data["command"]=="re_configure":
common.set_id(None)
self.channel_thread.disconnect()
#common.restart_please() segun yo no es necesario
if data["command"]=="invalidate_config_info":
print u"Parece que hay un problema con la configuración, el servidor contestó: %s"%data["reason"]
if data["command"]=="update_id":
if "new_id" in data:
common.set_id(data["new_id"])
elif data["command"]=="pause" or data["command"]=="unpause":
if self.routine!=None:
self.routine.job.buffer_manager.send_line("!" if data["command"]=="pause" else "~")
elif data["command"]=="send_gcode":
if self.routine is None:
try:
code=data.get("code",None)
if code is None:
parser=common.get_parser()
code=data.get(parser,None)
if code is not None:
code=code.strip()+"\n"
ThreadsafeSerial.get().write(code)
if "success_message" in data:
message= data["success_message"]
try:
self.show_message("success",message.get("title",""),message.get("description",""),message.get("timeout",5000))
except:
pass
except DeadSerialException:
self.show_message("error","El puerto serial está desconectado")
elif data["command"]=="stop_job":
if self.routine!=None:
self.routine.status.stop_please()
elif data["command"]=="run_queue":
if "job_data" in data:
self.start_routine("cut",job_id=data["job_data"]["job_id"],arguments=data["job_data"]["arguments"])
elif data["command"]=="do_homing":
self.start_routine("home",arguments=data.get("arguments",None))
elif data["command"]=="do_probing":
self.start_routine("probe",arguments=data.get("arguments",None))
def start_routine(self,routine_type,job_id=None,arguments={}):
if self.routine is not None: return
self.routine=RoutineThread(routine_type,job_id=job_id,arguments=arguments)
self.required_post=True
self.last_run_ending=None
self.routine.start()
def show_message(self,type,title,description="",timeonscreen=10000):
#type es error,warning,info, o success
self.message=[type,title,description,timeonscreen]
self.required_post=True
def check_version(self):
with open(os.path.join(common.get_current_path(),VERSIONFILE), 'r') as f:
version=f.read().strip()
print u"Verificando actualizaciones"
data={"checking_version":True,"version":version}
try:
response= common.send_data(data)
except:
return
if response.get("command",None)=="update":
print u"Nueva version disponible (Actual: %s,Nueva: %s)"%(version,response["new_version"])
file_url=common.URL+response["file_url"]
response = urllib2.urlopen(file_url)
_file = StringIO(response.read())
if zipfile.is_zipfile(_file):
zfl=zipfile.ZipFile(_file)
members=zfl.namelist()[:]
root_path=None
for n in members[:]:
if os.path.basename(n)=="cncid":
members.remove(n)
elif os.path.basename(n)=="main.py":
if not root_path:
root_path=os.path.dirname(n)
if root_path is not None:
for n in members[:]:
if not n.startswith(root_path):
members.remove(n)
for m in members:
source = zfl.open(m)
file_target=os.path.join(common.get_current_path(),os.path.relpath(m, root_path))
if os.path.basename(m)=="": #es directorio
if not os.path.exists(file_target):
os.makedirs(file_target)
else: #es archivo
target = file(file_target, "wb")
with source, target:
shutil.copyfileobj(source, target)
print u"Reiniciando script..."
common.restart_please()
else:
print u"Versión más reciente (%s)"%version