1
0

BatterySensorSystem.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using Content.Server.DeviceNetwork;
  2. using Content.Server.DeviceNetwork.Systems;
  3. using Content.Server.Power.Components;
  4. using Content.Shared.DeviceNetwork;
  5. namespace Content.Server.SensorMonitoring;
  6. public sealed class BatterySensorSystem : EntitySystem
  7. {
  8. public const string DeviceNetworkCommandSyncData = "bat_sync_data";
  9. [Dependency] private readonly DeviceNetworkSystem _deviceNetwork = default!;
  10. public override void Initialize()
  11. {
  12. SubscribeLocalEvent<BatterySensorComponent, DeviceNetworkPacketEvent>(PacketReceived);
  13. }
  14. private void PacketReceived(EntityUid uid, BatterySensorComponent component, DeviceNetworkPacketEvent args)
  15. {
  16. if (!args.Data.TryGetValue(DeviceNetworkConstants.Command, out string? cmd))
  17. return;
  18. switch (cmd)
  19. {
  20. case DeviceNetworkCommandSyncData:
  21. var battery = Comp<BatteryComponent>(uid);
  22. var netBattery = Comp<PowerNetworkBatteryComponent>(uid);
  23. var payload = new NetworkPayload
  24. {
  25. [DeviceNetworkConstants.Command] = DeviceNetworkCommandSyncData,
  26. [DeviceNetworkCommandSyncData] = new BatterySensorData(
  27. battery.CurrentCharge,
  28. battery.MaxCharge,
  29. netBattery.CurrentReceiving,
  30. netBattery.MaxChargeRate,
  31. netBattery.CurrentSupply,
  32. netBattery.MaxSupply)
  33. };
  34. _deviceNetwork.QueuePacket(uid, args.SenderAddress, payload);
  35. break;
  36. }
  37. }
  38. }