You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
4.7 KiB
172 lines
4.7 KiB
2 years ago
|
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.FenceAlarm;
|
||
|
using Newtonsoft.Json.Linq;
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Reactive.Linq;
|
||
|
|
||
|
namespace JiLinApp.Pages.FenceServer;
|
||
|
|
||
|
public partial class Fence : UserControl
|
||
|
{
|
||
|
#region Fields
|
||
|
|
||
|
private UdpManager Manager { get; } = new();
|
||
|
|
||
|
private IAlarmService AlarmService { get; set; }
|
||
|
|
||
|
private Window? _win;
|
||
|
|
||
|
private Window? Wdw
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
_win ??= ControlsUtil.GetWindow(this);
|
||
|
return _win;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion Fields
|
||
|
|
||
|
public Fence()
|
||
|
{
|
||
|
InitializeComponent();
|
||
|
|
||
|
AlarmService = Global.AlarmService;
|
||
|
OpenBtn.Click += OpenBtn_Click;
|
||
|
CloseBtn.Click += CloseBtn_Click;
|
||
|
|
||
|
Manager.OnFenceUdpDeviceState += Manager_OnFenceUdpDeviceState;
|
||
|
Manager.OnFenceUdpSectorState += Manager_OnFenceUdpSensorState;
|
||
|
Manager.OnFenceUdpAlarm += Manager_OnFenceUdpAlarm;
|
||
|
AlarmService.OnFenceUdpSendDevices += AlarmService_OnFenceUdpSendDevices;
|
||
|
AlarmService.OnFenceUdpSendSensors += AlarmService_OnFenceUdpSendSensors;
|
||
|
}
|
||
|
|
||
|
#region ElementEvent
|
||
|
|
||
|
private void OpenBtn_Click(object? sender, RoutedEventArgs e)
|
||
|
{
|
||
|
if (Manager.IsRunning()) return;
|
||
|
try
|
||
|
{
|
||
|
UdpManagerConfig 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_OnFenceUdpDeviceState(UdpAlarmHost device)
|
||
|
{
|
||
|
object deviceObj = new
|
||
|
{
|
||
|
id = device.DeviceId,
|
||
|
onlineState = device.OnlineState,
|
||
|
defenceState = device.DefenceState
|
||
|
};
|
||
|
AlarmService.SendDeviceState(DeviceType.Fence, deviceObj);
|
||
|
}
|
||
|
|
||
|
private void Manager_OnFenceUdpSensorState(SectorState sector)
|
||
|
{
|
||
|
object sensorObj = new
|
||
|
{
|
||
|
id = sector.Id,
|
||
|
deviceId = sector.DeviceId,
|
||
|
state = sector.State,
|
||
|
type = sector.Type,
|
||
|
};
|
||
|
AlarmService.SendSensorState(DeviceType.Fence, sensorObj);
|
||
|
}
|
||
|
|
||
|
private void Manager_OnFenceUdpAlarm(UdpAlarmHostMessage msg)
|
||
|
{
|
||
|
AlarmMessage alarm = msg.ToAlarmMessage();
|
||
|
AlarmService.SendAlarm(alarm);
|
||
|
MessageEvent.FenceAlarm.Publish(msg);
|
||
|
}
|
||
|
|
||
|
#endregion Manager Event
|
||
|
|
||
|
#region SubCmd Event
|
||
|
|
||
|
private void AlarmService_OnFenceUdpSendDevices(JObject reqObj)
|
||
|
{
|
||
|
List<object> respDeviceList = new();
|
||
|
List<UdpAlarmHost> deviceList = Manager.GetDeviceList();
|
||
|
foreach (var item in deviceList)
|
||
|
{
|
||
|
if (item.DeviceId <= 0) continue;
|
||
|
respDeviceList.Add(new
|
||
|
{
|
||
|
id = item.DeviceId,
|
||
|
//groupId = item.GroupId,
|
||
|
//userId = item.UserId,
|
||
|
onlineState = item.OnlineState,
|
||
|
defenceState = item.DefenceState
|
||
|
});
|
||
|
}
|
||
|
AlarmService.SendDevices(DeviceType.Fence, respDeviceList);
|
||
|
}
|
||
|
|
||
|
private void AlarmService_OnFenceUdpSendSensors(JObject reqObj)
|
||
|
{
|
||
|
int deviceId = reqObj.GetValue("deviceId", StringComparison.OrdinalIgnoreCase).ToInt();
|
||
|
List<SectorState> sectorList = Manager.TryGetDevice(deviceId, out UdpAlarmHost device) ? device.SectorDict.Values.ToList() : new();
|
||
|
List<object> respSensorList = new();
|
||
|
foreach (var item in sectorList)
|
||
|
{
|
||
|
respSensorList.Add(new
|
||
|
{
|
||
|
id = item.Id,
|
||
|
deviceId = item.DeviceId,
|
||
|
state = item.State,
|
||
|
type = item.Type,
|
||
|
});
|
||
|
}
|
||
|
AlarmService.SendSensors(DeviceType.Fence, deviceId, respSensorList);
|
||
|
}
|
||
|
|
||
|
#endregion SubCmd Event
|
||
|
}
|