cloudflight
2024-06-10 79fabf74a3fe97f1f2b8ae23e37cc33ba0d53ae2
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
using Dapper;
using Hydro.CommonBase;
using MetroFramework;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
 
namespace Hydro.ConfigModel
{
    public class LogicPoint
    {
        public dict<string, dynamic> History = null;
        public static dict<string, dynamic> Compare(LogicPoint host1, LogicPoint host2,out bool Sameflag,out bool Sameflag_Calc)
        {
            dict<string,dynamic> di=new dict<string, dynamic>();
            Sameflag = true;
            Sameflag_Calc = true;
            //比较host1和host2的所有属性,并记录不同的属性,按照以下格式记录:"属性名(old)":host1的属性值(old) "属性名(new)":host2的属性值(new),如果属性值相同,则不记录,例如:di.Add("Name (old)", host1.Name.ToString()); di.Add("Name (new)", host2.Name.ToString());
            
 
 
            if (host1.Name != host2.Name) { Sameflag_Calc=Sameflag = false; di.Add("Name (old)", host1.Name); di.Add("Name (new)", host2.Name); }
            if (host1.LogicType != host2.LogicType) { Sameflag_Calc = Sameflag = false; di.Add("LogicType (old)", host1.LogicType); di.Add("LogicType (new)", host1.LogicType); }
            if (host1.ExpressionType != host2.ExpressionType) { Sameflag_Calc = Sameflag = false; di.Add("ExpressionType (old)", host1.ExpressionType); di.Add("ExpressionType (new)", host2.ExpressionType); }
            if (host1.IndicatorType != host2.IndicatorType) { Sameflag_Calc = Sameflag = false; di.Add("IndicatorType (old)", host1.IndicatorType); di.Add("IndicatorType (new)", host2.IndicatorType); }
            if (host1.ModelObjectID != host2.ModelObjectID) { Sameflag_Calc = Sameflag = false; di.Add("ModelObjectID (old)", host1.ModelObjectID); di.Add("ModelObjectID (new)", host2.ModelObjectID); }
            //if (host1.Sort != host2.Sort) { flag = false; di.Add("Sort", host1.Sort); }
            if (host1.ParentId != host2.ParentId) { Sameflag_Calc = Sameflag = false; di.Add("ParentId (old)", host1.ParentId); di.Add("ParentId (new)", host2.ParentId); }
            if (host1.Expression != host2.Expression) { Sameflag_Calc = Sameflag = false; di.Add("Expression (old)", host1.Expression); di.Add("Expression (new)", host2.Expression); }
 
 
            //if (!isShowVisual)
            //{
                
            //}
            //else
            //{
            //    host1.ExtJson=JsonConvert.SerializeObject(host1.Ext);
            //    host2.ExtJson=JsonConvert.SerializeObject(host2.Ext);
            //    if (host1.ExtJson != host2.ExtJson) { Sameflag = false; di.Add("ExtJson (old)", host1.ExtJson); di.Add("ExtJson (new)", host2.ExtJson); }
            //}
            if (host1.ObjectType != host2.ObjectType) { Sameflag_Calc = Sameflag = false; di.Add("ObjectType (old)", host1.ObjectType); di.Add("ObjectType (new)", host2.ObjectType); }
            if (host1.accurary != host2.accurary) { Sameflag_Calc = Sameflag = false; di.Add("accurary (old)", host1.accurary); di.Add("accurary (new)", host2.accurary); }
            if (host1.childSolution != host2.childSolution) { Sameflag_Calc = Sameflag = false; di.Add("childSolution (old)", host1.childSolution); di.Add("childSolution (new)", host2.childSolution); }
            if (host1.childTag != host2.childTag) { Sameflag_Calc = Sameflag = false; di.Add("childTag (old)", host1.childTag); di.Add("childTag (new)", host2.childTag); }
            if (host1.childType != host2.childType) { Sameflag_Calc = Sameflag = false; di.Add("childType (old)", host1.childType); di.Add("childType (new)", host2.childType); }
            if (host1.SaveAcc != host2.SaveAcc) { Sameflag_Calc = Sameflag = false; di.Add("SaveAcc (old)", host1.SaveAcc); di.Add("SaveAcc (new)", host2.SaveAcc); }
            if (host1.isSave != host2.isSave) { Sameflag_Calc = Sameflag = false; di.Add("isSave (old)", host1.isSave); di.Add("isSave (new)", host2.isSave); }
            if (host1.SaveRange != host2.SaveRange) { Sameflag_Calc = Sameflag = false; di.Add("SaveRange (old)", host1.SaveRange); di.Add("SaveRange (new)", host2.SaveRange); }
            if (host1.isMemSave != host2.isMemSave) { Sameflag_Calc = Sameflag = false; di.Add("isMemSave (old)", host1.isMemSave); di.Add("isMemSave (new)", host2.isMemSave); }
            if (host1.MemSaveRange != host2.MemSaveRange) { Sameflag_Calc = Sameflag = false; di.Add("MemSaveRange (old)", host1.MemSaveRange); di.Add("MemSaveRange (new)", host2.MemSaveRange); }
            if (host1.MemSaveAcc != host2.MemSaveAcc) { Sameflag_Calc = Sameflag = false; di.Add("MemSaveAcc (old)", host1.MemSaveAcc); di.Add("MemSaveAcc (new)", host2.MemSaveAcc); }
            if (host1.MemSaveIndex != host2.MemSaveIndex) { Sameflag_Calc = Sameflag = false; di.Add("MemSaveIndex (old)", host1.MemSaveIndex); di.Add("MemSaveIndex (new)", host2.MemSaveIndex); }
            if (host1.Enable != host2.Enable) { Sameflag_Calc = Sameflag = false; di.Add("Enable (old)", host1.Enable); di.Add("Enable (new)", host2.Enable); }
            if (host1.Tag != host2.Tag) { Sameflag_Calc = Sameflag = false; di.Add("Tag (old)", host1.Tag); di.Add("Tag (new)", host2.Tag); }
 
 
            if (host1.Prefer != host2.Prefer) { Sameflag = false; di.Add("Prefer (old)", host1.Prefer); di.Add("Prefer (new)", host2.Prefer); }
            if (host1.Visible != host2.Visible) { Sameflag = false; di.Add("Visible (old)", host1.Visible); di.Add("Visible (new)", host2.Visible); }
            if (host1.isUseCustomColor != host2.isUseCustomColor) { Sameflag = false; di.Add("isUseCustomColor (old)", host1.isUseCustomColor); di.Add("isUseCustomColor (new)", host2.isUseCustomColor); }
            if (host1.ContentColor != host2.ContentColor) { Sameflag = false; di.Add("ContentColor (old)", host1.ContentColor); di.Add("ContentColor (new)", host2.ContentColor); }
 
 
 
            return di;
 
        }
        public static string selectColumn = "Id, Name, LogicType, ExpressionType, IndicatorType, ModelObjectID, Sort, ParentId, Expression, ExtJson";
        public void updateByhost(LogicPoint host,bool OnlyUpdateShowProperty)
        {
            
            if (!OnlyUpdateShowProperty)
            {
                //更新host的所有属性到当前对象
                this.Name = host.Name;
                this.LogicType = host.LogicType;
                this.ExpressionType = host.ExpressionType;
                this.IndicatorType = host.IndicatorType;
                this.ModelObjectID = host.ModelObjectID;
                this.Sort = host.Sort;
                this.ParentId = host.ParentId;
                this.Expression = host.Expression;
 
 
                //host.ExtJson = JsonConvert.SerializeObject(host.Ext);
                //this.ExtJson = host.ExtJson;
                //this._ext = null;
 
                this.accurary = host.accurary;
                this.childSolution = host.childSolution;
                this.childTag = host.childTag;
                this.childType = host.childType;
                this.SaveAcc = host.SaveAcc;
                this.isSave = host.isSave;
                this.SaveRange = host.SaveRange;
                this.isMemSave = host.isMemSave;
                this.MemSaveRange = host.MemSaveRange;
                this.MemSaveAcc = host.MemSaveAcc;
                this.MemSaveIndex = host.MemSaveIndex;
                this.Enable = host.Enable;
                this.Tag = host.Tag;
                this.ObjectType = host.ObjectType;
                this.VPN = host.VPN;
            }
            
 
 
 
            this.Visible = host.Visible;
            this.Prefer = host.Prefer;
            this.isUseCustomColor = host.isUseCustomColor;
            this.ContentColor = host.ContentColor;
            
            this.ExtJson = JsonConvert.SerializeObject(this.Ext);
            //this.ExtJson = host.ExtJson;
 
 
        }
 
        public int saveToDb(Db Db, bool delete = false, bool isSetID = true, bool isEdit = true, bool onlyUpdateShowProperty = false)
        {
            var host = this;
            host.ExtJson = JsonConvert.SerializeObject(host.Ext);
            int Exec_Num = 0;
            if (!delete)
            {
                if (isEdit)
                {
                    if (onlyUpdateShowProperty)
                    {
                        //只更新显示属性,将原LogicPoint查询出来,然后更新显示属性
                        var host0 = ((List<LogicPoint>)Db.Connection.Query<LogicPoint>(
                            $"SELECT {LogicPoint.selectColumn} FROM RemoteHost WHERE Id={host.Id}"))[0];
                        host0.updateByhost(host, true);
                        host = host0;
                    }
 
                    Exec_Num = Db.Connection.Execute(
                    @"UPDATE RemoteHost SET Name=@Name,LogicType=@LogicType,ExpressionType=@ExpressionType,IndicatorType=@IndicatorType,ModelObjectID=@ModelObjectID,Sort=@Sort,ParentId=@ParentId,
                            Expression=@Expression,ExtJson=@ExtJson,VPN=@VPN WHERE Id=" + host.Id, host);
 
                }
 
                if (Exec_Num == 0)//没有成功更新,则插入
                {
                    if (isSetID)
                    {
                        Exec_Num = Db.Connection.Execute(
                    @"INSERT INTO RemoteHost(Id,Name,LogicType,ExpressionType,IndicatorType,ModelObjectID,Sort,ParentId,Expression,ExtJson,VPN) 
                        VALUES(@Id,@Name,@LogicType,@ExpressionType,@IndicatorType,@ModelObjectID,@Sort,@ParentId,@Expression,@ExtJson,@VPN)",
                                                                              host);
                        return host.Id;
                    }
                    else
                    {
                        Exec_Num = Db.Connection.Execute(
                    @"INSERT INTO RemoteHost(Name,LogicType,ExpressionType,IndicatorType,ModelObjectID,Sort,ParentId,Expression,ExtJson,VPN) 
                        VALUES(@Name,@LogicType,@ExpressionType,@IndicatorType,@ModelObjectID,@Sort,@ParentId,@Expression,@ExtJson,@VPN)",
                                                                              host);
                        var ids = (List<int>)Db.Connection.Query<int>(
                $"SELECT Max(Id) FROM RemoteHost");
                        host.Id = ids[0];
                        return host.Id;
                    }
 
                }
                return host.Id;
            }
            else
            {
                Db.Connection.Execute("DELETE FROM RemoteHost WHERE Id=" + host.Id, " VACUUM");
                return 0;
            }
            return host.Id;
 
 
        }
        public void UpdateByOldVersion()
        {
            var host = this;
            if (host.LogicType == "集合")
            {
                List<string> list = new List<string>() { "最大值", "最小值", "平均值", "累计值", "计数" };
                if (list.IndexOf(host.ExpressionType) >= 0)
                {
                    host.accurary = host.ExpressionType;
                    host.ExpressionType = "集合计算";
                }
            }
        }
        public static List<string> Columns
        {
            get
            {
                return selectColumn.Split(',').Select(item => item.Trim()).ToList();
            }
            set {
                selectColumn = string.Concat(value, ',');
            }
        }
        [DisplayName("编号")]
        [Category("1、基本信息")]
        public int Id { get; set; }
        [DisplayName("名称")]
        [Category("1、基本信息")]
        public string Name { get; set; }
        /// <summary>
        /// 逻辑类型
        /// </summary>
        [DisplayName("类型")]
        [Category("1、基本信息")]
        public string LogicType { get; set; }
        /// <summary>
        /// 数据来源类型(接口值、基准值、计算值、变化值、初始计算值、最小、最大、∈、∉)
        /// </summary>
        [DisplayName("数据来源")]
        [Category("2、计算参数")]
        public string ExpressionType { get; set; }
        /// <summary>
        /// 数值表达式(接口值、基准值)
        /// </summary>
        [DisplayName("值")]
        [Category("2、计算参数")]
        public string Expression { get; set; }
 
        private string _fullAddress;
        [Browsable(false)]
        public string FullAddress => _fullAddress ?? (_fullAddress = ExpressionType == "3389" ? LogicType : $"{LogicType}:{ExpressionType}");
        [Browsable(false)]
        public string ExpressionToShow
        {
            get
            {
                if (ExpressionType == null) return null;
                if (ExpressionType=="集合计算")
                {
                    return ExpressionType + ":" + accurary;
                }
                else
                if (ExpressionType.IndexOf("接口") >= 0 )
                {
                    return ExpressionType + " " + Expression;
                }
                else if (ExpressionType.IndexOf("基准值") >= 0 || ExpressionType.IndexOf("计算值") >= 0 || ExpressionType.IndexOf("变化值") >= 0 || ExpressionType.IndexOf("初始计算值") >= 0)
                {
                    return ExpressionType + " " + Expression;
                }
                else if (ExpressionType.IndexOf("值") >= 0 || ExpressionType.IndexOf("系数") >= 0)
                {
                    return ExpressionType;
                }
                else
                {
                    return $"{ExpressionType} " + (ExpressionType == "∈" ? $"[{Expression}] ▲{accurary}" : (ExpressionType == "最小" || ExpressionType == "最大" ? $"| 容许范围[{Expression}]" : Expression)); 
                }               
            }
        }
        [DisplayName("监测类型")]
        [Category("1、基本信息")]
        public string IndicatorType { get; set; }
        [DisplayName("模型编号或内容")]
        [Category("2、计算参数")]
        public string ModelObjectID { get; set; }
        [DisplayName("排序")]
        [Category("4、其他")]
        public int Sort { get; set; }
        [DisplayName("父节点编号")]
        [Category("4、其他")]
        public int ParentId { get; set; }
 
        [Browsable(false)]
        public string ExtJson { get; set; } = "";
 
        private HostExt _ext;
        [Browsable(false)]
        public HostExt Ext
        {
            get
            {
                if (_ext == null)
                {
                    try
                    {
                        _ext = JsonConvert.DeserializeObject<HostExt>(ExtJson);
                        if (_ext == null) _ext = new HostExt();
                        return _ext;
                    }
                    catch
                    {
                        return (_ext=new HostExt());
                    }
                    
                }
                else
                    return _ext;// ?? 
            }
            
        }
        
        [Browsable(false)]
        public byte[] VPN { get; set; } = null;
        [DisplayName("结果")]
        [Category("2、计算参数")]
        public string Result 
        { 
            get
            {
                string result = null;
                if (Ext==null || Ext.Result_Arr == null)
                {
                    return "-";
                }
                int 小数位数 = Ext.小数位数;
                if (Ext.ResultShowType==0)
                {
                   
                    
                   
                    if (LogicType=="试算参数" )
                    {
                        try
                        {
                            var arr = Expression.Split(',').Select(v => Convert.ToDouble(v)).ToArray();
                            var range = new DRange(arr[0], arr[1]);
                            var accuracy = double.Parse(accurary);
                            result = getFormat(getRoundByAccuracy(Ext.Result_Arr[Ext.Period], range,accuracy));
                        }
                        catch 
                        {
                            result = "-";
                        }
                        
                    }
                    else
                    if ( Ext.Result_Arr.Length> Ext.Period && !double.IsNaN( Ext.Result_Arr[Ext.Period]))
                    {
                        result= getFormat(Math.Round(Ext.Result_Arr[Ext.Period],小数位数));
                    }
                    else
                    {
                        result= "-";
                    }
                }
                else if (Ext.ResultShowType == 1)
                {
                    int LengthLimit = 16; //GlobalConfig.LengthLimit??16;
                      
                    string txt = Ext.Result_Arr.ToList().Select(v0 => getFormat(Math.Round( v0, 小数位数))).Aggregate((a, b) => a + "," + b);
                    //最长保留16个字符
                    if (txt.Length > LengthLimit) txt = txt.Substring(0, LengthLimit) + "...";
                    result= txt;
                }
 
                if (Ext.bestObj!=null)
                {
                    result += Ext.bestObj;
                }
                return result;
                
            }
        }
        private double getRoundByAccuracy(double v, DRange range, double accuracy)
        {
            //精度为accuracy的倍数,且在range范围内,最小值为range.Min,取最接近v的值
            if (accuracy == 0) return v;
            if (v < range.Min) return range.Min;
            if (v > range.Max) v=range.Max;
            
            var delta = v - range.Min;
            //比较v - delta % accuracy和v+accuracy-delta % accuracy哪个接近v,且在range范围内
            double value = 0;
            if (delta % accuracy>accuracy/2)
            {
                value = v + accuracy - delta % accuracy;
            }
            else
            {
                value = v - delta % accuracy;
            }
            return Math.Round(value,5);
            //return v - delta % accuracy;
        }
        private string getFormat(double value)
        {
            if (Ext.ResultFormat == null) return value.ToString();
            if (Ext.ResultFormat.ContainsKey(value))
            {
                return Ext.ResultFormat[value];
            }
            else
            {
                return value.ToString();
            }
        }
 
        public string objListString = null;
 
        public string Text_change = null;
 
        [DisplayName("对象名称")]
        [Category("1、基本信息")]
        public string ObjectType
        {
            get { return this.Ext.ObjectType; }
            set { this.Ext.ObjectType = value; }
        }
        [DisplayName("精度")]
        [Category("2、计算参数")]
        public string accurary
        {
            get { return this.Ext.accurary; }
            set { this.Ext.accurary = value; }
        }
        [DisplayName("子方案名称")]
        [Category("3、子方案")]
        public string childSolution
        {
            get { return this.Ext.childSolution; }
            set { this.Ext.childSolution = value; }
        }
        [DisplayName("子方案键")]
        [Category("3、子方案")]
        public string childTag
        {
            get { return this.Ext.childTag; }
            set { this.Ext.childTag = value; }
        }
        [DisplayName("子方案传输")]
        [Category("3、子方案")]
        public string childType
        {
            get { return this.Ext.childType; }
            set { this.Ext.childType = value; }
        }
        [DisplayName("子搜索精度")]
        [Category("3、子方案")]
        public string SaveAcc
        {
            get { return this.Ext.SaveAcc; }
            set { this.Ext.SaveAcc = value; }
        }
        [DisplayName("是否存储")]
        [Category("3、子方案")]
        public string isSave
        {
            get { return this.Ext.isSave; }
            set { this.Ext.isSave = value; }
        }
        [DisplayName("存储键值")]
        [Category("3、子方案")]
        public string SaveRange
        {
            get { return this.Ext.SaveRange; }
            set { this.Ext.SaveRange = value; }
        }
 
        [DisplayName("是否内存存储")]
        [Category("3、子方案")]
        public string isMemSave
        {
            get { return this.Ext.isMemSave; }
            set { this.Ext.isMemSave = value; }
        }
        [DisplayName("内存存储范围")]
        [Category("3、子方案")]
        public string MemSaveRange
        {
            get { return this.Ext.MemSaveRange; }
            set { this.Ext.MemSaveRange = value; }
        }
        [DisplayName("内存存储精度")]
        [Category("3、子方案")]
        public string MemSaveAcc
        {
            get { return this.Ext.MemSaveAcc; }
            set { this.Ext.MemSaveAcc = value; }
        }
        [DisplayName("内存存储次序")]
        [Category("3、子方案")]
        public string MemSaveIndex
        {
            get { return this.Ext.MemSaveIndex; }
            set { this.Ext.MemSaveIndex = value; }
        }
        [DisplayName("启用")]
        [Category("1、基本信息")]
        public bool Enable
        {
            get { return this.Ext.Enable; }
            set { this.Ext.Enable = value; }
        }
 
        [DisplayName("可见性")]
        [Category("1、基本信息")]
        public bool Visible
        {
            get { return this.Ext.Visible; }
            set { this.Ext.Visible = value; }
        }
        [DisplayName("标签")]
        [Category("1、基本信息")]
        public string Tag 
        {
            get { return this.Ext.Tag; }
            set { this.Ext.Tag = value; }
        }
 
        [DisplayName("收藏")]
        [Category("1、基本信息")]
        public bool Prefer
        {
            get { return this.Ext.Prefer; }
            set { this.Ext.Prefer = value; }
        }
        [DisplayName("使用自定义色")]
        [Category("4、自定义")]
        public bool isUseCustomColor
        {
            get { return this.Ext.isUseCustomColor; }
            set { this.Ext.isUseCustomColor = value; }
        }
 
        [DisplayName("内容色")]
        [Category("4、自定义")]
        public MetroColorStyle_Inner ContentColor
        {
            get { return this.Ext.ContentColor; }
            set { this.Ext.ContentColor = value; }
        }
      
 
    }
}