forked from ianovir/HIMUServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHIMUServer.py
189 lines (172 loc) · 5.81 KB
/
HIMUServer.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
#===============================================================================
#
# MIT License
#
# HyperIMU Server (HIMU Server)
# Copyright (c) [2017] [Sebastiano Campisi - ianovir]
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
#===============================================================================
import socket
import math
import traceback
valuesPerSensor = 3
class HIMUServer:
def __init__(self, bufferSize = 2048, timeout = 10, separatorIndex = 0):
self.__listeners = []
self.__packSeparators = ["\r\n" , "#"]
self.__commentSymbol = '@'
self.timeout = timeout #timeout in seconds
self.bufferSize=bufferSize #bytes
self.go = True
if(separatorIndex == 0 ):
self.packSeparator = self.__packSeparators[0]
else:
self.packSeparator = self.__packSeparators[separatorIndex]
def addListener(self , newListener):
self.__listeners.append(newListener)
def __notifyListeners(self, recPacket):
for listener in self.__listeners:
listener.notify(recPacket)
def executeRAW(self , port):
'''
Print raw data received (UDP)
'''
UDPSocket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
UDPSocket.settimeout(timeout)
serverAddress = ('', port)
print('Listening on port ' + str(port))
UDPSocket.bind(serverAddress)
while self.go:
[data,attr] = UDPSocket.recvfrom(self.bufferSize)
if not data: break
self.__notifyListeners(self.__extractSensorData(data.decode("utf-8")))
UDPSocket.close()
def executeUDP(self , port):
'''
Performs data acquisition via UDP protocol
'''
UDPSocket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
UDPSocket.settimeout(self.timeout)
serverAddress = ('', port)
print('Listening on port ' + str(port))
UDPSocket.bind(serverAddress)
while self.go:
[data,attr] = UDPSocket.recvfrom(self.bufferSize)
if not data: break
self.__notifyListeners(self.__extractSensorData(data.decode("utf-8")))
UDPSocket.close()
def executeTCP(self , port):
'''
Performs data acquisition via TCP protocol
'''
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(self.timeout)
serverAddress = ('', port)
sock.bind(serverAddress)
sock.listen(1)
print ('waiting for connection...')
[connection, clientAddress] = sock.accept()
connection.setblocking(1)
print ('connection from ' + str(clientAddress))
while self.go:
data = connection.recv(self.bufferSize)
if not data: break
self.__notifyListeners(self.__extractSensorData(data.decode("utf-8")))
connection.close()
def executeFile(self , fileName):
'''
Performs data acquisition from local file
'''
print("Reading file " + fileName + " ...")
f = open(fileName,'r')
sline=f.readline()
while sline!='':
if sline[0]!= self.__commentSymbol:
self.__notifyListeners(self.__extractSensorData(sline))
sline=f.readline()
print('reached EOF.')
@staticmethod
def strings2Floats(listString):
'''
Converts a list of Strings to a list of floats; returns the converted list
'''
out=[]
for j in range(0, len(listString)):
if(listString[j]!=''):
out.append( float(listString[j]))
return out
@staticmethod
def printSensorsData (sensorData):
'''
Prints to console the acquired sensors'data
'''
try:
for acquisition in sensorData:
i = 1;
for sensorAcq in acquisition :
print('Sensor' + str(i) + ": " + str(sensorAcq))
i+=1
except Exception as ex:
print str(ex)
def __extractSensorData (self, dataString):
'''
Extracts sensors'data from the input raw data string.
The return object is an array of arrays [i][j], where i corresponds to sampled acquisitions and j corresponds to sensors' value.
All sensors' values are represented as strings.
'''
packages = dataString.split(self.packSeparator)
retVal = []
for pack in packages:
if pack!='' :
try:
packVal = []
packSplit = pack.replace('\n', '').replace('\r', '').split(",")
numSensors = int(math.floor( len(packSplit) / valuesPerSensor))
lFloat = HIMUServer.strings2Floats(packSplit)
for i in range(0 , numSensors):
p = packSplit[i*valuesPerSensor : (i+1)*(valuesPerSensor)]
packVal.append(p)
if(len(packVal)>0):
retVal.append(packVal)
except Exception as ex:
pass
return retVal
def start(self , protocol, arg):
'''
Executes the data acquisition;
<protocol> the supported protocol: 'UDP' , 'TCP' , 'FILE' o 'RAW'
<arg> the port number in case of UDP or TCP protocols, input file path in case of 'FILE' protocol
'''
print('protocol: ' + protocol)
try:
if protocol == 'RAW':
self.executeRAW(int(arg))
elif protocol == 'UDP':
self.executeUDP(int(arg))
elif protocol == 'TCP':
self.executeTCP(int(arg))
elif protocol == 'FILE':
self.executeFile(arg)
print("The End")
except Exception as ex:
print(str(ex))
def stop(self ):
self.go = false