Exemplo n.º 1
0
        public static PythonDictionary execute(PythonDictionary parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            try
            {
                var launchParameters = ParseLaunchParameters(parameters);
                var result           = new ExecuteResult();

                using (var process = StartProcess(launchParameters))
                {
                    var hasExited = process.WaitForExit(launchParameters.Timeout);
                    if (!hasExited)
                    {
                        process.Kill();
                    }

                    result.OutputData = process.StandardOutput.ReadToEnd();
                    result.ErrorData  = process.StandardError.ReadToEnd();

                    result.ExitCode = process.ExitCode;
                }

                return(ConvertToPythonDictionary(result));
            }
            catch (Exception exception)
            {
                return(PythonConvert.ToPythonDictionary(new ExceptionPythonModel(exception).ToDictionary()));
            }
        }
Exemplo n.º 2
0
 public string subscribe(string uid, PythonDictionary filter, MessageCallback callback)
 {
     return(_messageBusService.Subscribe(uid, filter, m =>
     {
         callback(PythonConvert.ToPythonDictionary(m));
     }));
 }
Exemplo n.º 3
0
        private CloudMessage InvokeRawRequest(CloudMessage requestMessage)
        {
            try
            {
                var jsonRequestPayload = Encoding.UTF8.GetString(requestMessage.Payload);
                var jsonRequest        = JObject.Parse(jsonRequestPayload);
                var pythonDictionary   = PythonConvert.ToPythonDictionary(jsonRequest);

                CloudMessageHandler cloudMessageHandler;
                lock (_rawMessageHandlers)
                {
                    if (!_rawMessageHandlers.TryGetValue(pythonDictionary.GetValueOr("type", string.Empty), out cloudMessageHandler))
                    {
                        throw new NotSupportedException("RAW message type handler not supported.");
                    }
                }

                var responseContent = cloudMessageHandler.Invoke(pythonDictionary);

                var jsonResponse = PythonConvert.FromPythonToJson(responseContent);

                return(new CloudMessage()
                {
                    CorrelationId = requestMessage.CorrelationId,
                    Payload = Encoding.UTF8.GetBytes(jsonResponse.ToString())
                });
            }
            catch (Exception exception)
            {
                return(_cloudMessageFactory.Create(exception));
            }
        }
        public static PythonDictionary publish_external(PythonDictionary parameters)
        {
            if (parameters is null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            var server   = Convert.ToString(parameters.get("server"));
            var port     = Convert.ToInt32(parameters.get("port", 1883));
            var username = Convert.ToString(parameters.get("username"));
            var password = Convert.ToString(parameters.get("password"));
            var clientId = Convert.ToString(parameters.get("client_id", Guid.NewGuid().ToString("N")));
            var topic    = Convert.ToString(parameters.get("topic"));
            var qos      = Convert.ToInt32(parameters.get("qos", 0));
            var retain   = Convert.ToBoolean(parameters.get("retain", false));
            var tls      = Convert.ToBoolean(parameters.get("tls", false));
            var timeout  = Convert.ToInt32(parameters.get("timeout", 5000));
            var payload  = parameters.get("payload", null);

            var mqttClient = new MqttFactory().CreateMqttClient();

            try
            {
                var options = new MqttClientOptionsBuilder().WithTcpServer(server, port).WithCredentials(username, password).WithClientId(clientId)
                              .WithTimeout(TimeSpan.FromMilliseconds(timeout)).WithTls(new MqttClientOptionsBuilderTlsParameters
                {
                    UseTls = tls
                }).Build();

                mqttClient.ConnectAsync(options).GetAwaiter().GetResult();

                var message = new MqttApplicationMessageBuilder().WithTopic(topic).WithPayload(PythonConvert.ToPayload(payload))
                              .WithQualityOfServiceLevel((MqttQualityOfServiceLevel)qos).WithRetainFlag(retain).Build();

                mqttClient.PublishAsync(message).GetAwaiter().GetResult();

                return(new PythonDictionary
                {
                    ["type"] = "success"
                });
            }
            catch (MqttConnectingFailedException)
            {
                return(new PythonDictionary
                {
                    ["type"] = "exception.connecting_failed"
                });
            }
            catch (Exception exception)
            {
                return(PythonConvert.ToPythonDictionary(new ExceptionPythonModel(exception).ToDictionary()));
            }
            finally
            {
                mqttClient?.DisconnectAsync().GetAwaiter().GetResult();
                mqttClient?.Dispose();
            }
        }
Exemplo n.º 5
0
        public void register_raw_message_handler(string type, RawMessageHandler handler)
        {
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            _cloudService.RegisterMessageHandler(type, p => handler(PythonConvert.ToPythonDictionary(p)));
        }
Exemplo n.º 6
0
 public string start_timer(string uid, int interval, Action <PythonDictionary> callback, object state = null)
 {
     return(_schedulerService.StartTimer(uid, TimeSpan.FromMilliseconds(interval), p =>
     {
         var pythonDictionary = PythonConvert.ToPythonDictionary(p);
         pythonDictionary["timer_uid"] = uid;
         callback(pythonDictionary);
     }, state));
 }
Exemplo n.º 7
0
 public string attach_to_default_timer(string uid, Action <PythonDictionary> callback, object state = null)
 {
     return(_schedulerService.AttachToDefaultTimer(uid, p =>
     {
         var pythonDictionary = PythonConvert.ToPythonDictionary(p);
         pythonDictionary["timer_uid"] = uid;
         callback(pythonDictionary);
     }, state));
 }
Exemplo n.º 8
0
 public string start_thread(string uid, Action <PythonDictionary> callback, object state = null)
 {
     return(_schedulerService.StartThread(uid, p =>
     {
         var pythonDictionary = PythonConvert.ToPythonDictionary(p);
         pythonDictionary["thread_uid"] = uid;
         callback(pythonDictionary);
     }, state));
 }
        public PythonDictionary invoke(string uid, PythonDictionary parameters)
        {
            if (uid == null)
            {
                throw new ArgumentNullException(nameof(uid));
            }

            return(PythonConvert.ToPythonDictionary(_functionPoolService.InvokeFunction(uid, parameters)));
        }
        public PythonDictionary read_object(string path, PythonDictionary defaultValue = null)
        {
            if (_valueStorageService.TryRead <IDictionary <object, object> >(RelativeValueStoragePath.Parse(path), out var value))
            {
                return(PythonConvert.ToPythonDictionary(value));
            }

            return(defaultValue);
        }
Exemplo n.º 11
0
 public string start_countdown(string uid, long duration, Action <PythonDictionary> callback, object state = null)
 {
     return(_schedulerService.StartCountdown(uid, TimeSpan.FromMilliseconds(duration), p =>
     {
         var pythonDictionary = PythonConvert.ToPythonDictionary(p);
         pythonDictionary["countdown_uid"] = uid;
         callback(pythonDictionary);
     }, state));
 }
        //public void publish_response(PythonDictionary request, PythonDictionary response)
        //{
        //    _messageBusService.PublishResponse(request, response);
        //}

        public string subscribe(string uid, PythonDictionary filter, Action <PythonDictionary> callback)
        {
            return(_messageBusService.Subscribe(uid, filter, m =>
            {
                var pythonDictionary = PythonConvert.ToPythonDictionary(m.Message);
                pythonDictionary["subscription_uid"] = uid;

                callback(pythonDictionary);
            }));
        }
        public void register(string uid, PythonDelegates.CallbackWithResultDelegate function)
        {
            if (uid == null)
            {
                throw new ArgumentNullException(nameof(uid));
            }
            if (function == null)
            {
                throw new ArgumentNullException(nameof(function));
            }

            _functionPoolService.RegisterFunction(uid, p => function(PythonConvert.ToPythonDictionary(p)));
        }
Exemplo n.º 14
0
 public PythonDictionary process_message(string component_uid, PythonDictionary message)
 {
     try
     {
         return(PythonConvert.ToPythonDictionary(_componentRegistryService.ProcessComponentMessage(component_uid, message)));
     }
     catch (ComponentNotFoundException)
     {
         return(new PythonDictionary
         {
             ["type"] = "exception.component_not_found",
             ["component_uid"] = component_uid
         });
     }
 }
Exemplo n.º 15
0
        public static PythonDictionary launch(PythonDictionary parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            try
            {
                var launchParameters = ParseLaunchParameters(parameters);
                var result           = new LaunchResult();

                using (var process = StartProcess(launchParameters))
                {
                    result.Pid = process.Id;
                }

                return(ConvertToPythonDictionary(result));
            }
            catch (Exception exception)
            {
                return(PythonConvert.ToPythonDictionary(new ExceptionPythonModel(exception).ToDictionary()));
            }
        }
Exemplo n.º 16
0
 public object ConvertToPython()
 {
     return(PythonConvert.ToPythonDictionary(this));
 }
Exemplo n.º 17
0
        public PythonDictionary send(PythonDictionary parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            try
            {
                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(PythonConvert.ToPythonDictionary(new ExceptionPythonModel(exception).ToDictionary()));
                            }
                        }
                    }

                    return(result);
                }
            }
            catch (Exception exception)
            {
                return(PythonConvert.ToPythonDictionary(new ExceptionPythonModel(exception).ToDictionary()));
            }
        }
Exemplo n.º 18
0
 public PythonDictionary ConvertToPythonDictionary()
 {
     return(PythonConvert.ToPythonDictionary(this));
 }
Exemplo n.º 19
0
 public string start_thread(string uid, Action <PythonDictionary> callback, object state = null)
 {
     return(_schedulerService.StartThread(uid, p => callback(PythonConvert.ToPythonDictionary(p)), state));
 }
Exemplo n.º 20
0
 public string attach_to_default_timer(string uid, Action <PythonDictionary> callback, object state = null)
 {
     return(_schedulerService.AttachToDefaultTimer(uid, p => callback(PythonConvert.ToPythonDictionary(p)), state));
 }
Exemplo n.º 21
0
 public IDictionary <object, object> GetDebugInformation(IDictionary <object, object> parameters)
 {
     ThrowIfLogicNotSet();
     return(_logic.GetDebugInformation(PythonConvert.ToPythonDictionary(parameters)));
 }
Exemplo n.º 22
0
 public IDictionary <object, object> ProcessMessage(IDictionary <object, object> message)
 {
     ThrowIfLogicNotSet();
     return(_logic.ProcessMessage(PythonConvert.ToPythonDictionary(message)));
 }
Exemplo n.º 23
0
        //public void publish_response(PythonDictionary request, PythonDictionary response)
        //{
        //    _messageBusService.PublishResponse(request, response);
        //}

        public string subscribe(string uid, PythonDictionary filter, Action <PythonDictionary> callback)
        {
            return(_messageBusService.Subscribe(uid, filter, m => callback(PythonConvert.ToPythonDictionary(m.Message))));
        }
Exemplo n.º 24
0
 public IDictionary <object, object> GetDebugInformation(string uid, [FromBody] IDictionary <object, object> parameters)
 {
     return(_componentRegistryService.GetComponent(uid).GetDebugInformation(PythonConvert.ToPythonDictionary(parameters)));
 }
Exemplo n.º 25
0
 public string register_route(string uid, string uri_template, RequestHandler handler)
 {
     return(_httpServerService.RegisterRoute(uid, uri_template, request => handler(PythonConvert.ToPythonDictionary(request))));
 }
Exemplo n.º 26
0
 public WirehomeDictionary ConvertToWirehomeDictionary()
 {
     return(PythonConvert.ToPythonDictionary(this));
 }
Exemplo n.º 27
0
 public PythonDictionary execute_macro(string uid)
 {
     return(PythonConvert.ToPythonDictionary(_macroRegistryService.ExecuteMacro(uid)));
 }