Different types of units. Writing values, relay unit
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace ESPServer
|
||||
{
|
||||
class RelayUnit : Unit
|
||||
{
|
||||
public override byte[] GetWriteCommand(int address, float value)
|
||||
{
|
||||
foreach (var v in values)
|
||||
{
|
||||
if (v.Value.name == "r" && v.Value.addressOffset + baseAddress == address)
|
||||
{
|
||||
return GetWritePayload(value);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private byte[] GetWritePayload(float value)
|
||||
{
|
||||
string t = "r_write\x01";
|
||||
if (MathF.Abs(value) < 1e-4) t += "0";
|
||||
else t += "1";
|
||||
t += "\x01";
|
||||
return Encoding.ASCII.GetBytes(t);
|
||||
}
|
||||
|
||||
public override bool IsValueWritable(UnitValue v)
|
||||
{
|
||||
return v.name == "r";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace ESPServer
|
||||
{
|
||||
/*
|
||||
* Описание параметра, который может придти от юнита
|
||||
*/
|
||||
class UnitValue
|
||||
{
|
||||
// Название параметра
|
||||
public string name;
|
||||
// Текущее значение
|
||||
public float value;
|
||||
// Сдвиг адреса, по которому это значение будет храниться в modbus
|
||||
public int addressOffset;
|
||||
}
|
||||
|
||||
/*
|
||||
* Описание конкретного модуля (релейный, датчик, whatever)
|
||||
*/
|
||||
class Unit
|
||||
{
|
||||
// Айдишник юнита (мак-адрес)
|
||||
public string id;
|
||||
// Название (для генерации конфигов например)
|
||||
public string name;
|
||||
// Базовый адрес в модбасе
|
||||
public int baseAddress;
|
||||
// Значения которые есть в юните
|
||||
public Dictionary<string, UnitValue> values;
|
||||
// текущий IP юнита
|
||||
public string ip;
|
||||
|
||||
public UnitValue GetValue(string name)
|
||||
{
|
||||
if (!values.ContainsKey(name))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return values[name];
|
||||
}
|
||||
|
||||
public int MinOffset
|
||||
{
|
||||
get
|
||||
{
|
||||
int min = int.MaxValue;
|
||||
foreach (var u in values) if (u.Value.addressOffset < min) min = u.Value.addressOffset;
|
||||
return min;
|
||||
}
|
||||
}
|
||||
|
||||
public int MaxOffset
|
||||
{
|
||||
get
|
||||
{
|
||||
int max = int.MinValue;
|
||||
foreach (var u in values) if (u.Value.addressOffset > max) max = u.Value.addressOffset;
|
||||
return max;
|
||||
}
|
||||
}
|
||||
|
||||
public bool HasAddress(int address)
|
||||
{
|
||||
foreach (var u in values)
|
||||
{
|
||||
if (u.Value.addressOffset + baseAddress == address) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public virtual byte[] GetWriteCommand(int address, float value)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public virtual bool IsValueWritable(UnitValue v)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ESPServer
|
||||
{
|
||||
static class UnitFactory
|
||||
{
|
||||
public static Unit CreateUnit(JsonElement config)
|
||||
{
|
||||
var type = "";
|
||||
JsonElement val;
|
||||
if (config.TryGetProperty("type", out val))
|
||||
{
|
||||
type = val.GetString();
|
||||
}
|
||||
Unit u;
|
||||
if (type == "relay") u = new RelayUnit();
|
||||
else u = new Unit();
|
||||
u.id = config.GetProperty("id").GetString();
|
||||
u.name = config.GetProperty("name").GetString();
|
||||
u.baseAddress = config.GetProperty("address").GetInt32();
|
||||
u.values = new Dictionary<string, UnitValue>();
|
||||
foreach (var knownValue in config.GetProperty("values").EnumerateArray())
|
||||
{
|
||||
var uv = new UnitValue();
|
||||
uv.name = knownValue.GetProperty("name").GetString();
|
||||
uv.addressOffset = knownValue.GetProperty("offset").GetInt32();
|
||||
u.values[uv.name] = uv;
|
||||
}
|
||||
|
||||
return u;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user