From c82e97ee9a427fbe9de0f3ff906471323d161002 Mon Sep 17 00:00:00 2001 From: fajiao <1519100073@qq.com> Date: Thu, 11 May 2023 10:36:22 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E6=9B=B4=E6=96=B0=E9=9C=87=E5=8A=A8?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EC.Util/CameraSDK/HiK/HiKSDK.cs | 6 +- EC.Util/CameraSDK/YuShi/YuShiSDK.cs | 21 ++-- EC.Util/Common/LogUnit.cs | 3 +- .../FenceAlarm/Entity/UdpAlarmHost.cs | 10 ++ .../FenceAlarm/Service/UdpManager.cs | 28 +++-- JiLinApp.Docking/Military/Config.cs | 2 + JiLinApp.Docking/Military/MilitaryService.cs | 13 +- .../VibrateAlarm/Entity/ClientMessage.cs | 8 ++ .../VibrateAlarm/Service/TcpManager.cs | 117 ++++++++++++------ JiLinApp/Components/CameraRealPlay.xaml | 6 +- JiLinApp/Pages/PtzManage/Main.xaml.cs | 42 +++++-- .../Pages/VibrateServerManage/Main.xaml.cs | 18 --- 12 files changed, 177 insertions(+), 97 deletions(-) diff --git a/EC.Util/CameraSDK/HiK/HiKSDK.cs b/EC.Util/CameraSDK/HiK/HiKSDK.cs index 84629d6..eec10ad 100644 --- a/EC.Util/CameraSDK/HiK/HiKSDK.cs +++ b/EC.Util/CameraSDK/HiK/HiKSDK.cs @@ -126,7 +126,7 @@ public class HiKSDK : ICameraSDK if (!ConnectSuccess()) return false; int lChannel = 1; bool flag = HiKOriSDK.NET_DVR_PTZControlWithSpeed_Other(LoginId, lChannel, (uint)cmd, (uint)stop, (uint)speed); - //if (flag) BuildException(); + if (flag) BuildException(); return flag; } @@ -134,7 +134,9 @@ public class HiKSDK : ICameraSDK { if (!ConnectSuccess()) return false; int lChannel = 1; - return HiKOriSDK.NET_DVR_PTZPreset_Other(LoginId, lChannel, (uint)cmd, (uint)presetId); + bool flag = HiKOriSDK.NET_DVR_PTZPreset_Other(LoginId, lChannel, (uint)cmd, (uint)presetId); + if (flag) BuildException(); + return flag; } #endregion Ptz Method diff --git a/EC.Util/CameraSDK/YuShi/YuShiSDK.cs b/EC.Util/CameraSDK/YuShi/YuShiSDK.cs index dec8f68..4b99a78 100644 --- a/EC.Util/CameraSDK/YuShi/YuShiSDK.cs +++ b/EC.Util/CameraSDK/YuShi/YuShiSDK.cs @@ -64,11 +64,9 @@ public class YuShiSDK : ICameraSDK { bool ret = ConnectSuccess(); if (!ret) return PtzInfo.Default; - YuShiOriSDK.NETDEV_PTZ_STATUS_S entity = new(); ret = YuShiOriSDK.NETDEV_PTZGetStatus(LoginId, 1, ref entity); if (!ret) { BuildException(); return PtzInfo.Default; } - return PtzInfo.New(entity.fPanTiltX, entity.fPanTiltY, entity.fZoomX); } @@ -76,11 +74,9 @@ public class YuShiSDK : ICameraSDK { bool ret = ConnectSuccess(); if (!ret) { ptzInfo = PtzInfo.Default; return false; } - YuShiOriSDK.NETDEV_PTZ_STATUS_S entity = new(); ret = YuShiOriSDK.NETDEV_PTZGetStatus(LoginId, 1, ref entity); if (!ret) { BuildException(); ptzInfo = PtzInfo.Default; return false; } - ptzInfo = PtzInfo.New(entity.fPanTiltX, entity.fPanTiltY, entity.fZoomX); return true; } @@ -89,18 +85,21 @@ public class YuShiSDK : ICameraSDK { if (!ConnectSuccess()) return false; if (stop == 1) cmd = YuShiOriSDK.ALLSTOP; - int channel = 1; - int flag = YuShiOriSDK.NETDEV_PTZControl_Other(LoginId, channel, cmd, speed); - //if (flag) BuildException(); - return flag == YuShiOriSDK.TRUE; + int channel = 0; + int ret = YuShiOriSDK.NETDEV_PTZControl_Other(LoginId, channel, cmd, speed); + bool flag = ret == YuShiOriSDK.TRUE; + if (!flag) BuildException(); + return flag; } public override bool PtzPreset(int cmd, int presetId) { if (!ConnectSuccess()) return false; - int channel = 1; - int flag = YuShiOriSDK.NETDEV_PTZPreset_Other(LoginId, channel, cmd, Array.Empty(), presetId); - return flag == YuShiOriSDK.TRUE; + int channel = 0; + int ret = YuShiOriSDK.NETDEV_PTZPreset_Other(LoginId, channel, cmd, Array.Empty(), presetId); + bool flag = ret == YuShiOriSDK.TRUE; + if (!flag) BuildException(); + return flag; } #endregion Ptz Method diff --git a/EC.Util/Common/LogUnit.cs b/EC.Util/Common/LogUnit.cs index fd16cd0..c77064c 100644 --- a/EC.Util/Common/LogUnit.cs +++ b/EC.Util/Common/LogUnit.cs @@ -9,7 +9,8 @@ public static class LogUnit static LogUnit() { - string fileName = Path.Combine("config", "log4net.Config"); + string fileName = Path.Combine("config", "log4net.config"); + if (!File.Exists(fileName)) throw new FileNotFoundException(fileName); XmlConfigurator.Configure(new FileInfo(fileName)); logger = LogManager.GetLogger(typeof(LogUnit)); } diff --git a/JiLinApp.Docking/FenceAlarm/Entity/UdpAlarmHost.cs b/JiLinApp.Docking/FenceAlarm/Entity/UdpAlarmHost.cs index dff4702..55f1924 100644 --- a/JiLinApp.Docking/FenceAlarm/Entity/UdpAlarmHost.cs +++ b/JiLinApp.Docking/FenceAlarm/Entity/UdpAlarmHost.cs @@ -112,6 +112,16 @@ public class UdpAlarmHost public ConcurrentDictionary SectorDict { get; } + public bool SectorsEmpty + { + get + { + return SectorDict == null || SectorDict.IsEmpty; + } + } + + public ReaderWriterLockSlim SectorsLock { get; } = new(); + public int OnlineState { get; set; }//设备在线状态 public int KeepLive { get; set; }//设备在线状态 diff --git a/JiLinApp.Docking/FenceAlarm/Service/UdpManager.cs b/JiLinApp.Docking/FenceAlarm/Service/UdpManager.cs index 508f7fd..6267006 100644 --- a/JiLinApp.Docking/FenceAlarm/Service/UdpManager.cs +++ b/JiLinApp.Docking/FenceAlarm/Service/UdpManager.cs @@ -103,8 +103,8 @@ public class UdpManager //解码 byte[] msg = AlarmEncode.DecodeMessage(e.Datagram); bool vaild = msg.Length >= 8 && msg[2] == AlarmEncode.Head[0] && msg[3] == AlarmEncode.Head[1]; + Console.WriteLine("Receive from {0}:{1} => {2}, {3}", ipep.Address.ToString(), ipep.Port, DataMessage.ToHexString(msg), vaild); if (!vaild) return; - Console.WriteLine("Receive from {0}:{1} => {2}", ipep.Address.ToString(), ipep.Port, DataMessage.ToHexString(msg)); //解析 DeviceCmd deviceCmd = (DeviceCmd)msg[4]; switch (deviceCmd) @@ -158,7 +158,7 @@ public class UdpManager OnlineState = 1, KeepLive = Config.DeviceHeartKeep }; - AddDevice(deviceId, device); + AddDevice(deviceId, ref device); } byte defenceState = msg[17]; ProcessDeviceStateEvent(ref device, 1, defenceState); @@ -205,7 +205,7 @@ public class UdpManager OnlineState = 1, KeepLive = Config.DeviceHeartKeep }; - AddDevice(deviceId, device); + AddDevice(deviceId, ref device); } string alarmTime = $"20{GetBCD(msg[17])}-{GetBCD(msg[18])}-{GetBCD(msg[19])} " + $"{GetBCD(msg[20])}:{GetBCD(msg[21])}:{GetBCD(msg[22])}"; @@ -244,7 +244,7 @@ public class UdpManager OnlineState = 1, KeepLive = Config.DeviceHeartKeep }; - AddDevice(deviceId, device); + AddDevice(deviceId, ref device); } device.SectorTotal = msg[17] * 256 + msg[18]; int sectorNum = msg[19], startIndex = msg[20]; @@ -283,7 +283,7 @@ public class UdpManager OnlineState = 1, KeepLive = Config.DeviceHeartKeep }; - AddDevice(deviceId, device); + AddDevice(deviceId, ref device); } } @@ -303,7 +303,7 @@ public class UdpManager OnlineState = 1, KeepLive = Config.DeviceHeartKeep }; - AddDevice(deviceId, device); + AddDevice(deviceId, ref device); } device.SectorTotal = msg[17] * 256 + msg[18]; } @@ -616,10 +616,24 @@ public class UdpManager return DeviceDict.TryGetValue(deviceId, out device); } - private bool AddDevice(int deviceId, UdpAlarmHost device) + private bool AddDevice(int deviceId, ref UdpAlarmHost device) { if (ContainsDevice(deviceId)) return false; DeviceDict[deviceId] = device; + UdpAlarmHost innerDevice = DeviceDict[deviceId]; + if (innerDevice.SectorsEmpty && innerDevice.SectorsLock.TryEnterWriteLock(1000)) + { + Task.Run(() => + { + innerDevice = DeviceDict[deviceId]; + while (innerDevice.SectorsEmpty) + { + SearchAllSectorState(innerDevice.DeviceId); + Thread.Sleep(1000); + } + innerDevice.SectorsLock.ExitWriteLock(); + }); + } return true; } diff --git a/JiLinApp.Docking/Military/Config.cs b/JiLinApp.Docking/Military/Config.cs index 79c9ce6..388452e 100644 --- a/JiLinApp.Docking/Military/Config.cs +++ b/JiLinApp.Docking/Military/Config.cs @@ -2,6 +2,8 @@ public class MilitaryConfig { + //public bool Enable { get; set; } + public string Url { get; set; } public string UserName { get; set; } diff --git a/JiLinApp.Docking/Military/MilitaryService.cs b/JiLinApp.Docking/Military/MilitaryService.cs index 1b91066..e00f656 100644 --- a/JiLinApp.Docking/Military/MilitaryService.cs +++ b/JiLinApp.Docking/Military/MilitaryService.cs @@ -28,6 +28,11 @@ public class MilitaryService return Config.Url; } + private IFlurlRequest WithToken(string url) + { + return url.WithHeader("X-Access-Token", Token); + } + #endregion Base #region Cmd @@ -43,7 +48,7 @@ public class MilitaryService JObject response = new(); for (int i = 0; i < Config.RequestRetryTime; i++) { - response = url.PostJsonAsync(data).ReceiveJson().Result; + response = WithToken(url).PostJsonAsync(data).ReceiveJson().Result; bool success = response["success"].ToBoolean(); if (!success) { @@ -63,7 +68,7 @@ public class MilitaryService JObject response = new(); for (int i = 0; i < Config.RequestRetryTime; i++) { - response = url.GetAsync().ReceiveJson().Result; + response = WithToken(url).GetAsync().ReceiveJson().Result; bool success = response["success"].ToBoolean(); if (!success) { @@ -101,7 +106,7 @@ public class MilitaryService JObject response = new(); for (int i = 0; i < Config.RequestRetryTime; i++) { - response = url.WithHeader("X-Access-Token", Token).GetAsync().ReceiveJson().Result; + response = WithToken(url).GetAsync().ReceiveJson().Result; bool success = response["success"].ToBoolean(); if (!success) { @@ -120,7 +125,7 @@ public class MilitaryService JObject response = new(); for (int i = 0; i < Config.RequestRetryTime; i++) { - response = url.GetAsync().ReceiveJson().Result; + response = WithToken(url).GetAsync().ReceiveJson().Result; bool success = response["success"].ToBoolean(); if (!success) { diff --git a/JiLinApp.Docking/VibrateAlarm/Entity/ClientMessage.cs b/JiLinApp.Docking/VibrateAlarm/Entity/ClientMessage.cs index 25584ee..85df307 100644 --- a/JiLinApp.Docking/VibrateAlarm/Entity/ClientMessage.cs +++ b/JiLinApp.Docking/VibrateAlarm/Entity/ClientMessage.cs @@ -33,6 +33,14 @@ public class ClientMessage public int OnlineState { get; set; }//设备在线状态 + public bool IsOnline + { + get + { + return Host != null && Host.Id >= 0 && OnlineState == 1; + } + } + public int SensorTotal { get diff --git a/JiLinApp.Docking/VibrateAlarm/Service/TcpManager.cs b/JiLinApp.Docking/VibrateAlarm/Service/TcpManager.cs index ba2cfe3..5dc29ba 100644 --- a/JiLinApp.Docking/VibrateAlarm/Service/TcpManager.cs +++ b/JiLinApp.Docking/VibrateAlarm/Service/TcpManager.cs @@ -91,6 +91,7 @@ public class TcpManager { foreach (var clientMsg in DeviceDict.Values) { + if (!clientMsg.IsOnline) continue; SendHostHeart_01(clientMsg); } } @@ -144,22 +145,20 @@ public class TcpManager #endregion Events - #region Analysisbo + #region Analysis private void AnalysisClientMessage(ref ClientMessage clientMsg) { - //AA AA 01 C0 A8 01 05 28 23 C0 A8 01 64 88 13 7C 04 02 04 00 2B - //AA AA 01 C0 A8 01 64 88 13 C0 A8 01 05 28 23 24 14 04 0A 00 02 00 03 List msglist = clientMsg.GetMessageList(); if (msglist == null || msglist.Count == 0) return; for (int i = 0; i < msglist.Count; i++) { byte[] msg = msglist[i]; bool vaild = msg.Length >= 19 && msg[0] == 0xAA && msg[1] == 0xAA; + Console.WriteLine("Receive from {0}:{1} => {2}, {3}", clientMsg.Ip, clientMsg.Port, DataMessage.ToHexString(msg), vaild); if (!vaild) continue; DataMessage mm = new(); mm.Decode(msglist[i]); - Console.WriteLine("Receive from {0}:{1} => {2}, {3}", clientMsg.Ip, clientMsg.Port, DataMessage.ToHexString(msglist[i]), JsonUtil.ToJson(mm)); switch (mm.FunctionNum) { case 0x00: @@ -168,26 +167,31 @@ public class TcpManager { clientMsg.Host = new() { Id = mm.DeviceId, Ip = mm.SendIp, Port = mm.SendPort.ToString() }; UpdateDevice(clientMsg.Ip, clientMsg.Host); - ProcessDeviceStateEvent(ref clientMsg, 1); } + ProcessDeviceStateEvent(ref clientMsg, 1); ResponseHostLogin_10(clientMsg, mm); - ClientMessage clientMsg2 = clientMsg; - if (clientMsg2.SensorsEmpty && clientMsg2.SensorsLock.TryEnterWriteLock(1000)) + ClientMessage taskClientMsg = clientMsg; + if (taskClientMsg.SensorsEmpty && taskClientMsg.SensorsLock.TryEnterWriteLock(1000)) { Task.Run(() => { - while (clientMsg2.SensorsEmpty) + while (taskClientMsg.SensorsEmpty) { - RequestSensorList_07(clientMsg2); + RequestSensorList_07(taskClientMsg); Thread.Sleep(1000); } - clientMsg2.SensorsLock.ExitWriteLock(); + taskClientMsg.SensorsLock.ExitWriteLock(); }); } break; case 0x01: Console.WriteLine("心跳:{0}", clientMsg.Ip); + if (clientMsg.Host == null || clientMsg.Host.Id != mm.DeviceId) + { + clientMsg.Host = new() { Id = mm.DeviceId, Ip = mm.SendIp, Port = mm.SendPort.ToString() }; + UpdateDevice(clientMsg.Ip, clientMsg.Host); + } ProcessDeviceStateEvent(ref clientMsg, 1); break; @@ -202,16 +206,11 @@ public class TcpManager break; case 0x14: - Console.WriteLine("传感器状态响应:{0}", clientMsg.Ip); + Console.WriteLine("传感器轮询状态响应:{0}", clientMsg.Ip); int sensorAddr = mm.Data[0] + mm.Data[1] * 256; int state = mm.Data[2] + mm.Data[3] * 256; int online = state % 2; int alarm = state / 2 % 2; - if (clientMsg.Host == null || clientMsg.Host.Id != mm.DeviceId) - { - clientMsg.Host = new() { Id = mm.DeviceId, Ip = mm.SendIp, Port = mm.SendPort.ToString() }; - UpdateDevice(clientMsg.Ip, clientMsg.Host); - } ProcessSensorStateEvent(ref clientMsg, mm, sensorAddr, online, alarm); SetDataRequest(mm, 0x04); break; @@ -223,11 +222,6 @@ public class TcpManager case 0x17: Console.WriteLine("返回传感器列表:{0}", clientMsg.Ip); - if (clientMsg.Host == null || clientMsg.Host.Id != mm.DeviceId) - { - clientMsg.Host = new() { Id = mm.DeviceId, Ip = mm.SendIp, Port = mm.SendPort.ToString() }; - UpdateDevice(clientMsg.Ip, clientMsg.Host); - } if (clientMsg.SensorsEmpty) { for (int j = 2; j < mm.Data.Length; j++) @@ -236,10 +230,16 @@ public class TcpManager SensorState sensor = new(mm.DeviceId, sensorAddr, 0, 0); clientMsg.SensorDict[sensorAddr] = sensor; } - RequestSensorState_04(clientMsg); + RequestSensorsStateActively_08(clientMsg); } SetDataRequest(mm, 0x07); break; + + case 0x18: + Console.WriteLine("传感器主动状态响应:{0}", clientMsg.Ip); + + SetDataRequest(mm, 0x08); + break; } } } @@ -296,7 +296,7 @@ public class TcpManager OnVibrateTcpAlarm?.Invoke(alarmMsg); } - #endregion Analysisbo + #endregion Analysis #region Send @@ -337,29 +337,40 @@ public class TcpManager public bool SendHostHeart_01(ClientMessage client) { - if (client.Host == null) return false; + if (!client.IsOnline) return false; DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x01, 1); msg.Data = new byte[] { 0xFF }; return SendNoRequest(msg); } - public bool RequestSensorChange_02(ClientMessage client) + public bool RequestSensorChange_02(ClientMessage client, int newSensorAddr) { - if (client.Host == null || client.Host.Id < 0) return false; + if (!client.IsOnline) return false; DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x02, 4); - int channel = 10; - msg.Data = new byte[] { (byte)(channel % 256), (byte)(channel / 256), (100 % 256), (100 / 256) }; + msg.Data = new byte[] { (byte)(newSensorAddr % 256), (byte)(newSensorAddr / 256), (100 % 256), (100 / 256) }; DataRequest request = new() { Request = msg }; - SendRequest(ref request); - return true; + return SendRequest(ref request); + } + + public bool RequestSensorState_04(ClientMessage client, int sensorAddr) + { + if (!client.IsOnline) return false; + if (!client.SensorDict.TryGetValue(sensorAddr, out _)) return false; + DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x04, 2); + msg.Data = new byte[] { (byte)(sensorAddr % 256), (byte)(sensorAddr / 256) }; + DataRequest request = new() + { + Request = msg + }; + return SendRequest(ref request); } - public bool RequestSensorState_04(ClientMessage client) + public bool RequestSensorsState_04(ClientMessage client) { - if (client.Host == null || client.Host.Id < 0) return false; + if (!client.IsOnline) return false; if (client.SensorsEmpty) return false; DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x04, 2); foreach (var item in client.SensorDict.Values) @@ -377,21 +388,20 @@ public class TcpManager public bool RequestSensorReset_05(ClientMessage client, int sensorAddr) { - if (client.Host == null || client.Host.Id < 0) return false; - if (client.SensorsEmpty) return false; + if (!client.IsOnline) return false; + if (!client.SensorDict.TryGetValue(sensorAddr, out _)) return false; DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x05, 2); msg.Data = new byte[] { (byte)(sensorAddr % 256), (byte)(sensorAddr / 256) }; DataRequest request = new() { Request = msg }; - SendRequest(ref request); - return true; + return SendRequest(ref request); } public bool RequestSensorList_07(ClientMessage client) { - if (client.Host == null || client.Host.Id < 0) return false; + if (!client.IsOnline) return false; DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x07, 0); DataRequest request = new() { @@ -400,9 +410,40 @@ public class TcpManager return SendRequest(ref request); } + public bool RequestSensorStateActively_08(ClientMessage client, int sensorAddr) + { + if (!client.IsOnline) return false; + if (!client.SensorDict.TryGetValue(sensorAddr, out _)) return false; + DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x08, 2); + msg.Data = new byte[] { (byte)(sensorAddr % 256), (byte)(sensorAddr / 256) }; + DataRequest request = new() + { + Request = msg + }; + return SendRequest(ref request); + } + + public bool RequestSensorsStateActively_08(ClientMessage client) + { + if (!client.IsOnline) return false; + if (client.SensorsEmpty) return false; + DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x08, 2); + foreach (var item in client.SensorDict.Values) + { + int sensorAddr = item.Addr; + msg.Data = new byte[] { (byte)(sensorAddr % 256), (byte)(sensorAddr / 256) }; + DataRequest request = new() + { + Request = msg + }; + SendRequest(ref request); + } + return true; + } + public bool ResponseHostLogin_10(ClientMessage client, DataMessage mm) { - if (client.Host == null) return false; + if (!client.IsOnline) return false; DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x10, 0); msg.FrameNum = mm.FrameNum; return SendResponse(msg); diff --git a/JiLinApp/Components/CameraRealPlay.xaml b/JiLinApp/Components/CameraRealPlay.xaml index 9180dcf..3ea387e 100644 --- a/JiLinApp/Components/CameraRealPlay.xaml +++ b/JiLinApp/Components/CameraRealPlay.xaml @@ -125,7 +125,7 @@ @@ -135,8 +135,8 @@ -