ningshuxia
2022-08-19 153da1eef16b51d75442779a3964a1c5413132e4
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.DataDockingApi
{
    /// <summary>
    /// 
    /// </summary>
    public class HandleHelper : IDataDockingApi.IHandleHelper
    {
        /// <summary>
        /// 
        /// </summary>
        public void HandleData(List<Model.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 数据对接中,缺少对接配置文件");
            }
            string token = null;
            if (appParas.istoken)
            {
                token = TokenHelper.Get();
                if (string.IsNullOrEmpty(token))
                {
                    LogHelper.Error("上海城投制水 Api 数据对接中,获取Token失败");
                    return;
                }
            }
 
            var list_external = mappers.Where(x => !string.IsNullOrEmpty(x.SignId)).Select(x => x.SignId).Distinct().ToList();
 
            var data = JsonHelper.Object2Json(list_external);
            var responseText = HttpRequestHelper.Post(appParas.url.last, token, data);
            var result = JsonHelper.Json2Object<Result<List<Record>>>(responseText);
            if (!result.success)
            {
                LogHelper.Info("上海城投制水 Api 数据对接中,获取实时数据失败(返回false)");
                return;
            }
            var apiModel = result.data;
            if (apiModel == null || apiModel.Count < 1)
            {
                LogHelper.Info("上海城投制水 Api 数据对接中,获取实时数据失败(返回结构为空)");
                return;
            }
 
            var record_receive_list = new List<Model.MonitorDataDockingReceiveRecord>();
            foreach (var mapper in mappers)
            {
                var src = apiModel.Find(x => x.mpId == mapper.SignId);
                if (src != null && src.data != null && src.data.Count > 0)
                {
                    var timeZone = TimeHelper.Convert(long.Parse(src.data[0][0].ToString()));
                    var time = DateTime.SpecifyKind(timeZone, DateTimeKind.Unspecified);
                    var record = new Model.MonitorDataDockingReceiveRecord
                    {
                        SysId = mapper.SysId,
                        RecordType = Model.eMonitorType.General,
                        SrcTime = time,
                        SrcValue = src.data[0][1].ToString()
                    };
                    record_receive_list.Add(record);
                }
            }
            receive(record_receive_list);
 
            if (appParas.debug)
            {
                DebugHelper.Debug(apiModel);
            }
        }
 
 
    }
 
}