コード例 #1
0
ファイル: RestConnection.cs プロジェクト: eltra-ch/eltra-sdk
        public async Task <bool> Send <T>(UserIdentity identity, T obj)
        {
            bool result = false;

            try
            {
                string path;
                bool   postMethod;

                if (GetPath(obj, out path, out postMethod))
                {
                    if (postMethod)
                    {
                        var requestResult = await Transporter.Post(identity, Url, path, JsonSerializer.Serialize(obj));

                        if (requestResult.StatusCode == HttpStatusCode.OK)
                        {
                            result = true;

                            OnMessageSent();
                        }
                        else
                        {
                            OnErrorOccured(requestResult?.StatusCode);
                        }
                    }
                    else
                    {
                        var response = await Transporter.Put(identity, Url, path, JsonSerializer.Serialize(obj));

                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            var json = response.Content;

                            if (!string.IsNullOrEmpty(json))
                            {
                                var requestResult = json.TryDeserializeObject <RequestResult>();

                                result = requestResult.Result;
                            }
                        }
                        else
                        {
                            OnErrorOccured(response?.StatusCode);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MsgLogger.Exception($"{GetType().Name} - Send", e);
            }

            return(result);
        }
コード例 #2
0
        private async Task <bool> UpdateParameterValue(int nodeId, ushort index, byte subIndex, ParameterValue actualValue)
        {
            bool result = false;

            try
            {
                var parameterUpdate = new ParameterValueUpdate
                {
                    ChannelId      = Channel.Id,
                    NodeId         = nodeId,
                    ParameterValue = actualValue,
                    Index          = index,
                    SubIndex       = subIndex
                };

                var path = $"api/parameter/value";

                var json = parameterUpdate.ToJson();

                var response = await Transporter.Put(_identity, Url, path, json);

                if (response != null)
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        MsgLogger.WriteDebug($"{GetType().Name} - UpdateParameterValue", $"Device Node id = {nodeId}, Parameter Index = 0x{index:X4}, Subindex = 0x{subIndex} value successfully written");

                        result = true;
                    }
                    else
                    {
                        MsgLogger.WriteError($"{GetType().Name} - UpdateParameterValue", $"Device Node id = {nodeId}, Parameter Index = 0x{index:X4}, Subindex = 0x{subIndex} value write failed, status code = {response.StatusCode}!");
                    }
                }
                else
                {
                    MsgLogger.WriteError($"{GetType().Name} - UpdateParameterValue", $"Device Node id = {nodeId}, Parameter Index = 0x{index:X4}, Subindex = 0x{subIndex} value write failed!");
                }
            }
            catch (Exception)
            {
                result = false;
            }

            return(result);
        }
コード例 #3
0
        private async Task <bool> UpdateParameter(EltraDevice device, Parameter parameter)
        {
            bool result = false;

            try
            {
                var parameterUpdate = new ParameterUpdate
                {
                    ChannelId = Channel.Id,
                    NodeId    = device.NodeId,
                    Parameter = parameter
                };

                var path = $"api/parameter/update";

                var json = parameterUpdate.ToJson();

                var response = await Transporter.Put(_identity, Url, path, json);

                if (response != null)
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        result = true;
                    }
                    else
                    {
                        MsgLogger.WriteError($"{GetType().Name} - UpdateParameter", $"update parameter '{parameter.UniqueId}' failed, reason = {response.StatusCode}");
                    }
                }
                else
                {
                    MsgLogger.WriteError($"{GetType().Name} - UpdateParameter", $"update parameter '{parameter.UniqueId}' failed, reason = no response");
                }
            }
            catch (Exception)
            {
                result = false;
            }

            return(result);
        }