-
Notifications
You must be signed in to change notification settings - Fork 1
/
rfcomm-server.py
210 lines (162 loc) · 7.49 KB
/
rfcomm-server.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
# file: rfcomm-server.py
# auth: Albert Huang <[email protected]>
# desc: simple demonstration of a server application that uses RFCOMM sockets
#
# $Id: rfcomm-server.py 518 2007-08-10 07:20:07Z albert $
# adapted by N. Bertuol and C. Pham
# University of Pau, France
#
from bluetooth import *
from MongoDB import get_documents, get_nodes_names
import datetime
import signal
import sys
server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
advertise_service( server_sock, "SampleServer",
service_id = uuid,
service_classes = [ uuid, SERIAL_PORT_CLASS ],
profiles = [ SERIAL_PORT_PROFILE ],
# protocols = [ OBEX_UUID ]
)
def signal_handler(signal, frame):
print('You pressed Ctrl+C or killed the process!')
print("disconnected")
#client_sock.close()
server_sock.close()
print "Bye."
sys.exit(0)
if (len(sys.argv)==2 and sys.argv[1]=="-bg"):
# if we run in background, we do not want to catch the CTRL-C signal, only the SIGTERM from kill -9
signal.signal(signal.SIGTERM, signal_handler)
else:
signal.signal(signal.SIGINT, signal_handler)
while True:
print "Waiting for connection on RFCOMM channel %d" % port
sys.stdout.flush()
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info
sys.stdout.flush()
try:
data = client_sock.recv(1024)
if len(data) == 0: break
print "received [%s]" % data
#send data in a string (date and data only)
if data.startswith('data'):
#data = 'data$nodes$dates'
#getting nodes names and dates(begin and end)
preferences = data.split('$')
nodeArray = None
beginDate = None
endDate = None
#check if nodes and dates were set
if(not preferences[1] == ''):
nodeArray = preferences[1].split(';') #array of nodes names
if(not preferences[2] == ''):
dateArray = preferences[2].split('-') #array of strings representing beginDate and endDate
#beginDate is the first date retrieved from preferences
if(dateArray[0].startswith('begin_')):
auxDate = dateArray[0].split('_') #retrieving {'begin',day,month-1,year}
beginDate = datetime.datetime(int(auxDate[3]), int(auxDate[2])+1, int(auxDate[1]), 0, 0, 0, 0)
auxDate = dateArray[1].split('_') #retrieving {'end',day,month-1,year}
endDate = datetime.datetime(int(auxDate[3]), int(auxDate[2])+1, int(auxDate[1]), 23, 59, 59, 999999)
#endDate is the first date retrieved from preferences
else:
auxDate = dateArray[1].split('_') #retrieving {'begin',day,month-1,year}
beginDate = datetime.datetime(int(auxDate[3]), int(auxDate[2])+1, int(auxDate[1]), 0, 0, 0, 0)
auxDate = dateArray[0].split('_') #retrieving {'end',day,month-1,year}
endDate = datetime.datetime(int(auxDate[3]), int(auxDate[2])+1, int(auxDate[1]), 23, 59, 59, 999999)
#reinitialize data
data = ''
#check if no node is checked, send an error message
if((nodeArray is None) and (not beginDate is None) and (not endDate is None)):
data = 'ERROR : no node is selected, please select at least one node in the preferences page.!'
print('ERROR: no node is checked')
#check if beginDate <= endDate
elif((not beginDate is None) and (not endDate is None) and (beginDate > endDate)):
data = 'ERROR : the begin date ('+beginDate.strftime("%d-%m-%Y")+') you have selected in the preferences page has passed after the selected end date ('+endDate.strftime("%d-%m-%Y")+'). Please, you must change your choosen dates in the preferences page.!'
print('ERROR : beginDate > endDate')
#check if good preferences are set (all preferences set, no preferences set or only nodes preferences set)
else:
documents = get_documents(nodeArray, beginDate, endDate)
for doc in documents:
data += 'NODE: '+str(doc['node_eui'])+' DATE: '+str(doc['time'])+' DATA: '+str(doc['data'])+'\n'
data += '!'
print("sending data")
sys.stdout.flush()
#send data which will be saved in a csv file (values separated by ";")
elif data.startswith('file'):
#data = 'data$nodes$dates'
#getting nodes names and dates(begin and end)
preferences = data.split('$')
nodeArray = None
beginDate = None
endDate = None
#check if nodes and dates were set
if(not preferences[1] == ''):
nodeArray = preferences[1].split(';') #array of nodes names
if(not preferences[2] == ''):
dateArray = preferences[2].split('-') #array of strings representing beginDate and endDate
#beginDate is the first date retrieved from preferences
if(dateArray[0].startswith('begin_')):
auxDate = dateArray[0].split('_') #retrieving {'begin',day,month-1,year}
beginDate = datetime.datetime(int(auxDate[3]), int(auxDate[2])+1, int(auxDate[1]), 0, 0, 0, 0)
auxDate = dateArray[1].split('_') #retrieving {'end',day,month-1,year}
endDate = datetime.datetime(int(auxDate[3]), int(auxDate[2])+1, int(auxDate[1]), 23, 59, 59, 999999)
#endDate is the first date retrieved from preferences
else:
auxDate = dateArray[1].split('_') #retrieving {'begin',day,month-1,year}
beginDate = datetime.datetime(int(auxDate[3]), int(auxDate[2])+1, int(auxDate[1]), 0, 0, 0, 0)
auxDate = dateArray[0].split('_') #retrieving {'end',day,month-1,year}
endDate = datetime.datetime(int(auxDate[3]), int(auxDate[2])+1, int(auxDate[1]), 23, 59, 59, 999999)
#reinitialize data
data = ''
#check if no node is checked, send an error message
if((nodeArray is None) and (not beginDate is None) and (not endDate is None)):
data = 'ERROR : no node is selected, please select at least one node in the preferences page.!'
print('ERROR: no node is checked')
#check if beginDate <= endDate
elif((not beginDate is None) and (not endDate is None) and (beginDate > endDate)):
data = 'ERROR : the begin date ('+beginDate.strftime("%d-%m-%Y")+') you have selected in the preferences page has passed after the selected end date ('+endDate.strftime("%d-%m-%Y")+'). Please, you must change your choosen dates in the preferences page.!'
print('ERROR : beginDate > endDate')
#check if good preferences are set (all preferences set, no preferences set or only nodes preferences set)
else:
data = 'type;gateway_eui;node_eui;snr;rssi;cr;datarate;time;data\n'
documents = get_documents(nodeArray, beginDate, endDate)
for doc in documents:
data += str(doc['type'])+';'
data += str(doc["gateway_eui"])+";"
data += str(doc["node_eui"])+";"
data += str(doc["snr"])+";"
data += str(doc["rssi"])+";"
data += str(doc["cr"])+";"
data += str(doc["datarate"])+";"
data += str(doc["time"].isoformat())+";"
data += str(doc["data"])
data += "\n"
data += '!'
print("sending data to be in the csv file")
#send node names in a string ("node1#datebegin_dateend;node2#datebegin_dateend;...")
elif data == 'nodes':
data = get_nodes_names()
data += '!'
print("sending nodes names")
#send a message which explains that the entry is wrong
else:
data = 'ERROR : BAD_ENTRY, PLEASE USE THE APP!'
print("bad entry")
sys.stdout.flush()
client_sock.send(data)
except IOError:
pass
except KeyboardInterrupt:
print("disconnected")
client_sock.close()
server_sock.close()
print "all done"
break
sys.stdout.flush()