Files
ss14-wega/Content.Server/SensorMonitoring/SensorMonitoringConsoleSystem.cs
Dmitry 25c5e59248 Upstream (#3172)
Signed-off-by: Prole <172158352+Prole0@users.noreply.github.com>
Co-authored-by: PJBot <pieterjan.briers+bot@gmail.com>
Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com>
Co-authored-by: Samuka-C <47865393+Samuka-C@users.noreply.github.com>
Co-authored-by: ArtisticRoomba <145879011+ArtisticRoomba@users.noreply.github.com>
Co-authored-by: Partmedia <kevinz5000@gmail.com>
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
Co-authored-by: themias <89101928+themias@users.noreply.github.com>
Co-authored-by: Victor Shen <71985089+Vexerot@users.noreply.github.com>
Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com>
Co-authored-by: Milon <milonpl.git@proton.me>
Co-authored-by: Kirus59 <145689588+Kirus59@users.noreply.github.com>
Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com>
Co-authored-by: Stomf <5dorkydorks@gmail.com>
Co-authored-by: drakewill-CRL <46307022+drakewill-CRL@users.noreply.github.com>
Co-authored-by: PraxisMapper <praxismapper@gmail.com>
Co-authored-by: EmoGarbage404 <retron404@gmail.com>
Co-authored-by: lzk <124214523+lzk228@users.noreply.github.com>
Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
Co-authored-by: IProduceWidgets <107586145+IProduceWidgets@users.noreply.github.com>
Co-authored-by: TytosB <54259736+TytosB@users.noreply.github.com>
Co-authored-by: abadaba695 <spacestation13thingy@gmail.com>
Co-authored-by: kosticia <kosticia46@gmail.com>
Co-authored-by: Thinbug <101073555+Thinbug0@users.noreply.github.com>
Co-authored-by: pathetic meowmeow <uhhadd@gmail.com>
Co-authored-by: SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com>
Co-authored-by: Boaz1111 <149967078+Boaz1111@users.noreply.github.com>
Co-authored-by: ActiveMammmoth <140334666+ActiveMammmoth@users.noreply.github.com>
Co-authored-by: Myra <vasilis@pikachu.systems>
Co-authored-by: Whatstone <166147148+whatston3@users.noreply.github.com>
Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
Co-authored-by: K-Dynamic <20566341+K-Dynamic@users.noreply.github.com>
Co-authored-by: Gentleman-Bird <dcgreen406@gmail.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: BIGZi0348 <svalker0348@gmail.com>
Co-authored-by: LaCumbiaDelCoronavirus <90893484+LaCumbiaDelCoronavirus@users.noreply.github.com>
Co-authored-by: imatsoup <93290208+imatsoup@users.noreply.github.com>
Co-authored-by: Matthew Herber <32679887+happyrobot33@users.noreply.github.com>
Co-authored-by: Ertanic <36124833+Ertanic@users.noreply.github.com>
Co-authored-by: MissKay1994 <15877268+MissKay1994@users.noreply.github.com>
Co-authored-by: Errant <35878406+Errant-4@users.noreply.github.com>
Co-authored-by: eoineoineoin <helloworld@eoinrul.es>
Co-authored-by: Tiniest Shark <head.rebel@yahoo.com>
Co-authored-by: nikitosych <boriszyn@gmail.com>
Co-authored-by: Tayrtahn <tayrtahn@gmail.com>
Co-authored-by: Perry Fraser <perryprog@users.noreply.github.com>
Co-authored-by: YoungThug <ramialanbagy@gmail.com>
Co-authored-by: beck-thompson <107373427+beck-thompson@users.noreply.github.com>
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
Co-authored-by: Southbridge <7013162+southbridge-fur@users.noreply.github.com>
Co-authored-by: Vladislav Suchkov <20380250+murolem@users.noreply.github.com>
Co-authored-by: Prole <172158352+Prole0@users.noreply.github.com>
Co-authored-by: Unkn0wn_Gh0st <shadowstalkermll@gmail.com>
Co-authored-by: 3nderall <101940324+3nderall@users.noreply.github.com>
Co-authored-by: Radezolid <snappednexus@gmail.com>
Co-authored-by: J <billsmith116@gmail.com>
Co-authored-by: Ghagliiarghii <68826635+Ghagliiarghii@users.noreply.github.com>
Co-authored-by: chromiumboy <50505512+chromiumboy@users.noreply.github.com>
Co-authored-by: youtissoum <51883137+youtissoum@users.noreply.github.com>
Co-authored-by: Minemoder5000 <minemoder50000@gmail.com>
Co-authored-by: Spanky <scott@wearejacob.com>
Co-authored-by: Spessmann <156740760+Spessmann@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: brainfood1183 <113240905+brainfood1183@users.noreply.github.com>
Co-authored-by: Deerstop <edainturner@gmail.com>
Co-authored-by: B_Kirill <153602297+B-Kirill@users.noreply.github.com>
Co-authored-by: archee1 <archee3@hotmail.co.uk>
Co-authored-by: Cojoke <83733158+Cojoke-dot@users.noreply.github.com>
Co-authored-by: Quantum-cross <7065792+Quantum-cross@users.noreply.github.com>
Co-authored-by: poklj <compgeek223@gmail.com>
Co-authored-by: Krunklehorn <42424291+Krunklehorn@users.noreply.github.com>
Co-authored-by: OnyxTheBrave <131422822+OnyxTheBrave@users.noreply.github.com>
Co-authored-by: UpAndLeaves <92269094+Alpha-Two@users.noreply.github.com>
Co-authored-by: Flareguy <78941145+Flareguy@users.noreply.github.com>
Co-authored-by: Zalycon <84675130+Zalycon@users.noreply.github.com>
Co-authored-by: deltanedas <39013340+deltanedas@users.noreply.github.com>
Co-authored-by: Verm <32827189+Vermidia@users.noreply.github.com>
Co-authored-by: nikthechampiongr <32041239+nikthechampiongr@users.noreply.github.com>
Co-authored-by: ScarKy0 <scarky0@onet.eu>
Co-authored-by: Dmitry <57028746+dimm00n@users.noreply.github.com>
2025-05-24 11:00:17 +03:00

328 lines
13 KiB
C#

using Content.Server.Atmos.Monitor.Components;
using Content.Server.Atmos.Monitor.Systems;
using Content.Server.Atmos.Piping.Components;
using Content.Server.Atmos.Piping.Unary.Components;
using Content.Server.DeviceNetwork.Systems;
using Content.Server.Power.Generation.Teg;
using Content.Shared.Atmos.Monitor;
using Content.Shared.Atmos.Piping.Binary.Components;
using Content.Shared.Atmos.Piping.Unary.Components;
using Content.Shared.DeviceNetwork;
using Content.Shared.DeviceNetwork.Components;
using Content.Shared.DeviceNetwork.Events;
using Content.Shared.DeviceNetwork.Systems;
using Content.Shared.SensorMonitoring;
using Robust.Server.GameObjects;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Server.SensorMonitoring;
public sealed partial class SensorMonitoringConsoleSystem : EntitySystem
{
// TODO: THIS THING IS HEAVILY WIP AND NOT READY FOR GENERAL USE BY PLAYERS.
// Some of the issues, off the top of my head:
// Way too huge network load when opened
// UI doesn't update properly in cases like adding new streams/devices
// Deleting connected devices causes exceptions
// UI sucks. need a way to make basic dashboards like Grafana, and save them.
private EntityQuery<DeviceNetworkComponent> _deviceNetworkQuery;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly DeviceNetworkSystem _deviceNetwork = default!;
[Dependency] private readonly UserInterfaceSystem _userInterface = default!;
public override void Initialize()
{
base.Initialize();
InitUI();
UpdatesBefore.Add(typeof(UserInterfaceSystem));
SubscribeLocalEvent<SensorMonitoringConsoleComponent, DeviceListUpdateEvent>(DeviceListUpdated);
SubscribeLocalEvent<SensorMonitoringConsoleComponent, ComponentStartup>(ConsoleStartup);
SubscribeLocalEvent<SensorMonitoringConsoleComponent, DeviceNetworkPacketEvent>(DevicePacketReceived);
SubscribeLocalEvent<SensorMonitoringConsoleComponent, AtmosDeviceUpdateEvent>(AtmosUpdate);
_deviceNetworkQuery = GetEntityQuery<DeviceNetworkComponent>();
}
public override void Update(float frameTime)
{
var consoles = EntityQueryEnumerator<SensorMonitoringConsoleComponent>();
while (consoles.MoveNext(out var entityUid, out var comp))
{
UpdateConsole(entityUid, comp);
}
}
private void UpdateConsole(EntityUid uid, SensorMonitoringConsoleComponent comp)
{
var minTime = _gameTiming.CurTime - comp.RetentionTime;
SensorUpdate(uid, comp);
foreach (var data in comp.Sensors.Values)
{
// Cull old data.
foreach (var stream in data.Streams.Values)
{
while (stream.Samples.TryPeek(out var sample) && sample.Time < minTime)
{
stream.Samples.Dequeue();
}
}
}
UpdateConsoleUI(uid, comp);
}
private void ConsoleStartup(EntityUid uid, SensorMonitoringConsoleComponent component, ComponentStartup args)
{
if (TryComp(uid, out DeviceListComponent? network))
UpdateDevices(uid, component, network.Devices, Array.Empty<EntityUid>());
}
private void DeviceListUpdated(
EntityUid uid,
SensorMonitoringConsoleComponent component,
DeviceListUpdateEvent args)
{
UpdateDevices(uid, component, args.Devices, args.OldDevices);
}
private void UpdateDevices(
EntityUid uid,
SensorMonitoringConsoleComponent component,
IEnumerable<EntityUid> newDevices,
IEnumerable<EntityUid> oldDevices)
{
var kept = new HashSet<EntityUid>();
foreach (var newDevice in newDevices)
{
var deviceType = DetectDeviceType(newDevice);
if (deviceType == SensorDeviceType.Unknown)
continue;
kept.Add(newDevice);
var sensor = component.Sensors.GetOrNew(newDevice);
sensor.DeviceType = deviceType;
if (sensor.NetId == 0)
sensor.NetId = MakeNetId(component);
}
foreach (var oldDevice in oldDevices)
{
if (kept.Contains(oldDevice))
continue;
if (component.Sensors.TryGetValue(oldDevice, out var sensorData))
{
component.RemovedSensors.Add(sensorData.NetId);
component.Sensors.Remove(oldDevice);
}
}
}
private SensorDeviceType DetectDeviceType(EntityUid entity)
{
if (HasComp<TegGeneratorComponent>(entity))
return SensorDeviceType.Teg;
if (HasComp<AtmosMonitorComponent>(entity))
return SensorDeviceType.AtmosSensor;
if (HasComp<GasThermoMachineComponent>(entity))
return SensorDeviceType.ThermoMachine;
if (HasComp<GasVolumePumpComponent>(entity))
return SensorDeviceType.VolumePump;
if (HasComp<BatterySensorComponent>(entity))
return SensorDeviceType.Battery;
return SensorDeviceType.Unknown;
}
private void DevicePacketReceived(EntityUid uid, SensorMonitoringConsoleComponent component,
DeviceNetworkPacketEvent args)
{
if (!component.Sensors.TryGetValue(args.Sender, out var sensorData))
return;
if (!args.Data.TryGetValue(DeviceNetworkConstants.Command, out string? command))
return;
switch (sensorData.DeviceType)
{
case SensorDeviceType.Teg:
if (command != TegSystem.DeviceNetworkCommandSyncData)
return;
if (!args.Data.TryGetValue(TegSystem.DeviceNetworkCommandSyncData, out TegSensorData? tegData))
return;
// @formatter:off
WriteSample(component, sensorData, "teg_last_generated", SensorUnit.EnergyJ, tegData.LastGeneration);
WriteSample(component, sensorData, "teg_power", SensorUnit.PowerW, tegData.PowerOutput);
if (component.DebugStreams)
WriteSample(component, sensorData, "teg_ramp_pos", SensorUnit.PowerW, tegData.RampPosition);
WriteSample(component, sensorData, "teg_circ_a_in_pressure", SensorUnit.PressureKpa, tegData.CirculatorA.InletPressure);
WriteSample(component, sensorData, "teg_circ_a_in_temperature", SensorUnit.TemperatureK, tegData.CirculatorA.InletTemperature);
WriteSample(component, sensorData, "teg_circ_a_out_pressure", SensorUnit.PressureKpa, tegData.CirculatorA.OutletPressure);
WriteSample(component, sensorData, "teg_circ_a_out_temperature", SensorUnit.TemperatureK, tegData.CirculatorA.OutletTemperature);
WriteSample(component, sensorData, "teg_circ_b_in_pressure", SensorUnit.PressureKpa, tegData.CirculatorB.InletPressure);
WriteSample(component, sensorData, "teg_circ_b_in_temperature", SensorUnit.TemperatureK, tegData.CirculatorB.InletTemperature);
WriteSample(component, sensorData, "teg_circ_b_out_pressure", SensorUnit.PressureKpa, tegData.CirculatorB.OutletPressure);
WriteSample(component, sensorData, "teg_circ_b_out_temperature", SensorUnit.TemperatureK, tegData.CirculatorB.OutletTemperature);
// @formatter:on
break;
case SensorDeviceType.AtmosSensor:
if (command != AtmosDeviceNetworkSystem.SyncData)
return;
if (!args.Data.TryGetValue(AtmosDeviceNetworkSystem.SyncData, out AtmosSensorData? atmosData))
return;
// @formatter:off
WriteSample(component, sensorData, "atmo_pressure", SensorUnit.PressureKpa, atmosData.Pressure);
WriteSample(component, sensorData, "atmo_temperature", SensorUnit.TemperatureK, atmosData.Temperature);
// @formatter:on
break;
case SensorDeviceType.ThermoMachine:
if (command != AtmosDeviceNetworkSystem.SyncData)
return;
if (!args.Data.TryGetValue(AtmosDeviceNetworkSystem.SyncData, out GasThermoMachineData? thermoData))
return;
// @formatter:off
WriteSample(component, sensorData, "abs_energy_delta", SensorUnit.EnergyJ, MathF.Abs(thermoData.EnergyDelta));
// @formatter:on
break;
case SensorDeviceType.VolumePump:
if (command != AtmosDeviceNetworkSystem.SyncData)
return;
if (!args.Data.TryGetValue(AtmosDeviceNetworkSystem.SyncData, out GasVolumePumpData? volumePumpData))
return;
// @formatter:off
WriteSample(component, sensorData, "moles_transferred", SensorUnit.Moles, volumePumpData.LastMolesTransferred);
// @formatter:on
break;
case SensorDeviceType.Battery:
if (command != BatterySensorSystem.DeviceNetworkCommandSyncData)
return;
if (!args.Data.TryGetValue(BatterySensorSystem.DeviceNetworkCommandSyncData, out BatterySensorData? batteryData))
return;
// @formatter:off
WriteSample(component, sensorData, "charge", SensorUnit.EnergyJ, batteryData.Charge);
WriteSample(component, sensorData, "charge_max", SensorUnit.EnergyJ, batteryData.MaxCharge);
WriteSample(component, sensorData, "receiving", SensorUnit.PowerW, batteryData.Receiving);
WriteSample(component, sensorData, "receiving_max", SensorUnit.PowerW, batteryData.MaxReceiving);
WriteSample(component, sensorData, "supplying", SensorUnit.PowerW, batteryData.Supplying);
WriteSample(component, sensorData, "supplying_max", SensorUnit.PowerW, batteryData.MaxSupplying);
// @formatter:on
break;
}
}
private void WriteSample(
SensorMonitoringConsoleComponent component,
SensorMonitoringConsoleComponent.SensorData sensorData,
string streamName,
SensorUnit unit,
float value)
{
var stream = sensorData.Streams.GetOrNew(streamName);
stream.Unit = unit;
if (stream.NetId == 0)
stream.NetId = MakeNetId(component);
var time = _gameTiming.CurTime;
stream.Samples.Enqueue(new SensorSample(time, value));
}
private static int MakeNetId(SensorMonitoringConsoleComponent component)
{
return ++component.IdCounter;
}
private void AtmosUpdate(
EntityUid uid,
SensorMonitoringConsoleComponent comp,
AtmosDeviceUpdateEvent args)
{
foreach (var (ent, data) in comp.Sensors)
{
// Send network requests for new data!
NetworkPayload payload;
switch (data.DeviceType)
{
case SensorDeviceType.Teg:
payload = new NetworkPayload
{
[DeviceNetworkConstants.Command] = TegSystem.DeviceNetworkCommandSyncData
};
break;
case SensorDeviceType.AtmosSensor:
case SensorDeviceType.ThermoMachine:
case SensorDeviceType.VolumePump:
payload = new NetworkPayload
{
[DeviceNetworkConstants.Command] = AtmosDeviceNetworkSystem.SyncData
};
break;
default:
// Unknown device type, don't do anything.
continue;
}
var address = _deviceNetworkQuery.GetComponent(ent);
_deviceNetwork.QueuePacket(uid, address.Address, payload);
}
}
private void SensorUpdate(EntityUid uid, SensorMonitoringConsoleComponent comp)
{
foreach (var (ent, data) in comp.Sensors)
{
// Send network requests for new data!
NetworkPayload payload;
switch (data.DeviceType)
{
case SensorDeviceType.Battery:
payload = new NetworkPayload
{
[DeviceNetworkConstants.Command] = BatterySensorSystem.DeviceNetworkCommandSyncData
};
break;
default:
// Unknown device type, don't do anything.
continue;
}
var address = _deviceNetworkQuery.GetComponent(ent);
_deviceNetwork.QueuePacket(uid, address.Address, payload);
}
}
}