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; }
|
public IEnumerable<AddressUnit<TKey>> OriginalAddresses { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 数据单元扩展,返回数据时会同时将其返回
|
|
||||||
/// </summary>
|
|
||||||
public class UnitExtend
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 返回的数据单元
|
/// 返回的数据单元
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -813,11 +806,6 @@ namespace Modbus.Net
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool CanWrite { get; set; } = true;
|
public bool CanWrite { get; set; } = true;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 扩展
|
|
||||||
/// </summary>
|
|
||||||
public UnitExtend UnitExtend { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 两个地址是否一致
|
/// 两个地址是否一致
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -50,7 +50,6 @@ namespace Modbus.Net
|
|||||||
Name = addressUnit.Name,
|
Name = addressUnit.Name,
|
||||||
Unit = addressUnit.Unit,
|
Unit = addressUnit.Unit,
|
||||||
CanWrite = addressUnit.CanWrite,
|
CanWrite = addressUnit.CanWrite,
|
||||||
UnitExtend = addressUnit.UnitExtend
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="DotNetty.Handlers" Version="0.7.5" />
|
<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="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
|
||||||
<PackageReference Include="Nito.AsyncEx" Version="5.1.2" />
|
<PackageReference Include="Nito.AsyncEx" Version="5.1.2" />
|
||||||
<PackageReference Include="Quartz" Version="3.6.2" />
|
<PackageReference Include="Quartz" Version="3.6.2" />
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
using Modbus.Net;
|
using Modbus.Net;
|
||||||
using Modbus.Net.Modbus;
|
|
||||||
using Modbus.Net.Siemens;
|
|
||||||
using DataReturnDef = Modbus.Net.DataReturnDef<string, double>;
|
using DataReturnDef = Modbus.Net.DataReturnDef<string, double>;
|
||||||
using ModbusMachine = Modbus.Net.Modbus.ModbusMachine<string, string>;
|
|
||||||
using MultipleMachinesJobScheduler = Modbus.Net.MultipleMachinesJobScheduler<Modbus.Net.IMachineMethodDatas, string, double>;
|
using MultipleMachinesJobScheduler = Modbus.Net.MultipleMachinesJobScheduler<Modbus.Net.IMachineMethodDatas, string, double>;
|
||||||
using SiemensMachine = Modbus.Net.Siemens.SiemensMachine<string, string>;
|
|
||||||
|
|
||||||
namespace MachineJob.Service
|
namespace MachineJob.Service
|
||||||
{
|
{
|
||||||
@@ -19,39 +15,7 @@ namespace MachineJob.Service
|
|||||||
|
|
||||||
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
|
var machines = MachineReader.ReadMachines();
|
||||||
List<AddressUnit> _addresses = new List<AddressUnit>
|
|
||||||
{
|
|
||||||
new AddressUnit() { Area = "4X", Address = 1, DataType = typeof(short), Id = "1", Name = "Test1" },
|
|
||||||
new AddressUnit() { Area = "4X", Address = 2, DataType = typeof(short), Id = "2", Name = "Test2" },
|
|
||||||
new AddressUnit() { Area = "4X", Address = 3, DataType = typeof(short), Id = "3", Name = "Test3" },
|
|
||||||
new AddressUnit() { Area = "4X", Address = 4, DataType = typeof(short), Id = "4", Name = "Test4" },
|
|
||||||
new AddressUnit() { Area = "4X", Address = 5, DataType = typeof(short), Id = "5", Name = "Test5" },
|
|
||||||
new AddressUnit() { Area = "4X", Address = 6, DataType = typeof(short), Id = "6", Name = "Test6" },
|
|
||||||
new AddressUnit() { Area = "4X", Address = 7, DataType = typeof(short), Id = "7", Name = "Test7" },
|
|
||||||
new AddressUnit() { Area = "4X", Address = 8, DataType = typeof(short), Id = "8", Name = "Test8" },
|
|
||||||
new AddressUnit() { Area = "4X", Address = 9, DataType = typeof(short), Id = "9", Name = "Test9" },
|
|
||||||
new AddressUnit() { Area = "4X", Address = 10, DataType = typeof(short), Id = "10", Name = "Test10" }
|
|
||||||
};
|
|
||||||
|
|
||||||
List<AddressUnit> _addresses2 = new List<AddressUnit>
|
|
||||||
{
|
|
||||||
new AddressUnit() { Area = "DB1", Address = 0, DataType = typeof(short), Id = "1", Name = "Test1" },
|
|
||||||
new AddressUnit() { Area = "DB1", Address = 2, DataType = typeof(short), Id = "2", Name = "Test2" },
|
|
||||||
new AddressUnit() { Area = "DB1", Address = 4, DataType = typeof(short), Id = "3", Name = "Test3" },
|
|
||||||
new AddressUnit() { Area = "DB1", Address = 6, DataType = typeof(short), Id = "4", Name = "Test4" },
|
|
||||||
new AddressUnit() { Area = "DB1", Address = 8, DataType = typeof(short), Id = "5", Name = "Test5" },
|
|
||||||
new AddressUnit() { Area = "DB1", Address = 10, DataType = typeof(short), Id = "6", Name = "Test6" },
|
|
||||||
new AddressUnit() { Area = "DB1", Address = 12, DataType = typeof(short), Id = "7", Name = "Test7" },
|
|
||||||
new AddressUnit() { Area = "DB1", Address = 14, DataType = typeof(short), Id = "8", Name = "Test8" },
|
|
||||||
new AddressUnit() { Area = "DB1", Address = 16, DataType = typeof(short), Id = "9", Name = "Test9" },
|
|
||||||
new AddressUnit() { Area = "DB1", Address = 18, DataType = typeof(short), Id = "10", Name = "Test10" }
|
|
||||||
};
|
|
||||||
|
|
||||||
IMachine<string> machine = new ModbusMachine("ModbusMachine1", ModbusType.Tcp, null, _addresses, true, 1, 2, Endian.BigEndianLsb);
|
|
||||||
IMachine<string> machine2 = new SiemensMachine("SiemensMachine1", SiemensType.Tcp, null, SiemensMachineModel.S7_1200, _addresses2, true, 1, 2);
|
|
||||||
IMachine<string> machine3 = new ModbusMachine("ModbusMachine2", ModbusType.Rtu, "COM1", _addresses, true, 3, 2);
|
|
||||||
var machines = new List<IMachine<string>>() { machine, machine2, machine3 };
|
|
||||||
return Task.Run(() => MultipleMachinesJobScheduler.RunScheduler(machines, async (machine, scheduler) =>
|
return Task.Run(() => MultipleMachinesJobScheduler.RunScheduler(machines, async (machine, scheduler) =>
|
||||||
{
|
{
|
||||||
await scheduler.From(machine.Id, machine, MachineDataType.Name).Result.Query(machine.Id + ".ConsoleQuery", QueryConsole).Result.To(machine.Id + ".To", machine).Result.Deal(machine.Id + ".Deal", OnSuccess, OnFailure).Result.Run();
|
await scheduler.From(machine.Id, machine, MachineDataType.Name).Result.Query(machine.Id + ".ConsoleQuery", QueryConsole).Result.To(machine.Id + ".To", machine).Result.Deal(machine.Id + ".Deal", OnSuccess, OnFailure).Result.Run();
|
||||||
|
|||||||
@@ -13,13 +13,188 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
"Modbus.Net": {
|
"Modbus.Net": {
|
||||||
"TCP": {
|
"Machine": [
|
||||||
"Modbus": {
|
{
|
||||||
"IP": "192.168.8.210"
|
"a:id": "ModbusMachine1",
|
||||||
|
"b:protocol": "Modbus",
|
||||||
|
"c:type": "Tcp",
|
||||||
|
"d:connectionString": "192.168.8.210",
|
||||||
|
"e:addressMap": "AddressMapModbus",
|
||||||
|
"f:keepConnect": true,
|
||||||
|
"g:slaveAddress": 1,
|
||||||
|
"h:masterAddress": 2,
|
||||||
|
"i:endian": "BigEndianLsb"
|
||||||
},
|
},
|
||||||
"Siemens": {
|
{
|
||||||
"IP": "192.168.8.210"
|
"a:id": "SiemensMachine1",
|
||||||
|
"b:protocol": "Siemens",
|
||||||
|
"c:type": "Tcp",
|
||||||
|
"d:connectionString": "192.168.8.210",
|
||||||
|
"e:model": "S7_1200",
|
||||||
|
"f:addressMap": "AddressMapSiemens",
|
||||||
|
"g:keepConnect": true,
|
||||||
|
"h:slaveAddress": 1,
|
||||||
|
"i:masterAddress": 2,
|
||||||
|
"j:src": 1,
|
||||||
|
"k:dst": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"a:id": "ModbusMachine2",
|
||||||
|
"b:protocol": "Modbus",
|
||||||
|
"c:type": "Rtu",
|
||||||
|
"d:connectionString": "COM1",
|
||||||
|
"e:addressMap": "AddressMapModbus",
|
||||||
|
"f:keepConnect": true,
|
||||||
|
"g:slaveAddress": 3,
|
||||||
|
"h:masterAddress": 2,
|
||||||
|
"i:endian": "BigEndianLsb"
|
||||||
}
|
}
|
||||||
|
],
|
||||||
|
"addressMap": {
|
||||||
|
"AddressMapModbus": [
|
||||||
|
{
|
||||||
|
"Area": "4X",
|
||||||
|
"Address": 1,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "1",
|
||||||
|
"Name": "Test1"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Area": "4X",
|
||||||
|
"Address": 2,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "2",
|
||||||
|
"Name": "Test2"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Area": "4X",
|
||||||
|
"Address": 3,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "3",
|
||||||
|
"Name": "Test3"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Area": "4X",
|
||||||
|
"Address": 4,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "4",
|
||||||
|
"Name": "Test4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Area": "4X",
|
||||||
|
"Address": 5,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "5",
|
||||||
|
"Name": "Test5"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Area": "4X",
|
||||||
|
"Address": 6,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "6",
|
||||||
|
"Name": "Test6"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Area": "4X",
|
||||||
|
"Address": 7,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "7",
|
||||||
|
"Name": "Test7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Area": "4X",
|
||||||
|
"Address": 8,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "8",
|
||||||
|
"Name": "Test8"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Area": "4X",
|
||||||
|
"Address": 9,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "9",
|
||||||
|
"Name": "Test9"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Area": "4X",
|
||||||
|
"Address": 10,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "10",
|
||||||
|
"Name": "Test10"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"AddressMapSiemens": [
|
||||||
|
{
|
||||||
|
"Area": "DB1",
|
||||||
|
"Address": 0,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "1",
|
||||||
|
"Name": "Test1"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Area": "DB1",
|
||||||
|
"Address": 2,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "2",
|
||||||
|
"Name": "Test2"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Area": "DB1",
|
||||||
|
"Address": 4,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "3",
|
||||||
|
"Name": "Test3"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Area": "DB1",
|
||||||
|
"Address": 6,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "4",
|
||||||
|
"Name": "Test4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Area": "DB1",
|
||||||
|
"Address": 8,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "5",
|
||||||
|
"Name": "Test5"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Area": "DB1",
|
||||||
|
"Address": 10,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "6",
|
||||||
|
"Name": "Test6"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Area": "DB1",
|
||||||
|
"Address": 12,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "7",
|
||||||
|
"Name": "Test7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Area": "DB1",
|
||||||
|
"Address": 14,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "8",
|
||||||
|
"Name": "Test8"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Area": "DB1",
|
||||||
|
"Address": 16,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "9",
|
||||||
|
"Name": "Test9"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Area": "DB1",
|
||||||
|
"Address": 18,
|
||||||
|
"DataType": "Int16",
|
||||||
|
"Id": "10",
|
||||||
|
"Name": "Test10"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user