use of com.microsoft.azure.sdk.iot.device.DeviceTwin.TwinPropertyCallBack in project azure-iot-sdk-java by Azure.
the class MainActivity method InitClient.
private void InitClient() throws URISyntaxException, IOException {
client = new DeviceClient(connString, protocol);
try {
client.open();
if (protocol == IotHubClientProtocol.MQTT) {
MessageCallbackMqtt callback = new MessageCallbackMqtt();
Counter counter = new Counter(0);
client.setMessageCallback(callback, counter);
} else {
MessageCallback callback = new MessageCallback();
Counter counter = new Counter(0);
client.setMessageCallback(callback, counter);
}
client.subscribeToDeviceMethod(new SampleDeviceMethodCallback(), null, new DeviceMethodStatusCallBack(), null);
Succeed.set(false);
client.startDeviceTwin(new DeviceTwinStatusCallBack(), null, new onProperty(), null);
do {
Thread.sleep(1000);
} while (!Succeed.get());
Map<Property, Pair<TwinPropertyCallBack, Object>> desiredProperties = new HashMap<Property, Pair<TwinPropertyCallBack, Object>>() {
{
put(new Property("HomeTemp(F)", null), new Pair<TwinPropertyCallBack, Object>(new onProperty(), null));
put(new Property("LivingRoomLights", null), new Pair<TwinPropertyCallBack, Object>(new onProperty(), null));
put(new Property("BedroomRoomLights", null), new Pair<TwinPropertyCallBack, Object>(new onProperty(), null));
put(new Property("HomeSecurityCamera", null), new Pair<TwinPropertyCallBack, Object>(new onProperty(), null));
}
};
client.subscribeToTwinDesiredProperties(desiredProperties);
System.out.println("Subscribe to Desired properties on device Twin...");
} catch (Exception e2) {
System.err.println("Exception while opening IoTHub connection: " + e2.getMessage());
client.closeNow();
System.out.println("Shutting down...");
}
}
use of com.microsoft.azure.sdk.iot.device.DeviceTwin.TwinPropertyCallBack in project azure-iot-sdk-java by Azure.
the class DigitalTwinClientTests method updateDigitalTwin.
@Test
@StandardTierHubOnlyTest
public void updateDigitalTwin() throws IOException {
// arrange
String newProperty = "currentTemperature";
String newPropertyPath = "/currentTemperature";
Integer newPropertyValue = 35;
// Property update callback
TwinPropertyCallBack twinPropertyCallBack = (property, context) -> {
Set<Property> properties = new HashSet<>();
properties.add(property);
try {
deviceClient.sendReportedProperties(properties);
} catch (IOException e) {
}
};
// IotHub event callback
IotHubEventCallback iotHubEventCallback = (responseStatus, callbackContext) -> {
};
// start device twin and setup handler for property updates in device
deviceClient.startDeviceTwin(iotHubEventCallback, null, twinPropertyCallBack, null);
Map<Property, Pair<TwinPropertyCallBack, Object>> desiredPropertyUpdateCallback = Collections.singletonMap(new Property(newProperty, null), new Pair<>(twinPropertyCallBack, null));
deviceClient.subscribeToTwinDesiredProperties(desiredPropertyUpdateCallback);
DigitalTwinUpdateRequestOptions optionsWithoutEtag = new DigitalTwinUpdateRequestOptions();
optionsWithoutEtag.setIfMatch("*");
// get digital twin and Etag before update
ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> responseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(deviceId, BasicDigitalTwin.class);
DigitalTwinUpdateRequestOptions optionsWithEtag = new DigitalTwinUpdateRequestOptions();
optionsWithEtag.setIfMatch(responseWithHeaders.headers().eTag());
// act
// Add properties at root level - conditional update with max overload
UpdateOperationUtility updateOperationUtility = new UpdateOperationUtility().appendAddPropertyOperation(newPropertyPath, newPropertyValue);
digitalTwinClient.updateDigitalTwinWithResponse(deviceId, updateOperationUtility.getUpdateOperations(), optionsWithEtag);
BasicDigitalTwin digitalTwin = digitalTwinClient.getDigitalTwinWithResponse(deviceId, BasicDigitalTwin.class).body();
// assert
assertEquals(E2ETestConstants.THERMOSTAT_MODEL_ID, digitalTwin.getMetadata().getModelId());
assertTrue(digitalTwin.getMetadata().getWriteableProperties().containsKey(newProperty));
assertEquals(newPropertyValue, digitalTwin.getMetadata().getWriteableProperties().get(newProperty).getDesiredValue());
}
Aggregations