Read machine from appsettings.json
This commit is contained in:
131
Modbus.Net/Modbus.Net/Configuration/MachineReader.cs
Normal file
131
Modbus.Net/Modbus.Net/Configuration/MachineReader.cs
Normal file
@@ -0,0 +1,131 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Modbus.Net
|
||||
{
|
||||
/// <summary>
|
||||
/// 从配置文件读取设备列表
|
||||
/// </summary>
|
||||
public class MachineReader
|
||||
{
|
||||
private static readonly IConfigurationRoot configuration = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.default.json")
|
||||
.AddJsonFile("appsettings.json")
|
||||
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true)
|
||||
.Build();
|
||||
|
||||
/// <summary>
|
||||
/// 读取设备列表
|
||||
/// </summary>
|
||||
/// <returns>设备的列表</returns>
|
||||
public static List<IMachine<string>> ReadMachines()
|
||||
{
|
||||
var ans = new List<IMachine<string>>();
|
||||
var root = configuration.GetSection("Modbus.Net").GetSection("Machine").GetChildren();
|
||||
foreach (var machine in root)
|
||||
{
|
||||
List<KeyValuePair<string, string>> kv = new List<KeyValuePair<string, string>>();
|
||||
Dictionary<string, string> dic = new Dictionary<string, string>();
|
||||
foreach (var paramO in machine.GetChildren())
|
||||
{
|
||||
foreach (var param in paramO.GetChildren())
|
||||
{
|
||||
kv.Add(new KeyValuePair<string, string>(param.Key, param.Value));
|
||||
dic[param.Key] = param.Value;
|
||||
}
|
||||
}
|
||||
|
||||
List<object> paramsSet = new List<object>();
|
||||
foreach (var param in kv)
|
||||
{
|
||||
switch (param.Key)
|
||||
{
|
||||
case "protocol":
|
||||
{
|
||||
paramsSet.Add(Enum.Parse(Assembly.Load("Modbus.Net." + dic["protocol"]).GetType("Modbus.Net." + dic["protocol"] + "." + dic["protocol"] + "Type"), dic["type"]));
|
||||
break;
|
||||
}
|
||||
case "type":
|
||||
{
|
||||
break;
|
||||
}
|
||||
case "model":
|
||||
{
|
||||
paramsSet.Add(Enum.Parse(Assembly.Load("Modbus.Net." + dic["protocol"]).GetType("Modbus.Net." + dic["protocol"] + "." + dic["protocol"] + "MachineModel"), dic["model"]));
|
||||
break;
|
||||
}
|
||||
case "addressMap":
|
||||
{
|
||||
paramsSet.Add(AddressReader.ReadAddresses(dic["addressMap"]));
|
||||
break;
|
||||
}
|
||||
case "endian":
|
||||
{
|
||||
paramsSet.Add(Enum.Parse<Endian>(dic["endian"]));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
string value = param.Value;
|
||||
bool boolValue;
|
||||
byte byteValue;
|
||||
if (!bool.TryParse(value, out boolValue))
|
||||
{
|
||||
if (!byte.TryParse(value, out byteValue))
|
||||
{
|
||||
paramsSet.Add(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
paramsSet.Add(byteValue);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
paramsSet.Add(boolValue);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Type machineType = Assembly.Load("Modbus.Net." + dic["protocol"]).GetType("Modbus.Net." + dic["protocol"] + "." + dic["protocol"] + "Machine`2");
|
||||
Type[] typeParams = new Type[] { typeof(string), typeof(string) };
|
||||
Type constructedType = machineType.MakeGenericType(typeParams);
|
||||
IMachine<string> machineInstance = Activator.CreateInstance(constructedType, paramsSet.ToArray()) as IMachine<string>;
|
||||
ans.Add(machineInstance);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
|
||||
class AddressReader
|
||||
{
|
||||
private static readonly IConfigurationRoot configuration = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.default.json")
|
||||
.AddJsonFile("appsettings.json")
|
||||
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true)
|
||||
.Build();
|
||||
|
||||
public static IEnumerable<AddressUnit<string>> ReadAddresses(string addressMapName)
|
||||
{
|
||||
var ans = new List<AddressUnit<string>>();
|
||||
var addressesRoot = configuration.GetSection("Modbus.Net").GetSection("AddressMap").GetSection(addressMapName).GetChildren();
|
||||
foreach (var address in addressesRoot)
|
||||
{
|
||||
|
||||
var addressNew = address.Get<AddressUnit<string>>();
|
||||
addressNew.DataType = "System." + address["DataType"] != null ? Type.GetType("System." + address["DataType"]) : throw new ArgumentNullException("DataType is null");
|
||||
if (addressNew.DataType == null) throw new ArgumentNullException(string.Format("DataType define error {0} {1} {2}", addressMapName, addressNew.Id, address["DataType"]));
|
||||
ans.Add(addressNew);
|
||||
}
|
||||
return ans.AsEnumerable();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -723,13 +723,6 @@ namespace Modbus.Net
|
||||
public IEnumerable<AddressUnit<TKey>> OriginalAddresses { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据单元扩展,返回数据时会同时将其返回
|
||||
/// </summary>
|
||||
public class UnitExtend
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回的数据单元
|
||||
/// </summary>
|
||||
@@ -813,11 +806,6 @@ namespace Modbus.Net
|
||||
/// </summary>
|
||||
public bool CanWrite { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 扩展
|
||||
/// </summary>
|
||||
public UnitExtend UnitExtend { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 两个地址是否一致
|
||||
/// </summary>
|
||||
|
||||
@@ -50,7 +50,6 @@ namespace Modbus.Net
|
||||
Name = addressUnit.Name,
|
||||
Unit = addressUnit.Unit,
|
||||
CanWrite = addressUnit.CanWrite,
|
||||
UnitExtend = addressUnit.UnitExtend
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="DotNetty.Handlers" Version="0.7.5" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
|
||||
<PackageReference Include="Nito.AsyncEx" Version="5.1.2" />
|
||||
<PackageReference Include="Quartz" Version="3.6.2" />
|
||||
|
||||
Reference in New Issue
Block a user