PLCCoreExtension.cs
1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using HHECS.BllModel;
using HHECS.Communication;
using HHECS.Communication.PLCComponent;
using HHECS.Model.Entities;
using MySqlX.XDevAPI.Common;
using System.Collections.Generic;
using System.Linq;
namespace HHECS.Executor
{
/// <summary>
/// todo:PLC读写扩展类
/// </summary>
public static class PLCCoreExtension
{
public static BllResult Reads(this PLCCore plc, params EquipmentProp[] equipmentProps)
{
try
{
var temp = equipmentProps.Select(t => new PLCAddressObj()
{
Index = t.Id,
IP = t.Equipment.IP,
Address = t.Address,
Value = t.Value,
}).ToList();
var result = plc.Reads(temp);
if (result.Success)
{
temp.ForEach(t => equipmentProps.First(a => a.Id == t.Index).Value = t.Value);
return BllResultFactory.Success(equipmentProps);
}
else
{
return BllResultFactory.Error(result.Msg, result.ErrorCode);
}
}
catch (System.Exception ex)
{
return BllResultFactory.Error("读取设备地址出现异常、"+ex.Message);
}
}
public static BllResult Writes(this PLCCore plc, params EquipmentProp[] equipmentProps)
{
var temp = equipmentProps.Select(t => new PLCAddressObj()
{
Index = t.Id,
IP = t.Equipment.IP,
Address = t.Address,
Value = t.Value,
}).ToList();
var result = plc.Writes(temp);
if (result.Success)
{
return BllResultFactory.Success(equipmentProps);
}
else
{
return BllResultFactory.Error(result.Msg, result.ErrorCode);
}
}
}
}