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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using FLY.OBJComponents.IService;
using FLY.OBJComponents.Server;
using FLY.Weight2.Common;
using FLY.Weight2.IService;
using FLY.Weight2.Server.Model;
using FObjBase;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using FLY.Modbus;
namespace FLY.Weight2.Server
{
public class WeightSystem : IWeightSystemService, IPropertyOpt
{
const int FlowIntervalSec = 10;
#region IWeightSystemService 接口
public ObservableCollection<WeighterC> Items { get; } = new ObservableCollection<WeighterC>();
public WeighterAccessory Accessory { get; } = new WeighterAccessory();
private PLCProxySystem plcos = new PLCProxySystem();
/// <summary>
/// PLC代理系统
/// </summary>
public IPLCProxySystemService PLCos { get { return plcos; } }
/// <summary>
/// 层数
/// </summary>
public int ItemsCnt { get; private set; } = 3;
#endregion
string[] NumberNames;
/// <summary>
/// 报警系统
/// </summary>
WarningSystem2 warning;
/// <summary>
/// 记录到数据库
/// </summary>
HistoryDb historyDb;
/// <summary>
/// 报警配置
/// </summary>
ErrorConf errorConf;
/// <summary>
/// 周期保存Ibc数据
/// </summary>
PeriodicallySaveData<Lc_Flow> psdFlow;
public WeightSystem()
{
if (!LoadNumberNames())
SaveNumberNames();
//--------------------------------------------------------------------------------
//step 1 加载PLC寄存器 文件
AddConfigFile();
}
public void Init(HistoryDb historyDb, WarningSystem2 warning)
{
this.historyDb = historyDb;
this.warning = warning;
//--------------------------------------------------------------------------------
//step 2 报警配置
errorConf = new ErrorConf(plcos, this.warning, "称重");
errorConf.AddErrorAction(Accessory);
for (int i = 0; i < Items.Count(); i++)
{
errorConf.AddErrorAction(
Items[i],
(ref string description, object state) => {
int idx = (int)state;
description = Items[idx].Number + "层 " + description;
},
i);
}
errorConf.InitError();
//报警使能
Misc.BindingOperations.SetBinding(Accessory, nameof(Accessory.AlarmIsOn), () =>
{
this.warning.Enable = !Accessory.AlarmIsOn;
});
Misc.BindingOperations.SetBinding(this.warning, nameof(this.warning.Enable), () =>
{
Accessory.AlarmIsOn = !this.warning.Enable;
});
//报警复位呢?
//Misc.BindingOperations.SetBinding(this.warning, "IsRinging", () =>
//{
// if (!this.warning.IsRinging)
// {
// for (int i = 0; i < ItemsCnt; i++)
// {
// Items[i].IsAlarmReseted = false;
// }
// }
//});
//step 3 历史数据记录
//添加任务
for (int i = 0; i < Items.Count(); i++)
{
List<string> props = new List<string>
{
nameof(WeighterC.CurrentFlow),
nameof(WeighterC.ScrewPDisp),
nameof(WeighterC.ScrewMotorFreq)
};
PLCos.SetPlan($"{nameof(Items)}[{i}]", props, 0);
}
PLCos.SetPlan(nameof(Accessory), new string[] { nameof(Accessory.TotalFlow) }, 0);
psdFlow = new PeriodicallySaveData<Lc_Flow>();
psdFlow.Init(
FlowIntervalSec,
this.historyDb.AddFlowRange,
() => {
if (!PLCos.IsConnectedWithPLC)
return null;
if (Accessory.TotalFlow < 5)//没有生产
return null;
//记录数据
Lc_Flow lc_Flow = new Lc_Flow
{
Time = DateTime.Now,
Total = Math.Round(Accessory.TotalFlow, 4),
Items = new Lc_FlowItem[Items.Count]
};
for (int i = 0; i < Items.Count; i++)
{
lc_Flow.Items[i] = new Lc_FlowItem
{
Flow = Math.Round(Items[i].CurrentFlow, 4),
ScrewPDisp = Math.Round(Items[i].ScrewPDisp, 2),
ScrewMotorFreq = Math.Round(Items[i].ScrewMotorFreq, 1)
};
}
return lc_Flow;
});
//--------------------------------------------------------------------------------
//last step PLC 更新计划服务初始化
plcos.Init();
}
string GetNumber(int index)
{
string number;
if (NumberNames != null
&& index < NumberNames.Count()
&& !string.IsNullOrEmpty(NumberNames[index]))
{
number = NumberNames[index];
}
else
{
number = ((char)('A' + index)).ToString();
}
return number;
}
void AddConfigFile()
{
plcos.AddConfigFile("plcgroup.json", (plcgroup) =>
{
//从plcgroup 分析出 有多少层,每层的仓数
int itemsCnt = 0;
Regex r = new Regex(@"Items\[([0-9])\]");
foreach (PLCGroup.PLCVariable var in plcgroup.Variables)
{
Match m = r.Match(var.OwnerName);
if (m.Success)
{
int weighter_idx = int.Parse(m.Groups[1].Value);
if (itemsCnt <= weighter_idx)//记录最大值
itemsCnt = weighter_idx + 1;
}
}
ItemsCnt = itemsCnt;
for (int i = 0; i < ItemsCnt; i++)
{
string number = GetNumber(i);
WeighterC w = new WeighterC(number, i);
Items.Add(w);
}
//objname 转 obj
Dictionary<string, INotifyPropertyChanged> objnames = new Dictionary<string, INotifyPropertyChanged>();
objnames.Add(nameof(Accessory), Accessory);
for (int i = 0; i < ItemsCnt; i++)
objnames.Add($"{nameof(Items)}[{i}]", Items[i]);
return objnames;
});
//这个是从测厚仪获取的,每几秒就写一次,不调试!!!!!
plcos.IgnoreLogProperties.Add(new SenderProperty() { sender = Accessory, propertyName = nameof(Accessory.CurrentVelocitySet) });
}
public event PropertyChangedEventHandler PropertyChanged;
string filePath_number = "weight2NumberNames.json";
bool LoadNumberNames()
{
if (!File.Exists(filePath_number))
return false;
string json = File.ReadAllText(filePath_number);
var p = Newtonsoft.Json.JsonConvert.DeserializeObject<NumberNamesJsonDb>(json);
NumberNames = p.NumberNames;
return true;
}
void SaveNumberNames()
{
NumberNamesJsonDb jsonDb = new NumberNamesJsonDb
{
NumberNames = NumberNames
};
string json = Newtonsoft.Json.JsonConvert.SerializeObject(jsonDb);
File.WriteAllText(filePath_number, json);
}
public string[] GetSyncPropNames()
{
return new string[]{
nameof(ItemsCnt)
};
}
public string[] GetNoSyncPropNames()
{
return null;
}
}
public class NumberNamesJsonDb
{
public string[] NumberNames;
}
}