From 09d6e3b94b6777474138ec059f672a899a208cc3 Mon Sep 17 00:00:00 2001 From: wenjy Date: Wed, 4 Dec 2024 10:35:13 +0800 Subject: [PATCH] =?UTF-8?q?change=20-=20=E7=9C=8B=E6=9D=BF=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.Business/AutoModeBusiness.cs | 4 +- SlnMesnac.Business/FixedPointBusiness.cs | 4 +- SlnMesnac.Business/InspModeBusiness.cs | 5 +- SlnMesnac.Business/SlnMesnac.Business.csproj | 4 + SlnMesnac.Business/WebSocketBusiness.cs | 134 ++++++++++++++++++ SlnMesnac.Business/base/BaseBusiness.cs | 40 ++++-- SlnMesnac.Extensions/SqlsugarSetup.cs | 3 + .../ViewModel/IndexControlViewModel.cs | 38 ++--- SlnMesnac.WPF/appsettings.json | 4 +- 9 files changed, 195 insertions(+), 41 deletions(-) create mode 100644 SlnMesnac.Business/WebSocketBusiness.cs diff --git a/SlnMesnac.Business/AutoModeBusiness.cs b/SlnMesnac.Business/AutoModeBusiness.cs index 0220ff2..2f3ccf6 100644 --- a/SlnMesnac.Business/AutoModeBusiness.cs +++ b/SlnMesnac.Business/AutoModeBusiness.cs @@ -41,8 +41,6 @@ namespace SlnMesnac.Business /// public class AutoModeBusiness : BaseBusiness { - public readonly RealTemperatureInfo _realTemperatureInfo = new RealTemperatureInfo(); - private readonly Irecord_busbar_tempServices _record_busbar_TempServices; private readonly AppConfig _appConfig; @@ -55,7 +53,7 @@ namespace SlnMesnac.Business public delegate void GimbaRotationControl(string wPanPos, string wTiltPos, string wZoomPos); public event GimbaRotationControl GimbaRotationControlEvent; - public AutoModeBusiness(SerilogHelper log, PlcAbsractFactory plc, Ibase_cabinet_infoServices base_Cabinet_InfoServices, Ibase_busbar_infoServices base_Busbar_InfoServices, Irecord_busbar_tempServices record_busbar_TempServices, Ibase_alarm_infoServices base_Alarm_InfoServices, AppConfig appConfig, Irecord_busbar_alarmServices record_Busbar_AlarmServices) : base(log, plc, base_Cabinet_InfoServices, base_Busbar_InfoServices, base_Alarm_InfoServices, record_Busbar_AlarmServices) + public AutoModeBusiness( Irecord_busbar_tempServices record_busbar_TempServices, AppConfig appConfig, IServiceProvider serviceProvider) : base(serviceProvider) { _record_busbar_TempServices = record_busbar_TempServices; _appConfig = appConfig; diff --git a/SlnMesnac.Business/FixedPointBusiness.cs b/SlnMesnac.Business/FixedPointBusiness.cs index 4d12b35..991d2a9 100644 --- a/SlnMesnac.Business/FixedPointBusiness.cs +++ b/SlnMesnac.Business/FixedPointBusiness.cs @@ -35,11 +35,9 @@ namespace SlnMesnac.Business { public class FixedPointBusiness : BaseBusiness { - - private readonly Irecord_busbar_tempServices _record_busbar_TempServices; - public FixedPointBusiness(SerilogHelper log, PlcAbsractFactory plc, Ibase_cabinet_infoServices base_Cabinet_InfoServices, Ibase_busbar_infoServices base_Busbar_InfoServices, Irecord_busbar_tempServices record_busbar_TempServices, Ibase_alarm_infoServices base_Alarm_InfoServices, Irecord_busbar_alarmServices record_Busbar_AlarmServices) : base(log, plc, base_Cabinet_InfoServices, base_Busbar_InfoServices, base_Alarm_InfoServices, record_Busbar_AlarmServices) + public FixedPointBusiness(Irecord_busbar_tempServices record_busbar_TempServices, IServiceProvider serviceProvider) : base(serviceProvider) { _record_busbar_TempServices = record_busbar_TempServices; } diff --git a/SlnMesnac.Business/InspModeBusiness.cs b/SlnMesnac.Business/InspModeBusiness.cs index d30bd9b..346f062 100644 --- a/SlnMesnac.Business/InspModeBusiness.cs +++ b/SlnMesnac.Business/InspModeBusiness.cs @@ -41,15 +41,14 @@ namespace SlnMesnac.Business private readonly Irecord_busbar_tempServices _record_busbar_TempServices; private readonly Irecord_inspection_cabinetServices _service; - public readonly RealTemperatureInfo _realTemperatureInfo = new RealTemperatureInfo(); - public delegate void CaptureAlarmPicture(string sJpegPicFileName); public event CaptureAlarmPicture CaptureAlarmPictureEvent; public delegate void GimbaRotationControl(string wPanPos, string wTiltPos, string wZoomPos); public event GimbaRotationControl GimbaRotationControlEvent; - public InspModeBusiness(SerilogHelper log, PlcAbsractFactory plc, Ibase_cabinet_infoServices base_Cabinet_InfoServices, Ibase_busbar_infoServices base_Busbar_InfoServices, Irecord_inspection_cabinetServices service, Irecord_busbar_tempServices irecord_Busbar_TempServices,Ibase_alarm_infoServices base_Alarm_InfoServices, Irecord_busbar_alarmServices record_Busbar_AlarmServices) : base(log, plc, base_Cabinet_InfoServices, base_Busbar_InfoServices, base_Alarm_InfoServices, record_Busbar_AlarmServices) + public InspModeBusiness( Irecord_inspection_cabinetServices service, + Irecord_busbar_tempServices irecord_Busbar_TempServices,IServiceProvider serviceProvider) : base(serviceProvider) { _record_busbar_TempServices = irecord_Busbar_TempServices; _service = service; diff --git a/SlnMesnac.Business/SlnMesnac.Business.csproj b/SlnMesnac.Business/SlnMesnac.Business.csproj index 19a9935..c3fd483 100644 --- a/SlnMesnac.Business/SlnMesnac.Business.csproj +++ b/SlnMesnac.Business/SlnMesnac.Business.csproj @@ -5,6 +5,10 @@ enable + + + + diff --git a/SlnMesnac.Business/WebSocketBusiness.cs b/SlnMesnac.Business/WebSocketBusiness.cs new file mode 100644 index 0000000..f0068d9 --- /dev/null +++ b/SlnMesnac.Business/WebSocketBusiness.cs @@ -0,0 +1,134 @@ +using Fleck; +using Newtonsoft.Json.Linq; +using SlnMesnac.Model.dto; +using SlnMesnac.Plc; +using SlnMesnac.Repository; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +#region << 版 本 注 释 >> +/*-------------------------------------------------------------------- +* 版权所有 (c) 2024 WenJY 保留所有权利。 +* CLR版本:4.0.30319.42000 +* 机器名称:T14-GEN3-7895 +* 命名空间:SlnMesnac.Business +* 唯一标识:2a743b18-b6e9-499e-87b5-15ad4a2041b6 +* +* 创建者:WenJY +* 电子邮箱: +* 创建时间:2024-12-04 9:10:44 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace SlnMesnac.Business +{ + public class WebSocketBusiness + { + private List allSockets = new List(); + + private readonly RealTemperatureInfo _realTemperatureInfo; + + public readonly PlcAbsractFactory _plc; + + public WebSocketBusiness(RealTemperatureInfo realTemperatureInfo, PlcAbsractFactory plc) + { + _plc = plc; + _realTemperatureInfo = realTemperatureInfo; + + this.Init(); + } + + public void Encapsulation(bool isAlarm ,base_alarm_info alarmInfo,record_busbar_alarm busbarAlarm,int source = 1) + { + if(_realTemperatureInfo != null) + { + JObject jsonObject = new JObject + { + ["Uuid"] = System.Guid.NewGuid().ToString(), + ["DeviceInfo"] = new JObject + { + ["deviceStatus"] = _plc.readInt32ByAddress("VD1508"), + ["runSpeed"] = _plc.readFloatByAddress("VD2022"), + ["liftStatus"] = 1, + ["cabinetCode"] = _plc.readInt32ByAddress("VD1100"), + ["realAddr"] = string.Empty, + }, + ["TemParam"] = new JObject + { + ["tempMax"] = _realTemperatureInfo.fMaxTemperature, + ["tempMin"] = _realTemperatureInfo.fMinTemperature, + ["tempAvg"] = _realTemperatureInfo.fAverageTemperature, + ["tempDiff"] = _realTemperatureInfo.fTemperatureDiff + }, + ["AlarmInfo"] = new JObject + { + ["isAlarm"] = isAlarm ? 1 : 0, + ["alarmAddr"] = busbarAlarm != null ? busbarAlarm.cabinetCode : null, + ["alarmType"] = alarmInfo != null ? alarmInfo.alarmType : null, + ["alarmAlias"] = alarmInfo != null ? alarmInfo.alarmAlias : null, + ["alarmContent"] = alarmInfo != null ? alarmInfo.alarmContent : null, + ["alarmSource"] = source, + ["isFlag"] = 0, + ["alarmTime"] = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + } + }; + + // 将 JObject 转换为 JSON 字符串 + string jsonString = jsonObject.ToString(); + + this.sendMessage(jsonString); + } + } + + public void Init() + { + var server = new Fleck.WebSocketServer($"ws://0.0.0.0:7181"); + server.Start(socket => + { + socket.OnOpen = () => + { + var data = socket.ConnectionInfo; + Console.WriteLine("WebSocket Open!"); + allSockets.Add(socket); + }; + + socket.OnClose = () => + { + Console.WriteLine("WebSocket Close!"); + allSockets.Remove(socket); + }; + + socket.OnMessage = message => + { + //ReceivedMessageRequestInfoEvent?.Invoke(socket, message); + }; + }); + } + + public void sendMessage(string message) + { + try + { + foreach (var socket in allSockets.ToList()) + { + socket.Send(message); + Console.WriteLine("WebSocket接口上传数据:" + message); + } + } + catch (Exception ex) + { + Console.WriteLine("WebSocket上传数据异常", ex); + } + } + } +} diff --git a/SlnMesnac.Business/base/BaseBusiness.cs b/SlnMesnac.Business/base/BaseBusiness.cs index fcce2c6..7ef488a 100644 --- a/SlnMesnac.Business/base/BaseBusiness.cs +++ b/SlnMesnac.Business/base/BaseBusiness.cs @@ -1,4 +1,5 @@ -using Serilog; +using Microsoft.Extensions.DependencyInjection; +using Serilog; using SlnMesnac.Model.dto; using SlnMesnac.Plc; using SlnMesnac.Repository; @@ -8,6 +9,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Net; +using System.Security.Claims; using System.Threading.Tasks; #region << 版 本 注 释 >> @@ -40,23 +42,35 @@ namespace SlnMesnac.Business.@base public readonly PlcAbsractFactory _plc; + public readonly RealTemperatureInfo _realTemperatureInfo; + + private readonly WebSocketBusiness _webSocketBusiness; + + private IServiceProvider _serviceProvider; + public readonly Ibase_cabinet_infoServices _ibase_Cabinet_InfoServices; public readonly Ibase_busbar_infoServices _ibase_Busbar_InfoServices; private readonly Ibase_alarm_infoServices _ibase_Alarm_InfoServices; private readonly Irecord_busbar_alarmServices _iRecord_Busbar_AlarmServices; public bool isFlag = false; - - public BaseBusiness(SerilogHelper log,PlcAbsractFactory plc, Ibase_cabinet_infoServices base_Cabinet_InfoServices, - Ibase_busbar_infoServices base_Busbar_InfoServices, Ibase_alarm_infoServices base_Alarm_InfoServices, - Irecord_busbar_alarmServices record_Busbar_AlarmServices) + public BaseBusiness(IServiceProvider serviceProvider) { - _log = log; - _plc = plc; - _ibase_Cabinet_InfoServices = base_Cabinet_InfoServices; - _ibase_Busbar_InfoServices = base_Busbar_InfoServices; - _ibase_Alarm_InfoServices = base_Alarm_InfoServices; - _iRecord_Busbar_AlarmServices = record_Busbar_AlarmServices; + _serviceProvider = serviceProvider; + + using (var scope = _serviceProvider.CreateScope()) + { + _log = scope.ServiceProvider.GetRequiredService(); + _plc = scope.ServiceProvider.GetRequiredService(); + _realTemperatureInfo = scope.ServiceProvider.GetRequiredService(); + _ibase_Cabinet_InfoServices = scope.ServiceProvider.GetRequiredService(); + _ibase_Busbar_InfoServices = scope.ServiceProvider.GetRequiredService(); + _ibase_Alarm_InfoServices = scope.ServiceProvider.GetRequiredService(); + _iRecord_Busbar_AlarmServices = scope.ServiceProvider.GetRequiredService(); + + _webSocketBusiness = scope.ServiceProvider.GetRequiredService(); + } + } /// @@ -172,6 +186,10 @@ namespace SlnMesnac.Business.@base _iRecord_Busbar_AlarmServices.Insert(busbarAlarm); busbarTemp.isAlarm = 1; + + _webSocketBusiness.Encapsulation(true,alarm,busbarAlarm); + + } } } diff --git a/SlnMesnac.Extensions/SqlsugarSetup.cs b/SlnMesnac.Extensions/SqlsugarSetup.cs index 8761bea..222113c 100644 --- a/SlnMesnac.Extensions/SqlsugarSetup.cs +++ b/SlnMesnac.Extensions/SqlsugarSetup.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.DependencyInjection; using SlnMesnac.Config; +using SlnMesnac.Model.dto; using SqlSugar; using System; using System.Collections.Generic; @@ -64,6 +65,8 @@ namespace SlnMesnac.Extensions return Db; }); + + services.AddSingleton(); } } } diff --git a/SlnMesnac.WPF/ViewModel/IndexControlViewModel.cs b/SlnMesnac.WPF/ViewModel/IndexControlViewModel.cs index 226b5da..87e6e8f 100644 --- a/SlnMesnac.WPF/ViewModel/IndexControlViewModel.cs +++ b/SlnMesnac.WPF/ViewModel/IndexControlViewModel.cs @@ -1,6 +1,8 @@ using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Microsoft.Extensions.DependencyInjection; +using Newtonsoft.Json.Linq; +using NVelocity.Util.Introspection; using SlnMesnac.Business; using SlnMesnac.Business.@base; using SlnMesnac.Config; @@ -46,6 +48,9 @@ namespace SlnMesnac.WPF.ViewModel private AutoModeBusiness autoModeBusiness; private InspModeBusiness inspModeBusiness; private FixedPointBusiness fixedPointBusiness; + + private WebSocketBusiness webSocketBusiness; + private AppConfig _appConfig; private CHCNetSDK.RemoteConfigCallback _callback; @@ -71,6 +76,7 @@ namespace SlnMesnac.WPF.ViewModel autoModeBusiness = App.ServiceProvider.GetService(); inspModeBusiness = App.ServiceProvider.GetService(); fixedPointBusiness = App.ServiceProvider.GetService(); + webSocketBusiness = App.ServiceProvider.GetService(); autoModeBusiness.CaptureAlarmPictureEvent += CapturePicture; autoModeBusiness.GimbaRotationControlEvent += GimbaRotationControl; @@ -205,25 +211,7 @@ namespace SlnMesnac.WPF.ViewModel public void GetThermInfoCallback(uint dwType, IntPtr lpBuffer, uint dwBufLen, IntPtr pUserData) { var ret = Marshal.PtrToStructure(lpBuffer); - if (autoModeBusiness != null) - { - autoModeBusiness._realTemperatureInfo.fMaxTemperature = (float)Math.Round(ret.struLinePolygonThermCfg.fMaxTemperature, 1); - autoModeBusiness._realTemperatureInfo.fMinTemperature = (float)Math.Round(ret.struLinePolygonThermCfg.fMinTemperature); - autoModeBusiness._realTemperatureInfo.fAverageTemperature = (float)Math.Round(ret.struLinePolygonThermCfg.fAverageTemperature, 1); - autoModeBusiness._realTemperatureInfo.fTemperatureDiff = (float)Math.Round(ret.struLinePolygonThermCfg.fTemperatureDiff, 1); - - fixedPointBusiness._realTemperatureInfo.fMaxTemperature = autoModeBusiness._realTemperatureInfo.fMaxTemperature; - fixedPointBusiness._realTemperatureInfo.fMinTemperature = autoModeBusiness._realTemperatureInfo.fMinTemperature; - fixedPointBusiness._realTemperatureInfo.fAverageTemperature = autoModeBusiness._realTemperatureInfo.fAverageTemperature; - fixedPointBusiness._realTemperatureInfo.fTemperatureDiff = autoModeBusiness._realTemperatureInfo.fTemperatureDiff; - - inspModeBusiness._realTemperatureInfo.fMaxTemperature = autoModeBusiness._realTemperatureInfo.fMaxTemperature; - inspModeBusiness._realTemperatureInfo.fMinTemperature = autoModeBusiness._realTemperatureInfo.fMinTemperature; - inspModeBusiness._realTemperatureInfo.fAverageTemperature = autoModeBusiness._realTemperatureInfo.fAverageTemperature; - inspModeBusiness._realTemperatureInfo.fTemperatureDiff = autoModeBusiness._realTemperatureInfo.fTemperatureDiff; - - } - + this.RealTemperature = new RealTemperatureInfo() { fMaxTemperature = (float)Math.Round(ret.struLinePolygonThermCfg.fMaxTemperature, 1), @@ -231,6 +219,17 @@ namespace SlnMesnac.WPF.ViewModel fAverageTemperature = (float)Math.Round(ret.struLinePolygonThermCfg.fAverageTemperature, 1), fTemperatureDiff = (float)Math.Round(ret.struLinePolygonThermCfg.fTemperatureDiff, 1), }; + + using (var scope = App.ServiceProvider.CreateScope()) + { + var info = scope.ServiceProvider.GetRequiredService(); + info.fMaxTemperature = _realTemperature.fMaxTemperature; + info.fMinTemperature = _realTemperature.fMinTemperature; + info.fAverageTemperature = _realTemperature.fAverageTemperature; + info.fTemperatureDiff = _realTemperature.fTemperatureDiff; + } + + webSocketBusiness.Encapsulation(false, null, null); } /// @@ -660,5 +659,6 @@ namespace SlnMesnac.WPF.ViewModel } }); } + } } diff --git a/SlnMesnac.WPF/appsettings.json b/SlnMesnac.WPF/appsettings.json index 338d741..6b88f47 100644 --- a/SlnMesnac.WPF/appsettings.json +++ b/SlnMesnac.WPF/appsettings.json @@ -29,8 +29,8 @@ { "configId": 1, "plcType": "SiemensPlc", - //"plcIp": "127.0.0.1", - "plcIp": "192.168.2.1", + "plcIp": "127.0.0.1", + //"plcIp": "192.168.2.1", "plcPort": 102, "plcKey": "iot", "isFlage": "true"