Skip to content

Commit

Permalink
Upload accelerometer events
Browse files Browse the repository at this point in the history
  • Loading branch information
ps2 committed May 25, 2017
1 parent 14f1eaf commit 5979370
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
3 changes: 3 additions & 0 deletions pyaci/aci/AciEvent.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def __repr__(self):
return str.format("%s length:%d opcode:0x%02x command_opcode:%s status_code:%s data:%s" %(self.__class__.__name__, self.Len, self.OpCode, AciCommand.AciCommandLookUp(self.CommandOpCode), AciStatusLookUp(self.StatusCode), self.Data))

class SensorValues(object):
STATUS_JOSTLE_FLAG = 0x01

def __init__(self, sensor_id, data):
self.sensor_id = sensor_id
if len(data) == 19:
Expand All @@ -112,6 +114,7 @@ def __init__(self, sensor_id, data):
self.is_valid = True
else:
self.is_valid = False
self.status = None
self.data = data
def __repr__(self):
if self.is_valid:
Expand Down
46 changes: 23 additions & 23 deletions pyaci/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from aci import AciCommand
from aci_serial import AciUart
from aci import AciEvent
from aci.AciEvent import SensorValues
from queue import Empty
import sys
import time
Expand Down Expand Up @@ -33,7 +34,7 @@ def __init__(self, sensei_config, options):

def handle_heartbeat(self, hb):
print(str.format("handling heartbeat: %s" %(hb)))
if hb.epoch_seconds != hb.received_at:
if hb.epoch_seconds != hb.received_at or abs(time.time() - hb.epoch_seconds) > 5:
print(str.format("Sensor %d clock offset detected; issuing sync_time." %(hb.sensor_id)))
self.sync_time()

Expand Down Expand Up @@ -94,30 +95,26 @@ def restart_serial(self):
self.aci = AciUart.AciUart(port=device, baudrate=115200)
self.last_event = time.time()

def upload_radio_observations(self, obs):
retries = 3
while (retries > 0):
def handle_exceptions_with_sleep_retry(self, callable, sleep_duration, num_retries, description):
while (num_retries > 0):
try:
self.api.upload_radio_observations(obs)
return
except
e = sys.exc_info()[0]
print(str.format("Exception while uploading radio obs: %s" %(e)))
retries = retries - 1
sleep(1)
return callable()
except Exception as e:
print(str.format("Exception while %s: %s" %(description, repr(e))))
num_retries = num_retries - 1
time.sleep(sleep_duration)


def upload_radio_observations(self, obs):
self.handle_exceptions_with_sleep_retry(lambda: self.api.upload_radio_observations(obs), 1, 3, "uploading radio obs")

def upload_accelerometer_observations(self, obs):
retries = 3
while (retries > 0):
try:
self.api.upload_accelerometer_observations(obs)
return
except
e = sys.exc_info()[0]
print(str.format("Exception while uploading accelerometer data: %s" %(e)))
retries = retries - 1
sleep(1)
self.handle_exceptions_with_sleep_retry(lambda: self.api.upload_accelerometer_observations(obs), 1, 3, "uploading accelerometer obs")

def upload_accelerometer_event(self, event_type, sensor_id, valid_time):
ob_time = datetime.datetime.utcfromtimestamp(valid_time)
events = [AccelerometerEvent(self.classroom_id, sensor_id, ob_time, event_type)]
self.handle_exceptions_with_sleep_retry(lambda: self.api.upload_accelerometer_events(events), 1, 3, "uploading accelerometer obs")

def handle_updates_from_serial(self, updates):
if not self.options.dry_run:
Expand All @@ -130,6 +127,9 @@ def handle_updates_from_serial(self, updates):
ob = self.accelerometer_measurement_from_update(update)
if ob:
accelerometer_obs.append(ob)
if update.status & SensorValues.STATUS_JOSTLE_FLAG:
self.upload_accelerometer_event('jostle', update.sensor_id, update.valid_time)
print("Jostle from %d" %(update.sensor_id))
if len(accelerometer_obs) > 0:
self.upload_accelerometer_observations(accelerometer_obs)

Expand All @@ -144,9 +144,9 @@ def run(self):
self.sync_time()

while True:
updates = self.get_sensor_updates()
updates = [u for u in self.get_sensor_updates() if u.is_valid]
if len(updates) > 0:
handle_updates_from_serial(updates)
self.handle_updates_from_serial(updates)
else:
time.sleep(0.5)

Expand Down

0 comments on commit 5979370

Please sign in to comment.