changes to first PR
This commit is contained in:
Родитель
862810590e
Коммит
e29503e13d
|
@ -11,7 +11,7 @@ function onWriteLine(request, response) {
|
|||
if (err) {
|
||||
console.error('An error occurred when sending a method response:\n' + err.toString());
|
||||
} else {
|
||||
console.log('Response to method \'' + request.methodName + '\' sent successfully.');
|
||||
console.log('Response to method \'' + request.methodName + "\' with Payload: \'" + request.payload + '\' sent successfully.');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -13,17 +13,16 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub.Config
|
|||
/// </summary>
|
||||
public class IoTCloudToDeviceAsyncCollector : IAsyncCollector<IoTCloudToDeviceItem>
|
||||
{
|
||||
private static ServiceClient serviceClient;
|
||||
private ServiceClient serviceClient;
|
||||
|
||||
public IoTCloudToDeviceAsyncCollector(ServiceClient serviceClient, IoTCloudToDeviceAttribute attribute)
|
||||
{
|
||||
// create client;
|
||||
IoTCloudToDeviceAsyncCollector.serviceClient = serviceClient;
|
||||
this.serviceClient = serviceClient;
|
||||
}
|
||||
public Task AddAsync(IoTCloudToDeviceItem item, CancellationToken cancellationToken = default(CancellationToken))
|
||||
public async Task AddAsync(IoTCloudToDeviceItem item, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
SendCloudToDeviceMessageAsync(item).Wait();
|
||||
return Task.CompletedTask;
|
||||
await SendCloudToDeviceMessageAsync(item);
|
||||
}
|
||||
|
||||
public Task FlushAsync(CancellationToken cancellationToken = default(CancellationToken))
|
||||
|
@ -31,7 +30,7 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub.Config
|
|||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async static Task SendCloudToDeviceMessageAsync(IoTCloudToDeviceItem item)
|
||||
private async Task SendCloudToDeviceMessageAsync(IoTCloudToDeviceItem item)
|
||||
{
|
||||
char[] messageCharArr = item.Message.ToCharArray();
|
||||
var deviceToCloudMessage = new Message(Encoding.ASCII.GetBytes(messageCharArr));
|
||||
|
|
|
@ -9,11 +9,13 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub.Config
|
|||
{
|
||||
public class IoTCloudToDeviceExtension : IExtensionConfigProvider
|
||||
{
|
||||
private static string connectionString;
|
||||
private static ServiceClient serviceClient;
|
||||
private Dictionary<string, ServiceClient> _clients;
|
||||
private string connectionString;
|
||||
private ServiceClient serviceClient;
|
||||
|
||||
public void Initialize(ExtensionConfigContext context)
|
||||
{
|
||||
_clients = new Dictionary<string, ServiceClient>();
|
||||
|
||||
// This allows a user to bind to IAsyncCollector<string>, and the sdk
|
||||
// will convert that to IAsyncCollector<IoTCloudToDeviceItem>
|
||||
|
@ -35,22 +37,20 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub.Config
|
|||
|
||||
private IoTCloudToDeviceItem ConvertToItem(string str)
|
||||
{
|
||||
var item = JsonConvert.DeserializeObject<Dictionary<string, string>>(str);
|
||||
|
||||
return new IoTCloudToDeviceItem
|
||||
{
|
||||
DeviceId = item["DeviceId"],
|
||||
MessageId = item["MessageId"],
|
||||
Message = str
|
||||
};
|
||||
return JsonConvert.DeserializeObject<IoTCloudToDeviceItem>(str);
|
||||
}
|
||||
|
||||
private IAsyncCollector<IoTCloudToDeviceItem> BuildCollector(IoTCloudToDeviceAttribute attribute)
|
||||
{
|
||||
if (serviceClient == null)
|
||||
connectionString = attribute.Connection;
|
||||
if (_clients.ContainsKey(connectionString))
|
||||
{
|
||||
serviceClient = _clients[connectionString];
|
||||
}
|
||||
else
|
||||
{
|
||||
connectionString = attribute.Connection;
|
||||
serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
|
||||
_clients.Add(connectionString, serviceClient);
|
||||
}
|
||||
|
||||
return new IoTCloudToDeviceAsyncCollector(serviceClient, attribute);
|
||||
|
|
|
@ -8,18 +8,16 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub.Config
|
|||
{
|
||||
public class IoTDirectMethodAsyncCollector : IAsyncCollector<IoTDirectMethodItem>
|
||||
{
|
||||
private static ServiceClient serviceClient;
|
||||
private ServiceClient serviceClient;
|
||||
|
||||
public IoTDirectMethodAsyncCollector(ServiceClient serviceClient, IoTDirectMethodAttribute attribute)
|
||||
{
|
||||
// create client;
|
||||
IoTDirectMethodAsyncCollector.serviceClient = serviceClient;
|
||||
this.serviceClient = serviceClient;
|
||||
}
|
||||
|
||||
public Task AddAsync(IoTDirectMethodItem item, CancellationToken cancellationToken = default(CancellationToken))
|
||||
public async Task AddAsync(IoTDirectMethodItem item, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
InvokeMethod(item.DeviceId, item.MethodName).Wait();
|
||||
return Task.CompletedTask;
|
||||
await InvokeMethod(item.DeviceId, item.MethodName, item.Payload, cancellationToken);
|
||||
}
|
||||
|
||||
public Task FlushAsync(CancellationToken cancellationToken = default(CancellationToken))
|
||||
|
@ -27,14 +25,11 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub.Config
|
|||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static async Task InvokeMethod(string deviceID, string methodName)
|
||||
private async Task InvokeMethod(string deviceID, string methodName, string payload, CancellationToken cancellationToken)
|
||||
{
|
||||
var methodInvocation = new CloudToDeviceMethod(methodName) { ResponseTimeout = TimeSpan.FromSeconds(30) };
|
||||
|
||||
var response = await serviceClient.InvokeDeviceMethodAsync(deviceID, methodInvocation);
|
||||
|
||||
Console.WriteLine("Response status: {0}, payload:", response.Status);
|
||||
Console.WriteLine(response.GetPayloadAsJson());
|
||||
methodInvocation.SetPayloadJson(payload);
|
||||
var response = await serviceClient.InvokeDeviceMethodAsync(deviceID, methodInvocation, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,11 +9,14 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub.Config
|
|||
{
|
||||
public class IoTDirectMethodExtension : IExtensionConfigProvider
|
||||
{
|
||||
private static string connectionString;
|
||||
private static ServiceClient serviceClient;
|
||||
private Dictionary<string, ServiceClient> _clients;
|
||||
private string connectionString;
|
||||
private ServiceClient serviceClient;
|
||||
|
||||
public void Initialize(ExtensionConfigContext context)
|
||||
{
|
||||
_clients = new Dictionary<string, ServiceClient>();
|
||||
|
||||
// This allows a user to bind to IAsyncCollector<string>, and the sdk
|
||||
// will convert that to IAsyncCollector<IoTCloudToDeviceItem>
|
||||
context.AddConverter<string, IoTDirectMethodItem>(ConvertToItem);
|
||||
|
@ -35,22 +38,34 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub.Config
|
|||
|
||||
private IoTDirectMethodItem ConvertToItem(string str)
|
||||
{
|
||||
var item = JsonConvert.DeserializeObject<Dictionary<string, string>>(str);
|
||||
//return JsonConvert.DeserializeObject<IoTDirectMethodItem>(str);
|
||||
var item = JsonConvert.DeserializeObject<Dictionary<string, Object>>(str);
|
||||
|
||||
return new IoTDirectMethodItem
|
||||
{
|
||||
DeviceId = item["DeviceId"],
|
||||
InvokeId = item["InvokeId"],
|
||||
MethodName = item["MethodName"]
|
||||
};
|
||||
return (item.ContainsKey("Payload")) ?
|
||||
new IoTDirectMethodItem
|
||||
{
|
||||
DeviceId = item["DeviceId"].ToString(),
|
||||
MethodName = item["MethodName"].ToString(),
|
||||
Payload = item["Payload"].ToString()
|
||||
} :
|
||||
new IoTDirectMethodItem
|
||||
{
|
||||
DeviceId = item["DeviceId"].ToString(),
|
||||
MethodName = item["MethodName"].ToString()
|
||||
};
|
||||
}
|
||||
|
||||
private IAsyncCollector<IoTDirectMethodItem> BuildCollector(IoTDirectMethodAttribute attribute)
|
||||
{
|
||||
if (serviceClient == null)
|
||||
connectionString = attribute.Connection;
|
||||
if (_clients.ContainsKey(connectionString))
|
||||
{
|
||||
serviceClient = _clients[connectionString];
|
||||
}
|
||||
else
|
||||
{
|
||||
connectionString = attribute.Connection;
|
||||
serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
|
||||
_clients.Add(connectionString, serviceClient);
|
||||
}
|
||||
|
||||
return new IoTDirectMethodAsyncCollector(serviceClient, attribute);
|
||||
|
|
|
@ -11,12 +11,15 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub.Config
|
|||
{
|
||||
public class IoTGetDeviceTwinExtension : IExtensionConfigProvider
|
||||
{
|
||||
private static string connectionString;
|
||||
static RegistryManager registryManager;
|
||||
private Dictionary<string, RegistryManager> _manager;
|
||||
private string connectionString;
|
||||
private RegistryManager registryManager;
|
||||
private Twin deviceTwin;
|
||||
|
||||
public void Initialize(ExtensionConfigContext context)
|
||||
{
|
||||
_manager = new Dictionary<string, RegistryManager>();
|
||||
|
||||
// This is useful on input.
|
||||
context.AddConverter<Twin, string>(ConvertToString);
|
||||
context.AddConverter<Twin, Newtonsoft.Json.Linq.JObject>(ConvertToJObject);
|
||||
|
@ -31,12 +34,7 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub.Config
|
|||
private string ConvertToString(Twin item)
|
||||
{
|
||||
return JsonConvert.SerializeObject(item);
|
||||
}
|
||||
|
||||
//private Twin ConvertToTwin(Twin item)
|
||||
//{
|
||||
// return item;
|
||||
//}
|
||||
}
|
||||
|
||||
private JObject ConvertToJObject(Twin results)
|
||||
{
|
||||
|
@ -64,10 +62,15 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub.Config
|
|||
|
||||
private async Task GetDeviceTwinAsync(IoTGetDeviceTwinAttribute attribute)
|
||||
{
|
||||
if (registryManager == null)
|
||||
connectionString = attribute.Connection;
|
||||
if (_manager.ContainsKey(connectionString))
|
||||
{
|
||||
registryManager = _manager[connectionString];
|
||||
}
|
||||
else
|
||||
{
|
||||
connectionString = attribute.Connection;
|
||||
registryManager = RegistryManager.CreateFromConnectionString(connectionString);
|
||||
_manager.Add(connectionString, registryManager);
|
||||
}
|
||||
|
||||
deviceTwin = await registryManager.GetTwinAsync(attribute.DeviceId);
|
||||
|
|
|
@ -8,18 +8,17 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub.Config
|
|||
{
|
||||
public class IoTSetDeviceTwinAsyncCollector : IAsyncCollector<IoTSetDeviceTwinItem>
|
||||
{
|
||||
static RegistryManager registryManager;
|
||||
private RegistryManager registryManager;
|
||||
|
||||
public IoTSetDeviceTwinAsyncCollector(RegistryManager registryManager, IoTSetDeviceTwinAttribute attribute)
|
||||
{
|
||||
// create client;
|
||||
IoTSetDeviceTwinAsyncCollector.registryManager = registryManager;
|
||||
this.registryManager = registryManager;
|
||||
}
|
||||
|
||||
public Task AddAsync(IoTSetDeviceTwinItem item, CancellationToken cancellationToken = default(CancellationToken))
|
||||
public async Task AddAsync(IoTSetDeviceTwinItem item, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
SetDesiredConfigurationAndQuery(item).Wait();
|
||||
return Task.CompletedTask;
|
||||
await SetDesiredConfigurationAndQuery(item, cancellationToken);
|
||||
}
|
||||
|
||||
public Task FlushAsync(CancellationToken cancellationToken = default(CancellationToken))
|
||||
|
@ -27,11 +26,10 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub.Config
|
|||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
static private async Task SetDesiredConfigurationAndQuery(IoTSetDeviceTwinItem item)
|
||||
private async Task SetDesiredConfigurationAndQuery(IoTSetDeviceTwinItem item, CancellationToken cancellationToken)
|
||||
{
|
||||
var twin = await registryManager.GetTwinAsync(item.DeviceId);
|
||||
var twin = await registryManager.GetTwinAsync(item.DeviceId, cancellationToken); // how to include cancellation token?
|
||||
await registryManager.UpdateTwinAsync(twin.DeviceId, item.Patch, twin.ETag);
|
||||
Console.WriteLine("Updated desired configuration");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,11 +9,14 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub.Config
|
|||
{
|
||||
public class IoTSetDeviceTwinExtension : IExtensionConfigProvider
|
||||
{
|
||||
private static string connectionString;
|
||||
static RegistryManager registryManager;
|
||||
private Dictionary<string, RegistryManager> _manager;
|
||||
private string connectionString;
|
||||
private RegistryManager registryManager;
|
||||
|
||||
public void Initialize(ExtensionConfigContext context)
|
||||
{
|
||||
_manager = new Dictionary<string, RegistryManager>();
|
||||
|
||||
// This allows a user to bind to IAsyncCollector<string>, and the sdk
|
||||
// will convert that to IAsyncCollector<IoTCloudToDeviceItem>
|
||||
context.AddConverter<string, IoTSetDeviceTwinItem>(ConvertToItem);
|
||||
|
@ -35,22 +38,27 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub.Config
|
|||
|
||||
private IoTSetDeviceTwinItem ConvertToItem(string str)
|
||||
{
|
||||
//return JsonConvert.DeserializeObject<IoTSetDeviceTwinItem>(str);
|
||||
var item = JsonConvert.DeserializeObject<Dictionary<string, object>>(str);
|
||||
|
||||
return new IoTSetDeviceTwinItem
|
||||
{
|
||||
DeviceId = (string)item["DeviceId"],
|
||||
UpdateId = (string)item["UpdateId"],
|
||||
Patch = JsonConvert.SerializeObject(item["Patch"])
|
||||
};
|
||||
}
|
||||
|
||||
private IAsyncCollector<IoTSetDeviceTwinItem> BuildCollector(IoTSetDeviceTwinAttribute attribute)
|
||||
{
|
||||
if (registryManager == null)
|
||||
connectionString = attribute.Connection;
|
||||
if (_manager.ContainsKey(connectionString))
|
||||
{
|
||||
registryManager = _manager[connectionString];
|
||||
}
|
||||
else
|
||||
{
|
||||
connectionString = attribute.Connection;
|
||||
registryManager = RegistryManager.CreateFromConnectionString(connectionString);
|
||||
_manager.Add(connectionString, registryManager);
|
||||
}
|
||||
|
||||
return new IoTSetDeviceTwinAsyncCollector(registryManager, attribute);
|
||||
|
|
|
@ -15,13 +15,7 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub
|
|||
public class IoTCloudToDeviceAttribute : Attribute
|
||||
{
|
||||
[AutoResolve]
|
||||
public string DeviceId { get; set; }
|
||||
|
||||
[AutoResolve]
|
||||
public string MessageId { get; set; }
|
||||
|
||||
[AutoResolve]
|
||||
public string Message { get; set; }
|
||||
public string DeviceId { get; set; }
|
||||
|
||||
[AppSetting]
|
||||
public string Connection { get; set; }
|
||||
|
|
|
@ -16,9 +16,6 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub
|
|||
// IoT DeviceId
|
||||
public string DeviceId { set; get; }
|
||||
|
||||
// MessegeId starting with 1 per DeviceId
|
||||
public string MessageId { set; get; }
|
||||
|
||||
// Messege sent from device to cloud
|
||||
// Invariant: {paramerter key}:{paramerter value};...
|
||||
public string Message { set; get; }
|
||||
|
|
|
@ -17,12 +17,6 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub
|
|||
[AutoResolve]
|
||||
public string DeviceId { get; set; }
|
||||
|
||||
[AutoResolve]
|
||||
public string InvokeId { get; set; }
|
||||
|
||||
[AutoResolve]
|
||||
public string MethodName { get; set; }
|
||||
|
||||
[AppSetting]
|
||||
public string Connection { get; set; }
|
||||
}
|
||||
|
|
|
@ -11,10 +11,10 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub
|
|||
// Destination IoT DeviceId
|
||||
public string DeviceId { set; get; }
|
||||
|
||||
// InvokeId starting with 1 per DeviceId
|
||||
public string InvokeId { set; get; }
|
||||
|
||||
// MethodName to be invoked
|
||||
public string MethodName { set; get; }
|
||||
|
||||
// Payload as arguments to the method
|
||||
public string Payload { set; get; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,12 +17,6 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub
|
|||
[AutoResolve]
|
||||
public string DeviceId { get; set; }
|
||||
|
||||
[AutoResolve]
|
||||
public string UpdateId { get; set; }
|
||||
|
||||
[AutoResolve]
|
||||
public string Patch { get; set; }
|
||||
|
||||
[AppSetting]
|
||||
public string Connection { get; set; }
|
||||
}
|
||||
|
|
|
@ -11,9 +11,6 @@ namespace Microsoft.Azure.WebJobs.Extensions.IoTHub
|
|||
// Destination IoT DeviceId
|
||||
public string DeviceId { set; get; }
|
||||
|
||||
// InvokeId starting with 1 per DeviceId
|
||||
public string UpdateId { set; get; }
|
||||
|
||||
// new configuration to change (either tags or desired properties)
|
||||
public string Patch { set; get; }
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@ namespace SampleFunctions
|
|||
var item = new
|
||||
{
|
||||
DeviceId = "receiverBob",
|
||||
MessageId = "1",
|
||||
Message = "Hello"
|
||||
};
|
||||
output.Add(JsonConvert.SerializeObject(item));
|
||||
|
@ -25,7 +24,6 @@ namespace SampleFunctions
|
|||
item = new
|
||||
{
|
||||
DeviceId = "receiverBob",
|
||||
MessageId = "2",
|
||||
Message = "From"
|
||||
};
|
||||
output.Add(JsonConvert.SerializeObject(item));
|
||||
|
@ -33,7 +31,6 @@ namespace SampleFunctions
|
|||
item = new
|
||||
{
|
||||
DeviceId = "receiverBob",
|
||||
MessageId = "3",
|
||||
Message = "Cloud"
|
||||
};
|
||||
output.Add(JsonConvert.SerializeObject(item));
|
||||
|
@ -47,7 +44,6 @@ namespace SampleFunctions
|
|||
var item = new
|
||||
{
|
||||
DeviceId = deviceId,
|
||||
MessageId = "1",
|
||||
Message = "telemetry data point"
|
||||
};
|
||||
output.Add(JsonConvert.SerializeObject(item));
|
||||
|
@ -58,29 +54,34 @@ namespace SampleFunctions
|
|||
public void DirectInvokeMethod(string deviceId, // from trigger
|
||||
[IoTDirectMethod(DeviceId = "{deviceId}", Connection = "IoTConnectionString")] ICollector<string> output)
|
||||
{
|
||||
var item = new
|
||||
var item1 = new
|
||||
{
|
||||
DeviceId = deviceId,
|
||||
InvokeId = "1",
|
||||
MethodName = "writeLine"
|
||||
MethodName = "writeLine",
|
||||
Payload = new
|
||||
{
|
||||
arg1 = "HiHo1"
|
||||
}
|
||||
};
|
||||
output.Add(JsonConvert.SerializeObject(item));
|
||||
output.Add(JsonConvert.SerializeObject(item1));
|
||||
|
||||
item = new
|
||||
var item2 = new
|
||||
{
|
||||
DeviceId = deviceId,
|
||||
InvokeId = "2",
|
||||
MethodName = "writeLine"
|
||||
MethodName = "writeLine",
|
||||
Payload = new
|
||||
{
|
||||
arg1 = "HiHo2"
|
||||
}
|
||||
};
|
||||
output.Add(JsonConvert.SerializeObject(item));
|
||||
output.Add(JsonConvert.SerializeObject(item2));
|
||||
|
||||
item = new
|
||||
var item3 = new
|
||||
{
|
||||
DeviceId = deviceId,
|
||||
InvokeId = "3",
|
||||
MethodName = "writeLine"
|
||||
};
|
||||
output.Add(JsonConvert.SerializeObject(item));
|
||||
output.Add(JsonConvert.SerializeObject(item3));
|
||||
}
|
||||
|
||||
// Write some messages
|
||||
|
@ -92,7 +93,6 @@ namespace SampleFunctions
|
|||
var item2 = new
|
||||
{
|
||||
DeviceId = deviceId,
|
||||
UpdateId = "2",
|
||||
Patch = new
|
||||
{
|
||||
properties = new
|
||||
|
@ -112,7 +112,6 @@ namespace SampleFunctions
|
|||
var item = new
|
||||
{
|
||||
DeviceId = deviceId,
|
||||
UpdateId = "1",
|
||||
Patch = new
|
||||
{
|
||||
tags = new
|
||||
|
@ -130,7 +129,6 @@ namespace SampleFunctions
|
|||
var item3 = new
|
||||
{
|
||||
DeviceId = deviceId,
|
||||
UpdateId = "3",
|
||||
Patch = new
|
||||
{
|
||||
properties = new
|
||||
|
|
|
@ -34,6 +34,8 @@ namespace Host
|
|||
|
||||
var host = new JobHost(config);
|
||||
|
||||
//var method = typeof(Functions).GetMethod("Dummy");
|
||||
|
||||
//Test some invocations.
|
||||
//var method = typeof(Functions).GetMethod("WriteMessageFromC2D");
|
||||
//host.Call(method);
|
||||
|
@ -52,7 +54,7 @@ namespace Host
|
|||
|
||||
//// Test some invocations.
|
||||
method = typeof(Functions).GetMethod("GetDeviceTwinTwinObject");
|
||||
host.Call(method, new { deviceId = "receiverCarol", messageId = "123" });
|
||||
host.Call(method, new { deviceId = "receiverBob", messageId = "123" });
|
||||
|
||||
// host.RunAndBlock();
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче