コード例 #1
0
        private CloudMessage InvokeRawRequest(CloudMessage requestMessage)
        {
            try
            {
                WirehomeDictionary responseContent;

                var requestContent = requestMessage.GetContent <JToken>();

                // TODO: Refactor this and build converter for JSON to WirehomeDictionary and WirehomeList
                if (!(PythonConvert.ToPython(requestContent) is PythonDictionary parameters))
                {
                    responseContent = new WirehomeDictionary().WithType(ControlType.ParameterInvalidException);
                }
                else
                {
                    if (!_messageHandlers.TryGetValue(parameters.GetValueOr("type", string.Empty), out var messageHandler))
                    {
                        responseContent = new WirehomeDictionary().WithType(ControlType.NotSupportedException);
                    }
                    else
                    {
                        responseContent = messageHandler.Invoke(parameters);
                    }
                }

                var responseMessage = new CloudMessage();
                responseMessage.SetContent(responseContent);

                return(responseMessage);
            }
コード例 #2
0
        // TODO: Move to "JsonPythonProxy (deserialize(json), serialize(source))
        public object deserialize_json(object source)
        {
            var jsonText = to_string(source);
            var json     = JToken.Parse(jsonText);

            return(PythonConvert.ToPython(json));
        }
コード例 #3
0
        private CloudMessage InvokeRawRequest(CloudMessage requestMessage)
        {
            try
            {
                object content;

                // TODO: Refactor this and build converter for JSON to WirehomeDictionary and WirehomeList
                if (!(PythonConvert.ToPython(requestMessage.Content) is PythonDictionary parameters))
                {
                    content = new WirehomeDictionary().WithType(ControlType.ParameterInvalidException);
                }
                else
                {
                    if (!_messageHandlers.TryGetValue(parameters.GetValueOr("type", string.Empty), out var messageHandler))
                    {
                        content = new WirehomeDictionary().WithType(ControlType.NotSupportedException);
                    }
                    else
                    {
                        content = messageHandler.Invoke(parameters);
                    }
                }

                return(_messageFactory.CreateResponseMessage(requestMessage, content));
            }
コード例 #4
0
        public object deserialize(object value)
        {
            var jsonText = new ConverterPythonProxy().to_string(value);
            var json     = JToken.Parse(jsonText);

            return(PythonConvert.ToPython(json));
        }
コード例 #5
0
        public static List to_list(object value)
        {
            if (value is IEnumerable enumerable)
            {
                PythonConvert.ToPythonList(enumerable);
            }

            return(new List {
                PythonConvert.ToPython(value)
            });
        }
コード例 #6
0
        public object get_setting(string componentUid, string settingUid)
        {
            if (componentUid == null)
            {
                throw new ArgumentNullException(nameof(componentUid));
            }
            if (settingUid == null)
            {
                throw new ArgumentNullException(nameof(settingUid));
            }

            return(PythonConvert.ToPython(_componentRegistryService.GetComponentSetting(componentUid, settingUid)));
        }
コード例 #7
0
 public object get_configuration(string configuration_uid, [DefaultParameterValue(null)] object default_value)
 {
     return(PythonConvert.ToPython(_componentRegistryService.GetComponentConfigurationValue(_componentUid, configuration_uid, default_value)));
 }
コード例 #8
0
 public object get_setting(string setting_uid, [DefaultParameterValue(null)] object default_value)
 {
     return(PythonConvert.ToPython(_componentRegistryService.GetComponentSetting(_componentUid, setting_uid, default_value)));
 }
コード例 #9
0
 public object get_setting(string component_uid, string setting_uid, object default_value = null)
 {
     return(PythonConvert.ToPython(_componentRegistryService.GetComponentSetting(component_uid, setting_uid, default_value)));
 }
コード例 #10
0
 public object get_status(string component_uid, string status_uid, object default_value = null)
 {
     return(PythonConvert.ToPython(_componentRegistryService.GetComponentStatusValue(component_uid, status_uid, default_value)));
 }
コード例 #11
0
        public PythonDictionary send(PythonDictionary parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            try
            {
                using (var httpClient = new HttpClient())
                    using (var request = CreateRequest(parameters))
                    {
                        var response = httpClient.SendAsync(request).GetAwaiter().GetResult();
                        var content  = response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();

                        var result = new PythonDictionary
                        {
                            ["type"]        = ControlType.Success,
                            ["status_code"] = (int)response.StatusCode
                        };

                        var responseContentType = Convert.ToString(parameters.get("response_content_type", "text"));
                        if (responseContentType == "raw")
                        {
                            var rawContent = new List();
                            foreach (var contentByte in content)
                            {
                                rawContent.Add(contentByte);
                            }

                            result["content"] = rawContent;
                        }
                        else if (responseContentType == "text")
                        {
                            result["content"] = Encoding.UTF8.GetString(content);
                        }
                        else if (responseContentType == "json")
                        {
                            var jsonString = Encoding.UTF8.GetString(content);
                            if (!string.IsNullOrEmpty(jsonString))
                            {
                                try
                                {
                                    var json = JObject.Parse(jsonString);

                                    var convertedJson = PythonConvert.ToPython(json);
                                    result["content"] = convertedJson;
                                }
                                catch (Exception exception)
                                {
                                    return(new ExceptionPythonModel(exception).ConvertToPythonDictionary());
                                }
                            }
                        }

                        return(result);
                    }
            }
            catch (Exception exception)
            {
                return(new ExceptionPythonModel(exception).ConvertToPythonDictionary());
            }
        }
コード例 #12
0
 public object get_setting(string setting_uid, object default_value = null)
 {
     return(PythonConvert.ToPython(_automationRegistryService.GetAutomationSetting(_automationUid, setting_uid, default_value)));
 }
コード例 #13
0
 public object get_configuration(string configuration_uid, object default_value = null)
 {
     return(PythonConvert.ToPython(_componentRegistryService.GetComponentConfiguration(_componentUid, configuration_uid, default_value)));
 }
コード例 #14
0
 public object invoke(string service_id, string function_name, params object[] parameters)
 {
     return(PythonConvert.ToPython(_serviceHostService.InvokeFunction(service_id, function_name, parameters)));
 }