Beispiel #1
0
        protected override bool PublishNodes(List <NodeIdInfo> nodeIdInfos, CancellationToken ct, string endpointUrl = null)
        {
            string logPrefix  = $"{_logClassPrefix}:PublishNodes:";
            bool   result     = true;
            int    retryCount = MAX_RETRY_COUNT;

            try
            {
                PublishNodesMethodRequestModel publishNodesMethodRequestModel = new PublishNodesMethodRequestModel(endpointUrl ?? TestserverUrl);
                foreach (var nodeIdInfo in nodeIdInfos)
                {
                    publishNodesMethodRequestModel.OpcNodes.Add(new OpcNodeOnEndpointModel(nodeIdInfo.Id));
                }
                CloudToDeviceMethodResult methodResult = new CloudToDeviceMethodResult();
                methodResult.Status = (int)HttpStatusCode.NotAcceptable;
                while (methodResult.Status == (int)HttpStatusCode.NotAcceptable && retryCount-- > 0)
                {
                    _publishNodesMethod.SetPayloadJson(JsonConvert.SerializeObject(publishNodesMethodRequestModel));
                    if (string.IsNullOrEmpty(_publisherModuleName))
                    {
                        methodResult = _iotHubClient.InvokeDeviceMethodAsync(_publisherDeviceName, _publishNodesMethod, ct).Result;
                    }
                    else
                    {
                        methodResult = _iotHubClient.InvokeDeviceMethodAsync(_publisherDeviceName, _publisherModuleName, _publishNodesMethod, ct).Result;
                    }
                    if (methodResult.Status == (int)HttpStatusCode.NotAcceptable)
                    {
                        Thread.Sleep(MaxShortWaitSec * 1000);
                    }
                    else
                    {
                        break;
                    }
                }
                foreach (var nodeIdInfo in nodeIdInfos)
                {
                    if (!nodeIdInfo.Published && methodResult.Status != (int)HttpStatusCode.OK && methodResult.Status != (int)HttpStatusCode.Accepted)
                    {
                        Logger.Warning($"{logPrefix} failed (nodeId: '{nodeIdInfo.Id}', statusCode: '{methodResult.Status}', publishedState: '{nodeIdInfo.Published}')");
                        result = false;
                    }
                    else
                    {
                        nodeIdInfo.Published = true;
                        Logger.Debug($"{logPrefix} succeeded (nodeId: '{nodeIdInfo.Id}', statusCode: '{methodResult.Status}')");
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Fatal(e, $"{logPrefix} Exception");
            }
            return(result);
        }
        public async Task <bool> PublishNodesAsync(List <OpcNodeOnEndpointModel> nodesToPublish, CancellationToken ct, string endpointUrl = null)
        {
            bool result     = false;
            int  retryCount = MAX_RETRY_COUNT;

            try
            {
                PublishNodesMethodRequestModel publishNodesMethodRequestModel = new PublishNodesMethodRequestModel(endpointUrl);
                publishNodesMethodRequestModel.OpcNodes.AddRange(nodesToPublish);
                CloudToDeviceMethodResult methodResult = new CloudToDeviceMethodResult();
                methodResult.Status = (int)HttpStatusCode.NotAcceptable;
                while (methodResult.Status == (int)HttpStatusCode.NotAcceptable && retryCount-- > 0)
                {
                    _publishNodesMethod.SetPayloadJson(JsonConvert.SerializeObject(publishNodesMethodRequestModel));
                    if (string.IsNullOrEmpty(_publisherModuleName))
                    {
                        methodResult = await _iotHubClient.InvokeDeviceMethodAsync(_publisherDeviceName, _publishNodesMethod, ct).ConfigureAwait(false);
                    }
                    else
                    {
                        methodResult = await _iotHubClient.InvokeDeviceMethodAsync(_publisherDeviceName, _publisherModuleName, _publishNodesMethod, ct).ConfigureAwait(false);
                    }
                    if (methodResult.Status == (int)HttpStatusCode.NotAcceptable)
                    {
                        Thread.Sleep(MAXSHORTWAITSEC * 1000);
                    }
                    else
                    {
                        if (ct.IsCancellationRequested)
                        {
                            break;
                        }
                        LogMethodResult(methodResult, _publishNodesMethod.MethodName);
                        result = methodResult.Status == (int)HttpStatusCode.OK;
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error(e, $"Exception");
            }
            return(result);
        }