Hi Watson
- You cann’t set
opTypefrom the device.opTypeis set by IoT Hub in the “device twin change event” payload to reflect what kind of operation changed the twin:-
updateTwin⇒ a partial update (PATCH) — e.g., when a device sends reported properties, or when a service does a partial twin update. -
replaceTwin⇒ a full replace (PUT) — e.g., when a service replaces the entire twin document.
-
- From the C device SDK, you can only PATCH reported properties (which results in
updateTwin). You cannot triggerreplaceTwinfrom the device SDK.replaceTwincan be done by the service (via REST or a service SDK).
Update Twin
string deviceId = "myDevice";
// Retrieve current twin to get ETag
Twin twin = await registry.GetTwinAsync(deviceId);
// Build a JSON patch
string patchJson = @"{
properties: {
desired: {
threshold: 25,
telemetryIntervalSeconds: 30
}
},
tags: {
location: { region: 'US', site: 'Redmond' }
}
}";
// Apply patch (ETag for optimistic concurrency)
Twin updated = await registry.UpdateTwinAsync(deviceId, patchJson, twin.ETag);
// => Emits opType = "updateTwin"
Replace twin
string replaceDesiredJson = @"{
properties: {
desired: {
threshold: 10,
telemetryIntervalSeconds: 5,
mode: 'eco'
}
}
}";
Twin current = await registry.GetTwinAsync(deviceId);
Twin replacedDesired = await registry.UpdateTwinAsync(deviceId, replaceDesiredJson, current.ETag);
// Replace all tags
string replaceTagsJson = @"{
tags: {
location: { region: 'EU', site: 'Dublin' },
owner: 'QuadrantResource'
}
}";
Twin replacedTags = await registry.UpdateTwinAsync(deviceId, replaceTagsJson, replacedDesired.ETag);
// => Each of these PUT‑style replaces causes opType = "replaceTwin".
Hope it clarifies the situation.
Thank you.