76 lines
2.1 KiB
C#
76 lines
2.1 KiB
C#
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
|
|
namespace Barsik
|
|
{
|
|
class ExternalMonitoringUnit : BaseMonitoringUnit
|
|
{
|
|
public ExternalMonitoringUnit(JsonElement config, RegistersContainer registers) : base(config, registers)
|
|
{
|
|
}
|
|
|
|
public override void GenerateItemsConfig(string name, StringBuilder config)
|
|
{
|
|
config.Append(string.Format("Number {0}_{1} \"{0} {1}\"", name, unitName));
|
|
config.Append(" {channel=\"");
|
|
config.AppendFormat("modbus:data:{0}:{1}:", name, unitName);
|
|
config.AppendFormat("{1}:number", name, unitName);
|
|
config.Append("\"}\n");
|
|
}
|
|
|
|
public override void GenerateThingsConfig(StringBuilder config)
|
|
{
|
|
config.Append("\tBridge poller ");
|
|
config.Append(unitName);
|
|
config.AppendFormat("[start = {0}, length = 2, type = \"holding\", refresh = 500] {{\n", baseAddress);
|
|
config.Append("\t\tThing data ");
|
|
config.Append(unitName);
|
|
config.AppendFormat("[readStart = \"{0}\", readValueType = \"float32\"]\n", baseAddress);
|
|
config.Append("\t}\n");
|
|
}
|
|
|
|
public override void Run()
|
|
{
|
|
string program = config.GetProperty("Program").GetString();
|
|
string args = config.GetProperty("Arguments").GetString();
|
|
var logger = LogManager.GetCurrentClassLogger();
|
|
while (true)
|
|
{
|
|
ProcessStartInfo procStartInfo = new ProcessStartInfo(program);
|
|
procStartInfo.Arguments = args;
|
|
procStartInfo.RedirectStandardOutput = true;
|
|
procStartInfo.UseShellExecute = false;
|
|
procStartInfo.CreateNoWindow = true;
|
|
using (Process proc = new Process())
|
|
{
|
|
proc.StartInfo = procStartInfo;
|
|
proc.Start();
|
|
while (!proc.HasExited)
|
|
{
|
|
Thread.Sleep(100);
|
|
}
|
|
string result = proc.StandardOutput.ReadToEnd();
|
|
result = result.Trim();
|
|
float resultFloat;
|
|
if (float.TryParse(result, out resultFloat))
|
|
{
|
|
registers.SetFloat(baseAddress, resultFloat);
|
|
}
|
|
else
|
|
{
|
|
logger.Error("Non-float result from process " + program + " with args " + args);
|
|
logger.Error("Result:");
|
|
logger.Error(result);
|
|
}
|
|
}
|
|
Thread.Sleep(2000);
|
|
}
|
|
}
|
|
}
|
|
}
|