public override async Task <string> SetNodeTrigger(string nodeId, NodeActionTriggerTypes type, IDbConnectorService dbservice)
        {
            //IDbConnectorService dbservice = new DbConnectorService();

            try
            {
                if (EconomyMainContext.Nodes.TryGetValue(nodeId, out var node))
                {
                    node.SetNodeTriggerType(type);

                    if (EconomyMainContext.WorkWithDb)
                    {
                        if (!dbservice.SaveNode(node))
                        {
                            return("Cannot save Node to the db!");
                        }
                    }

                    return("OK");
                }
                else
                {
                    log.Error("Cannot find node");
                    return("Cannot find node!");
                }
            }
            catch (Exception ex)
            {
                log.Error("Cannot set node", ex);
                return("Cannot set node!");
            }
        }
예제 #2
0
 public override Task <NodeActionFinishedArgs> InvokeNodeFunction(NodeActionTriggerTypes actionType, string[] otherData, string altFunction = "")
 {
     return(Task.FromResult(new NodeActionFinishedArgs()
     {
         result = "ERROR", data = "Not Implemented!"
     }));
 }
        public override async Task <string> TriggerNodesActions(NodeActionTriggerTypes type, NewTransactionDTO txdata, object payload)
        {
            var result = "ERROR";

            if (EconomyMainContext.Accounts.TryGetValue(txdata.AccountAddress, out var account))
            {
                var pld = JsonConvert.SerializeObject(payload);
                var acc = JsonConvert.SerializeObject(account);

                foreach (var node in EconomyMainContext.Nodes)
                {
                    if (node.Value.AccountId == account.Id)
                    {
                        var res = await node.Value.InvokeNodeFunction(type, new string[] { pld, acc });;
                    }
                }
            }

            return(result);
        }
예제 #4
0
 public override void SetNodeTriggerType(NodeActionTriggerTypes type)
 {
     ActualTriggerType        = type;
     ParsedParams.TriggerType = type;
     Parameters = JsonConvert.SerializeObject(ParsedParams);
 }
 public abstract Task <NodeActionFinishedArgs> InvokeNodeFunction(NodeActionTriggerTypes actionType, string[] otherData, string altFunction = "");
 public abstract void SetNodeTriggerType(NodeActionTriggerTypes type);
        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!"
            }));
        }
예제 #8
0
 public abstract Task <string> TriggerNodesActions(NodeActionTriggerTypes type, NewTransactionDTO txdata, object payload);
예제 #9
0
 public abstract Task <string> SetNodeTrigger(string nodeId, NodeActionTriggerTypes type, IDbConnectorService dbservice);
예제 #10
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!"
            }));
        }