GQ_Communicate/GQ_URP/GQ/Assets/script/LocalVideo.cs

263 lines
8.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Unity.VisualScripting;
using UnityEngine;
using static InterfaceManager;
/// <summary>
/// 本地视频
/// </summary>
[AddComponentMenu("LocalVideo/本地视频")]
public class LocalVideo : MonoBehaviour
{
public static LocalVideo Inst;
public WebSocketT socketT = new WebSocketT();
public List<ClickEventLens> clickEventLens;
public List<BaseConf> baseConfs;
/// <summary>
/// 上次点击的本地视频弹窗
/// </summary>
public LocalVideoAlarmUI current_UI = null;
public string webSite { get; set; }
private void Awake()
{
Inst = this;
clickEventLens = GameObject.FindObjectsOfType<ClickEventLens>().ToList();
baseConfs = clickEventLens.Select(x => x.baseConf).ToList();
}
// Start is called before the first frame update
void Start()
{
///获取配置信息
StartCoroutine(GetRequest(Path.Combine(Application.streamingAssetsPath, "ws.txt"), null, null, (_error, _json) =>
{
if (_error != null)
{
Debug.Log("请求websocket【配置信息】错误" + _error);
return;
}
JObject _j = JObject.Parse(_json);
webSite = _j["webSite"].ToString();
socketT.Init(webSite, WebSocketT.WstypesOf.);
}));
}
[ContextMenu("测试时间戳")]
public void ConvertUnixTimestampToDateTime(/*long unixTimestamp*/)
{
//GetTime(1700227312, false);
Debug.Log(GetTime(1698723542));
}
/// <summary>
/// 指定时间戳转为时间。
/// </summary>
/// <param name="timeStamp">需要被反转的时间戳</param>
/// <param name="accurateToMilliseconds">是否精确到毫秒</param>
/// <returns>返回时间戳对应的DateTime</returns>
public static DateTime GetTime(long timeStamp, bool accurateToMilliseconds = false)
{
if (accurateToMilliseconds)
{
return DateTimeOffset.FromUnixTimeMilliseconds(timeStamp).LocalDateTime;
}
else
{
return DateTimeOffset.FromUnixTimeSeconds(timeStamp).LocalDateTime;
}
}
// Update is called once per frame
void Update()
{
}
public void Dispose(string message)
{
// 清除信息
for (int j = 0; j < baseConfs.Count; j++)
{
baseConfs[j].conf1.alarmEntities.Clear();
}
try
{
//Root root = JsonConvert.DeserializeObject<Root>(message);
//for (int i = 0; i < baseConfs.Count; i++)
//{
//if (baseConfs[i].conf1.isPicture && root.sourceName == baseConfs[i].conf1.LensName)
//{
// baseConfs[i].conf1.base64 = root.alarmPicture;
//}
//}
ReadAllocationInformation(message);
AlarmFlashing();
RecoveryUI();
AssignDataToUI();
if (clickEventLens != null)
{
var localVideoAlarm_list = GameObject.FindObjectsOfType<LocalVideoAlarm>(true).ToList();
for (int i = 0; i < localVideoAlarm_list.Count; i++)
{
localVideoAlarm_list[i].EnterTheCameraModule();
}
}
}
catch (Exception e)
{
Debug.Log("解析本地视频错误:" + e.Message + " " + e.StackTrace);
}
}
/// <summary>
/// 读取分配信息
/// </summary>
/// <param name="message"></param>
public void ReadAllocationInformation(string message)
{
List<AlarmEntity> alarmList = JsonConvert.DeserializeObject<List<AlarmEntity>>(message);
for (int i = 0; i < alarmList.Count; i++)
{
string cameraName = string.Empty;
if (alarmList[i].cameraName.Contains("摄像头"))
{
string s1 = alarmList[i].cameraName.Replace("摄像头", "").PadLeft(2, '0');
cameraName = "摄像头" + s1;
}
else
continue;
for (int j = 0; j < baseConfs.Count; j++)
{
if (cameraName.Contains(baseConfs[j].conf1.LensName))
{
baseConfs[j].conf1.alarmEntities.Add(alarmList[i]);
break;
}
}
}
}
/// <summary>
/// 告警闪烁
/// </summary>
public void AlarmFlashing()
{
for (int j = 0; j < baseConfs.Count; j++)
{
Debug.Log($"摄像头{baseConfs[j].conf1.LensName}{baseConfs[j].conf1.alarmEntities.Count}个报警");
if (baseConfs[j].conf1.alarmEntities.Count > 0 && clickEventLens[j].coroutine == null)
{
clickEventLens[j].coroutine = StartCoroutine(clickEventLens[j].AlwaysFlashing(0.25f));
}
else if (clickEventLens[j].coroutine != null && baseConfs[j].conf1.alarmEntities.Count == 0)
{
StopCoroutine(clickEventLens[j].coroutine);
clickEventLens[j].coroutine = null;
}
}
}
/// <summary>
/// 回收
/// </summary>
public void RecoveryUI()
{
for (int i = 0; i < clickEventLens.Count; i++)
{
LocalVideoAlarm localVideoAlarm = clickEventLens[i].GetComponentInChildren<LocalVideoAlarm>(true);
if (!localVideoAlarm)
continue;
LocalVideoAlarmUI localVideoAlarmUI = localVideoAlarm.localVideoAlarmUI;
var localVideoAlarmItem = localVideoAlarmUI.Content.GetComponentsInChildren<LocalVideoAlarmItem>(true).ToList();
for (int j = 0; j < localVideoAlarmItem.Count; j++)
{
AlarmObjectPool.instance.PushItemToPool<LocalVideoAlarmItem>(localVideoAlarmItem[j]);
}
}
}
/// <summary>
/// 赋值数据到UI
/// </summary>
public void AssignDataToUI()
{
for (int i = 0; i < clickEventLens.Count; i++)
{
LocalVideoAlarm localVideoAlarm = clickEventLens[i].GetComponentInChildren<LocalVideoAlarm>(true);
if (!localVideoAlarm)
continue;
LocalVideoAlarmUI localVideoAlarmUI = localVideoAlarm.localVideoAlarmUI;
localVideoAlarmUI.textMeshPro.text = baseConfs[i].conf1.alarmEntities.Count.ToString();
var list_Confs = baseConfs[i].conf1.alarmEntities;
for (int j = 0; j < list_Confs.Count; j++)
{
var _item = AlarmObjectPool.instance.GetItemFromPool<LocalVideoAlarmItem>(localVideoAlarmUI.preform);
_item.gameObject.SetActive(true);
_item.transform.SetParent(localVideoAlarmUI.Content.transform);
_item.textMeshPro_object.text = list_Confs[j].sourceName + "监测异常";
_item.textMeshPro_time.text = GetTime(long.Parse(list_Confs[j].timeStamp)).ToString();
var t = CombineJSON.Base64StringToTexture(list_Confs[j].alarmPicture);
var u = Sprite.Create(t, new Rect(0, 0, t.width, t.height), Vector2.zero);
_item.sprites.Add(u);
}
}
}
[Serializable]
public class AlarmEntity
{
public int id;
/// <summary>
/// //1-安全帽2-安全服3-抽烟
/// </summary>
public string type;
public string reference;
public string alarmType;
public object deviceId;
/// <summary>
/// 时间戳
/// </summary>
public string timeStamp;
public object sourceId;
public string seq;
public object relatedId;
/// <summary>
/// //告警源名称
/// </summary>
public string sourceName;
/// <summary>
/// 告警图片
/// </summary>
public string alarmPicture;
public string alarmPictureType;
/// <summary>
/// 摄像头名字
/// </summary>
public string cameraName;
}
[Serializable]
public class Root
{
public List<AlarmEntity> alarms;
}
}