forked from Tanganelli/CoAPthon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collectclient.py
187 lines (157 loc) · 5.75 KB
/
collectclient.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
#!/usr/bin/env python
import getopt
import json
import socket
import sys
import threading
import MySQLdb
import time
from coapthon.client.helperclient import HelperClient
from coapthon.utils import parse_uri
__author__ = 'Giacomo Tanganelli'
# parameters = ['power', 'temperature', 'battery', 'radio', 'humidity', 'light']
parameters = ['temperature', 'humidity', 'light']
client = None
def usage(): # pragma: no cover
print ("Command:\tcollectclient.py -c ")
print ("Options:")
print ("\t-c, --config=\t\tConfig file")
def client_callback(response):
print ("Callback")
def client_callback_observe(response): # pragma: no cover
global client
print ("Callback_observe")
print (response.pretty_print())
check = True
while check:
chosen = raw_input("Stop observing? [y/N]: ")
if chosen != "" and not (chosen == "n" or chosen == "N" or chosen == "y" or chosen == "Y"):
print ("Unrecognized choose.")
continue
elif chosen == "y" or chosen == "Y":
while True:
rst = raw_input("Send RST message? [Y/n]: ")
if rst != "" and not (rst == "n" or rst == "N" or rst == "y" or rst == "Y"):
print ("Unrecognized choose.")
continue
elif rst == "" or rst == "y" or rst == "Y":
client.cancel_observing(response, True)
else:
client.cancel_observing(response, False)
check = False
break
else:
break
def insert_to_db(msg, table):
elem = json.loads(msg.payload)
# etx="0"
for var in elem:
name = var['n']
if str(name) == "best_neighbor_id":
etx = var['etx']
print("ETX: " + str(etx))
if str(table) != "radio":
unit = var['u']
try:
times = var['bt']
except KeyError:
times = var['t']
value = var['v']
host, port = msg.source
conf = "collectmapping.json"
conf = open(conf, "r")
conf = json.load(conf)
for n in conf["nodes"]:
if n['port'] == port:
id_node = n['id']
print("type: " + str(table))
localtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(times))
print("valori: " + name + " " + str(value) + " " + localtime)
# Open database connection
db = MySQLdb.connect("localhost", "root", "Root", "data_db")
# print("dopo connessione")
# prepare a cursor object using cursor() method
cursor = db.cursor()
# Prepare SQL query to INSERT a record into the database.
if str(table) == "radio":
# print("dentro radio")
if str(name) == "best_neighbor_id":
# print("INSERT INTO {}(name, ts, value, id_node, etx)VALUES('{}', '{}', '{}', '{}', '{}')".format(str(table),name,localtime,str(value),id_node,etx))
sql = "INSERT INTO {}(name, ts, value, id_node, etx)VALUES('{}', '{}', '{}', '{}', '{}')".format(
str(table), name, localtime, str(value), id_node, etx)
else:
# print("dentro else")
sql = "INSERT INTO {}(name, ts, value, id_node, etx) VALUES ('{}', '{}', '{}', '{}', '{}')".format(
str(table), name, localtime, str(value), id_node, "0")
else:
sql = "INSERT INTO {}(name, ts, value, unit, id_node) VALUES ('{}', '{}', '{}', '{}', '{}')".format(
str(table), name, localtime, str(value), str(unit), id_node)
# print("dopo query")
try:
# Execute the SQL command
cursor.execute(sql)
# print("dopo execute")
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error
db.rollback()
# disconnect from server
db.close()
def main(): # pragma: no cover
global client
config = None
wait = 0
try:
opts, args = getopt.getopt(sys.argv[1:], "hc:w", ["help", "config=", "wait="])
except getopt.GetoptError as err:
# print help information and exit:
print (str(err)) # will print something like "option -a not recognized"
usage()
sys.exit(2)
for o, a in opts:
if o in ("-c", "--config"):
config = a
print("conf: " + str(a))
elif o in ("-w", "--wait"):
print("dentro wait")
wait = a
print("wait: " + wait)
elif o in ("-h", "--help"):
usage()
sys.exit()
else:
usage()
sys.exit(2)
if config is None:
print ("Config file must be specified")
usage()
sys.exit(2)
config = open(config, "r")
config = json.load(config)
while True:
for n in config["nodes"]:
for i in parameters:
path = "coap://" + n["ip"] + ":" + str(n["port"]) + "/" + str(i)
print("path: " + path)
host, port, path = parse_uri(path)
try:
tmp = socket.gethostbyname(host)
host = tmp
except socket.gaierror:
pass
client = HelperClient(server=(host, port))
response = client.get(path, timeout=5.0)
print "INSERT TO DB"
try:
print(response.pretty_print())
insert_to_db(response, i)
client.stop()
except AttributeError:
pass
try:
time.sleep(float(wait))
except:
time.sleep(30.0)
if __name__ == '__main__': # pragma: no cover
main()