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
using SQLite;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FLY.HeatingHelper
{
class ThickHeatData4SQLite_V2 : ThickHeatData
{
#region constructor
public ThickHeatData4SQLite_V2() : base() { }
public ThickHeatData4SQLite_V2(string dbname)
:base(dbname)
{
}
#endregion
#region 数据库接口
class Temp
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
}
private SQLiteHelper mSQLiteHelper = new SQLiteHelper();
private string ConnectionString
{
get
{
return string.Format("Data Source={0};Version=3;", DBName);
}
}
/// <summary>
/// 从数据库中获取数据,清除以前数据
/// </summary>
public override void GetDataFromDB(bool reload = false)
{
//Model.Model_HeatData db;
//string connectionString = ConfigurationManager.ConnectionStrings[DBName].ConnectionString.ToString();
//using (db = new Model.Model_HeatData(DBName))
//{
// var result = db.Database.SqlQuery<Temp>("SELECT min(\"Time\")as A,MAX(\"Time\") as B from ThickHeat").First();
// TotalDataFrom = DateTime.Parse(result.A);
// TotalDataTo = DateTime.Parse(result.B);
// DataTo = new DateTime(TotalDataTo.Ticks);
// DataSpan = new TimeSpan(2, 0, 0, 0);
// //DataFrom = DBTo - new TimeSpan(2, 0, 0, 0);
//}
mSQLiteHelper.ConnectionString = ConnectionString;
var dtb = mSQLiteHelper.ExecuteReader("SELECT min(\"Time\")as A,MAX(\"Time\") as B from ThickHeat");
if (dtb.Rows[0].IsNull("A")) return;
TotalDataFrom = DateTime.Parse(dtb.Rows[0].Field<string>("A"));
TotalDataTo = DateTime.Parse(dtb.Rows[0].Field<string>("B"));
LoadDataTo = new DateTime((TotalDataTo+new TimeSpan(0,0,1)).Ticks);
LoadDataSpan = new TimeSpan(2, 0, 0, 0);
string cmdstr;
if (reload || _ids.Count() == 0)
{
if(IsFilterUnstable)
cmdstr = string.Format("select * from [ThickHeat] where IsStable=1 and Time>=\"{0}\" and Time<=\"{1}\"",
(LoadDataTo - LoadDataSpan).ToString("yyyy-MM-dd HH:mm:ss"),
LoadDataTo.ToString("yyyy-MM-dd HH:mm:ss"));
else
cmdstr = string.Format("select * from [ThickHeat] where Time>=\"{0}\" and Time<=\"{1}\"",
(LoadDataTo - LoadDataSpan).ToString("yyyy-MM-dd HH:mm:ss"),
LoadDataTo.ToString("yyyy-MM-dd HH:mm:ss"));
}
else
{
if(IsFilterUnstable)
cmdstr = string.Format("select * from [ThickHeat] where ID>\"{0}\" and IsStable=1", _ids.Last());
else
cmdstr = string.Format("select * from [ThickHeat] where ID>\"{0}\"", _ids.Last());
}
dataTable = mSQLiteHelper.ExecuteReader(cmdstr);
//SQLiteDataAdapter dbDataAdapter = new SQLiteDataAdapter(cmdstr, connectionString);
//SQLiteCommandBuilder cb = new SQLiteCommandBuilder(dbDataAdapter);
//dataTable.Clear();
//dbDataAdapter.Fill(dataTable);
//FetchData();
//ResetCluster();
}
class Boltmap
{
public int OldNo;
public int NewNo;
}
/// <summary>
/// 从dataTable中提取数据到内部缓冲区
/// </summary>
protected override int FetchData()
{
_dat_times.Clear();
_thicks.Clear();
_thick_means.Clear();
_thick_2sigmas.Clear();
_heats.Clear();
_ids.Clear();
_resetBolts.Clear();
_rotAngles.Clear();
_mapbacks.Clear();
_mapback_thicks.Clear();
int cnt = 0;
foreach (DataRow dr in dataTable.Rows)
{
AddOneRecord(dr);
cnt++;
}
return cnt;
}
/// <summary>
/// 从dataTable中提取新数据到内部缓冲区
/// </summary>
protected override int FetchNewData()
{
int lastID = _ids.Last();
int cnt = 0;
foreach (DataRow dr in dataTable.Rows)
{
var id = (int)dr.Field<long>("ID");
if (lastID >= id) continue;
AddOneRecord(dr);
cnt++;
}
return cnt;
}
private void AddOneRecord(DataRow dr)
{
string th_dat = dr.Field<string>("Thicks");
string heat_dat = dr.Field<string>("Heats");
var map_dat = dr.Field<string>("Boltmap");
var t1 = Newtonsoft.Json.JsonConvert.DeserializeObject<double[]>(th_dat);
var h1 = Newtonsoft.Json.JsonConvert.DeserializeObject<double[]>(heat_dat);
var map = Newtonsoft.Json.JsonConvert.DeserializeObject<Boltmap[]>(map_dat);
if ((_heats.Count() > 0) && IsDataFilter)
{
if (IsHeatSame(h1, _heats.Last())) DeleteDataLast();
}
_ids.Add((int)dr.Field<long>("ID"));
_dat_times.Add(dr.Field<DateTime>("Time"));
_thicks.Add(t1);
_heats.Add(h1);
var t2 = RemoveNaN(t1);
var r = MathNet.Numerics.Statistics.Statistics.MeanStandardDeviation(t2);
_thick_means.Add(r.Item1);
_thick_2sigmas.Add(r.Item2 * 2);
_resetBolts.Add((int)dr.Field<long>("OrgBoltNo"));
_rotAngles.Add(dr.Field<double>("RAngle"));
if(map != null)
{
Dictionary<int, int> m = new Dictionary<int, int>();
foreach (Boltmap bm in map)
{
m.Add(bm.NewNo - 1, bm.OldNo - 1);
}
_mapbacks.Add(m);
_mapback_thicks.Add(Misc.MyMath.Map(t1, m));
}
else
{
_mapbacks.Add(null);
_mapback_thicks.Add(t1);
}
}
/// <summary>
/// 判断两个加热是否相同
/// </summary>
/// <param name="h1"></param>
/// <param name="h2"></param>
/// <returns></returns>
private bool IsHeatSame(double[] h1, double[] h2)
{
int cnt = h1.Count();
if (h2.Count() != cnt) return false;
for (int i = 0; i < cnt; i++)
{
if (Math.Abs(h1[i] - h2[i]) > 0.1) return false;
}
return true;
}
private void DeleteDataLast()
{
int idx = _ids.Count() - 1;
_ids.RemoveAt(idx);
_dat_times.RemoveAt(idx);
_thicks.RemoveAt(idx);
_thick_2sigmas.RemoveAt(idx);
_thick_means.RemoveAt(idx);
_heats.RemoveAt(idx);
_resetBolts.RemoveAt(idx);
_rotAngles.RemoveAt(idx);
_mapbacks.RemoveAt(idx);
_mapback_thicks.RemoveAt(idx);
}
public override int LoadMetaDataFromDB()
{
mSQLiteHelper.ConnectionString = ConnectionString;
var dtb = mSQLiteHelper.ExecuteReader("SELECT min(\"Time\")as A,MAX(\"Time\") as B from ThickHeat");
if (dtb.Rows[0].IsNull("A")) return -1;
TotalDataFrom = DateTime.Parse(dtb.Rows[0].Field<string>("A"));
TotalDataTo = DateTime.Parse(dtb.Rows[0].Field<string>("B"));
return 0;
}
public override int LoadDataFromDB(int reload)
{
mSQLiteHelper.ConnectionString = ConnectionString;
string cmdstr;
bool clear = false;
if ((reload == 0 && _ids.Count() == 0) || (reload == 1))
{
clear = true;
DateTime to = LoadDataTo + new TimeSpan(0, 0, 1);
if(IsFilterUnstable)
cmdstr = string.Format("select * from [ThickHeat] where IsStable=1 and Time>=\"{0}\" and Time<=\"{1}\"",
LoadDataFrom.ToString("yyyy-MM-dd HH:mm:ss"),
to.ToString("yyyy-MM-dd HH:mm:ss"));
else
cmdstr = string.Format("select * from [ThickHeat] where Time>=\"{0}\" and Time<=\"{1}\"",
LoadDataFrom.ToString("yyyy-MM-dd HH:mm:ss"),
to.ToString("yyyy-MM-dd HH:mm:ss"));
}
else
{
if(IsFilterUnstable)
cmdstr = string.Format("select * from [ThickHeat] where ID>\"{0}\" and IsStable=1", _ids.Last());
else
cmdstr = string.Format("select * from [ThickHeat] where ID>\"{0}\"", _ids.Last());
}
try
{
dataTable = mSQLiteHelper.ExecuteReader(cmdstr);
if (clear)
{
return FetchData();
}
else
{
return FetchNewData();
}
}
catch
{
return -99;
}
}
private DataTable dataTable = new DataTable();
#endregion
}
}