Beispiel #1
0
    def _shadow_update_accepted(self,
                                response: iotshadow.UpdateShadowResponse):
        state = response.state
        if state:
            desired_state = state.desired
            if desired_state:
                door_status = desired_state.get(
                    GarageDoorStatus.DOOR_STATUS_PROP)
                if door_status == GarageDoorStatus.SIGNALED:
                    current_status = self._sensor.read()
                    new_desired_state = (GarageDoorStatus.CLOSED
                                         if current_status == OPEN else
                                         GarageDoorStatus.OPENED)
                    current_state = (GarageDoorStatus.CLOSED if current_status
                                     == CLOSE else GarageDoorStatus.OPENED)
                    self._correlation_token = desired_state.get(
                        GarageDoorStatus.CORRELATION_TOKEN_PROP)

                    reported_state_obj = GarageDoorStatus(
                        self._endpoint_id, current_state,
                        self._correlation_token)
                    desired_state_obj = GarageDoorStatus(
                        self._endpoint_id, new_desired_state,
                        self._correlation_token)
                    request = iotshadow.UpdateShadowRequest(
                        thing_name=self._thing_name,
                        state=iotshadow.ShadowState(
                            reported=reported_state_obj.json(),
                            desired=desired_state_obj.json(),
                        ),
                    )
                    future = self._device_shadow.publish_update_shadow(
                        request, qos=mqtt.QoS.AT_LEAST_ONCE)
                    future.add_done_callback(self._on_publish_update_shadow)
                    self._garage_door_relay.signal()
def change_shadow_values(values):
    with locked_data.lock:
        newValues = {}
        for key in values:
            if locked_data.shadow_properties[key] == values[key]:
                print("Local value is already '{}'.".format(values[key]))
            else:
                print("Changed local shadow value to '{}'.".format(
                    values[key]))
                newValues[key] = values[key]
                locked_data.shadow_properties[key] = values[key]

        if not newValues:
            return

        print("Updating reported shadow value to '{}'...".format(values))

        # use a unique token so we can correlate this "request" message to
        # any "response" messages received on the /accepted and /rejected topics
        token = str(uuid4())

        request = iotshadow.UpdateShadowRequest(
            thing_name=thing_name,
            state=iotshadow.ShadowState(reported=newValues, desired=newValues),
            client_token=token,
        )
        future = shadow_client.publish_update_shadow(request,
                                                     mqtt.QoS.AT_LEAST_ONCE)

        locked_data.request_tokens.add(token)

        future.add_done_callback(on_publish_update_shadow)
async def change_shadow_value(thing_name, values):

    print("Updating reported shadow value to '{}'...".format(values))
    request = iotshadow.UpdateShadowRequest(
        thing_name=thing_name, state=iotshadow.ShadowState(reported=values))
    shadow_client = iotshadow.IotShadowClient(mqtt_connection)

    await asyncio.wrap_future(
        shadow_client.publish_update_shadow(request, mqtt.QoS.AT_LEAST_ONCE))

    logger.info("Update request published.")
def change_shadow_value(value):
    """
    Update shadow reported state

    Parameters
    ----------
    value: int
    """
    logger.info("Updating reported shadow to...")
    new_state = iotshadow.ShadowState(reported={SHADOW_WAIT_TIME_KEY: value})
    request = iotshadow.UpdateShadowRequest(thing_name=device_name,
                                            state=new_state)
    future = shadow_client.publish_update_shadow(request,
                                                 mqtt.QoS.AT_LEAST_ONCE)
    future.add_done_callback(on_publish_update_shadow)
Beispiel #5
0
def change_shadow_value(value):
    with locked_data.lock:
        if locked_data.shadow_value == value:
            print("Local value is already '{}'.".format(value))
            print("Enter desired value: ") # remind user they can input new values
            return

        print("Changed local shadow value to '{}'.".format(value))
        locked_data.shadow_value = value

    print("Updating reported shadow value to '{}'...".format(value))
    request = iotshadow.UpdateShadowRequest(
        thing_name=thing_name,
        state=iotshadow.ShadowState(
            reported={ shadow_property: value },
            desired={ shadow_property: value },
        )
    )
    future = shadow_client.publish_update_shadow(request, mqtt.QoS.AT_LEAST_ONCE)
    future.add_done_callback(on_publish_update_shadow)
Beispiel #6
0
    def publishState(self):
        #Update thing state corresponding to the gateway
        sa = []
        for s in self.service_list:
            sd = {'SERVICE_TYPE': s.type, 'SERVICE_NAME': s.name}
            sa.append(sd)

        msg = {'services': sa, 'service_count': len(sa)}

        request = iotshadow.UpdateShadowRequest(
            thing_name=self.thing_name,
            state=iotshadow.ShadowState(reported=msg))

        future = self.shadow_client.publish_update_shadow(
            request, mqtt.QoS.AT_LEAST_ONCE)
        # Ensure that publish succeeds
        try:
            future.result()
            print("Update request published.")
        except:
            print("Failed to publish update request.")
Beispiel #7
0
 def _report_status(self, status):
     print("Gotten status: {}".format(status))
     reported_status = GarageDoorStatus(
         endpoint_id=self._endpoint_id,
         door_status=status,
         correlation_token=self._correlation_token,
     )
     desired_status = GarageDoorStatus(
         endpoint_id=self._endpoint_id,
         correlation_token=self._correlation_token)
     update_request = iotshadow.UpdateShadowRequest(
         thing_name=self._thing_name,
         state=iotshadow.ShadowState(
             reported=reported_status.json(),
             desired=desired_status.json(),
         ),
     )
     future = self._device_shadow.publish_update_shadow(
         update_request, qos=mqtt.QoS.AT_LEAST_ONCE)
     future.add_done_callback(self._on_publish_update_shadow)
     self._correlation_token = None
Beispiel #8
0
    except Exception as e:
        exit(e)

    publish_count = 1

    # Take initial reading
    psutil.cpu_percent(percpu=False)
    locked_data.thing_name = args.thing_name
    locked_data.before_ts = time.time()
    locked_data.ioBefore = psutil.net_io_counters()
    locked_data.diskBefore = psutil.disk_io_counters()
    psutil.disk_io_counters(perdisk=False, nowrap=True)

    while True:
        time.sleep(AWS_PUBLISH_DELAY_SECS)

        print("[aws] Updating shadow values [%d]" % (publish_count))
        data = locked_data.toJSON()
        request = iotshadow.UpdateShadowRequest(thing_name=args.thing_name,
                                                state=iotshadow.ShadowState(
                                                    reported=data,
                                                    desired=data,
                                                ))
        future = shadow_client.publish_update_shadow(request,
                                                     mqtt.QoS.AT_LEAST_ONCE)
        future.add_done_callback(on_publish_update_shadow)

        publish_count += 1

# vim: set tabstop=4 shiftwidth=4 expandtab: