예제 #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);
    }
예제 #3
0
    public async Task ReadRtTagAsync()
    {
        try
        {
            if (!modbusClient.Connected)
            {
                modbusClient.Init();
            }

            foreach (var block in settings.ReadBlocks)
            {
                var data = await ReadBlockAsync(block);

                ParseBlock(block, data);
            }

            prevException              = null;
            processInfo.ScanDateTime   = DateTime.Now;
            processInfo.ScanCounter   += 1;
            processInfo.ScanRetry      = 0;
            processInfo.ScanLastErrror = "";

            await processInfo.SetValuesFromPropertiesAsync(new[] { nameof(processInfo.ScanCounter) });
        }
        catch (Exception e)
        {
            if (e.Message != prevException?.Message)
            {
                logger.LogError(e, "Scan Fail!");
                prevException = e;
                processInfo.ScanLastErrror = e.Message;
            }
            processInfo.ScanRetry  += 1;
            processInfo.ScanCounter = 0;
            modbusClient.Terminate();
        }
    }