class LosantClient(threading.Thread): def __init__(self, my_device_id, my_app_access_key, my_app_access_secret): threading.Thread.__init__(self) self.my_device_id = my_device_id self.my_app_access_key = my_app_access_key self.my_app_access_secret = my_app_access_secret # Construct Losant device self.device = Device(self.my_device_id, self.my_app_access_key, self.my_app_access_secret) self.callback = None def set_callback(self, callback): self.callback = callback def run(self): # Connect to Losant and leave the connection open self.device.add_event_observer("command", self.on_command) self.device.connect(blocking=True) def sendDeviceState(self, name, value): print("Sending Device State") self.device.send_state( {str(name) : value} ) def on_command(self, device, command): print(command["name"] + " command received.") if command["name"] == "toggle": self.callback(command) print("Do something")
class TestDevice(unittest.TestCase): def setUp(self): self.device = Device("device_id", "device_key", "device_secret") def test_correct_props(self): self.assertEqual(self.device._device_id, "device_id") self.assertEqual(self.device._key, "device_key") self.assertEqual(self.device._secret, "device_secret") self.assertEqual(self.device._secure, True) self.assertEqual(self.device._command_topic(), "losant/device_id/command") self.assertEqual(self.device._state_topic(), "losant/device_id/state") def test_add_remove_observer(self): self.event_fired = 0 def on_event(device): self.assertEqual(device, self.device) self.event_fired += 1 self.device.add_event_observer("test", on_event) self.device._fire_event("test") self.assertEqual(self.event_fired, 1) self.device.remove_event_observer("test", on_event) self.device._fire_event("test") self.assertEqual(self.event_fired, 1) def test_send_state(self): self.device._mqtt_client = MqttMock() calls = self.device._mqtt_client.publish_calls self.assertEqual(len(calls), 0) result = self.device.send_state({"one": "two"}, 1234) self.assertEqual(result, True) calls = self.device._mqtt_client.publish_calls self.assertEqual(len(calls), 1) self.assertEqual(calls[0][0], "losant/device_id/state") expected_payload = '{"data": {"one": "two"}, "time": 1234}' self.assertEqual(calls[0][1], expected_payload) def test_receive_command(self): self.cmd_msg = None def on_command(device, msg): self.assertEqual(device, self.device) self.cmd_msg = msg self.device.add_event_observer("command", on_command) mock = MsgMock('{"name":"start","payload":{"one":[2,3]},"time":{"$date":"2016-06-01T01:09:51.145Z"}}') self.device._cb_client_command(None, None, mock) self.assertEqual(self.cmd_msg["name"], "start") self.assertEqual(self.cmd_msg["payload"], {"one": [2, 3]}) self.assertEqual(self.cmd_msg["time"].microsecond, 145000) self.assertEqual(calendar.timegm(self.cmd_msg["time"].utctimetuple()), 1464743391.0)
device = Device("my-device-id", "my-app-access-key", "my-app-access-secret") Check it out at: https://docs.losant.com/mqtt/python/ ****************************************************************************************''' # Construct device device = Device("YourDeviceIDHere", "Your Access Key", "Your Access Secret") def on_command(device, command): print("Command received.") print(command["name"]) print(command["payload"]) # Listen for commands. device.add_event_observer("command", on_command) # Connect to Losant. device.connect(blocking=False) '''**************************************************************************************** While True loop to run for eternity.. or Answer = 42, you decide ****************************************************************************************''' # Send values once every second. while True: device.loop() if device.is_connected(): # Read Lid state to check if its open
print(keyStatus) new_color = 'blue' #default offline if(keyStatus == 'engaged'): new_color = 'green' if(keyStatus == 'disengaged'): new_color = 'red' setColor(deviceId, new_color) if(command["name"] == "btnPressedAnim"): animColor = 'red' # assume failure if(command["payload"] and command["payload"]["status"] == "succeeded"): animColor = 'green' #yay! statusBlink(animColor, 150, 7) # Construct device device = Device(losantconfig.MY_DEVICE_ID, losantconfig.ACCESS_KEY, losantconfig.ACCESS_SECRET) # Listen for commands. device.add_event_observer("command", on_command) # Connect to Losant. device.connect(blocking=False) #### REST setup ### client = Client() creds = { 'deviceId': losantconfig.MY_DEVICE_ID, 'key': losantconfig.ACCESS_KEY, 'secret': losantconfig.ACCESS_SECRET } # Connect via REST and save the response for future connections rest_response = client.auth.authenticate_device(credentials=creds) client.auth_token = rest_response['token'] app_id = rest_response['applicationId']