atmosdevicenetworksystem for generic device network stuff for atmos devices
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
namespace Content.Server.Atmos.Monitor.Components;
|
||||
|
||||
[RegisterComponent]
|
||||
public sealed class AtmosAlarmingComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// All registered receivers in this alarmer.
|
||||
/// </summary>
|
||||
public HashSet<string> RegisteredReceivers = new();
|
||||
|
||||
// Somebody should do this someday. I'll leave it here as a reminder,
|
||||
// just in case.
|
||||
// public string StationAlarmMonitorFrequencyId
|
||||
}
|
||||
@@ -71,6 +71,12 @@ namespace Content.Server.Atmos.Monitor.Components
|
||||
[ViewVariables]
|
||||
public Dictionary<string, AtmosMonitorAlarmType> NetworkAlarmStates = new();
|
||||
|
||||
/// <summary>
|
||||
/// Registered devices in this atmos monitor. Alerts will be sent directly
|
||||
/// to these devices.
|
||||
/// </summary>
|
||||
[ViewVariables] public HashSet<string> RegisteredDevices = new();
|
||||
|
||||
// Calculates the highest alarm in the network, including itself.
|
||||
[ViewVariables]
|
||||
public AtmosMonitorAlarmType HighestAlarmInNetwork
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace Content.Server.Atmos.Monitor.Systems
|
||||
public sealed class AirAlarmSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly DeviceNetworkSystem _deviceNet = default!;
|
||||
[Dependency] private readonly AtmosMonitorSystem _atmosMonitorSystem = default!;
|
||||
[Dependency] private readonly AtmosDeviceNetworkSystem _atmosDevNetSystem = default!;
|
||||
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
|
||||
[Dependency] private readonly AccessReaderSystem _accessSystem = default!;
|
||||
[Dependency] private readonly PopupSystem _popup = default!;
|
||||
@@ -76,54 +76,36 @@ namespace Content.Server.Atmos.Monitor.Systems
|
||||
/// <param name="data">The data to send to the device.</param>
|
||||
public void SetData(EntityUid uid, string address, IAtmosDeviceData data)
|
||||
{
|
||||
if (EntityManager.TryGetComponent(uid, out AtmosMonitorComponent? monitor)
|
||||
&& !monitor.NetEnabled)
|
||||
return;
|
||||
|
||||
var payload = new NetworkPayload
|
||||
{
|
||||
[DeviceNetworkConstants.Command] = AirAlarmSetData,
|
||||
// [AirAlarmTypeData] = type,
|
||||
[AirAlarmSetData] = data
|
||||
};
|
||||
|
||||
_deviceNet.QueuePacket(uid, address, payload);
|
||||
_atmosDevNetSystem.SetDeviceState(uid, address, data);
|
||||
_atmosDevNetSystem.Sync(uid, address);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Broadcast a sync packet to an air alarm's local network.
|
||||
/// </summary>
|
||||
public void SyncAllDevices(EntityUid uid)
|
||||
private void SyncAllDevices(EntityUid uid)
|
||||
{
|
||||
if (EntityManager.TryGetComponent(uid, out AtmosMonitorComponent? monitor)
|
||||
&& !monitor.NetEnabled)
|
||||
return;
|
||||
|
||||
var payload = new NetworkPayload
|
||||
{
|
||||
[DeviceNetworkConstants.Command] = AirAlarmSyncCmd
|
||||
};
|
||||
|
||||
_deviceNet.QueuePacket(uid, null, payload);
|
||||
_atmosDevNetSystem.Sync(uid, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a sync packet to a specific device from an air alarm.
|
||||
/// </summary>
|
||||
/// <param name="address">The address of the device.</param>
|
||||
public void SyncDevice(EntityUid uid, string address)
|
||||
private void SyncDevice(EntityUid uid, string address)
|
||||
{
|
||||
if (EntityManager.TryGetComponent(uid, out AtmosMonitorComponent? monitor)
|
||||
&& !monitor.NetEnabled)
|
||||
return;
|
||||
_atmosDevNetSystem.Sync(uid, address);
|
||||
}
|
||||
|
||||
|
||||
var payload = new NetworkPayload
|
||||
{
|
||||
[DeviceNetworkConstants.Command] = AirAlarmSyncCmd
|
||||
};
|
||||
|
||||
_deviceNet.QueuePacket(uid, address, payload);
|
||||
/// <summary>
|
||||
/// Register and synchronize with all devices
|
||||
/// on this network.
|
||||
/// </summary>
|
||||
/// <param name="uid"></param>
|
||||
public void SyncRegisterAllDevices(EntityUid uid)
|
||||
{
|
||||
_atmosDevNetSystem.Register(uid, null);
|
||||
_atmosDevNetSystem.Sync(uid, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -284,7 +266,7 @@ namespace Content.Server.Atmos.Monitor.Systems
|
||||
component.ScrubberData.Clear();
|
||||
component.SensorData.Clear();
|
||||
|
||||
SyncAllDevices(uid);
|
||||
SyncRegisterAllDevices(uid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -425,7 +407,7 @@ namespace Content.Server.Atmos.Monitor.Systems
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case AirAlarmSyncData:
|
||||
case AtmosDeviceNetworkSystem.SyncData:
|
||||
if (!args.Data.TryGetValue(AirAlarmSyncData, out IAtmosDeviceData? data)
|
||||
|| !controller.CanSync)
|
||||
break;
|
||||
@@ -452,16 +434,6 @@ namespace Content.Server.Atmos.Monitor.Systems
|
||||
|
||||
UpdateUI(uid, controller);
|
||||
|
||||
return;
|
||||
case AirAlarmSetDataStatus:
|
||||
if (!args.Data.TryGetValue(AirAlarmSetDataStatus, out bool dataStatus))
|
||||
break;
|
||||
|
||||
// Sync data to interface.
|
||||
// This should say if the result
|
||||
// failed, or succeeded. Don't save it.l
|
||||
SyncDevice(uid, args.SenderAddress);
|
||||
|
||||
return;
|
||||
case AirAlarmSetMode:
|
||||
if (!args.Data.TryGetValue(AirAlarmSetMode, out AirAlarmMode alarmMode)) break;
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
using Content.Server.Atmos.Monitor.Components;
|
||||
|
||||
namespace Content.Server.Atmos.Monitor.Systems;
|
||||
|
||||
/// <summary>
|
||||
/// System that alarms AtmosAlarmables via DeviceNetwork.
|
||||
/// This is one way, and is usually triggered by an event.
|
||||
/// </summary>
|
||||
public sealed class AtmosAlarmingSystem : EntitySystem
|
||||
{
|
||||
/// <summary>
|
||||
/// The alarm command key.
|
||||
/// </summary>
|
||||
public const string AtmosAlarmCmd = "atmos_alarming_alarm_cmd";
|
||||
|
||||
/// <summary>
|
||||
/// Register command. Registers this address so that the alarm can send
|
||||
/// to the given device.
|
||||
/// </summary>
|
||||
public const string AtmosAlarmRegisterCmd = "atmos_alarming_register_cmd";
|
||||
|
||||
/// <summary>
|
||||
/// Alarm data. Contains the alert passed into this alarmer.
|
||||
/// </summary>
|
||||
public const string AtmosAlarmData = "atmos_alarming_alarm_data";
|
||||
|
||||
private void OnAlert(EntityUid uid, AtmosAlarmingComponent component, AtmosMonitorAlarmEvent args)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
65
Content.Server/Atmos/Monitor/Systems/AtmosDeviceNetwork.cs
Normal file
65
Content.Server/Atmos/Monitor/Systems/AtmosDeviceNetwork.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using Content.Server.DeviceNetwork;
|
||||
using Content.Server.DeviceNetwork.Systems;
|
||||
using Content.Shared.Atmos.Monitor.Components;
|
||||
|
||||
namespace Content.Server.Atmos.Monitor.Systems;
|
||||
|
||||
/// <summary>
|
||||
/// Generic device network commands useful for atmos devices,
|
||||
/// as well as some helper commands.
|
||||
/// </summary>
|
||||
public sealed class AtmosDeviceNetworkSystem : EntitySystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Any information about atmosphere that a device can scan.
|
||||
/// </summary>
|
||||
public const string AtmosData = "atmos_atmosphere_data";
|
||||
|
||||
/// <summary>
|
||||
/// Register a device's address on this device.
|
||||
/// </summary>
|
||||
public const string RegisterDevice = "atmos_register_device";
|
||||
|
||||
/// <summary>
|
||||
/// Synchronize the data this device has with the sender.
|
||||
/// </summary>
|
||||
public const string SyncData = "atmos_sync_data";
|
||||
|
||||
/// <summary>
|
||||
/// Set the state of this device using the contained data.
|
||||
/// </summary>
|
||||
public const string SetState = "atmos_set_state";
|
||||
|
||||
[Dependency] private readonly DeviceNetworkSystem _deviceNet = default!;
|
||||
|
||||
public void Register(EntityUid uid, string? address)
|
||||
{
|
||||
var registerPayload = new NetworkPayload
|
||||
{
|
||||
[DeviceNetworkConstants.Command] = RegisterDevice
|
||||
};
|
||||
|
||||
_deviceNet.QueuePacket(uid, address, registerPayload);
|
||||
}
|
||||
|
||||
public void Sync(EntityUid uid, string? address)
|
||||
{
|
||||
var syncPayload = new NetworkPayload
|
||||
{
|
||||
[DeviceNetworkConstants.Command] = SyncData
|
||||
};
|
||||
|
||||
_deviceNet.QueuePacket(uid, address, syncPayload);
|
||||
}
|
||||
|
||||
public void SetDeviceState(EntityUid uid, string address, IAtmosDeviceData data)
|
||||
{
|
||||
var payload = new NetworkPayload()
|
||||
{
|
||||
[DeviceNetworkConstants.Command] = SetState,
|
||||
[SetState] = data
|
||||
};
|
||||
|
||||
_deviceNet.QueuePacket(uid, address, payload);
|
||||
}
|
||||
}
|
||||
@@ -51,29 +51,14 @@ namespace Content.Server.Atmos.Monitor.Systems
|
||||
|
||||
public const string AtmosMonitorSetThresholdCmd = "atmos_monitor_set_threshold";
|
||||
|
||||
public const string AtmosMonitorGetDataCmd = "atmos_monitor_get_data";
|
||||
|
||||
// Packet data
|
||||
/// <summary>
|
||||
/// Data response that contains the threshold types in an atmos monitor alarm.
|
||||
/// </summary>
|
||||
public const string AtmosMonitorAlarmThresholdTypes = "atmos_monitor_alarm_threshold_types";
|
||||
|
||||
/// <summary>
|
||||
/// Data response that contains the source of an atmos alarm.
|
||||
/// </summary>
|
||||
public const string AtmosMonitorAlarmSrc = "atmos_monitor_alarm_source";
|
||||
|
||||
/// <summary>
|
||||
/// Data response that contains the maximum alarm in an atmos alarm network.
|
||||
/// </summary>
|
||||
public const string AtmosMonitorAlarmNetMax = "atmos_monitor_alarm_net_max";
|
||||
|
||||
/// <summary>
|
||||
/// Data response that contains the last state recorded by this air monitor.
|
||||
/// </summary>
|
||||
public const string AtmosMonitorAtmosData = "atmos_monitor_atmos_data";
|
||||
|
||||
public const string AtmosMonitorThresholdData = "atmos_monitor_threshold_data";
|
||||
|
||||
public const string AtmosMonitorThresholdDataType = "atmos_monitor_threshold_type";
|
||||
@@ -128,43 +113,20 @@ namespace Content.Server.Atmos.Monitor.Systems
|
||||
// sync the internal 'last alarm state' from
|
||||
// the other alarms, so that we can calculate
|
||||
// the highest network alarm state at any time
|
||||
if (!args.Data.TryGetValue(DeviceNetworkConstants.Command, out string? cmd)
|
||||
|| !EntityManager.TryGetComponent(uid, out AtmosAlarmableComponent? alarmable)
|
||||
|| !EntityManager.TryGetComponent(uid, out DeviceNetworkComponent? netConn))
|
||||
if (!args.Data.TryGetValue(DeviceNetworkConstants.Command, out string? cmd))
|
||||
{
|
||||
return;
|
||||
|
||||
// ignore packets from self, ignore from different frequency
|
||||
if (netConn.Address == args.SenderAddress) return;
|
||||
|
||||
var responseKey = string.Empty;
|
||||
}
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
/*
|
||||
// sync on alarm or explicit sync
|
||||
case AtmosMonitorAlarmCmd:
|
||||
case AtmosMonitorAlarmSyncCmd:
|
||||
if (args.Data.TryGetValue(AtmosMonitorAlarmSrc, out string? src)
|
||||
&& alarmable.AlarmedByPrototypes.Contains(src)
|
||||
&& args.Data.TryGetValue(DeviceNetworkConstants.CmdSetState, out AtmosMonitorAlarmType state)
|
||||
&& !component.NetworkAlarmStates.TryAdd(args.SenderAddress, state))
|
||||
component.NetworkAlarmStates[args.SenderAddress] = state;
|
||||
case AtmosDeviceNetworkSystem.RegisterDevice:
|
||||
component.RegisteredDevices.Add(args.SenderAddress);
|
||||
break;
|
||||
*/
|
||||
case AtmosMonitorAlarmResetCmd:
|
||||
Reset(uid);
|
||||
// Don't clear alarm states here.
|
||||
break;
|
||||
/*
|
||||
case AtmosMonitorAlarmResetAllCmd:
|
||||
if (args.Data.TryGetValue(AtmosMonitorAlarmSrc, out string? resetSrc)
|
||||
&& alarmable.AlarmedByPrototypes.Contains(resetSrc))
|
||||
{
|
||||
component.LastAlarmState = AtmosMonitorAlarmType.Normal;
|
||||
component.NetworkAlarmStates.Clear();
|
||||
}
|
||||
break;
|
||||
*/
|
||||
case AtmosMonitorSetThresholdCmd:
|
||||
if (args.Data.TryGetValue(AtmosMonitorThresholdData, out AtmosAlarmThreshold? thresholdData)
|
||||
&& args.Data.TryGetValue(AtmosMonitorThresholdDataType, out AtmosMonitorThresholdType? thresholdType))
|
||||
@@ -174,9 +136,9 @@ namespace Content.Server.Atmos.Monitor.Systems
|
||||
}
|
||||
|
||||
break;
|
||||
case AtmosMonitorGetDataCmd:
|
||||
case AtmosDeviceNetworkSystem.SyncData:
|
||||
var payload = new NetworkPayload();
|
||||
payload.Add(DeviceNetworkConstants.Command, AtmosMonitorAtmosData);
|
||||
payload.Add(DeviceNetworkConstants.Command, AtmosDeviceNetworkSystem.SyncData);
|
||||
if (component.TileGas != null)
|
||||
{
|
||||
var gases = new Dictionary<Gas, float>();
|
||||
@@ -185,7 +147,7 @@ namespace Content.Server.Atmos.Monitor.Systems
|
||||
gases.Add(gas, component.TileGas.GetMoles(gas));
|
||||
}
|
||||
|
||||
payload.Add(AtmosMonitorAtmosData, new AtmosSensorData(
|
||||
payload.Add(AtmosDeviceNetworkSystem.SyncData, new AtmosSensorData(
|
||||
component.TileGas.Pressure,
|
||||
component.TileGas.Temperature,
|
||||
component.TileGas.TotalMoles,
|
||||
@@ -418,12 +380,13 @@ namespace Content.Server.Atmos.Monitor.Systems
|
||||
{
|
||||
[DeviceNetworkConstants.Command] = AtmosMonitorAlarmCmd,
|
||||
[DeviceNetworkConstants.CmdSetState] = monitor.LastAlarmState,
|
||||
[AtmosMonitorAlarmNetMax] = monitor.HighestAlarmInNetwork,
|
||||
[AtmosMonitorAlarmThresholdTypes] = alarms,
|
||||
[AtmosMonitorAlarmSrc] = source
|
||||
};
|
||||
|
||||
_deviceNetSystem.QueuePacket(monitor.Owner, null, payload);
|
||||
foreach (var addr in monitor.RegisteredDevices)
|
||||
{
|
||||
_deviceNetSystem.QueuePacket(monitor.Owner, addr, payload);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -188,23 +188,19 @@ namespace Content.Server.Atmos.Piping.Unary.EntitySystems
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case AirAlarmSystem.AirAlarmSyncCmd:
|
||||
payload.Add(DeviceNetworkConstants.Command, AirAlarmSystem.AirAlarmSyncData);
|
||||
payload.Add(AirAlarmSystem.AirAlarmSyncData, component.ToAirAlarmData());
|
||||
case AtmosDeviceNetworkSystem.SyncData:
|
||||
payload.Add(DeviceNetworkConstants.Command, AtmosDeviceNetworkSystem.SyncData);
|
||||
payload.Add(AtmosDeviceNetworkSystem.SyncData, component.ToAirAlarmData());
|
||||
|
||||
_deviceNetSystem.QueuePacket(uid, args.SenderAddress, payload, device: netConn);
|
||||
|
||||
return;
|
||||
case AirAlarmSystem.AirAlarmSetData:
|
||||
if (!args.Data.TryGetValue(AirAlarmSystem.AirAlarmSetData, out GasVentPumpData? setData))
|
||||
case AtmosDeviceNetworkSystem.SetState:
|
||||
if (!args.Data.TryGetValue(AtmosDeviceNetworkSystem.SetState, out GasVentPumpData? setData))
|
||||
break;
|
||||
|
||||
component.FromAirAlarmData(setData);
|
||||
UpdateState(uid, component);
|
||||
payload.Add(DeviceNetworkConstants.Command, AirAlarmSystem.AirAlarmSetDataStatus);
|
||||
payload.Add(AirAlarmSystem.AirAlarmSetDataStatus, true);
|
||||
|
||||
_deviceNetSystem.QueuePacket(uid, null, payload, device: netConn);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -154,23 +154,19 @@ namespace Content.Server.Atmos.Piping.Unary.EntitySystems
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case AirAlarmSystem.AirAlarmSyncCmd:
|
||||
payload.Add(DeviceNetworkConstants.Command, AirAlarmSystem.AirAlarmSyncData);
|
||||
payload.Add(AirAlarmSystem.AirAlarmSyncData, component.ToAirAlarmData());
|
||||
case AtmosDeviceNetworkSystem.SyncData:
|
||||
payload.Add(DeviceNetworkConstants.Command, AtmosDeviceNetworkSystem.SyncData);
|
||||
payload.Add(AtmosDeviceNetworkSystem.SyncData, component.ToAirAlarmData());
|
||||
|
||||
_deviceNetSystem.QueuePacket(uid, args.SenderAddress, payload, device: netConn);
|
||||
|
||||
return;
|
||||
case AirAlarmSystem.AirAlarmSetData:
|
||||
if (!args.Data.TryGetValue(AirAlarmSystem.AirAlarmSetData, out GasVentScrubberData? setData))
|
||||
case AtmosDeviceNetworkSystem.SetState:
|
||||
if (!args.Data.TryGetValue(AtmosDeviceNetworkSystem.SetState, out GasVentScrubberData? setData))
|
||||
break;
|
||||
|
||||
component.FromAirAlarmData(setData);
|
||||
UpdateState(uid, component);
|
||||
payload.Add(DeviceNetworkConstants.Command, AirAlarmSystem.AirAlarmSetDataStatus);
|
||||
payload.Add(AirAlarmSystem.AirAlarmSetDataStatus, true);
|
||||
|
||||
_deviceNetSystem.QueuePacket(uid, null, payload, device: netConn);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user