sample/scenarized/aircond_shadow_sample_v2.c
. It adds the following logic to sample/scenarized/aircond_shadow_sample.c
:IOT_Shadow_Register_Property
to bind the shadow's configuration class attribute and callback function. When the shadow has a configuration change of this attribute, the underlying layer of the SDK will perform the corresponding callback. The temperatureDesire
field in the shadow is registered here, which means that when the application sets the target temperature for the device shadow, the local configuration can be corrected by the callback function to adjust the desired temperature. You can also implement custom configuration-based attribute listening and callback binding.rc = _register_config_shadow_property();
make
in the root directory of the SDK, compile, and get the aircond_shadow_sample_v2
executable program../aircond_shadow_sample_v2
in the ./output/release/bin
directory. Please note that if MQTT asymmetric encryption is used, the root certificate, device certificate, and device key files should be placed in the parent directory of ./../certs
../door_mqtt_sample come_home airConditioner1
in the ./output/release/bin
directory to turn on airConditioner
.INF|2018-01-11 20:52:50|aircond_shadow_sample_v2.c|main(377): Cloud Device Construct Success
INF|2018-01-11 20:52:50|aircond_shadow_sample_v2.c|main(389): Cloud Device Register Delta Success
In the output log, it can be seen that the on_temperature_actuate_callback
function has been called, indicating that the delta
topic sent by the shadow has been received, and the operation modify desire temperature to: 10.000000
has been performed for updating the locally set temperature.
INF|2018-01-11 21:04:31|aircond_shadow_sample_v2.c|on_temperature_actuate_callback(181): actuate callback jsonString=10},"desired":{"temperatureDesire":10},"reported":{"energyConsumption":0.0}},"timestamp":1515675847609,"version":5},"result":0,"timestamp":1515675871,"type":"get"}|dataLen=2
INF|2018-01-11 21:04:31|aircond_shadow_sample_v2.c|on_temperature_actuate_callback(184): modify desire temperature to: 10.000000
INF|2018-01-11 21:04:31|aircond_shadow_sample_v2.c|on_request_handler(123): Method=GET|Ack=ACK_ACCEPTED
INF|2018-01-11 21:04:31|aircond_shadow_sample_v2.c|on_request_handler(124): received jsonString={"clientToken":"EJSKHKIS1M-0","payload":{"metadata":{"delta":{"temperatureDesire":{"timestamp":1515675847609}},"desired":{"temperatureDesire":{"timestamp":1515675847609}},"reported":{"energyConsumption":{"timestamp":1515674881485}}},"state":{"delta":{"temperatureDesire":10},"desired":{"temperatureDesire":10},"reported":{"energyConsumption":0.0}},"timestamp":1515675847609,"version":5},"result":0,"timestamp":1515675871,"type":"get"}
In the above output log of airConditioner1
, it can be seen that the configuration operation has taken effect and airConditioner
has adjusted the locally set temperature.
ShadowSample.java is the device shadow class with the following main features:
connect()
API of TXShadowConnect
.disconnect()
API of TXShadowConnection
.registerProperty()
API of TXShadowConnection
.get()
API of TXShadowConnection
.update()
API of TXShadowConnection
.Before running the application, please enter the PRODUCT_ID, DEVICE_NAME, DEVICE_CERT_NAME, and DEVICE_KEY_NAME obtained in the previous steps for product and device creation and place the device certificate and device private key files in the assets directory:
/**
* Product ID
*/
private static final String PRODUCT_ID = "YOUR_PRODUCT_ID";
/**
* Device name
*/
protected static final String DEVICE_NAME = "YOUR_DEVICE_NAME";
/**
* Key
*/
private static final String SECRET_KEY = "YOUR_DEVICE_PSK";
/**
* Device certificate name
*/
private static final String DEVICE_CERT_NAME = "YOUR_DEVICE_NAME_cert.crt";
/**
* Device private key file name
*/
private static final String DEVICE_KEY_NAME = "YOUR_DEVICE_NAME_private.key";
Call the RESTful API UpdateDeviceShadow to simulate the home appliance management backend and publish the target temperature configuration.
The RESTful API request parameter is: deviceName=airConditioner1, state={"desired" : {"temperatureDesire": 10}}, productName=AirConditioner
, which adjusts the control temperature to 10°C.
Was this page helpful?