1
1
import base64
2
2
import os
3
+ import sys
3
4
import tempfile
4
5
import logging
5
6
@@ -26,8 +27,13 @@ def __init__(self, version, queue):
26
27
27
28
self .version = version
28
29
self .queue = queue
30
+ self .appdir = os .path .dirname (sys .argv [0 ])
31
+ self .logger .debug ("Application started from: %s" , self .appdir )
32
+
29
33
queue .put ((10 , 'INFO' , 'Server started' ))
30
34
35
+ propellerLoad = PropellerLoad ()
36
+
31
37
@cherrypy .expose ()
32
38
@cherrypy .tools .json_out ()
33
39
@cherrypy .tools .allow (methods = ['GET' ])
@@ -57,13 +63,13 @@ def ports(self):
57
63
for port in ports :
58
64
self .logger .debug ('Port %s discovered.' , port )
59
65
if ' bt ' not in port .lower () and 'bluetooth' not in port .lower ():
60
- self .logger .debug ('Port %2 appended to list.' , port )
61
66
filtered_ports .append (port )
67
+ self .logger .debug ("Port %2 appended to list." , port )
62
68
return filtered_ports
63
69
else :
64
70
# No useable ports detected. Need to determine how the browser
65
71
# handles an empty list of available ports.
66
- self .logger .debug (' No ports detected. Replying with /dev/null' )
72
+ self .logger .debug (" No ports detected. Replying with /dev/null" )
67
73
return '/dev/null'
68
74
69
75
@@ -72,20 +78,27 @@ def ports(self):
72
78
@cherrypy .tools .allow (methods = ['POST' ])
73
79
def load (self , action , binary , extension , comport = None ):
74
80
if action is None :
75
- logger = logging .getLogger ('blockly.server' )
76
- logger .error ('Load action is undefined.' )
81
+ self .logger .error ('Load action is undefined.' )
82
+ return {
83
+ 'message' : 'Load action is undefined' ,
84
+ 'success' : False
85
+ }
77
86
78
87
cherrypy .response .headers ['Access-Control-Allow-Origin' ] = '*'
79
88
80
89
self .logger .debug ('Writing program payload to temp file.' )
90
+
81
91
binary_file = tempfile .NamedTemporaryFile (suffix = extension , delete = False )
82
92
binary_file .write (base64 .b64decode (binary ))
83
93
binary_file .close ()
94
+
84
95
self .logger .debug ('%s saved.' , binary_file .name )
85
96
86
97
self .logger .debug ('Loading program to device.' )
98
+
87
99
(success , out , err ) = self .propellerLoad .load (action , binary_file , comport )
88
100
self .queue .put ((10 , 'INFO' , 'Application loaded (%s)' % action ))
101
+
89
102
self .logger .info ('Application load complete.' )
90
103
91
104
os .remove (binary_file .name )
@@ -102,8 +115,6 @@ def serial_socket(self):
102
115
self .queue .put ((10 , 'INFO' , 'Serial socket set up' ))
103
116
handler = cherrypy .request .ws_handler
104
117
105
- propellerLoad = PropellerLoad ()
106
-
107
118
108
119
def main (port , version , queue ):
109
120
module_logger .info ("Server starting" )
0 commit comments