lixiaojun
2023-04-12 23f6326b8df32975df95099c9b3d9c958c8c2459
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
using IStation.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WebSocket4Net;
 
namespace IStation.DataDockingApi
{
    /// <summary>
    /// 
    /// </summary>
    public class HandleHelper : IDataDockingApi.IHandleHelper 
    {
        /// <summary>
        /// 接收
        /// </summary>
        public void HandleData(List<DataDockingConfigure.Mapper> mappers, Action<List<Model.MonitorDataDockingReceiveRecord>> receive)
        {
            if (mappers == null || mappers.Count < 1)
                return;
            var appParas = AppParasHelper.Get();
            if (appParas == null)
            {
                LogHelper.Error("上海城投原水 Api 数据对接中,缺少对接配置文件");
                return;
            }
            var url = appParas.url + Guid.NewGuid().ToString("N");
            {
                LogHelper.Info($"上海城投原水 Api 数据对接中,WebSocket访问url:{url}");
                LogHelper.Info($"上海城投原水 Api 数据对接中,WebSocket指令:{appParas.key}");
            }
            var webSocket4Net = new WebSocket(url);
            webSocket4Net.Opened += (sender, e) =>
            {
                webSocket4Net.Send(appParas.key);
                LogHelper.Info($"上海城投原水 Api 数据对接中,已发送指令:{appParas.key}");
            };
            webSocket4Net.MessageReceived += (sender, e) =>
            {
                LogHelper.Info($"上海城投原水 Api 数据对接中,接收消息:{e.Message}");
                if (e.Message == "连接成功")
                {
                    LogHelper.Info("上海城投原水 Api 数据对接中,连接成功 ");
                }
                else if (e.Message.Contains("库中无此表"))
                {
                    webSocket4Net.Close();
                    LogHelper.Info($"上海城投原水 Api 数据对接中,提示:{e.Message},连接关闭");
                }
                else
                {
                    var dict = JsonHelper.Json2Object<Dictionary<string, Record>>(e.Message);
                    if (dict == null || dict.Count < 1)
                    {
                        LogHelper.Info("上海城投原水 Api 数据对接中,接收到的信息反序列化失败");
                    }
                    else
                    {
                        var recordList = dict.Select(x => x.Value).ToList();
                        var receiveList = new List<Model.MonitorDataDockingReceiveRecord>();
                        foreach (var mapper in mappers)
                        {
                            var src = recordList.Find(x=>x.key==mapper.SignId);
                            if (src != null)
                            {
                                var recordContent = JsonHelper.Json2Object<RecordContent>(src.vals);
                                if (recordContent != null)
                                {
                                    if (DateTime.TryParse(recordContent.time, out DateTime time_result))
                                    {
                                        var receiveRecord = new Model.MonitorDataDockingReceiveRecord()
                                        {
                                            SysId = mapper.SysId,
                                            RecordType = Model.eMonitorType.General,
                                            SrcTime = time_result,
                                            SrcValue = recordContent.value
                                        };
                                        receiveList.Add(receiveRecord);
                                    }
                                }
                            }
                        }
                        receive(receiveList);
 
                        #region 生成调试记录
 
                        if (appParas.debug)
                        {
                            DebugHelper.Debug(recordList);
                        }
 
                        #endregion
                    }
                    webSocket4Net.Close();
                }
            };
            webSocket4Net.Open();
        }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
    }
 
}