Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Commit

Permalink
changes to first PR
Browse files Browse the repository at this point in the history
  • Loading branch information
L committed Aug 9, 2017
1 parent 8628105 commit e29503e
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 112 deletions.
2 changes: 1 addition & 1 deletion samples/simulatedDevices/receiverAlice/receiveralice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,24 @@ 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))
{
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));
Expand Down
24 changes: 12 additions & 12 deletions src/WebJobs.Extensions.IoTHub/Config/IoTCloudToDeviceExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -35,22 +37,20 @@ private string ConvertToString(IoTCloudToDeviceItem item)

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,28 @@ 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))
{
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);
}
}
}
37 changes: 26 additions & 11 deletions src/WebJobs.Extensions.IoTHub/Config/IoTDirectMethodExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -35,22 +38,34 @@ private string ConvertToString(IoTDirectMethodItem item)

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);
Expand Down
23 changes: 13 additions & 10 deletions src/WebJobs.Extensions.IoTHub/Config/IoTGetDeviceTwinExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -31,12 +34,7 @@ public void Initialize(ExtensionConfigContext context)
private string ConvertToString(Twin item)
{
return JsonConvert.SerializeObject(item);
}

//private Twin ConvertToTwin(Twin item)
//{
// return item;
//}
}

private JObject ConvertToJObject(Twin results)
{
Expand Down Expand Up @@ -64,10 +62,15 @@ private Twin BuildItemFromAttr(IoTGetDeviceTwinAttribute attribute)

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,28 @@ 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))
{
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");
}
}
}
18 changes: 13 additions & 5 deletions src/WebJobs.Extensions.IoTHub/Config/IoTSetDeviceTwinExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -35,22 +38,27 @@ private string ConvertToString(IoTSetDeviceTwinItem item)

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);
Expand Down
8 changes: 1 addition & 7 deletions src/WebJobs.Extensions.IoTHub/IoTCloudToDeviceAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
3 changes: 0 additions & 3 deletions src/WebJobs.Extensions.IoTHub/IoTCloudToDeviceItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ public class IoTCloudToDeviceItem
// 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; }
Expand Down
6 changes: 0 additions & 6 deletions src/WebJobs.Extensions.IoTHub/IoTDirectMethodAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ public class IoTDirectMethodAttribute : Attribute
[AutoResolve]
public string DeviceId { get; set; }

[AutoResolve]
public string InvokeId { get; set; }

[AutoResolve]
public string MethodName { get; set; }

[AppSetting]
public string Connection { get; set; }
}
Expand Down
6 changes: 3 additions & 3 deletions src/WebJobs.Extensions.IoTHub/IoTDirectMethodItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public class IoTDirectMethodItem
// 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; }
}
}
6 changes: 0 additions & 6 deletions src/WebJobs.Extensions.IoTHub/IoTSetDeviceTwinAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ public class IoTSetDeviceTwinAttribute : Attribute
[AutoResolve]
public string DeviceId { get; set; }

[AutoResolve]
public string UpdateId { get; set; }

[AutoResolve]
public string Patch { get; set; }

[AppSetting]
public string Connection { get; set; }
}
Expand Down
3 changes: 0 additions & 3 deletions src/WebJobs.Extensions.IoTHub/IoTSetDeviceTwinItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ public class IoTSetDeviceTwinItem
// 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; }
}
Expand Down
Loading

0 comments on commit e29503e

Please sign in to comment.