Different types of units. Writing values, relay unit

This commit is contained in:
ftc
2021-09-12 16:01:26 +03:00
parent 80cad276e8
commit cfd8d4c018
10 changed files with 296 additions and 74 deletions
+36
View File
@@ -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;
}
}
}