137 lines
3.0 KiB
C#
137 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
|
|
namespace ESPServer
|
|
{
|
|
class UnitsDatabase
|
|
{
|
|
List<Unit> units;
|
|
|
|
public UnitsDatabase(JsonElement config)
|
|
{
|
|
units = new List<Unit>();
|
|
foreach (var unitConfig in config.EnumerateArray())
|
|
{
|
|
units.Add(UnitFactory.CreateUnit(unitConfig));
|
|
}
|
|
}
|
|
|
|
internal Unit AddUnit(string id)
|
|
{
|
|
lock (units)
|
|
{
|
|
var u = new Unit();
|
|
u.id = id;
|
|
u.name = id;
|
|
u.baseAddress = -1;
|
|
u.values = new Dictionary<string, UnitValue>();
|
|
units.Add(u);
|
|
return u;
|
|
}
|
|
}
|
|
|
|
internal Dictionary<string, string> GetUnitIPs()
|
|
{
|
|
var result = new Dictionary<string, string>();
|
|
lock (units)
|
|
{
|
|
foreach (var u in units)
|
|
{
|
|
if (!string.IsNullOrEmpty(u.ip))
|
|
{
|
|
result[u.id] = u.ip;
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public Unit GetUnit(string id)
|
|
{
|
|
lock (units)
|
|
{
|
|
foreach (var u in units)
|
|
{
|
|
if (u.id == id) return u;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves unit by modbus address of any value in it
|
|
/// </summary>
|
|
/// <param name="valueAddress"></param>
|
|
/// <returns></returns>
|
|
public Unit GetUnit(int valueAddress)
|
|
{
|
|
lock (units)
|
|
{
|
|
foreach (var u in units)
|
|
{
|
|
if (u.HasAddress(valueAddress)) return u;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
#region OpenHAB config generation
|
|
internal void GenerateItemsConfig(StringBuilder config)
|
|
{
|
|
foreach (var u in units)
|
|
{
|
|
foreach (var uv in u.values)
|
|
{
|
|
var v = uv.Value;
|
|
config.Append(string.Format("Number {0}_{1} \"{0} {1}\"", u.name, v.name));
|
|
config.Append(" {channel=\"");
|
|
config.AppendFormat("modbus:data:esp_server:esp_server:");
|
|
config.AppendFormat("{0}_{1}:number", u.name, v.name);
|
|
config.Append("\"}\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
internal void GenerateThingsConfig(StringBuilder config)
|
|
{
|
|
int minAddress = int.MaxValue;
|
|
int maxAddress = int.MinValue;
|
|
foreach (var u in units)
|
|
{
|
|
if (u.baseAddress + u.MinOffset < minAddress)
|
|
{
|
|
minAddress = u.baseAddress + u.MinOffset;
|
|
}
|
|
if (u.baseAddress + u.MaxOffset > maxAddress)
|
|
{
|
|
maxAddress = u.baseAddress + u.MaxOffset;
|
|
}
|
|
}
|
|
// поскольку читаем флоаты, нужен ещё регистр запаса
|
|
maxAddress++;
|
|
config.Append("\tBridge poller ");
|
|
config.Append("esp_server");
|
|
config.AppendFormat("[start = {0}, length = {1}, type = \"holding\", refresh = 500] {{\n", minAddress, maxAddress - minAddress + 1);
|
|
foreach (var u in units)
|
|
{
|
|
foreach (var uv in u.values)
|
|
{
|
|
var v = uv.Value;
|
|
config.Append("\t\tThing data ");
|
|
config.Append(u.name + "_" + v.name);
|
|
config.AppendFormat("[readStart = \"{0}\", readValueType = \"float32\"", u.baseAddress + v.addressOffset);
|
|
if (u.IsValueWritable(v))
|
|
{
|
|
config.AppendFormat(", writeStart = \"{0}\", writeValueType = \"float32\", writeType=\"holding\"", u.baseAddress + v.addressOffset);
|
|
}
|
|
config.Append("]\n");
|
|
}
|
|
}
|
|
config.Append("\t}\n");
|
|
}
|
|
#endregion
|
|
}
|
|
}
|