예제 #1
0
        public virtual async Task <MethodResponse> HandleGetOpcPublishedConfigurationAsJson(MethodRequest methodRequest, object userContext)
        {
            const string logPrefix  = "HandleGetOpcPublishedConfigurationAsJson:";
            var          statusCode = HttpStatusCode.OK;
            GetOpcPublishedConfigurationMethodResponseModel getOpcPublishedConfigurationMethodResponseModel = new GetOpcPublishedConfigurationMethodResponseModel();
            var statusResponse = new List <string>();

            var configJson = await NodeConfiguration.ReadConfigAsyncAsJson();

            if (configJson == null)
            {
                var statusMessage = $"Error while reading opc publisher json configuration.";
                Logger.Information($"{logPrefix} there is no valid configuration file to return.");
                statusResponse.Add(statusMessage);
                statusCode = HttpStatusCode.NotFound;
            }

            getOpcPublishedConfigurationMethodResponseModel.ConfigurationJson = configJson;
            // build response
            string resultString;

            if (statusCode == HttpStatusCode.OK)
            {
                resultString = JsonConvert.SerializeObject(getOpcPublishedConfigurationMethodResponseModel);
                Logger.Information($"{logPrefix} Success returning current JSON configuration of OPC Publisher!");
            }
            else
            {
                resultString = JsonConvert.SerializeObject(statusResponse);
            }
            byte[] result = Encoding.UTF8.GetBytes(resultString);
            if (result.Length > MaxResponsePayloadLength)
            {
                Logger.Error($"{logPrefix} Response size is too long");
                Array.Resize(ref result, result.Length > MaxResponsePayloadLength ? MaxResponsePayloadLength : result.Length);
            }
            MethodResponse methodResponse = new MethodResponse(result, (int)statusCode);

            Logger.Information($"{logPrefix} completed with result {statusCode.ToString()}");
            return(methodResponse);
        }