Esempio n. 1
0
    private bool ActionOnChange(ITagRuleDefinition <string> arg)
    {
        var result = new List <bool>();
        var rule   = settings.Rules.First(r => r.EventId == arg.EventId);

        foreach (var writeTagConfig in rule.WriteTags)
        {
            var read     = ReadValue(writeTagConfig.Value.Trim(), arg);
            var tagWrite = connector.GetOrAddTag(writeTagConfig.Tag);

            logger?.LogInformation("Action [Event={0} Type={1}]. Write Tag:[{2} Value={3} Status={4}] Whit [<{5}> Value:{6} Status:{7}]",
                                   arg.EventId, arg.RuleCheckType, tagWrite.TagName, tagWrite.Value, tagWrite.Status, read.name, read.value, read.status);

            if (read.status)
            {
                result.Add(tagWrite.SetText(read.value.Text));
            }
        }

        processInfo.RuleEventId     = arg.EventId;
        processInfo.ActionExecuted += 1;
        processInfo.ActionWriteTags = result.Count();
        processInfo.SetValuesFromPropertiesAsync().Wait();
        return(result.Any());
    }
    private bool ActionOnChange(ITagRuleDefinition <string> arg)
    {
        logger?.LogInformation("Action for Event:[{0}] {1} TagName:[{2}] Expresion:[{3}] PrevValue:{4} Value:{5} Updated:{6}",
                               arg.EventId, arg.RuleCheckType, arg.TagExpression.TagName, arg.TagExpression, arg.PrevValue, arg.Value, arg.Updated);

        var rule = settings.Rules.First(r => r.EventId == arg.EventId);

        var sql        = string.Join("\n", rule.WriteSql.FormatSql);
        var parameters = GetSqlParameters(rule, sql, arg);

        logger.LogInformation("Try SaveToDb \nSql:{@sql} \nParameters:{@param}", sql, parameters);
        var result = database.SaveToDb(rule.WriteSql.ConnectionFactory, sql, rule.WriteSql.CommandType, parameters);

        logger.LogInformation("SaveToDb affected rows: {rows}", result);

        processInfo.RuleEventId        = arg.EventId;
        processInfo.ActionExecuted    += 1;
        processInfo.ActionAffectedRows = result;
        processInfo.SetValuesFromPropertiesAsync().Wait();

        return(true);
    }
    private Dictionary <string, object> GetSqlParameters(RuleConfiguration rule, string commandSql, ITagRuleDefinition <string> arg)
    {
        var result  = new Dictionary <string, object>();
        var sqlArgs = rule.WriteSql.ReadTags.Select(rt => ReadValue(rt, arg)).ToList();
        var matches = regParameters.Matches(commandSql).Select(m => m.Value).ToList();

        var count = 0;

        foreach (var tag in sqlArgs)
        {
            result.Add($"@tag_{count}", tag.value.Text);
            result.Add($"@tag_{count}_numeric", tag.value.Numeric);
            result.Add($"@tag_{count}_name", tag.name);
            result.Add($"@tag_{count}_status", tag.status);
            result.Add($"@tag_{count}_updated", tag.updated);
            count++;
        }

        return(result
               .Where(r => matches.Contains(r.Key))
               .ToDictionary(p => p.Key, p => p.Value));
    }
    public (IRtValue value, bool status, string name, DateTime updated) ReadValue(string readExpression, ITagRuleDefinition <string> arg)
    {
        IRtTag tagRead;

        switch (readExpression)
        {
        case "TagExpression":
            tagRead = arg.TagExpression;
            return(tagRead.Value, tagRead.Status, tagRead.TagName, tagRead.Updated);

        case "PrevValue":
            return(RtValue.Create(arg.PrevValue), true, readExpression, arg.PrevUpdated);

        case "Value":
            return(RtValue.Create(arg.Value), true, readExpression, arg.Updated);

        default:
            tagRead = GetTag(readExpression);
            return(tagRead.Value, tagRead.Status, tagRead.TagName, tagRead.Updated);
        }
    }
Esempio n. 5
0
    public (IRtValue value, bool status, string name) ReadValue(string readExpression, ITagRuleDefinition <string> arg)
    {
        if (readExpression.StartsWith("'") && readExpression.EndsWith("'"))
        {
            return(RtValue.Create(readExpression.Trim('\'')), true, readExpression);
        }

        IRtTag tagRead;

        switch (readExpression)
        {
        case "TagExpression":
            tagRead = arg.TagExpression;
            return(tagRead.Value, tagRead.Status, tagRead.ToString());

        case "PrevValue":
            return(RtValue.Create(arg.PrevValue), true, readExpression);

        case "Value":
            return(RtValue.Create(arg.Value), true, readExpression);

        case "UpdatedUnixTimestamp":
            return(RtValue.Create(arg.Updated.ToUnixTimestamp()), true, "UpdatedUnixTimestamp");

        default:
            tagRead = GetTag(readExpression);
            return(tagRead.Value, tagRead.Status, tagRead.ToString());
        }
    }