return int(dateparser.parse(str).timestamp())
def get_ascii(self, data):
+ for item in data:
+ t,h,p = 0,0,0
+ t = datetime.fromtimestamp(int(item['timestamp']))
+ if 'humidity' in item:
+ h = item['humidity']
+ if 'pressure' in item:
+ p = item['pressure']
+ m = "{0!s} {1!s} {2!s} {3!s} {4!s}".format(t.isoformat(),
+ item['timestamp'],
+ item['name'],
+ h,
+ p)
+ for sensor in sorted(item['sensors'], key=lambda x: x['id']):
+ m = m + " {0!s} {1:.2f}".format(sensor['id'], float(sensor['value']))
+ yield m + "\n"
+
@cherrypy.expose
def download(self, *args, **kwargs):
try:
data = self.get_since(param_from, param_until)
if cherrypy.request.params.get('type') == 'text':
cherrypy.response.headers['content-type'] = 'text/plain'
- raise Exception("not implemented")
+ return self.get_ascii(data)
else:
cherrypy.response.headers['content-type'] = 'application/json'
res = dict(response="ok", result=[x for x in data], version=self.version)
sensors = []
for sensor,value in zip(['office1','office2','office3'], m.group('T').split()):
sensors.append({'id': sensor, 'value': value })
- return dict(timestamp=timestamp, name='spd-office', sensors=sensors)
+ item = dict(timestamp=timestamp, name='spd-office', sensors=sensors)
+ humidity = m.group('H').split()
+ if len(humidity) > 0:
+ item['humidity'] = float(humidity[0])
+ pressure = m.group('P').split()
+ if len(pressure) > 0:
+ item['pressure'] = float(pressure[0])
+ return item
class SensorData():
def __init__(self, filename):
for sensor in item['sensors']:
check.append(sensor['id'])
self.assertListEqual(['office1','office2','office3'], check)
+ self.assertIn('humidity', item.keys())
+ self.assertIn('pressure', item.keys())
count = count + 1
self.assertEqual(3, count)
mongo = MongoClient()
db = mongo.sensorhub.sensorlog
db.drop()
+ db.create_index([("timestamp", pymongo.ASCENDING)]) # creates if not already present
r = db.insert_many([item for item in GranularData(iter(SensorData(filename)), granularity)])
print("imported {0} records".format(len(r.inserted_ids)))
return 0
cursor = db.find().sort([("timestamp", pymongo.DESCENDING)]).limit(1)
item = next(cursor)
return item
-
+
def opendb():
config = ConfigParser()
config.read('sensor-hub.config')