Skip to content

Commit

Permalink
Uploader submitting obs
Browse files Browse the repository at this point in the history
  • Loading branch information
ps2 committed Apr 30, 2017
1 parent ef22c3b commit 8ea1628
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions pyaci/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,81 @@
from aci import AciCommand
from aci_serial import AciUart
from aci import AciEvent
from queue import Empty
import time
import sensei_cmd
import sensei
from sensei import *
import datetime
import yaml
from os.path import expanduser
import urllib2
import os.path

class Uploader(object):
# Synchronize once every five minutes
TIME_SYNC_INTERVAL=5*60

def __init__(self, sensei_config):
api_url = sensei_config["server"]["url"] + 'api/v1/'
self.api = sensei.Api(api_url, sensei_config["server"]["username"], sensei_config["server"]["password"])
self.api = Api(api_url, sensei_config["server"]["username"], sensei_config["server"]["password"])
self.aci = AciUart.AciUart(port=sensei_config['mesh_network']['serial_path'], baudrate=115200)
self.classroom_id = sensei_config["classroom_id"]

def get_sensor_updates(self):
updates = []
while True:
try:
self.acidev.events_queue.get_nowait()
evt = self.aci.events_queue.get_nowait()
if isinstance(evt, AciEvent.AciEventNew) and evt.is_sensor_update():
updates.append(evt.sensor_values())
except Empty:
break
return updates

def runAppCommand(self, command):
data = command.serialize()
return self.aci.write_aci_cmd(AciCommand.AciAppCommand(data=data,length=len(data)+1))

def sync_time(self):
self.last_time_sync = time.time()
result = self.runAppCommand(sensei_cmd.SetTime())
print(str.format("Synced time: %s" %(result)))

def radio_obs_from_sensor_updates(updates):

def radio_obs_from_update(self, update):
obs = []
for remote_id, rssi in zip(update.proximity_ids, update.proximity_rssi):
ob_time = datetime.datetime.utcfromtimestamp(update.valid_time)
if remote_id > 0 and rssi > 0:
obs.append(RadioObservation(self.classroom_id, update.sensor_id, remote_id, ob_time, -rssi))
return obs

def run(self):
# Wait for serial connection to be ready
time.sleep(2)
self.sync_time()

while True:
updates = self.get_sensor_updates()
if len(updates) > 0:
self.api.upload_obs([self.])
obs = [self.radio_obs_from_update(update) for update in updates]
flattened_obs = [ob for sublist in obs for ob in sublist]
self.api.upload_obs(flattened_obs)
else:
time.sleep(2)

time.sleep(0.5)
if time.time() - self.last_time_sync > Uploader.TIME_SYNC_INTERVAL:
self.sync_time()


if __name__ == '__main__':

config_path = expanduser("~") + "/.sensei.yaml"
if os.path.isfile(config_path):
with open(config_path, 'r') as stream:
try:
sensei_config = yaml.load(stream)
uploader = Uploader(sensei_config)
upload
except yaml.YAMLError as exc:
print(exc)
try:
sensei_config = yaml.load(stream)
uploader = Uploader(sensei_config)
uploader.run()
except yaml.YAMLError as exc:
print(exc)
else:
print(str.format("Please configure settings in %s" %(config_path)))
exit(-1)

0 comments on commit 8ea1628

Please sign in to comment.