Different types of units. Writing values, relay unit
This commit is contained in:
@@ -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