Beispiel #1
0
        public static JSResultDto RunNodeJsScript(string script, List <string> nodeParams, string[] otherData)
        {
            if (otherData.Length < 2)
            {
                return(getErrorJSResult("ERROR IN SCRIPT - Not added payload!"));
            }

            var p = new List <string>();

            foreach (var o in otherData)
            {
                p.Add(o);
            }

            foreach (var o in nodeParams)
            {
                p.Add(o);
            }

            try
            {
                var scriptResult = JSScriptHelper.RunJavaScript(script, "nodeJSfunction", p.ToArray());

                if (string.IsNullOrEmpty(scriptResult))
                {
                    return(getErrorJSResult("ERROR IN SCRIPT - Script Result Not Received"));
                }

                var srParsed = JsonConvert.DeserializeObject <JSResultDto>(scriptResult);

                if (srParsed == null)
                {
                    return(getErrorJSResult("ERROR IN SCRIPT - wrong return format. Must match JSResultDto!"));
                }

                if (srParsed.done)
                {
                    return(srParsed);
                }
                else
                {
                    return(srParsed);
                }
            }
            catch (Exception ex)
            {
                log.Error("Error in running node custom JavaScript", ex);
                throw new Exception($"Cannot run node custom JavaScirpt: {ex}");
            }
        }
        public override async Task <NodeActionFinishedArgs> InvokeNodeFunction(NodeActionTriggerTypes actionType, string[] otherData, string altFunction = "")
        {
            if (!(bool)IsActivated)
            {
                return(await Task.FromResult(new NodeActionFinishedArgs()
                {
                    result = "NOT_ACTIVATED", data = $"MQTT Publish Node - {Name} - Node is not activated. You cannot invoke action!"
                }));
            }

            if (actionType != ActualTriggerType)
            {
                return(await Task.FromResult(new NodeActionFinishedArgs()
                {
                    result = "NOT_INVOKED", data = $"MQTT Publish Node - {Name} - Node is not set to this {Enum.GetName(actionType)} of trigger. It is set to {Enum.GetName(ActualTriggerType)}!"
                }));
            }

            JSResultDto jsRes = new JSResultDto();

            // Node Custom JavaScript call
            if (ParsedParams.IsScriptActive)
            {
                if (!string.IsNullOrEmpty(ParsedParams.Script) && (otherData.Length > 0))
                {
                    if (!string.IsNullOrEmpty(altFunction))
                    {
                        jsRes = JSScriptHelper.RunNodeJsScript(altFunction, ParsedParams.ScriptParametersList, otherData);
                    }

                    jsRes = JSScriptHelper.RunNodeJsScript(ParsedParams.Script, ParsedParams.ScriptParametersList, otherData);

                    if (!jsRes.done)
                    {
                        return(await Task.FromResult(new NodeActionFinishedArgs()
                        {
                            result = "NOT_INVOKED", data = $"MQTT Publish Node - {Name} - Custom JS script runned with error {jsRes.payload}!"
                        }));
                    }
                }
            }

            try
            {
                var p = JsonConvert.DeserializeObject <MQTTNodeParams>(ParsedParams.Parameters);

                if (p != null)
                {
                    if (string.IsNullOrEmpty(p.Topic))
                    {
                        return(await Task.FromResult(new NodeActionFinishedArgs()
                        {
                            result = "NOT_INVOKED", data = $"MQTT Publish Node - {Name} - Topic is not set!"
                        }));
                    }

                    if (ParsedParams.TimeDelay > 0)
                    {
                        await Task.Delay(ParsedParams.TimeDelay);
                    }

                    var res = string.Empty;

                    if (ParsedParams.Command != null)
                    {
                        var payload = string.Empty;

                        if (ParsedParams.IsScriptActive && !string.IsNullOrEmpty(jsRes.payload))
                        {
                            payload = jsRes.payload;
                        }
                        else
                        {
                            payload = p.Data;
                        }

                        LastPayload   = payload;
                        LastOtherData = otherData;

                        NodeActionRequestTypes type = NodeActionRequestTypes.MQTTPublishNotRetain;

                        if (p.Retain)
                        {
                            type = NodeActionRequestTypes.MQTTPublishRetain;
                        }

                        ActionRequest?.Invoke(this, new NodeActionRequestArgs()
                        {
                            Type    = type,
                            Topic   = p.Topic,
                            Payload = payload
                        });
                    }
                    else
                    {
                        res = $"MQTT Publish Node - {Name} - Command cannot be null. Please fill full Topic name!";
                        log.Warn($"Node {Name} MQTT Publish Node - Command cannot be null. Please fill full Topic name!!");
                    }

                    ActionFinished?.Invoke(this, new NodeActionFinishedArgs()
                    {
                        result = "OK", data = res
                    });

                    return(await Task.FromResult(new NodeActionFinishedArgs()
                    {
                        result = "OK", data = res
                    }));
                }
                else
                {
                    log.Warn($"Node {Name} cannot send MQTT Publish. Cannot parse the parameters!");
                    return(await Task.FromResult(new NodeActionFinishedArgs()
                    {
                        result = "ERROR", data = $"MQTT Publish Node - {Name} - Cannot parse the parameters!"
                    }));
                }
            }
            catch (Exception ex)
            {
                log.Error($"Node {Name} cannot send MQTT Publish. ", ex);
            }

            return(await Task.FromResult(new NodeActionFinishedArgs()
            {
                result = "ERROR", data = "Unexpected error!"
            }));
        }
Beispiel #3
0
        public override async Task <NodeActionFinishedArgs> InvokeNodeFunction(NodeActionTriggerTypes actionType, string[] otherData, string altFunction = "")
        {
            if (!(bool)IsActivated)
            {
                return(await Task.FromResult(new NodeActionFinishedArgs()
                {
                    result = "NOT_ACTIVATED", data = "HTTP API Node - Node is not activated. You cannot invoke action!"
                }));
            }

            if (actionType != ActualTriggerType)
            {
                return(await Task.FromResult(new NodeActionFinishedArgs()
                {
                    result = "NOT_INVOKED", data = $"HTTP API Node - Node is not set to this {Enum.GetName(actionType)} of trigger. It is set to {Enum.GetName(ActualTriggerType)}!"
                }));
            }

            JSResultDto jsRes = new JSResultDto();

            // Node Custom JavaScript call
            if (ParsedParams.IsScriptActive)
            {
                if (!string.IsNullOrEmpty(ParsedParams.Script) && (otherData.Length > 0))
                {
                    if (!string.IsNullOrEmpty(altFunction))
                    {
                        jsRes = JSScriptHelper.RunNodeJsScript(altFunction, ParsedParams.ScriptParametersList, otherData);
                    }

                    jsRes = JSScriptHelper.RunNodeJsScript(ParsedParams.Script, ParsedParams.ScriptParametersList, otherData);

                    if (!jsRes.done)
                    {
                        return(await Task.FromResult(new NodeActionFinishedArgs()
                        {
                            result = "NOT_INVOKED", data = $"HTTP API Node - Custom JS script runned with error {jsRes.payload}!"
                        }));
                    }
                }
            }

            try
            {
                var p = JsonConvert.DeserializeObject <HTTPApiRequestNodeParams>(ParsedParams.Parameters);

                if (p != null)
                {
                    var url = string.Empty;
                    if (!p.UseHttps)
                    {
                        url = $"http://{p.Host}";
                    }
                    else
                    {
                        url = $"https://{p.Host}";
                    }

                    if (!string.IsNullOrEmpty(p.Port))
                    {
                        url += $":{p.Port}/";
                    }
                    else
                    {
                        url += "/";
                    }

                    if (!string.IsNullOrEmpty(p.ApiCommand))
                    {
                        url += p.ApiCommand;
                    }

                    if (ParsedParams.TimeDelay > 0)
                    {
                        await Task.Delay(ParsedParams.TimeDelay);
                    }

                    var payload = string.Empty;
                    if (ParsedParams.IsScriptActive && !string.IsNullOrEmpty(jsRes.payload))
                    {
                        payload = jsRes.payload;
                    }
                    else
                    {
                        payload = p.Data;
                    }

                    LastPayload   = payload;
                    LastOtherData = otherData;

                    var res = string.Empty;

                    if (ParsedParams.Command != null)
                    {
                        switch (ParsedParams.Command)
                        {
                        case "GET":
                            res = await SendGETRequest(url, payload);

                            break;

                        case "PUT":
                            res = await SendPURequest(url, payload);

                            break;

                        case "":
                            res = "HTTP API Node - Command cannot be empty. Please fill GET, POST or PUT!";
                            log.Warn($"Node {Name} cannot send HTTP Request. Command is empty. Please fill GET, POST or PUT!");
                            break;
                        }
                    }
                    else
                    {
                        res = "HTTP API Node - Command cannot be null. Please fill GET, POST or PUT!";
                        log.Warn($"Node {Name} cannot send HTTP Request. Command is null. Please fill GET, POST or PUT!");
                    }

                    ActionFinished?.Invoke(this, new NodeActionFinishedArgs()
                    {
                        result = "OK", data = res
                    });

                    return(await Task.FromResult(new NodeActionFinishedArgs()
                    {
                        result = "OK", data = res
                    }));
                }
                else
                {
                    return(await Task.FromResult(new NodeActionFinishedArgs()
                    {
                        result = "ERROR", data = "HTTP API Node - Cannot parse the parameters!"
                    }));

                    log.Warn($"Node {Name} cannot send HTTP Request. Cannot parse the parameters!");
                }
            }
            catch (Exception ex)
            {
                log.Error($"Node {Name} cannot send HTTP Request. ", ex);
            }

            return(await Task.FromResult(new NodeActionFinishedArgs()
            {
                result = "ERROR", data = "Unexpected error!"
            }));
        }