I have a Stream Analytics that depending on what it recives it outputs some data to an Azure function in C#. This functions must send a JSON to the device and when this reads the JSON it runs a local method depending on the data received. My problem is that I cant find the way to do this.
I have already done this in Java but I can't find the way to do it in C#. The way that I do this is with a HTTP trigger and the code below.
This is how I deserialize the received data:
String body = request.getBody().get().toString().replace("[", "").replace("\"", "\'");
JSONObject bodyJson = new JSONObject(body);
String deviceMac = bodyJson.getString("deviceid");
deviceId = bodyJson.getString("receiveruuid");
And this is how I send the result to the device:
DeviceMethod methodClient = DeviceMethod.createFromConnectionString(iotHubConnectionString);
Map<String, Object> payload = new HashMap<String, Object>() {
{
//PAYLOAD DATA
}};
MethodResult result = methodClient.invoke(deviceId, methodName, responseTimeout, connectTimeout, payload);
Thank you for your help
UPDATE
I finally resolved the problem in this way.
var serviceClient = ServiceClient.CreateFromConnectionString(iotHubConnectionString);
var methodname = new CloudToDeviceMethod("method_defatult").SetPayloadJson(" {\"message\": \""+ message +"\"}");
await serviceClient.InvokeDeviceMethodAsync("moviltest", methodname);
With the first line you create the connection string to the iotHub. The second line is creates the CloudToDeviceMethod and also you set the payload. Make sure that the JSON is correct. Finally it invokes the method in the device.
I finally resolved the problem in this way.
var serviceClient = ServiceClient.CreateFromConnectionString(iotHubConnectionString);
var methodname = new CloudToDeviceMethod("method_defatult").SetPayloadJson(" {\"message\": \""+ message +"\"}");
await serviceClient.InvokeDeviceMethodAsync("moviltest", methodname);
With the first line you create the connection string to the iotHub. The second line is creates the CloudToDeviceMethod and also you set the payload. Make sure that the JSON is correct. Finally it invokes the method in the device.