public async Task <bool> UnpublishNodesAsync(List <OpcNodeOnEndpointModel> nodesToUnpublish, string endpointUrl, CancellationToken ct)
        {
            bool result = false;

            try
            {
                UnpublishNodesMethodRequestModel unpublishNodesMethodRequestModel = new UnpublishNodesMethodRequestModel(endpointUrl);
                unpublishNodesMethodRequestModel.OpcNodes.AddRange(nodesToUnpublish);
                _unpublishNodesMethod.SetPayloadJson(JsonConvert.SerializeObject(unpublishNodesMethodRequestModel));
                CloudToDeviceMethodResult methodResult;
                if (string.IsNullOrEmpty(_publisherModuleName))
                {
                    methodResult = await _iotHubClient.InvokeDeviceMethodAsync(_publisherDeviceName, _unpublishNodesMethod, ct).ConfigureAwait(false);
                }
                else
                {
                    methodResult = await _iotHubClient.InvokeDeviceMethodAsync(_publisherDeviceName, _publisherModuleName, _unpublishNodesMethod, ct).ConfigureAwait(false);
                }
                if (!ct.IsCancellationRequested)
                {
                    LogMethodResult(methodResult, _publishNodesMethod.MethodName);
                    result = methodResult.Status == (int)HttpStatusCode.OK;
                }
            }
            catch (Exception e)
            {
                Logger.Error(e, $"Exception");
            }
            return(result);
        }
Esempio n. 2
0
        protected override bool UnpublishNodes(List <NodeIdInfo> nodeIdInfos, CancellationToken ct, string endpointUrl)
        {
            string logPrefix = $"{_logClassPrefix}:UnpublishNodes:";

            try
            {
                UnpublishNodesMethodRequestModel unpublishNodesMethodRequestModel = new UnpublishNodesMethodRequestModel(endpointUrl);
                foreach (var nodeIdInfo in nodeIdInfos)
                {
                    unpublishNodesMethodRequestModel.OpcNodes.Add(new OpcNodeOnEndpointModel(nodeIdInfo.Id));
                }
                _unpublishNodesMethod.SetPayloadJson(JsonConvert.SerializeObject(unpublishNodesMethodRequestModel));
                CloudToDeviceMethodResult result;
                if (string.IsNullOrEmpty(_publisherModuleName))
                {
                    result = _iotHubClient.InvokeDeviceMethodAsync(_publisherDeviceName, _unpublishNodesMethod, ct).Result;
                }
                else
                {
                    result = _iotHubClient.InvokeDeviceMethodAsync(_publisherDeviceName, _publisherModuleName, _unpublishNodesMethod, ct).Result;
                }
                foreach (var nodeIdInfo in nodeIdInfos)
                {
                    if (nodeIdInfo.Published && result.Status != (int)HttpStatusCode.OK && result.Status != (int)HttpStatusCode.Accepted)
                    {
                        Logger.Warning($"{logPrefix} failed (nodeId: '{nodeIdInfo.Id}', statusCode: '{result.Status}', publishedState: '{nodeIdInfo.Published}')");
                        return(false);
                    }
                    else
                    {
                        nodeIdInfo.Published = false;
                        Logger.Debug($"{logPrefix} succeeded (nodeId: '{nodeIdInfo.Id}', statusCode: '{result.Status}'");
                        return(true);
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Fatal(e, $"{logPrefix} Exception");
            }
            return(false);
        }