PLCCoreExtension.cs
4.29 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using HHECS.BllModel;
using HHECS.Communication.PLC;
using HHECS.Communication.PLC.PLCComponent;
using HHECS.Model.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace HHECS.Executor
{
/// <summary>
/// PLC读写扩展类
/// </summary>
public static class PLCCoreExtension
{
public static event Action<EquipmentProp[], long, BllResult> ReadRecord;
public static event Action<EquipmentProp[], long, BllResult> WriteRecord;
public static BllResult Reads(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();
Stopwatch stopwatch = Stopwatch.StartNew();
var result = plc.Reads(temp);
stopwatch.Stop();
if (result.Success)
{
temp.ForEach(t => equipmentProps.First(a => a.Id == t.Index).Value = t.Value);
ReadRecord?.Invoke(equipmentProps, stopwatch.ElapsedMilliseconds, result);
return BllResultFactory.Success(equipmentProps);
}
else
{
ReadRecord?.Invoke(equipmentProps, stopwatch.ElapsedMilliseconds, result);
return BllResultFactory.Error(result.Msg, result.ErrorCode);
}
}
public static BllResult Write(this PLCCore plc, EquipmentProp equipmentProps)
{
return Writes(plc, new List<EquipmentProp> { equipmentProps });
}
public static BllResult Writes(this PLCCore plc, List<EquipmentProp> equipmentProps)
{
return Writes(plc, equipmentProps.ToArray());
}
/// <summary>
/// 写入后再次读取,如果不符合,则报错
/// </summary>
/// <param name="plc"></param>
/// <param name="equipmentProps"></param>
/// <returns></returns>
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();
Stopwatch stopwatch = Stopwatch.StartNew();
var result = plc.Writes(temp);
if (!result.Success)
{
return BllResultFactory.Error(result.Msg);
}
//foreach (var item in temp)
//{
// var prop = equipmentProps.First(a => a.Id == item.Index);
// var tempValue = item.Value;
// var result = plc.Write(item);
// if (!result.Success)
// {
// return BllResultFactory.Error(result.Msg, result.ErrorCode);
// }
// if (prop.EquipmentTypePropTemplateCode != StationProps.WCSReplyMessage.ToString()
// && prop.EquipmentTypePropTemplateCode != StationProps.WCSACKMessage.ToString()
// && prop.EquipmentTypePropTemplateCode != StationProps.WCSControlMessage.ToString()
// && prop.EquipmentTypePropTemplateCode != SRMProps.WCSForkFlag.ToString())
// {
// result = plc.Read(item);
// if (!result.Success)
// {
// return BllResultFactory.Error($"反读校验出错,属性:{prop.EquipmentTypePropTemplateCode},设备:{prop.Equipment};详情:" + result.Msg, result.ErrorCode);
// }
// if (item.Value.Trim() != tempValue)
// {
// return BllResultFactory.Error($"反读校验写入前后不一致,地址{item.Address},属性:{prop.EquipmentTypePropTemplateCode},设备:{prop.Equipment.Code}");
// }
// }
//}
stopwatch.Stop();
WriteRecord?.Invoke(equipmentProps, stopwatch.ElapsedMilliseconds, BllResultFactory.Success());
return BllResultFactory.Success(equipmentProps);
}
}
}