Files
tbd-station-14/Content.Server/DeviceNetwork/Systems/Devices/ApcNetSwitchSystem.cs
Julian Giebel 45caf25ea9 Apc device network and apc based light switch (#4908)
* Remove WireNet node group from cables
Implement extension cable components and system
Remove connection over distance logic from ApcPower... components

* Add extension cable components to prototypes

* Implement ApcNetwork
Implement ApcNetSwitch

* Fix ignoredComponents.cs

* Add friend attribute to new components

* Add construction graph for a light switch

* Address reviews

* Fix broken test

* Move ConnectionType enum to DeviceNetworkComponent
Change netId data definition to use the ConnectionType enum values

Co-authored-by: Julian Giebel <j.giebel@netrocks.info>
2021-10-23 16:23:19 -07:00

55 lines
2.3 KiB
C#

using Content.Server.DeviceNetwork.Components;
using Content.Server.DeviceNetwork.Components.Devices;
using Content.Shared.Interaction;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.DeviceNetwork.Systems.Devices
{
public sealed class ApcNetSwitchSystem : EntitySystem
{
[Dependency] private readonly DeviceNetworkSystem _deviceNetworkSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ApcNetSwitchComponent, InteractHandEvent>(OnInteracted);
SubscribeLocalEvent<ApcNetSwitchComponent, PacketSentEvent>(OnPackedReceived);
}
/// <summary>
/// Toggles the state of the switch and sents a <see cref="DeviceNetworkConstants.CmdSetState"/> command with the
/// <see cref="DeviceNetworkConstants.StateEnabled"/> value set to state.
/// </summary>
private void OnInteracted(EntityUid uid, ApcNetSwitchComponent component, InteractHandEvent args)
{
if (!EntityManager.TryGetComponent(uid, out DeviceNetworkComponent? networkComponent)) return;
component.State = !component.State;
var payload = new NetworkPayload
{
[DeviceNetworkConstants.Command] = DeviceNetworkConstants.CmdSetState,
[DeviceNetworkConstants.StateEnabled] = component.State,
};
_deviceNetworkSystem.QueuePacket(uid, DeviceNetworkConstants.NullAddress, networkComponent.Frequency, payload, true);
args.Handled = true;
}
/// <summary>
/// Listens to the <see cref="DeviceNetworkConstants.CmdSetState"/> command of other switches to sync state
/// </summary>
private void OnPackedReceived(EntityUid uid, ApcNetSwitchComponent component, PacketSentEvent args)
{
if (!EntityManager.TryGetComponent(uid, out DeviceNetworkComponent? networkComponent) || args.SenderAddress == networkComponent.Address) return;
if (!args.Data.TryGetValue(DeviceNetworkConstants.Command, out string? command) || command != DeviceNetworkConstants.CmdSetState) return;
if (!args.Data.TryGetValue(DeviceNetworkConstants.StateEnabled, out bool enabled)) return;
component.State = enabled;
}
}
}