Newer
Older
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
#!/usr/bin/env python
import select
import re
import time
import array
import struct
"""*****************************************************"""
""" """
""" Software Defines & Parmeters """
""" """
"""*****************************************************"""
SOFTWARE_VERSION = '1.0'
RS232_BAUDRATE = 115200
SERVER_TCP_IP = '192.168.1.1'
SERVER_TCP_PORT = 3081
SERVER_BUFFER_SIZE = 128
SERVER_TIMEOUT = 600
RETRIES = 3
"""*****************************************************"""
""" """
""" Numeriflash Xml Parser """
""" """
"""*****************************************************"""
from xml.dom import minidom
class NumeriflashXmlParser:
@staticmethod
def getValueNode( node ):
for nv in node.childNodes:
if nv.nodeType == 1: ##Node.ELEMENT_NODE:
if nv.nodeName == "value":
return nv
@staticmethod
def parseCommandNode( node ):
""" A command node should have the following form :
<command name="XXXXX"><value type="XX">XXXXX</value></command> """
try:
nv = NumeriflashXmlParser.getValueNode( node )
name = node.attributes.item(0).value
try: type = nv.attributes.item(0).value
except: type = None
try: val = nv.childNodes[0].nodeValue
except: val = None
if type is None: return (name, val)
elif type == "i" or type == "l": return (name, int( val ))
elif type == "f" or type == "d": return (name, float( val ))
else: return (name, val)
except:
return None
@staticmethod
def parse( data ):
doc = minidom.parseString( data )
""" First, get Numeriflash Node """
node_Numeriflash = None
for node in doc.childNodes:
if node.nodeType == 1: ##Node.ELEMENT_NODE:
if node.nodeName == "Numeriflash":
node_Numeriflash = node
break
if node_Numeriflash is None:
return None
""" Then, get the command node(s) """
resList = []
for node in node_Numeriflash.childNodes:
if node.nodeType == 1: ##Node.ELEMENT_NODE:
if node.nodeName == "command":
res = NumeriflashXmlParser.parseCommandNode( node )
if not res is None:
resList.append( res )
return resList
"""*****************************************************"""
""" """
""" Numeriflash Xml Server """
""" """
"""*****************************************************"""
import threading
import socket
class NumeriflashXmlServerThread( threading.Thread ):
devLock = threading.Condition(threading.Lock())
watchdog = False
buf = None
def __init__(self, conn, ip, port, device=None, NumeriflashCompat=True, args=None):
threading.Thread.__init__( self )
self.__conn = conn
self.__ip = ip
self.__port = port
self.__compat = NumeriflashCompat
self.__device = device
self.__dargs = args
self.Terminated = False
print( 'Connected to ' + ip )
self.start()
def stop(self):
self.Terminated = True
self.__conn.close()
def __recoverSerial( self ):
NumeriflashXmlServerThread.devLock.acquire()
self.__device.close()
NumeriflashXmlServerThread.devLock.release()
def __listen( self ):
while( True ):
data = self.__conn.recv( SERVER_BUFFER_SIZE )
if not data:
raise IOError( 'No data received' )
try:
start = data.index( '<Numeriflash>' )
end = data.index( '</Numeriflash>', start )
res = data[ start:(end+14) ]
#self.buf = data[ (end+14): ]
return res
except:
pass
def __send( self, msg ):
msglen = len(msg)
totalsent = 0
while totalsent < msglen:
sent = self.__conn.send( msg[totalsent:] )
if sent == 0:
print( 'socket connection broken' )
raise IOError( 'socket connection broken' )
totalsent = totalsent + sent
def __parseRequest( self, data ):
res = NumeriflashXmlParser.parse( data )
name = (res[0])[0]
value = (res[0])[1]
return ( ( name, value ), Samsung2Distec.S2D( name, value ) )[ self.__compat ]
def __execute( self, name, value ):
NumeriflashXmlServerThread.devLock.acquire()
try:
result = None
if value is None:
result = self.__device.cmd( name )
else:
result = self.__device.cmd( name, value )
except:
NumeriflashXmlServerThread.devLock.release()
raise
NumeriflashXmlServerThread.devLock.release()
return result
def __validate( self, name, value ):
if ( name=='Power' and value==2 ) or ( name=='Power' and value==8 ) or ( name=='MainSourceInput' and value==1 ) or ( name=='MainSourceInput' and value==2 ):
return False
else:
return True
def run( self ):
while not self.Terminated:
cname = None
cvalue = None
resname = None
resvalue = None
res = None
# I - Recive data from client
try:
data = self.__listen()
except Exception, e:
print "Unexpected error 0 :", e
print( 'Socket Timeout => Connection closed by remote client' )
break
# II - Parse request
try:
cname, cvalue = self.__parseRequest( data )
except Exception, e:
print "Unexpected error:", e
print( 'Failed to parse client request' )
try:
self.__send( '<Numeriflash><error><value>Invalid or malformed request</value></error></Numeriflash>' )
except:
print( 'Socket Error => Connection closed by remote client' )
break
continue
# III - Execute command
try:
command = envbox_db.getCommandByName( cname )
# Avoid timeout for Numeriflash Compatibility
if command is None:
try:
( resname, resvalue ) = ( ( cname, cvalue ), Samsung2Distec.D2S( cname, cvalue ) )[ self.__compat ]
self.__send( '<Numeriflash><command name="' + resname + '"><value type="i"></value></command></Numeriflash>' )
except Exception, e:
print "Unexpected error:", e
print( 'Socket Error => Connection closed by remote client' )
break
continue
if cname=='Power':
if not cvalue is None:
if cvalue == 0:
try:
self.__device.cmd( 'PowerOffType', 0 )
except:
pass
for i in range( 0, RETRIES ):
try:
res = self.__execute( cname, cvalue )
except:
print( 'Serial IO Error' )
self.__recoverSerial()
res = None
if res is None:
print( 'Serial port timeout' )
self.__recoverSerial()
elif not self.__validate( cname, res ):
print( 'Bug : Unexpected value returned by envbox (' + cname + '=' + str( res ) + ')' )
self.__recoverSerial()
res=None
else:
break
except:
print( 'Serial IO Error' )
self.__recoverSerial()
try:
self.__send( '<Numeriflash><error><value type="s">error</value></error></Numeriflash>' )
except Exception, e:
print "Unexpected error 2:", e
print( 'Socket Error => Connection closed by remote client' )
break
continue
# IV - Build response
try:
( resname, resvalue ) = ( ( cname, res ), Samsung2Distec.D2S( cname, res ) )[ self.__compat ]
if not resvalue is None:
msg = '<Numeriflash><command name="' + resname + '"><value type="i">' + str(resvalue) + '</value></command></Numeriflash>'
else:
msg = '<Numeriflash><error><value type="s">' + 'error' + '</value></error></Numeriflash>'
except:
pass
# V - Send response
try:
self.__send( msg )
except Exception, e:
print "Unexpected error 3:", e
print( 'Socket Error => Connection closed by remote client' )
break
# VI - Tick watchdog
NumeriflashXmlServerThread.watchdog = False
#
# End run function
#
print( 'Connection from ' + str(self.__ip) + ':' + str(self.__port) + ' closed' )