1
0

CrewMonitoringConsoleSystem.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System.Linq;
  2. using Content.Server.DeviceNetwork;
  3. using Content.Server.DeviceNetwork.Systems;
  4. using Content.Server.PowerCell;
  5. using Content.Shared.Medical.CrewMonitoring;
  6. using Content.Shared.Medical.SuitSensor;
  7. using Content.Shared.Pinpointer;
  8. using Robust.Server.GameObjects;
  9. namespace Content.Server.Medical.CrewMonitoring;
  10. public sealed class CrewMonitoringConsoleSystem : EntitySystem
  11. {
  12. [Dependency] private readonly PowerCellSystem _cell = default!;
  13. [Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
  14. public override void Initialize()
  15. {
  16. base.Initialize();
  17. SubscribeLocalEvent<CrewMonitoringConsoleComponent, ComponentRemove>(OnRemove);
  18. SubscribeLocalEvent<CrewMonitoringConsoleComponent, DeviceNetworkPacketEvent>(OnPacketReceived);
  19. SubscribeLocalEvent<CrewMonitoringConsoleComponent, BoundUIOpenedEvent>(OnUIOpened);
  20. }
  21. private void OnRemove(EntityUid uid, CrewMonitoringConsoleComponent component, ComponentRemove args)
  22. {
  23. component.ConnectedSensors.Clear();
  24. }
  25. private void OnPacketReceived(EntityUid uid, CrewMonitoringConsoleComponent component, DeviceNetworkPacketEvent args)
  26. {
  27. var payload = args.Data;
  28. // Check command
  29. if (!payload.TryGetValue(DeviceNetworkConstants.Command, out string? command))
  30. return;
  31. if (command != DeviceNetworkConstants.CmdUpdatedState)
  32. return;
  33. if (!payload.TryGetValue(SuitSensorConstants.NET_STATUS_COLLECTION, out Dictionary<string, SuitSensorStatus>? sensorStatus))
  34. return;
  35. component.ConnectedSensors = sensorStatus;
  36. UpdateUserInterface(uid, component);
  37. }
  38. private void OnUIOpened(EntityUid uid, CrewMonitoringConsoleComponent component, BoundUIOpenedEvent args)
  39. {
  40. if (!_cell.TryUseActivatableCharge(uid))
  41. return;
  42. UpdateUserInterface(uid, component);
  43. }
  44. private void UpdateUserInterface(EntityUid uid, CrewMonitoringConsoleComponent? component = null)
  45. {
  46. if (!Resolve(uid, ref component))
  47. return;
  48. if (!_uiSystem.IsUiOpen(uid, CrewMonitoringUIKey.Key))
  49. return;
  50. // The grid must have a NavMapComponent to visualize the map in the UI
  51. var xform = Transform(uid);
  52. if (xform.GridUid != null)
  53. EnsureComp<NavMapComponent>(xform.GridUid.Value);
  54. // Update all sensors info
  55. var allSensors = component.ConnectedSensors.Values.ToList();
  56. _uiSystem.SetUiState(uid, CrewMonitoringUIKey.Key, new CrewMonitoringState(allSensors));
  57. }
  58. }