예제 #1
0
        protected override List <OpcNodeOnEndpointModel> GetConfiguredNodesOnEndpoint(string endpointUrl, CancellationToken ct)
        {
            string logPrefix = $"{_logClassPrefix}:GetConfiguredNodesOnEndpoint:";
            GetConfiguredNodesOnEndpointMethodResponseModel response = null;
            List <OpcNodeOnEndpointModel> nodes = new List <OpcNodeOnEndpointModel>();

            try
            {
                GetConfiguredNodesOnEndpointMethodRequestModel getConfiguredNodesOnEndpointMethodRequestModel = new GetConfiguredNodesOnEndpointMethodRequestModel(endpointUrl);
                getConfiguredNodesOnEndpointMethodRequestModel.EndpointUrl = endpointUrl;
                ulong?continuationToken = null;
                while (true)
                {
                    getConfiguredNodesOnEndpointMethodRequestModel.ContinuationToken = continuationToken;
                    _getConfiguredNodesOnEndpointMethod.SetPayloadJson(JsonConvert.SerializeObject(getConfiguredNodesOnEndpointMethodRequestModel));
                    CloudToDeviceMethodResult result;
                    if (string.IsNullOrEmpty(_publisherModuleName))
                    {
                        result = _iotHubClient.InvokeDeviceMethodAsync(_publisherDeviceName, _getConfiguredNodesOnEndpointMethod, ct).Result;
                    }
                    else
                    {
                        result = _iotHubClient.InvokeDeviceMethodAsync(_publisherDeviceName, _publisherModuleName, _getConfiguredNodesOnEndpointMethod, ct).Result;
                    }
                    if (result.Status == (int)HttpStatusCode.OK)
                    {
                        Logger.Debug($"{logPrefix} succeeded, got {nodes.Count} nodes are published on endpoint '{endpointUrl}')");
                        response = JsonConvert.DeserializeObject <GetConfiguredNodesOnEndpointMethodResponseModel>(result.GetPayloadAsJson());
                        if (response != null && response.OpcNodes != null)
                        {
                            nodes.AddRange(response.OpcNodes);
                        }
                        if (response == null || response.ContinuationToken == null)
                        {
                            break;
                        }
                        continuationToken = response.ContinuationToken;
                    }
                    else
                    {
                        if (result.Status == (int)HttpStatusCode.Gone)
                        {
                            Logger.Debug($"{logPrefix} node configuration has changed for endpoint {endpointUrl}");
                        }
                        else
                        {
                            Logger.Warning($"{logPrefix} failed for endpoint {endpointUrl}");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Fatal(e, $"{logPrefix} Exception");
            }
            return(nodes);
        }
        public async Task <List <OpcNodeOnEndpointModel> > GetConfiguredNodesOnEndpointAsync(string endpointUrl, CancellationToken ct)
        {
            GetConfiguredNodesOnEndpointMethodResponseModel response = null;
            List <OpcNodeOnEndpointModel> nodes = new List <OpcNodeOnEndpointModel>();

            try
            {
                GetConfiguredNodesOnEndpointMethodRequestModel getConfiguredNodesOnEndpointMethodRequestModel = new GetConfiguredNodesOnEndpointMethodRequestModel(endpointUrl);
                getConfiguredNodesOnEndpointMethodRequestModel.EndpointUrl = endpointUrl;
                ulong?continuationToken = null;
                while (true)
                {
                    getConfiguredNodesOnEndpointMethodRequestModel.ContinuationToken = continuationToken;
                    _getConfiguredNodesOnEndpointMethod.SetPayloadJson(JsonConvert.SerializeObject(getConfiguredNodesOnEndpointMethodRequestModel));
                    CloudToDeviceMethodResult methodResult;
                    if (string.IsNullOrEmpty(_publisherModuleName))
                    {
                        methodResult = await _iotHubClient.InvokeDeviceMethodAsync(_publisherDeviceName, _getConfiguredNodesOnEndpointMethod, ct).ConfigureAwait(false);
                    }
                    else
                    {
                        methodResult = await _iotHubClient.InvokeDeviceMethodAsync(_publisherDeviceName, _publisherModuleName, _getConfiguredNodesOnEndpointMethod, ct).ConfigureAwait(false);
                    }
                    if (!ct.IsCancellationRequested)
                    {
                        if (methodResult.Status == (int)HttpStatusCode.OK)
                        {
                            response = JsonConvert.DeserializeObject <GetConfiguredNodesOnEndpointMethodResponseModel>(methodResult.GetPayloadAsJson());
                            if (response != null && response.OpcNodes != null)
                            {
                                nodes = response.OpcNodes;
                            }
                            if (response == null || response.ContinuationToken == null)
                            {
                                break;
                            }
                            continuationToken = response.ContinuationToken;
                        }
                        else
                        {
                            LogMethodResult(methodResult, _getConfiguredNodesOnEndpointMethod.MethodName);
                            break;;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error(e, $"Exception");
            }
            return(nodes);
        }
        public List <NodeModel> GetConfiguredNodesOnEndpoint(string endpointUrl, CancellationToken ct)
        {
            GetConfiguredNodesOnEndpointMethodResponseModel response = null;
            List <NodeModel> nodes = new List <NodeModel>();

            try
            {
                GetConfiguredNodesOnEndpointMethodRequestModel getConfiguredNodesOnEndpointMethodRequestModel = new GetConfiguredNodesOnEndpointMethodRequestModel(endpointUrl);
                getConfiguredNodesOnEndpointMethodRequestModel.EndpointUrl = endpointUrl;
                ulong?continuationToken = null;
                while (true)
                {
                    getConfiguredNodesOnEndpointMethodRequestModel.ContinuationToken = continuationToken;
                    _getConfiguredNodesOnEndpointMethod.SetPayloadJson(JsonConvert.SerializeObject(getConfiguredNodesOnEndpointMethodRequestModel));
                    CloudToDeviceMethodResult result;
                    if (string.IsNullOrEmpty(_publisherModuleName))
                    {
                        result = _iotHubClient.InvokeDeviceMethodAsync(_publisherDeviceName, _getConfiguredNodesOnEndpointMethod, ct).Result;
                    }
                    else
                    {
                        result = _iotHubClient.InvokeDeviceMethodAsync(_publisherDeviceName, _publisherModuleName, _getConfiguredNodesOnEndpointMethod, ct).Result;
                    }
                    response = JsonConvert.DeserializeObject <GetConfiguredNodesOnEndpointMethodResponseModel>(result.GetPayloadAsJson());
                    if (response != null && response.Nodes != null)
                    {
                        nodes.AddRange(response.Nodes);
                    }
                    if (response == null || response.ContinuationToken == null)
                    {
                        break;
                    }
                    continuationToken = response.ContinuationToken;
                }
            }
            catch (Exception e)
            {
                Logger.Fatal(e, $"GetConfiguredNodesOnEndpoint Exception");
            }
            Logger.Debug($"GetConfiguredNodesOnEndpoint succeeded, got {nodes.Count} nodes are published on endpoint '{endpointUrl}')");
            return(nodes);
        }