forked from Alasterer/kkengraver_enhanced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
934 lines (768 loc) · 28.8 KB
/
gui.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
#!/usr/bin/env python3
########################################################################
# Copyright 2019 Bernd Breitenbach
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>
#
########################################################################
import sys
import os
import json
import hashlib
import base64
import struct
import traceback
import codecs
import threading
import queue
import argparse
import cgi
import webbrowser
import socket
import time
import re
import ctypes
from select import select
from http.server import SimpleHTTPRequestHandler,HTTPServer
from PIL import Image,ImageDraw,ImageFont
from urllib.parse import parse_qs
from io import BytesIO
from engraver import Logger,Engraver,EngraverData,DESCRIPTION,VERSION,unitValue,imageTrf,UI,contrastBrightnessValue
##############################################################################
FONTDIR='fonts'
STREAM = 0x0
TEXT = 0x1
BINARY = 0x2
CLOSE = 0x8
PING = 0x9
PONG = 0xA
HEADERB1 = 1
HEADERB2 = 3
LENGTHSHORT = 4
LENGTHLONG = 5
MASK = 6
PAYLOAD = 7
STATUS_CODES = [1000, 1001, 1002, 1003, 1007, 1008, 1009, 1010, 1011, 3000, 3999, 4000, 4999]
# key value store
STORAGE={}
def StoreImage(fd):
global args
img=Image.open(fd)
img.load()
img=EngraverData.preprocessImage(img,args)
STORAGE['image']=img
class Websocket(object):
def __init__(self,socket,registry):
self.socket=socket
self.registry=registry
self.fileno=lambda s=self: s.socket
self.fin = 0
self.data = bytearray()
self.opcode = 0
self.hasmask = 0
self.maskarray = None
self.length = 0
self.lengtharray = None
self.index = 0
self.request = None
self.usingssl = False
self.frag_start = False
self.frag_type = BINARY
self.frag_buffer = None
self.frag_decoder = codecs.getincrementaldecoder('utf-8')(errors='strict')
self.closed = False
self.lastupdate=0;
self.state = HEADERB1
# restrict the size of header and payload for security reasons
self.maxheader = 65536
self.maxpayload = 33554432
self.registry.Register(self)
def DoRead(self):
msg=self.socket.recv(16384)
if msg:
for d in msg:
self.DecodeMessage(d)
else:
self.DoClose()
def DoClose(self):
print ('websocket closed',self.fileno())
self.socket.close()
self.registry.Unregister(self)
def DoWrite(self,data):
if isinstance(data, str):
opcode = TEXT
data = data.encode('utf-8')
else:
opcode = BINARY
payload = bytearray()
b1 = 0x80|opcode
length = len(data)
payload.append(b1)
if length <= 125:
b2 = length
payload.append(b2)
elif length >= 126 and length <= 65535:
b2 = 126
payload.append(b2)
payload.extend(struct.pack("!H", length))
else:
b2 = 127
payload.append(b2)
payload.extend(struct.pack("!Q", length))
if length > 0:
payload.extend(data)
self.socket.send(payload)
def HandlePacket(self):
if self.opcode == CLOSE:
status = 1000
reason = u''
length = len(self.data)
if length == 0:
pass
elif length >= 2:
status = struct.unpack_from('!H', self.data[:2])[0]
reason = self.data[2:]
if status not in STATUS_CODES:
status = 1002
if len(reason) > 0:
try:
reason = reason.decode('utf8', errors='strict')
except:
status = 1002
else:
status = 1002
return
elif self.fin == 0:
if self.opcode != STREAM:
if self.opcode == PING or self.opcode == PONG:
raise Exception('control messages can not be fragmented')
self.frag_type = self.opcode
self.frag_start = True
self.frag_decoder.reset()
if self.frag_type == TEXT:
self.frag_buffer = []
utf_str = self.frag_decoder.decode(self.data, final = False)
if utf_str:
self.frag_buffer.append(utf_str)
else:
self.frag_buffer = bytearray()
self.frag_buffer.extend(self.data)
else:
if self.frag_start is False:
raise Exception('fragmentation protocol error')
if self.frag_type == TEXT:
utf_str = self.frag_decoder.decode(self.data, final = False)
if utf_str:
self.frag_buffer.append(utf_str)
else:
self.frag_buffer.extend(self.data)
else:
if self.opcode == STREAM:
if self.frag_start is False:
raise Exception('fragmentation protocol error')
if self.frag_type == TEXT:
utf_str = self.frag_decoder.decode(self.data, final = True)
self.frag_buffer.append(utf_str)
self.data = u''.join(self.frag_buffer)
else:
self.frag_buffer.extend(self.data)
self.data = self.frag_buffer
self.registry.Receive(self.data)
self.frag_decoder.reset()
self.frag_type = BINARY
self.frag_start = False
self.frag_buffer = None
elif self.opcode == PING:
self._sendMessage(False, PONG, self.data)
elif self.opcode == PONG:
pass
else:
if self.frag_start is True:
raise Exception('fragmentation protocol error')
if self.opcode == TEXT:
try:
self.data = self.data.decode('utf8', errors='strict')
except Exception as exp:
raise Exception('invalid utf-8 payload')
self.registry.Receive(self.data)
def DecodeMessage(self, byte):
if self.state == HEADERB1:
self.fin = byte & 0x80
self.opcode = byte & 0x0F
self.state = HEADERB2
self.index = 0
self.length = 0
self.lengtharray = bytearray()
self.data = bytearray()
rsv = byte & 0x70
if rsv != 0:
raise Exception('RSV bit must be 0')
elif self.state == HEADERB2:
mask = byte & 0x80
length = byte & 0x7F
if self.opcode == PING and length > 125:
raise Exception('ping packet is too large')
if mask == 128:
self.hasmask = True
else:
self.hasmask = False
if length <= 125:
self.length = length
# if we have a mask we must read it
if self.hasmask is True:
self.maskarray = bytearray()
self.state = MASK
else:
# if there is no mask and no payload we are done
if self.length <= 0:
try:
self.HandlePacket()
finally:
self.state = HEADERB1
self.data = bytearray()
# we have no mask and some payload
else:
#self.index = 0
self.data = bytearray()
self.state = PAYLOAD
elif length == 126:
self.lengtharray = bytearray()
self.state = LENGTHSHORT
elif length == 127:
self.lengtharray = bytearray()
self.state = LENGTHLONG
elif self.state == LENGTHSHORT:
self.lengtharray.append(byte)
if len(self.lengtharray) > 2:
raise Exception('short length exceeded allowable size')
if len(self.lengtharray) == 2:
self.length = struct.unpack_from('!H', self.lengtharray)[0]
if self.hasmask is True:
self.maskarray = bytearray()
self.state = MASK
else:
# if there is no mask and no payload we are done
if self.length <= 0:
try:
self.HandlePacket()
finally:
self.state = HEADERB1
self.data = bytearray()
# we have no mask and some payload
else:
#self.index = 0
self.data = bytearray()
self.state = PAYLOAD
elif self.state == LENGTHLONG:
self.lengtharray.append(byte)
if len(self.lengtharray) > 8:
raise Exception('long length exceeded allowable size')
if len(self.lengtharray) == 8:
self.length = struct.unpack_from('!Q', self.lengtharray)[0]
if self.hasmask is True:
self.maskarray = bytearray()
self.state = MASK
else:
# if there is no mask and no payload we are done
if self.length <= 0:
try:
self.HandlePacket()
finally:
self.state = HEADERB1
self.data = bytearray()
# we have no mask and some payload
else:
#self.index = 0
self.data = bytearray()
self.state = PAYLOAD
# MASK STATE
elif self.state == MASK:
self.maskarray.append(byte)
if len(self.maskarray) > 4:
raise Exception('mask exceeded allowable size')
if len(self.maskarray) == 4:
# if there is no mask and no payload we are done
if self.length <= 0:
try:
self.HandlePacket()
finally:
self.state = HEADERB1
self.data = bytearray()
# we have no mask and some payload
else:
#self.index = 0
self.data = bytearray()
self.state = PAYLOAD
# PAYLOAD STATE
elif self.state == PAYLOAD:
if self.hasmask is True:
self.data.append( byte ^ self.maskarray[self.index % 4] )
else:
self.data.append( byte )
# if length exceeds allowable size then we except and remove the connection
if len(self.data) >= self.maxpayload:
raise Exception('payload exceeded allowable size')
# check if we have processed length bytes; if so we are done
if (self.index+1) == self.length:
try:
self.HandlePacket()
finally:
#self.index = 0
self.state = HEADERB1
self.data = bytearray()
else:
self.index += 1
##############################################################################
def parseTrf(trf):
if trf:
trf=[imageTrf(i) for i in trf.split(' ')]
return trf
class GUIHandler(SimpleHTTPRequestHandler):
def __init__(self,fd,addr,server):
self.server=server
self.hand_over=False
return SimpleHTTPRequestHandler.__init__(self,fd,addr,server)
def handle(self):
self.close_connection = 1
self.handle_one_request()
while not self.close_connection and not self.hand_over:
self.handle_one_request()
def finish(self):
if not self.wfile.closed:
try:
self.wfile.flush()
except socket.error:
# A final socket error may have occurred here, such as
# the local error ECONNABORTED.
pass
if not self.hand_over:
self.wfile.close()
self.rfile.close()
def _JSONHeader(self):
self.send_response(200)
self.send_header("Content-Type","application/json; charset=utf-8");
self.end_headers()
def log_message(self, format, *args):
sys.stderr.write("%s - - [%s] %s\n" %
(self.client_address[0],
self.log_date_time_string(),
format%args))
def GenSecAccept(self,key):
UUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
k = key.encode('ascii') + UUID.encode('ascii')
k_s = base64.b64encode(hashlib.sha1(k).digest()).decode('ascii')
return k_s
def CreateWS(self,args):
key=self.headers.get("Sec-WebSocket-Key")
if key and self.headers.get("Upgrade","").lower().index("websocket")>=0:
self.hand_over=True
self.close_connection=0
self.protocol_version = "HTTP/1.1"
self.send_response(101)
self.send_header("Access-Control-Allow-Credentials","true")
self.send_header("Access-Control-Allow-Headers","content-type")
self.send_header("Access-Control-Allow-Headers","authorization")
self.send_header("Access-Control-Allow-Headers","x-websocket-extensions")
self.send_header("Access-Control-Allow-Headers","x-websocket-version")
self.send_header("Access-Control-Allow-Headers","x-websocket-protocol")
self.send_header("Connection","Upgrade")
self.send_header("Sec-WebSocket-Accept",self.GenSecAccept(key))
self.send_header("Upgrade","websocket")
self.end_headers()
self.server.KeepOpen()
ws=Websocket(self.request,self.server)
else:
self.send_error(400,"illegal request")
def output(self,text,flush=False):
self.wfile.write(bytes(text,'utf-8'))
if flush:
self.wfile.flush()
def GetFonts(self,args):
exp=re.compile("\.(ttf|pfb)$")
fonts=list(filter(lambda f: exp.search(f,re.I)!=None,os.listdir(FONTDIR)))
fonts.sort()
digest=hashlib.sha1(bytes(str(fonts),'utf-8')).hexdigest()
flist=[]
if STORAGE.get('fonthash')!=digest:
for f in fonts:
try:
fname=ImageFont.truetype('%s/%s'%(FONTDIR,f)).getname()
fname="%s (%s)"%(fname[0],fname[1])
flist.append({'name':fname,'file':f})
except:
Logger.LOGGER.error("cannot load font from file '%s'\n",f)
STORAGE['fonts']=flist
STORAGE['fonthash']=digest
self._JSONHeader()
self.output(json.dumps(STORAGE['fonts']))
def SendImage(self,img):
self.send_response(200)
self.send_header("Pragma-directive","no-cache")
self.send_header("Cache-directive","no-cache")
self.send_header("Cache-Control","no-store, no-cache, must-revalidate")
self.send_header("Content-Type","image/png");
self.end_headers()
fd = BytesIO()
img.save(fd, "png")
self.wfile.write(fd.getvalue())
self.wfile.flush()
def RenderImageFromText(self,dict):
for p in ['text','font','width','height']:
if p not in dict:
self.send_error(404, "parameter '%s' is missing"%p)
return
hsh=hashlib.sha1()
global args
args.text=dict['text'][0]
hsh.update(bytes(args.text,'utf-8'))
args.size=(unitValue(dict['width'][0]),unitValue(dict['height'][0]))
hsh.update(bytes(str(args.size),'utf-8'))
args.font="%s/%s"%(FONTDIR,dict['font'][0])
hsh.update(bytes(args.font,'utf-8'))
digest=hsh.hexdigest()
if STORAGE.get('imagehash')==digest:
img=STORAGE['textimage']
else:
STORAGE['imagehash']=digest
img=EngraverData.imageFromText(args)
STORAGE['textimage']=img
args.trf=parseTrf(dict.get('trf',[None])[0])
self.SendImage(EngraverData._trfImage(img.copy(),args))
def _getEnhanceValue(self,dict,key):
res=None
val=dict.get(key)
if val and abs(float(val[0]))>0.001:
res=contrastBrightnessValue(val[0])
return res
def RenderImage(self,dict):
for p in ['width','height']:
if p not in dict:
self.send_error(404, "parameter '%s' is missing"%p)
return
global args
args.size=(unitValue(dict['width'][0]),unitValue(dict['height'][0]))
args.trf=parseTrf(dict.get('trf',[None])[0])
args.contrast=self._getEnhanceValue(dict,'contrast')
args.brightness=self._getEnhanceValue(dict,'brightness')
img=EngraverData.processImage(STORAGE['image'].copy(),args)
self.SendImage(img)
def do_GET(self):
dict={}
l=self.path.split('?')
if len(l)>1: dict=parse_qs(l[1])
f=self.pathtofunc.get(l[0])
if not f:
self.path='/web%s'%self.path
self.log_message("path:'%s' => '%s'",self.path,self.translate_path(self.path))
if self.translate_path(self.path).startswith(os.getcwd()+os.sep+'web'):
SimpleHTTPRequestHandler.do_GET(self)
else:
self.send_error(404, "File not found")
else:
f(self,dict)
def do_POST(self):
l=self.path.split('?')
f=self.ppathtofunc.get(l[0])
if not f:
self.send_error(404, "File not found")
else:
f(self)
def SaveImage(self):
global args
content_length = int(self.headers['Content-Length'])
ctype, pdict = cgi.parse_header(self.headers['Content-Type'])
if ctype == 'multipart/form-data':
encoding='utf-8'
errors='replace'
boundary = pdict['boundary']
ctype = self.headers['Content-Type']
headers = cgi.Message()
headers.set_type(ctype)
headers['Content-Length'] = self.headers['Content-Length']
fs = cgi.FieldStorage(self.rfile, headers=headers, encoding=encoding, errors=errors,
environ={'REQUEST_METHOD': 'POST'})
try:
image=fs['file']
data=fs.getlist('file')[0]
except:
image=None
if image!=None:
fd=BytesIO(data)
StoreImage(fd)
self.send_response(200)
self.end_headers()
else:
self.send_error(400,"content dispostion `file0` expected\n",'utf-8')
else:
self.send_error(400,"content type `multipart/form-data` expected\n",'utf-8')
pathtofunc={
'/ws':CreateWS,
'/fonts':GetFonts,
'/textimage':RenderImageFromText,
'/image':RenderImage
}
ppathtofunc={
'/image':SaveImage
}
class Httpd(HTTPServer):
allow_reuse_address = True
def __init__(self,bind,port):
HTTPServer.__init__(self,(bind,port), GUIHandler)
self.listeners={}
self.intervall=0.5
self.msg=None
self.do_close=True
self.messageHandler=lambda p: None
self.Register(self)
self.lock=threading.Lock()
def DoRead(self):
self.handle_request()
def DoWrite(self,msg):
pass
def DoClose(self):
os.close(self.fileno())
def KeepOpen(self):
self.do_close=False
def shutdown_request(self, request):
if self.do_close:
HTTPServer.shutdown_request(self,request)
else:
print ("do close:",self.do_close,request.fileno())
self.do_close=True
def Register(self,client):
if client.fileno()!=None:
self.listeners[client.fileno()]=client
def Unregister(self,listener):
if type(listener)!=int:
listener=listener.fileno()
try:
del self.listeners[listener]
except:
pass
def SetMessageHandler(self,handler):
self.messageHandler=handler
def HandleRead(self,fd):
self.listeners[fd].DoRead()
def HandleClose(self,fd):
client=self.listeners[fd]
client.DoClose()
self.Unregister(client)
def Loop(self):
while self.listeners:
fds=self.listeners.keys()
rList, dummy, xList = select(fds, [], fds, self.intervall)
for ready in rList:
if ready not in fds:
continue
try:
self.HandleRead(ready)
except Exception as n:
print(n)
self.HandleClose(ready)
for failed in xList:
self.HandleClose(ready)
def Send(self,obj):
self.lock.acquire()
msg=json.dumps(obj)
for client in self.listeners.values():
client.DoWrite(msg)
self.lock.release()
def Receive(self,msg):
obj=json.loads(msg)
self.messageHandler.receive(obj)
class StdoutClient(object):
def DoWrite(self,msg):
print(msg)
def DoRead(self):
os.read(1,16384)
def fileno(self):
return None
class ExternalLogger(Logger):
def __init__(self,verbosity,channel):
Logger.__init__(self,verbosity)
self.success=True
self.channel=channel
def fatal(self,fmt,*args):
self.log("FATAL",fmt,*args)
def log(self,severity,fmt,*args):
lev=self.LEVELS.get(severity,99)
if lev<self.LEVELS["WARN"]:
self.success=False
if self.verbosity>=lev:
self.channel.Send({'type':'message','severity':severity,'content':fmt%args})
def resetError(self):
res=self.success
self.success=True
return res
class Worker(threading.Thread):
def __init__(self,engraver,channel):
self.engraver=engraver
self.channel=channel
self.doStop=False
self.busy=False
self.engraving=False
self.framing=False
self.centerAxis=None
self.useCenter=False
self.burner=None
self.queue=queue.Queue(1)
self.commands={
'connect':self.connect,
'disconnect':self.disconnect,
'fan':Engraver.fan,
'home':Engraver.home,
'nop':lambda e: None,
'move':Engraver.move,
'status':lambda e: None,
'frameStart':self.frameStart,
'frameStop':self.frameStop,
'engrave':self.engrave,
'stopEngraving':self.stopEngraving
}
threading.Thread.__init__(self)
self.setDaemon(True)
def connect(self,engraver):
if not engraver.isOpened():
engraver.open()
if engraver.isOpened():
engraver.connect()
engraver.fan(True)
def disconnect(self,engraver):
engraver.close()
def status(self,engraver):
return {'version':VERSION,
'type':'status',
'connected':engraver.isOpened(),
'engraving':self.engraving,
'framing':self.framing,
'fanOn':engraver.isFanOn(),
'success':Logger.LOGGER.resetError(),
'centerAxis':self.centerAxis,
'useCenter':self.useCenter
}
def frameStart(self,engraver,fx,fy,useCenter,centerAxis):
self.centerAxis=centerAxis
self.useCenter=useCenter
self.framing=True
engraver.frameStart(fx,fy,useCenter,centerAxis)
def frameStop(self,engraver,fx,fy,useCenter,centerAxis):
self.framing=False
engraver.frameStop(fx,fy,useCenter,centerAxis)
def engrave(self,engraver,mode,useCenter,trf,width,height,power,depth):
global args
if mode=='image':
img=STORAGE['image'].copy()
else:
img=STORAGE['textimage'].copy()
args.size=(width,height)
args.trf=parseTrf(trf)
args.power=power
args.depth=depth
data=EngraverData._imageToData(img,args)
self.burner=BurnThread(self,engraver,data,useCenter)
self.burner.start()
self.engraving=True
def stopEngraving(self,engraver):
if self.burner:
self.burner.pause()
def run(self):
while not self.doStop:
msg=self.queue.get()
self.busy=True
cmd=self.commands.get(msg.get('cmd'))
if cmd:
res=cmd(self.engraver,**msg.get('args',{}))
if res!=None:
self.channel.Send(res)
else:
Logger.LOGGER.error("unknown command: %s\n",msg.get('cmd'))
self.channel.Send(self.status(self.engraver))
self.busy=False
def stop(self):
self.doStop=True
self.receive({"cmd":"nop"})
def receive(self,obj):
self.queue.put(obj)
def engravingDone(self):
self.engraving=False
self.burner=None
self.receive({"cmd":"nop"})
class UrlOpener(threading.Thread):
def __init__(self,bname,host,port):
self.port=port
self.host=host
self.browser=None
if not bname:
self.browser=webbrowser.get()
else:
self.browser=webbrowser.get(bname)
threading.Thread.__init__(self)
self.setDaemon(True)
def run(self):
if not self.browser:
Logger.LOGGER.error("could not find webbrowser '%s'",args.browser)
return
while True:
time.sleep(0.1)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((self.host,self.port))
if result == 0:
sock.close()
break
self.browser.open_new_tab('http://%s:%s'%(self.host,self.port))
class BurnThread(threading.Thread):
def __init__(self,worker, engraver,data,useCenter):
threading.Thread.__init__(self)
self.engraver=engraver
self.useCenter=useCenter
self.data=data
self.worker=worker
def run(self):
self.thread_id=threading.current_thread().ident
engraver.burn(self.data,self.useCenter)
worker.engravingDone()
def pause(self):
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(self.thread_id),ctypes.py_object(KeyboardInterrupt))
if res != 1:
print('Exception raise failure')
UI.setAsk(lambda msg: True)
parser = argparse.ArgumentParser(description=DESCRIPTION,formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-d', '--device',metavar="device",help='the serial device',default="/dev/ttyUSB0")
parser.add_argument('-s', '--speed',metavar="speed",help='the speed of the serial device',type=int,default=115200)
parser.add_argument('-v', '--verbosity',help='increase verbosity level ',action='count',default=0)
parser.add_argument('--limit', help='set maximum no. of steps in x/y direction',metavar=('steps'),dest='lim',type=int,default=1575)
parser.add_argument('-b', '--browser',metavar="browser",help='use browser to open gui, set to - to not open the gui',default='')
parser.add_argument('-B', '--bind',metavar="bind",help='use the given address to bind to; use 0.0.0.0 for all interfaces',
default='127.0.0.1')
parser.add_argument('-P', '--port',metavar="port",help='use the given port',
default=8008)
parser.add_argument('-T','--transform', help=argparse.SUPPRESS,dest='trf')
parser.add_argument('--dry-run',dest='dummy', help=argparse.SUPPRESS)
parser.add_argument('--invert',dest='invert', help=argparse.SUPPRESS,default=False,action='store_true')
parser.add_argument('--brightness',dest='brightness', help=argparse.SUPPRESS,default=None)
parser.add_argument('--contrast',dest='contrast', help=argparse.SUPPRESS,default=None)
args = parser.parse_args()
if args.browser!='-':
if args.bind not in ['0.0.0.0','127.0.0.1']:
host=args.bind
else:
host='localhost'
UrlOpener(args.browser,host,args.port).start()
httpd = Httpd(args.bind,args.port)
httpd.Register(StdoutClient())
Logger.set(ExternalLogger(args.verbosity,httpd))
engraver=Engraver(args)
worker=Worker(engraver,httpd)
httpd.SetMessageHandler(worker)
StoreImage('web/logo.png')
worker.start()
httpd.Loop()