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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace FLY.Thick.Base.Common
{
//格式
//时间, 报警码, 总长0, 留白0, 总长1, 留白1, 总长2, 留白2
//2016-12-30 12:20:30, 0, 300.1, 18.1, 700.1, 18.1, 400.5, 18.1
//2016-12-30 12:20:30, 0, 299.8, 18.0, 700.5, 18.1, 401.0, 17.7
public class FlyData_CoatingSegmentHistory : FLY.Thick.RemoteHistory.IFlyData, INotifyPropertyChanged
{
private DateTime time;
public DateTime Time
{
get {
return time;
}
set
{
time = value;
NotifyPropertyChanged("Time");
}
}
int errno;
/// <summary>
/// 报警码, 0 没错误, 1总长报警,2留白报警,3数量错
/// </summary>
public int Errno
{
get
{
return errno;
}
set
{
errno = value;
NotifyPropertyChanged("Errno");
}
}
int partcount = 1;
/// <summary>
/// 段数,prfile设定,不是测的!!!
/// </summary>
public int PartCount
{
get {
return partcount;
}
set
{
if (partcount != value)
{
partcount = value;
NotifyPropertyChanged("PartCount");
}
}
}
ObservableCollection<CoatingSegmentPart> coatingPart = new ObservableCollection<CoatingSegmentPart>();
public ObservableCollection<CoatingSegmentPart> CoatingPart
{
get
{
return coatingPart;
}
}
public string GetHeader()
{
string str = "时间,报警码";
for (int i = 0; i < PartCount; i++)
{
str += ",总长" + i.ToString();
str += ",留白" + i.ToString();
}
return str;
}
public override string ToString()
{
string str;
str = Time.ToString("yyyy/MM/dd HH:mm:ss") + "," + Errno.ToString();
for (int i = 0; i < PartCount; i++)
{
if (i < CoatingPart.Count)
{
str += "," + CoatingPart[i].Total.ToString("F1");
str += "," + CoatingPart[i].Distance.ToString("F1");
}
else
{
str += ",,";
}
}
return str;
}
public bool TryParse(string header_str, string str)
{
CoatingPart.Clear();
int idx = 0;
//通过header_str, 解析PartCount
string[] items_header = header_str.Split(new char[] { ',' });
if (items_header.Length < 4)
return false;
//看最后一个 是“留白x”
Regex r = new Regex(@"留白\d+");
Match m = r.Match(items_header.Last());
if (!m.Success)
return false;
r = new Regex(@"\d+");
PartCount = int.Parse(r.Match(m.Value).Value) + 1;
//正文
string[] items = str.Split(new char[] { ',' });
if (items.Length < (2 + PartCount * 2))
return false;
DateTime dt;
int numi;
double numd;
if (!DateTime.TryParse(items[idx], out dt))
return false;
idx++;
Time = dt;
if (!int.TryParse(items[idx], out numi))
return false;
idx++;
Errno = numi;
for (int i = 0; i < PartCount; i++)
{
if (string.IsNullOrWhiteSpace(items[idx]))
{
idx += 2;
}
CoatingSegmentPart cp = new CoatingSegmentPart();
if (!double.TryParse(items[idx], out numd))
return false;
idx++;
cp.Total = numd;
if (!double.TryParse(items[idx], out numd))
return false;
idx++;
cp.Distance = numd;
CoatingPart.Add(cp);
}
return true;
}
/// <summary>
/// 最少12个字节转换
/// </summary>
/// <param name="value">需要转换的字符串</param>
/// <param name="offset">在字符串的偏移</param>
/// <param name="count">转换的字符数</param>
/// <returns></returns>
public bool TryParse(byte[] value, int offset, out int count)
{
int idx = offset;
count = 8 + 4 + 4 + 4;
if (value.Length - offset < count)
return false;
CoatingPart.Clear();
Time = new DateTime(BitConverter.ToInt64(value, idx));
idx += 8;
Errno = BitConverter.ToInt32(value, idx);
idx += 4;
PartCount = BitConverter.ToInt32(value, idx);
idx += 4;
int c = BitConverter.ToInt32(value, idx);
idx += 4;
count += c * 16;
if (value.Length - offset < count)
return false;
for (int i = 0; i < c; i++)
{
CoatingSegmentPart p = new CoatingSegmentPart();
p.TryParse(value, idx);
idx += 16;
CoatingPart.Add(p);
}
return true;
}
public byte[] ToBytes()
{
List<byte> buf = new List<byte>();
buf.AddRange(BitConverter.GetBytes(Time.Ticks));
buf.AddRange(BitConverter.GetBytes(Errno));
buf.AddRange(BitConverter.GetBytes(PartCount));
int cnt = (PartCount < CoatingPart.Count) ? PartCount : CoatingPart.Count;
buf.AddRange(BitConverter.GetBytes(cnt));
for (int i = 0; i < cnt; i++)
buf.AddRange(CoatingPart[i].ToByte());
return buf.ToArray();
}
#region INotifyPropertyChanged 成员
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyname)
{
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
}
#endregion
}
}