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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;
using FLY.Weight.OBJ_INTERFACE;
using System.Reflection;
using Misc;
using System.ComponentModel;
using System.Net;
using System.Xml;
using FLY.Modbus;
using FLY.Weight.IService;
using FLY.Weight.Common;
using FLY.OBJComponents.Client;
using FLY.OBJComponents.IService;
using System.Collections.ObjectModel;
using FLY.Weight.Server.Model;
using System.IO;
namespace FLY.Weight.Client
{
public class WeightSystemServiceClient: FObjServiceClient, IWeightSystemService, IPropertyOpt
{
#region IWeightSystemService 接口
public ObservableCollection<WeighterC> Items { get; } = new ObservableCollection<WeighterC>();
public WeighterAccessory Accessory { get; } = new WeighterAccessory();
/// <summary>
/// PLC代理系统
/// </summary>
public IPLCProxySystemService PLCos { get; set; }
/// <summary>
/// 流量记录周期,单位s
/// </summary>
public int FlowInterval { get; set; } = 10;
/// <summary>
/// 层数
/// </summary>
public int ItemsCnt { get; set; } = 3;
/// <summary>
/// 每层仓数
/// </summary>
public int[] BinCnts { get; set; } = new int[] { 4, 4, 4 };
#endregion
SyncPropServiceClient syncPropServiceClient;
public event Action ResetItemsEvent;
/// <summary>
/// TODO
/// </summary>
/// <param name="serviceId"></param>
public WeightSystemServiceClient(UInt32 serviceId) : base(serviceId) {
Init();
}
public WeightSystemServiceClient(UInt32 serviceId, string connName) : base(serviceId) {
Init();
ConnName = connName;
filePath = connName + "." +filePath;
FObjServiceClientManager.Instance.Connect_to_Another_OBJSys(connName, this);
}
/// <summary>
/// 以给定的各层仓数创建,当接收到 BinCnts变化,
/// 重新修改各层, 删除,或添加
/// </summary>
/// <param name="bincnts"></param>
void Init()
{
Load();
//BinCnts = bincnts;
for (int i = 0; i < BinCnts.Count(); i++)
{
WeighterC w = new WeighterC(((char)('A' + i)).ToString(), BinCnts[i]);
Items.Add(w);
}
ItemsCnt = BinCnts.Count();
//--------------------------------------------------------------
//属性同步
Dictionary<string, INotifyPropertyChanged> objnames = new Dictionary<string, INotifyPropertyChanged>();
objnames.Add(".", this);
objnames.Add("Accessory", Accessory);
for (int i = 0; i < Items.Count(); i++)
objnames.Add($"Items[{i}]", Items[i]);
syncPropServiceClient = new SyncPropServiceClient(mServerID + 1, objnames);
//--------------------------------------------------------------
//PLC代理,用于添加更新任务
Dictionary<string, INotifyPropertyChanged> objnames2 = new Dictionary<string, INotifyPropertyChanged>();
objnames2.Add("Accessory", Accessory);
for (int i = 0; i < Items.Count(); i++)
objnames2.Add($"Items[{i}]", Items[i]);
PLCos = new PLCProxySystemServiceClient(mServerID + 2, objnames2);
this.PropertyChanged += WeightSystemClient_PropertyChanged;
}
string filePath = "weightSystemClient.json";
void Save()
{
WeightSystemClientJsonDb jsonDb = new WeightSystemClientJsonDb()
{
BinCnts = BinCnts
};
string json = Newtonsoft.Json.JsonConvert.SerializeObject(jsonDb);
File.WriteAllText(filePath, json);
}
void Load() {
if (!File.Exists(filePath))
return;
string json =File.ReadAllText(filePath);
var p = Newtonsoft.Json.JsonConvert.DeserializeObject< WeightSystemClientJsonDb>(json);
BinCnts = p.BinCnts;
}
private void WeightSystemClient_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "BinCnts")
{
ResetItems();
Save();
}
}
/// <summary>
/// 重新创建 Items
/// </summary>
void ResetItems()
{
int last_itemcnt = Items.Count();
for (int i = 0; i < Items.Count(); i++)
{
if (i < BinCnts.Count())
{
if (Items[i].BinCnt != BinCnts[i])
{
Items[i].BinsReBinding(BinCnts[i]);
}
}
else
{
//多出来的Item 都要删除
int remove_cnt = Items.Count() - BinCnts.Count();
for (i = 0; i < remove_cnt; i++)
Items.RemoveAt(Items.Count() - 1);
goto _step1;
}
}
for (int i = Items.Count(); i < BinCnts.Count(); i++)
{
//少了,需要添加
WeighterC w = new WeighterC(((char)('A' + i)).ToString(), BinCnts[i]);
Items.Add(w);
}
_step1:
//处理属性同步
if (last_itemcnt == Items.Count())//数量相同,不需要处理
goto _step2;
if (last_itemcnt < Items.Count())
{
//少了需要添加
for (int i = last_itemcnt; i < Items.Count(); i++)
{
syncPropServiceClient.Add($"Items[{i}]", Items[i]);
PLCos.ObjNames.Add($"Items[{i}]", Items[i]);
}
}
else
{
//多了需要删除
for (int i = Items.Count(); i < last_itemcnt; i++)
{
syncPropServiceClient.Remove($"Items[{i}]");
PLCos.ObjNames.Remove($"Items[{i}]");
}
}
_step2:
ResetItemsEvent?.Invoke();
return;
}
public override UInt32[] GetIDs()
{
List<UInt32> IDs = new List<uint>
{
ID,
syncPropServiceClient.ID,
};
IDs.AddRange(((PLCProxySystemServiceClient)PLCos).GetIDs());
return IDs.ToArray();
}
public override void ConnectNotify(IFConn from)
{
base.ConnectNotify(from);
if (from.IsConnected)
{
CurrObjSys.SenseConfigEx(mConn, mServerID, ID,
0xffffffff, SENSE_CONFIG.ADD);
}
}
public string[] GetSyncPropNames()
{
return new string[]{
"FlowInterval",
"ItemsCnt",
"BinCnts"
};
}
public string[] GetNoSyncPropNames()
{
return null;
}
}
public class WeightSystemClientJsonDb
{
/// <summary>
/// 每层仓数
/// </summary>
public int[] BinCnts { get; set; } = new int[] { 4, 4, 4 };
}
}