using EasyInject.Attributes;
using BestHTTP.WebSocket;
using UnityEngine;
using System.Collections.Generic;
using static IWebSocketChannle;
using Newtonsoft.Json;
using System;
///
/// 与后台的WCS数据通信
///
[Component("WcsWebSocket")]
public class WcsWebSocket : AbstractWebSocketChannle
{
public override string _url => string.Format("ws://{0}:{1}/{2}",GlobalConfig.serverIp,GlobalConfig.serverPort,GlobalConfig.wsDeviceMonitorAddress);
public override string Description => "与后台 WCS 连接";
public override object MesageFilter(string message)
{
List devices = JsonConvert.DeserializeObject>(message);
return devices;
}
public override void Message(WebSocket webSocket, string message)
{
//序列化设备数据
List devices = (List)this.MesageFilter(message);
if (devices == null && devices.Count < 1)
{
return;
}
//有设备监听数据变化,则触发监听事件
foreach (DeviceData deviceData in devices)
{
//存在key(设备代码+设备类型)
WebSocketChannleKey key = new WebSocketChannleKey(deviceData.deviceCode, deviceData.deviceType);
if (messageMap.TryGetValue(key,out OnMessageEvent messageEvent))
{
try
{
//触发事件
messageEvent?.Invoke(deviceData.extra,deviceData);
}catch (Exception ex)
{
Debug.LogError(ex);
}
}
}
}
}