using Avalonia.Controls; using Avalonia.Interactivity; using EC.Util.Common; using JiLinApp.Biz.TransmitAlarm; using JiLinApp.Core; using JiLinApp.Core.App; using JiLinApp.Core.Avalonia; using JiLinApp.Docking.VibrateAlarm; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Reactive.Linq; namespace JiLinApp.Pages.VibrateServer; public partial class Vibrate : UserControl { #region Fields private TcpManager Manager { get; } = new(); private IAlarmService AlarmService { get; set; } private Window? _win; private Window? Wdw { get { _win ??= ControlsUtil.GetWindow(this); return _win; } } #endregion Fields public Vibrate() { InitializeComponent(); AlarmService = Global.AlarmService; OpenBtn.Click += OpenBtn_Click; CloseBtn.Click += CloseBtn_Click; Manager.OnVibrateTcpDeviceState += Manager_OnVibrateTcpDeviceState; Manager.OnVibrateTcpSensorState += Manager_OnVibrateTcpSensorSate; Manager.OnVibrateTcpAlarm += Manager_OnVibrateTcpAlarm; AlarmService.OnVibrateTcpSendDevices += AlarmService_OnVibrateTcpSendDevices; AlarmService.OnVibrateTcpSendSensors += AlarmService_OnVibrateTcpSendSensors; } #region ElementEvent private void OpenBtn_Click(object? sender, RoutedEventArgs e) { if (Manager.IsRunning()) return; try { TcpManagerConfig config = new() { ServerPort = int.Parse(ServerPortTb.GetText()), DeviceHeartKeep = int.Parse(HeartKeepTb.GetText()) }; Manager.Start(config); ChangeServerBtnState(true); } catch (Exception ex) { _ = ControlsUtil.ShowWarnDialog(Wdw, message: ex.ToString()); LogUnit.Error(ex); } } private void CloseBtn_Click(object? sender, RoutedEventArgs e) { if (!Manager.IsRunning()) return; try { Manager.Stop(); ChangeServerBtnState(false); } catch (Exception ex) { _ = ControlsUtil.ShowWarnDialog(Wdw, message: ex.ToString()); LogUnit.Error(ex); } } private void ChangeServerBtnState(bool openState) { OpenBtn.IsEnabled = !openState; CloseBtn.IsEnabled = openState; } #endregion ElementEvent #region Manager Event private void Manager_OnVibrateTcpDeviceState(ClientMessage device) { object deviceObj = new { id = device.Host.Id, onlineState = device.OnlineState }; AlarmService.SendDeviceState(DeviceType.Vibrate, deviceObj); } private void Manager_OnVibrateTcpSensorSate(SensorState sensor) { object sensorObj = new { id = sensor.Addr, deviceId = sensor.DeviceId, onlineState = sensor.OnlineState, alarmState = sensor.AlarmState, }; AlarmService.SendSensorState(DeviceType.Vibrate, sensorObj); } private void Manager_OnVibrateTcpAlarm(TcpAlarmHostMessage msg) { AlarmMessage alarm = msg.ToAlarmMessage(); AlarmService.SendAlarm(alarm); MessageEvent.VibrateAlarm.Publish(msg); } #endregion Manager Event #region SubCmd Event private void AlarmService_OnVibrateTcpSendDevices(JObject reqObj) { List respDeviceList = new(); List deviceList = Manager.GetDeviceList(); foreach (var item in deviceList) { if (item.Host.Id <= 0) continue; respDeviceList.Add(new { id = item.Host.Id, onlineState = item.OnlineState }); } AlarmService.SendDevices(DeviceType.Vibrate, respDeviceList); } private void AlarmService_OnVibrateTcpSendSensors(JObject reqObj) { int deviceId = reqObj.GetValue("deviceId", StringComparison.OrdinalIgnoreCase).ToInt(); List sensorList = Manager.TryGetDevice(deviceId, out ClientMessage device) ? device.SensorDict.Values.ToList() : new(); List respSensorList = new(); foreach (var item in sensorList) { respSensorList.Add(new { id = item.Addr, deviceId = item.DeviceId, onlineState = item.OnlineState, alarmState = item.AlarmState, }); } AlarmService.SendSensors(DeviceType.Vibrate, deviceId, respSensorList); } #endregion SubCmd Event }