-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
180 lines (153 loc) · 6.23 KB
/
utils.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# This file is a part of IoT-LAB embers tools
# Copyright (C) 2015 INRIA (Contact: [email protected])
# Contributor(s) : see AUTHORS file
#
# This software is governed by the CeCILL license under French law
# and abiding by the rules of distribution of free software. You can use,
# modify and/ or redistribute the software under the terms of the CeCILL
# license as circulated by CEA, CNRS and INRIA at the following URL
# http://www.cecill.info.
#
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited
# liability.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license and that you accept its terms.
"""Utils methods"""
import sys
import csv
from ConfigParser import SafeConfigParser, ParsingError
CONFIG_FILE = 'broker.cfg'
def get_broker_config(broker_name):
try:
parser = SafeConfigParser()
with open(CONFIG_FILE) as conf_f:
parser.readfp(conf_f)
if parser.has_section(broker_name):
config = dict(parser.items(broker_name))
if any(option == '' for option in config.itervalues()):
raise ParsingError('%s %s config is empty' % (CONFIG_FILE,
broker_name))
return config
else:
raise ParsingError('%s %s config doesn\'t exist' % (CONFIG_FILE,
broker_name))
except IOError:
print 'Config file %s doesn\'t exist' % CONFIG_FILE
sys.exit(1)
except ParsingError, err:
print err
sys.exit(1)
REGISTRY_FILE = 'meshblu_registry.csv'
def get_registry_device(node):
try:
with open(REGISTRY_FILE, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['device'] == node:
return {'uuid': row['uuid'], 'token': row['token']}
except IOError:
pass
return None
def store_registry_device(node, uuid, token):
with open(REGISTRY_FILE, 'a') as csvfile:
fieldnames = ['device', 'uuid', 'token']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
if csvfile.tell() == 0:
writer.writeheader()
writer.writerow({'device': node, 'uuid': uuid, 'token': token})
def remove_registry_device(node):
from cStringIO import StringIO
tmp_devices = StringIO()
with open(REGISTRY_FILE, 'r') as csvfile:
reader = csv.reader(csvfile)
writer = csv.writer(tmp_devices)
for row in reader:
if row[0] != node:
writer.writerow(row)
with open(REGISTRY_FILE, 'w') as csvfile:
csvfile.write(tmp_devices.getvalue())
NODE_ATTR = ['network_address', 'uid', 'site', 'archi']
def get_iotlab_attrs(exp_nodes):
attr_nodes = {}
for node, props in exp_nodes.iteritems():
attrs = {}
for attr in NODE_ATTR:
attrs.update({attr: props[attr]})
attr_nodes[node] = attrs
return attr_nodes
def get_iotlab_parking_coordinates(site):
iotlab_parking_coordinates = {}
with open('coordinates/parking/%s.csv' % site) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
node = '%s.%s.iot-lab.info' % (row['node'], site)
iotlab_parking_coordinates[node] = \
{'longitude': row['longitude'], 'latitude': row['latitude']}
return iotlab_parking_coordinates
def get_parking_coordinates(exp_nodes):
iotlab_parking_coordinates = {}
parking_coordinates = {}
for node, attr in exp_nodes.iteritems():
if node not in iotlab_parking_coordinates:
iotlab_parking_coordinates.update(get_iotlab_parking_coordinates(attr['site']))
# static allocation only for a subset of iotlab nodes site
if node in iotlab_parking_coordinates:
parking_coordinates[node] = iotlab_parking_coordinates[node]
return parking_coordinates
def get_traffic_metadata(exp_nodes):
traffic_metadata = {}
with open('datasets/citypulse/traffic_metadata.csv') as csvfile:
reader = csv.DictReader(csvfile)
for node, attr in exp_nodes.iteritems():
metadata = reader.next()
for discard in [ 'REPORT_NAME', 'RBA_ID', '_id' ]:
del metadata[discard]
traffic_metadata[node] = metadata
return traffic_metadata
def get_traffic_data_readers(attr_nodes):
data_readers = {}
datafile_path = 'datasets/citypulse/traffic_feb_june/trafficData%s.csv'
for node, attr in attr_nodes.iteritems():
csvfile = open(datafile_path % attr['REPORT_ID'])
data_readers[node] = csv.DictReader(csvfile)
return data_readers
def get_traffic_payload(reader):
vehicle_count = reader.next()['vehicleCount']
payload = "traffic %s" % vehicle_count
return payload
def get_attr_nodes(opts, node_type, exp_nodes):
if opts.traffic or opts.pollution:
metadata = get_traffic_metadata(exp_nodes)
else:
metadata = get_parking_coordinates(exp_nodes)
iotlab_attrs = get_iotlab_attrs(exp_nodes)
attr_nodes = {}
for node in exp_nodes:
attr_nodes[node] = {'type': node_type}
for node in iotlab_attrs:
attr_nodes[node].update(iotlab_attrs[node])
for node in metadata:
attr_nodes[node].update(metadata[node])
return attr_nodes
def get_pollution_data_readers(attr_nodes):
data_readers = {}
datafile_path = 'datasets/citypulse/pollution/pollutionData%s.csv'
for node, attr in attr_nodes.iteritems():
csvfile = open(datafile_path % attr['REPORT_ID'])
data_readers[node] = csv.DictReader(csvfile)
return data_readers
def get_pollution_payload(reader):
fields = [
"ozone", "particullate_matter", "carbon_monoxide",
"sulfure_dioxide", "nitrogen_dioxide",
# longitude,latitude,timestamp
]
data = reader.next()
payload = "pollution %s" % ",".join([data[f] for f in fields])
return payload