def test_autostart_enabled(self): with mock.patch('urllib3.PoolManager.request') as mocked: mocked.return_value = mock.MagicMock() mocked.return_value.status = 200 a = ConnectAPI(dict(autostart_notification_thread=True)) self.assertFalse(a.has_active_notification_thread) a.get_resource_value_async('abc123', '/3/4/5') self.assertTrue(a.has_active_notification_thread)
def test_autostart_disabled(self): with mock.patch('urllib3.PoolManager.request') as mocked: mocked.return_value = mock.MagicMock() mocked.return_value.status = 200 a = ConnectAPI(dict(autostart_notification_thread=False)) self.assertFalse(a.has_active_notification_thread) with self.assertRaises(CloudUnhandledError) as e: a.get_resource_value_async('abc123', '/3/4/5') self.assertFalse(a.has_active_notification_thread) self.assertIn('notifications thread', str(e.exception))
def _run_async(): api = ConnectAPI() api.start_notifications() devices = api.list_connected_devices().data if not devices: raise Exception("No devices registered. Aborting") current_value = None while True: async_resp = api.get_resource_value_async(devices[0].id, BUTTON_RESOURCE) # Busy wait - block the thread and wait for the response to finish. while not async_resp.is_done: time.sleep(0.1) # Check if we have a async error response, and abort if it is. if async_resp.error: raise Exception("Got async error response: %r" % async_resp.error) # Get the value from the async response, as we know it's done and it's not # an error. new_value = async_resp.value # Print new value to user, if it has changed. if current_value != new_value: print("Current value: %r" % (new_value, )) # Save new current value current_value = new_value
def _run_async(): api = ConnectAPI() api.start_notifications() devices = api.list_connected_devices().data if not devices: raise Exception("No devices registered. Aborting") gyro_cur = acceler_cur = None while True: gyro_resp = api.get_resource_value_async(devices[0].id, GYRO_RESOURCE) acceler_resp = api.get_resource_value_async(devices[0].id, ACCELERO_RESOURCE) # Busy wait - block the thread and wait for the response to finish. while not gyro_resp.is_done: time.sleep(0.1) while not acceler_resp.is_done: time.sleep(0.1) # Check if we have a async error response, and abort if it is. if gyro_resp.error: continue raise Exception("Got async error response: %r" % gyro_resp.error) if acceler_resp.error: continue raise Exception("Got async error response: %r" % acceler_resp.error) # Get the value from the async response, as we know it's done and it's not # an error. gyro_new_value = gyro_resp.value acceler_new_value = acceler_resp.value # Print new value to user, if it has changed. if gyro_cur != gyro_new_value and acceler_cur != acceler_new_value: current_time = datetime.datetime.now() timestamp = current_time.strftime("%Y-%m-%d %H-%M-%S-%f") hour = current_time.hour minutes = current_time.minute seconds = current_time.second microsecond = current_time.microsecond accX, accY, accZ = acceler_new_value.values() accX, accY, accZ = (float(accX) / 1000), (float(accY) / 1000), (float(accZ) / 1000) gyroX, gyroY, gyroZ = gyro_new_value.values() gyroX, gyroY, gyroZ = ((float(gyroX) / 1000) * (pi / 180)), ((float(gyroY) / 1000) * (pi / 180)), ( (float(gyroZ) / 1000) * (pi / 180)) # pred = clf.predict([[accX, accY, accZ, gyroX, gyroY, gyroZ, hour, minutes, seconds, microsecond]]) start = time.time() pred = clf.predict([[accX, accY, accZ, gyroX, gyroY, gyroZ]]) stop = time.time() print(f"classifying time: {stop - start}") # print(accX, accY, accZ) # print(gyroX, gyroY, gyroZ) # print("gyro {}".format(gyro_new_value)) # print("accelero{}".format(acceler_new_value)) if pred[0] == 0: classification = 'walking' else: classification = 'running' with conn.cursor() as cursor: cursor.execute(add_row, (timestamp, classification)) if classification == 'walking': cursor.execute(add_walking) else: cursor.execute(add_running) conn.commit() print(classification) print("row added") # Save new current value gyro_cur = gyro_new_value acceler_cur = acceler_new_value