-
Notifications
You must be signed in to change notification settings - Fork 1
/
CloudMongoDB.py
181 lines (144 loc) · 4.95 KB
/
CloudMongoDB.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
#-------------------------------------------------------------------------------
# Copyright 2016 Congduc Pham, University of Pau, France.
#
#
# This file is part of the low-cost LoRa gateway developped at University of Pau
#
# 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 the program. If not, see <http://www.gnu.org/licenses/>.
#-------------------------------------------------------------------------------
import os
import json
import time
import datetime
import sys
import re
global add_document, remove_if_new_month, mongodb_set_max_months
from MongoDB import add_document, remove_if_new_month, mongodb_set_max_months
#see how we can get additional information for the internal usage of the script
#
#name of json file containing the cloud declarations
cloud_filename = "clouds.json"
#enabled cloud array
_enabled_clouds = []
#open json file to retrieve enabled clouds
f = open(os.path.expanduser(cloud_filename),"r")
string = f.read()
f.close()
#change it into a python array
json_array = json.loads(string)
#retrieving all cloud declarations
clouds = json_array["clouds"]
#here we check for our own script entry
#
for cloud in clouds:
if "CloudMongoDB.py" in cloud["script"]:
mongodb_set_max_months(cloud["max_months_to_store"])
print "MongoDB with max months to store is %d" % cloud["max_months_to_store"]
# main
# -------------------
def main(ldata, pdata, rdata, tdata, gwid):
now = datetime.datetime.utcnow()
# this is common code to process packet information provided by the main gateway script (i.e. post_processing_gw.py)
# these information are provided in case you need them
arr = map(int,pdata.split(','))
dst=arr[0]
ptype=arr[1]
src=arr[2]
seq=arr[3]
datalen=arr[4]
SNR=arr[5]
RSSI=arr[6]
arr = map(int,rdata.split(','))
bw=arr[0]
cr=arr[1]
sf=arr[2]
# this part depends on the syntax used by the end-device
# we use: TC/22.4/HU/85...
#
# but we accept also a_str#b_str#TC/22.4/HU/85...
# or simply 22.4 in which case, the nomemclature will be DEF
# get number of '#' separator
nsharp = ldata.count('#')
nslash=0
#will field delimited by '#' in the MongoDB
data=['','']
#no separator
if nsharp==0:
# get number of '/' separator on ldata
nslash = ldata.count('/')
#contains ['', '', "s1", s1value, "s2", s2value, ...]
data_array = data + re.split("/", ldata)
else:
data_array = re.split("#", ldata)
# only 1 separator
if nsharp==1:
# get number of '/' separator on data_array[1]
nslash = data_array[1].count('/')
# then reconstruct data_array
data_array=data + re.split("/", data_array[1])
# we have 2 separators
if nsharp==2:
# get number of '/' separator on data_array[2]
nslash = data_array[2].count('/')
# then reconstruct data_array
data_array=data + re.split("/", data_array[2])
#just in case we have an ending CR or 0
data_array[len(data_array)-1] = data_array[len(data_array)-1].replace('\n', '')
data_array[len(data_array)-1] = data_array[len(data_array)-1].replace('\0', '')
#test if there are characters at the end of each value, then delete these characters
i = 3
while i < len(data_array) :
while not data_array[i][len(data_array[i])-1].isdigit() :
data_array[i] = data_array[i][:-1]
i += 2
print("MongoDB: removing obsolete entries")
#check if new month
remove_if_new_month(now)
print("MongoDB: saving the document in the collection...")
#saving data in a JSON var
str_json_data = "{"
# add here the RSSI
str_json_data += "\"RSSI\" : "+str(RSSI)+", "
#start from the first nomenclature
iteration = 2
if nslash==0:
# no nomenclature, use DEF
str_json_data += "\"DEF\" : "+data_array[iteration]
else:
while iteration < len(data_array)-1 :
#last iteration, do not add "," at the end
if iteration == len(data_array)-2 :
str_json_data += "\""+data_array[iteration]+"\" : "+data_array[iteration+1]
else :
str_json_data += "\""+data_array[iteration]+"\" : "+data_array[iteration+1]+", "
iteration += 2
str_json_data += "}"
#creating document to add
doc = {
"type" : ptype,
"gateway_eui" : gwid,
"node_eui" : src,
"snr" : SNR,
"rssi" : RSSI,
"cr" : cr,
"datarate" : "SF"+str(sf)+"BW"+str(bw),
"time" : now,
"data" : json.dumps(json.loads(str_json_data))
}
#adding the document
add_document(doc)
print("MongoDB: saving done")
if __name__ == "__main__":
main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])