<commit_before>using System.Linq;
using Content.Server.Atmos.Monitor.Components;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Atmos.Piping.EntitySystems;
using Content.Server.Atmos.Piping.Components;
using Content.Server.DeviceNetwork;
using Content.Server.DeviceNetwork.Components;
using Content.Server.DeviceNetwork.Systems;
using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Monitor;
using Content.Shared.Tag;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;

namespace Content.Server.Atmos.Monitor.Systems
{
    // AtmosMonitorSystem. Grabs all the AtmosAlarmables connected
    // to it via local APC net, and starts sending updates of the
    // current atmosphere. Monitors fire (which always triggers as
    // a danger), and atmos (which triggers based on set thresholds).
    public sealed class AtmosMonitorSystem : EntitySystem
    {
        [Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
        [Dependency] private readonly AtmosDeviceSystem _atmosDeviceSystem = default!;
        [Dependency] private readonly DeviceNetworkSystem _deviceNetSystem = default!;
        [Dependency] private readonly IPrototypeManager _prototypeManager = default!;

        // Commands
        public const string AtmosMonitorSetThresholdCmd = "atmos_monitor_set_threshold";

        // Packet data
        public const string AlertTypes = "atmos_monitor_alert_types";

        public const string AtmosMonitorThresholdData = "atmos_monitor_threshold_data";

        public const string AtmosMonitorThresholdDataType = "atmos_monitor_threshold_type";

        public const string AtmosMonitorThresholdGasType = "atmos_monitor_threshold_gas";

        public override void Initialize()
        {
            SubscribeLocalEvent<AtmosMonitorComponent, ComponentInit>(OnAtmosMonitorInit);
            SubscribeLocalEvent<AtmosMonitorComponent, ComponentStartup>(OnAtmosMonitorStartup);
            SubscribeLocalEvent<AtmosMonitorComponent, AtmosDeviceUpdateEvent>(OnAtmosUpdate);
            SubscribeLocalEvent<AtmosMonitorComponent, TileFireEvent>(OnFireEvent);
            SubscribeLocalEvent<AtmosMonitorComponent, PowerChangedEvent>(OnPowerChangedEvent);
            SubscribeLocalEvent<AtmosMonitorComponent, BeforePacketSentEvent>(BeforePacketRecv);
            SubscribeLocalEvent<AtmosMonitorComponent, DeviceNetworkPacketEvent>(OnPacketRecv);
        }

        private void OnAtmosMonitorInit(EntityUid uid, AtmosMonitorComponent component, ComponentInit args)
        {
            if (component.TemperatureThresholdId != null)
                component.TemperatureThreshold = new(_prototypeManager.Index<AtmosAlarmThreshold>(component.TemperatureThresholdId));

            if (component.PressureThresholdId != null)
                component.PressureThreshold = new(_prototypeManager.Index<AtmosAlarmThreshold>(component.PressureThresholdId));

            if (component.GasThresholdIds != null)
            {
                component.GasThresholds = new();
                foreach (var (gas, id) in component.GasThresholdIds)
                    if (_prototypeManager.TryIndex<AtmosAlarmThreshold>(id, out var gasThreshold))
                        component.GasThresholds.Add(gas, new(gasThreshold));
            }
        }

        private void OnAtmosMonitorStartup(EntityUid uid, AtmosMonitorComponent component, ComponentStartup args)
        {
            if (!HasComp<ApcPowerReceiverComponent>(uid)
                && TryComp<AtmosDeviceComponent>(uid, out var atmosDeviceComponent))
            {
                _atmosDeviceSystem.LeaveAtmosphere(atmosDeviceComponent);
                return;
            }
        }

        private void BeforePacketRecv(EntityUid uid, AtmosMonitorComponent component, BeforePacketSentEvent args)
        {
            if (!component.NetEnabled) args.Cancel();
        }

        private void OnPacketRecv(EntityUid uid, AtmosMonitorComponent component, DeviceNetworkPacketEvent args)
        {
            // sync the internal 'last alarm state' from
            // the other alarms, so that we can calculate
            // the highest network alarm state at any time
            if (!args.Data.TryGetValue(DeviceNetworkConstants.Command, out string? cmd))
            {
                return;
            }

            switch (cmd)
            {
                case AtmosDeviceNetworkSystem.RegisterDevice:
                    component.RegisteredDevices.Add(args.SenderAddress);
                    break;
                case AtmosAlarmableSystem.ResetAll:
                    Reset(uid);
                    // Don't clear alarm states here.
                    break;
                case AtmosMonitorSetThresholdCmd:
                    if (args.Data.TryGetValue(AtmosMonitorThresholdData, out AtmosAlarmThreshold? thresholdData)
                        && args.Data.TryGetValue(AtmosMonitorThresholdDataType, out AtmosMonitorThresholdType? thresholdType))
                    {
                        args.Data.TryGetValue(AtmosMonitorThresholdGasType, out Gas? gas);
                        SetThreshold(uid, thresholdType.Value, thresholdData, gas);
                    }
<commit_msg>ResetAll no longer clears the alarm state for an atmos monitor<commit_after>using System.Linq;
using Content.Server.Atmos.Monitor.Components;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Atmos.Piping.EntitySystems;
using Content.Server.Atmos.Piping.Components;
using Content.Server.DeviceNetwork;
using Content.Server.DeviceNetwork.Components;
using Content.Server.DeviceNetwork.Systems;
using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Monitor;
using Content.Shared.Tag;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;

namespace Content.Server.Atmos.Monitor.Systems
{
    // AtmosMonitorSystem. Grabs all the AtmosAlarmables connected
    // to it via local APC net, and starts sending updates of the
    // current atmosphere. Monitors fire (which always triggers as
    // a danger), and atmos (which triggers based on set thresholds).
    public sealed class AtmosMonitorSystem : EntitySystem
    {
        [Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
        [Dependency] private readonly AtmosDeviceSystem _atmosDeviceSystem = default!;
        [Dependency] private readonly DeviceNetworkSystem _deviceNetSystem = default!;
        [Dependency] private readonly IPrototypeManager _prototypeManager = default!;

        // Commands
        public const string AtmosMonitorSetThresholdCmd = "atmos_monitor_set_threshold";

        // Packet data
        public const string AlertTypes = "atmos_monitor_alert_types";

        public const string AtmosMonitorThresholdData = "atmos_monitor_threshold_data";

        public const string AtmosMonitorThresholdDataType = "atmos_monitor_threshold_type";

        public const string AtmosMonitorThresholdGasType = "atmos_monitor_threshold_gas";

        public override void Initialize()
        {
            SubscribeLocalEvent<AtmosMonitorComponent, ComponentInit>(OnAtmosMonitorInit);
            SubscribeLocalEvent<AtmosMonitorComponent, ComponentStartup>(OnAtmosMonitorStartup);
            SubscribeLocalEvent<AtmosMonitorComponent, AtmosDeviceUpdateEvent>(OnAtmosUpdate);
            SubscribeLocalEvent<AtmosMonitorComponent, TileFireEvent>(OnFireEvent);
            SubscribeLocalEvent<AtmosMonitorComponent, PowerChangedEvent>(OnPowerChangedEvent);
            SubscribeLocalEvent<AtmosMonitorComponent, BeforePacketSentEvent>(BeforePacketRecv);
            SubscribeLocalEvent<AtmosMonitorComponent, DeviceNetworkPacketEvent>(OnPacketRecv);
        }

        private void OnAtmosMonitorInit(EntityUid uid, AtmosMonitorComponent component, ComponentInit args)
        {
            if (component.TemperatureThresholdId != null)
                component.TemperatureThreshold = new(_prototypeManager.Index<AtmosAlarmThreshold>(component.TemperatureThresholdId));

            if (component.PressureThresholdId != null)
                component.PressureThreshold = new(_prototypeManager.Index<AtmosAlarmThreshold>(component.PressureThresholdId));

            if (component.GasThresholdIds != null)
            {
                component.GasThresholds = new();
                foreach (var (gas, id) in component.GasThresholdIds)
                    if (_prototypeManager.TryIndex<AtmosAlarmThreshold>(id, out var gasThreshold))
                        component.GasThresholds.Add(gas, new(gasThreshold));
            }
        }

        private void OnAtmosMonitorStartup(EntityUid uid, AtmosMonitorComponent component, ComponentStartup args)
        {
            if (!HasComp<ApcPowerReceiverComponent>(uid)
                && TryComp<AtmosDeviceComponent>(uid, out var atmosDeviceComponent))
            {
                _atmosDeviceSystem.LeaveAtmosphere(atmosDeviceComponent);
                return;
            }
        }

        private void BeforePacketRecv(EntityUid uid, AtmosMonitorComponent component, BeforePacketSentEvent args)
        {
            if (!component.NetEnabled) args.Cancel();
        }

        private void OnPacketRecv(EntityUid uid, AtmosMonitorComponent component, DeviceNetworkPacketEvent args)
        {
            // sync the internal 'last alarm state' from
            // the other alarms, so that we can calculate
            // the highest network alarm state at any time
            if (!args.Data.TryGetValue(DeviceNetworkConstants.Command, out string? cmd))
            {
                return;
            }

            switch (cmd)
            {
                case AtmosDeviceNetworkSystem.RegisterDevice:
                    component.RegisteredDevices.Add(args.SenderAddress);
                    break;
                case AtmosMonitorSetThresholdCmd:
                    if (args.Data.TryGetValue(AtmosMonitorThresholdData, out AtmosAlarmThreshold? thresholdData)
                        && args.Data.TryGetValue(AtmosMonitorThresholdDataType, out AtmosMonitorThresholdType? thresholdType))
                    {
                        args.Data.TryGetValue(AtmosMonitorThresholdGasType, out Gas? gas);
                        SetThreshold(uid, thresholdType.Value, thresholdData, gas);
                    }

{'fraction_non_alphanumeric': 0.06960950764006792, 'fraction_numerical': 0.0, 'mean_word_length': 2.8626593806921674, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3870465', 'n_tokens_mistral': 2607, 'n_tokens_neox': 2500, 'n_words': 563}