コード例 #1
0
        public async Task <ActionResult> GetCommand(string modelid)
        {
            List <COMMAND_DATA> commandData = new List <COMMAND_DATA>();

            try
            {
                DTDLModelResolver resolver = new DTDLModelResolver(_modelRepoUrl, _gitToken, _logger);

                var modelData = await resolver.ParseModelAsync(modelid);

                var interfaces = modelData.Where(r => r.Value.EntityKind == DTEntityKind.Command).ToList();

                foreach (var dt in interfaces)
                {
                    COMMAND_DATA data = new COMMAND_DATA();

                    DTCommandInfo commandInfo = dt.Value as DTCommandInfo;

                    if (commandInfo.DisplayName.Count > 0)
                    {
                        data.CommandDisplayName = commandInfo.DisplayName["en"];
                    }

                    if (commandInfo.Description.Count > 0)
                    {
                        data.CommandDescription = commandInfo.Description["en"];
                    }

                    data.CommandName = commandInfo.Name;

                    if (commandInfo.Request != null)
                    {
                        if (data.request == null)
                        {
                            data.request = new List <COMMAND_REQUEST>();
                        }
                        COMMAND_REQUEST request = new COMMAND_REQUEST();
                        request.requestName        = commandInfo.Request.Name;
                        request.requestKind        = commandInfo.Request.Schema.EntityKind.ToString();
                        request.requestDescription = commandInfo.Request.Description["en"];
                        request.requestisplayName  = commandInfo.Request.DisplayName["en"];
                        data.request.Add(request);
                    }

                    commandData.Add(data);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error {ex}");
            }

            return(Json(commandData));
        }
コード例 #2
0
        private static async Task ProcessDeviceConnected(JObject deviceEventData, ILogger log)
        {
            log.LogInformation(">> DeviceConnected Event");
            // Process Device Connected Event.
            // https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-event-grid#device-connected-schema
            // Example of sending a direct method (command)
            try
            {
                // Get Device Id
                string deviceId = deviceEventData["deviceId"].ToString();

                // Get Device Instance from IoT Hub
                // https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.devices.registrymanager.getdeviceasync?view=azure-dotnet
                Device device = await _registryManager.GetDeviceAsync(deviceId);

                if (device.ConnectionState != DeviceConnectionState.Connected)
                {
                    log.LogWarning($"Device {deviceId} is not connected");
                    return;
                }

                // Get DTDL Model ID from Device Twin
                // https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.devices.registrymanager.gettwinasync?view=azure-dotnet
                var twin = await _registryManager.GetTwinAsync(deviceId);

                if (!string.IsNullOrEmpty(twin.ModelId))
                {
                    IReadOnlyDictionary <Dtmi, DTEntityInfo> parsedModel = null;
                    string commandName = string.Empty;

                    log.LogInformation($"Model ID : {twin.ModelId}");

                    // Parse DTDL Model
                    parsedModel = await DeviceModelResolveAndParse(twin.ModelId);

                    if (twin.ModelId.Contains("impinj"))
                    {
                        commandName = "Presets";
                    }
                    else if (twin.ModelId.Contains("wioterminal_aziot_example"))
                    {
                        commandName = "ringBuzzer";
                    }

                    // We are interested in only commands
                    DTCommandInfo command = parsedModel.Where(r => r.Value.EntityKind == DTEntityKind.Command).Select(x => x.Value as DTCommandInfo).Where(x => x.Name == commandName).FirstOrDefault();

                    if (command != null)
                    {
                        string componentName = string.Empty;
                        commandName = string.Empty;

                        // If no match, this interface must be from Component
                        if (!twin.ModelId.Equals(command.DefinedIn.AbsoluteUri))
                        {
                            var component = parsedModel.Where(r => r.Value.EntityKind == DTEntityKind.Component).Select(x => x.Value as DTComponentInfo).Where(x => x.Schema.Id.ToString() == command.ChildOf.AbsoluteUri).FirstOrDefault();
                            if (component != null)
                            {
                                componentName = component.Name;
                            }
                        }

                        if (!string.IsNullOrEmpty(componentName))
                        {
                            // Add component name
                            // https://docs.microsoft.com/en-us/azure/iot-pnp/concepts-convention#commands
                            commandName = $"{componentName}*{command.Name}";
                        }
                        else
                        {
                            commandName = $"{command.Name}";
                        }

                        log.LogInformation($"Sending command {commandName} / Description : {command.Description}");

                        var cmd = new CloudToDeviceMethod(commandName)
                        {
                            ResponseTimeout = TimeSpan.FromSeconds(30)
                        };

                        if (commandName.Equals("ringBuzzer"))
                        {
                            cmd.SetPayloadJson("500");
                        }
                        var response = await _serviceClient.InvokeDeviceMethodAsync(deviceId, cmd);

                        log.LogInformation($"Response status: {response.Status}, payload: {response.GetPayloadAsJson()}");
                    }
                }
            }
            catch (Exception ex)
            {
                log.LogWarning($"Failed to process Device Connected Event : Exception '{ex.Message}'");
            }
            log.LogInformation("<< DeviceConnected Event");
        }