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();
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|