1
0

EdgeDetectorSystem.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using Content.Server.DeviceLinking.Components;
  2. using Content.Server.DeviceNetwork;
  3. using SignalReceivedEvent = Content.Server.DeviceLinking.Events.SignalReceivedEvent;
  4. namespace Content.Server.DeviceLinking.Systems;
  5. public sealed class EdgeDetectorSystem : EntitySystem
  6. {
  7. [Dependency] private readonly DeviceLinkSystem _deviceLink = default!;
  8. public override void Initialize()
  9. {
  10. base.Initialize();
  11. SubscribeLocalEvent<EdgeDetectorComponent, ComponentInit>(OnInit);
  12. SubscribeLocalEvent<EdgeDetectorComponent, SignalReceivedEvent>(OnSignalReceived);
  13. }
  14. private void OnInit(EntityUid uid, EdgeDetectorComponent comp, ComponentInit args)
  15. {
  16. _deviceLink.EnsureSinkPorts(uid, comp.InputPort);
  17. _deviceLink.EnsureSourcePorts(uid, comp.OutputHighPort, comp.OutputLowPort);
  18. }
  19. private void OnSignalReceived(EntityUid uid, EdgeDetectorComponent comp, ref SignalReceivedEvent args)
  20. {
  21. // only handle signals with edges
  22. var state = SignalState.Momentary;
  23. if (args.Data == null ||
  24. !args.Data.TryGetValue(DeviceNetworkConstants.LogicState, out state) ||
  25. state == SignalState.Momentary)
  26. return;
  27. if (args.Port != comp.InputPort)
  28. return;
  29. // make sure the level changed, multiple devices sending the same level are treated as one spamming
  30. if (comp.State != state)
  31. {
  32. comp.State = state;
  33. var port = state == SignalState.High ? comp.OutputHighPort : comp.OutputLowPort;
  34. _deviceLink.InvokePort(uid, port);
  35. }
  36. }
  37. }