Skip to content

Commit

Permalink
Add space heaters (space-wizards#25250)
Browse files Browse the repository at this point in the history
  • Loading branch information
Menshin committed Feb 28, 2024
1 parent 221719c commit 9884351
Show file tree
Hide file tree
Showing 21 changed files with 770 additions and 15 deletions.
90 changes: 90 additions & 0 deletions Content.Client/Atmos/UI/SpaceHeaterBoundUserInterface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Content.Shared.Atmos.Piping.Portable.Components;
using JetBrains.Annotations;
using Robust.Client.UserInterface.Controls;

namespace Content.Client.Atmos.UI;

/// <summary>
/// Initializes a <see cref="SpaceHeaterWindow"/> and updates it when new server messages are received.
/// </summary>
[UsedImplicitly]
public sealed class SpaceHeaterBoundUserInterface : BoundUserInterface
{
[ViewVariables]
private SpaceHeaterWindow? _window;

public SpaceHeaterBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
}

protected override void Open()
{
base.Open();

_window = new SpaceHeaterWindow();

if (State != null)
UpdateState(State);

_window.OpenCentered();

_window.OnClose += Close;

_window.ToggleStatusButton.OnPressed += _ => OnToggleStatusButtonPressed();
_window.IncreaseTempRange.OnPressed += _ => OnTemperatureRangeChanged(_window.TemperatureChangeDelta);
_window.DecreaseTempRange.OnPressed += _ => OnTemperatureRangeChanged(-_window.TemperatureChangeDelta);
_window.ModeSelector.OnItemSelected += OnModeChanged;

_window.PowerLevelSelector.OnItemSelected += OnPowerLevelChange;
}

private void OnToggleStatusButtonPressed()
{
_window?.SetActive(!_window.Active);
SendMessage(new SpaceHeaterToggleMessage());
}

private void OnTemperatureRangeChanged(float changeAmount)
{
SendMessage(new SpaceHeaterChangeTemperatureMessage(changeAmount));
}

private void OnModeChanged(OptionButton.ItemSelectedEventArgs args)
{
_window?.ModeSelector.SelectId(args.Id);
SendMessage(new SpaceHeaterChangeModeMessage((SpaceHeaterMode)args.Id));
}

private void OnPowerLevelChange(RadioOptionItemSelectedEventArgs<int> args)
{
_window?.PowerLevelSelector.Select(args.Id);
SendMessage(new SpaceHeaterChangePowerLevelMessage((SpaceHeaterPowerLevel)args.Id));
}

/// <summary>
/// Update the UI state based on server-sent info
/// </summary>
/// <param name="state"></param>
protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);
if (_window == null || state is not SpaceHeaterBoundUserInterfaceState cast)
return;

_window.SetActive(cast.Enabled);
_window.ModeSelector.SelectId((int)cast.Mode);
_window.PowerLevelSelector.Select((int)cast.PowerLevel);

_window.MinTemp = cast.MinTemperature;
_window.MaxTemp = cast.MaxTemperature;
_window.SetTemperature(cast.TargetTemperature);
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing)
return;
_window?.Dispose();
}
}
34 changes: 34 additions & 0 deletions Content.Client/Atmos/UI/SpaceHeaterWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<DefaultWindow xmlns="https://spacestation14.io"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MinSize="280 160" Title="Temperature Control Unit">

<BoxContainer Name="VboxContainer" Orientation="Vertical" Margin="5 5 5 5" SeparationOverride="10">

<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<Button Text="{Loc comp-space-heater-ui-status-disabled}" Access="Public" Name="ToggleStatusButton"/>
</BoxContainer>
<BoxContainer Orientation="Horizontal" SeparationOverride="5">
<Label Text="{Loc comp-space-heater-ui-mode}"/>
<OptionButton Access="Public" Name="ModeSelector"/>
</BoxContainer>
</BoxContainer>

<BoxContainer Orientation="Horizontal" HorizontalExpand="True" SeparationOverride="5">
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<Label Text="{Loc comp-space-heater-ui-thermostat}"/>
</BoxContainer>
<BoxContainer Orientation="Horizontal" HorizontalExpand="True" HorizontalAlignment="Right">
<Button Text="{Loc comp-space-heater-ui-decrease-temperature-range}" Access="Public" Name="DecreaseTempRange" StyleClasses="OpenRight"/>
<LineEdit Name ="Thermostat" MinSize="55 0"></LineEdit>
<Button Text="{Loc comp-space-heater-ui-increase-temperature-range}" Access="Public" Name="IncreaseTempRange" StyleClasses="OpenLeft"/>
</BoxContainer>
</BoxContainer>

<BoxContainer Orientation="Horizontal" HorizontalExpand="True" SeparationOverride="5">
<Label Text="{Loc comp-space-heater-ui-power-consumption}"/>
<BoxContainer Name="PowerLevelSelectorHBox" Access="Public" SeparationOverride="2"/>
</BoxContainer>

</BoxContainer>
</DefaultWindow>
73 changes: 73 additions & 0 deletions Content.Client/Atmos/UI/SpaceHeaterWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Piping.Portable.Components;

namespace Content.Client.Atmos.UI;

/// <summary>
/// Client-side UI used to control a space heater.
/// </summary>
[GenerateTypedNameReferences]
public sealed partial class SpaceHeaterWindow : DefaultWindow
{
// To account for a minimum delta temperature for atmos equalization to trigger we use a fixed step for target temperature increment/decrement
public int TemperatureChangeDelta = 5;
public bool Active;

// Temperatures range bounds in Kelvin (K)
public float MinTemp;
public float MaxTemp;

public RadioOptions<int> PowerLevelSelector;

public SpaceHeaterWindow()
{
RobustXamlLoader.Load(this);

// Add the Mode selector list
foreach (var value in Enum.GetValues<SpaceHeaterMode>())
{
ModeSelector.AddItem(Loc.GetString($"comp-space-heater-mode-{value}"), (int)value);
}

// Add the Power level radio buttons
PowerLevelSelectorHBox.AddChild(PowerLevelSelector = new RadioOptions<int>(RadioOptionsLayout.Horizontal));
PowerLevelSelector.FirstButtonStyle = "OpenRight";
PowerLevelSelector.LastButtonStyle = "OpenLeft";
PowerLevelSelector.ButtonStyle = "OpenBoth";
foreach (var value in Enum.GetValues<SpaceHeaterPowerLevel>())
{
PowerLevelSelector.AddItem(Loc.GetString($"comp-space-heater-ui-{value}-power-consumption"), (int)value);
}

// Only allow temperature increment/decrement of TemperatureChangeDelta
Thermostat.Editable = false;
}

public void SetActive(bool active)
{
Active = active;
ToggleStatusButton.Pressed = active;

if (active)
{
ToggleStatusButton.Text = Loc.GetString("comp-space-heater-ui-status-enabled");
}
else
{
ToggleStatusButton.Text = Loc.GetString("comp-space-heater-ui-status-disabled");
}
}

public void SetTemperature(float targetTemperature)
{
Thermostat.SetText($"{targetTemperature - Atmospherics.T0C} °C");

IncreaseTempRange.Disabled = targetTemperature + TemperatureChangeDelta > MaxTemp;
DecreaseTempRange.Disabled = targetTemperature - TemperatureChangeDelta < MinTemp;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ private void OnBeforeOpened(Entity<GasThermoMachineComponent> ent, ref BeforeAct

private void OnThermoMachineUpdated(EntityUid uid, GasThermoMachineComponent thermoMachine, ref AtmosDeviceUpdateEvent args)
{
thermoMachine.LastEnergyDelta = 0f;
if (!(_power.IsPowered(uid) && TryComp<ApcPowerReceiverComponent>(uid, out var receiver)))
return;

Expand Down Expand Up @@ -100,12 +101,14 @@ private void OnThermoMachineUpdated(EntityUid uid, GasThermoMachineComponent the
if (thermoMachine.Atmospheric)
{
_atmosphereSystem.AddHeat(heatExchangeGasMixture, dQActual);
thermoMachine.LastEnergyDelta = dQActual;
}
else
{
float dQLeak = dQActual * thermoMachine.EnergyLeakPercentage;
float dQPipe = dQActual - dQLeak;
_atmosphereSystem.AddHeat(heatExchangeGasMixture, dQPipe);
thermoMachine.LastEnergyDelta = dQPipe;

if (dQLeak != 0f && _atmosphereSystem.GetContainingMixture(uid) is { } containingMixture)
_atmosphereSystem.AddHeat(containingMixture, dQLeak);
Expand Down
58 changes: 58 additions & 0 deletions Content.Server/Atmos/Portable/SpaceHeaterComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Content.Shared.Atmos;
using Content.Shared.Atmos.Piping.Portable.Components;
using Content.Shared.Atmos.Visuals;

namespace Content.Server.Atmos.Portable;

[RegisterComponent]
public sealed partial class SpaceHeaterComponent : Component
{
/// <summary>
/// Current mode the space heater is in. Possible values : Auto, Heat and Cool
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
public SpaceHeaterMode Mode = SpaceHeaterMode.Auto;

/// <summary>
/// The power level the space heater is currently set to. Possible values : Low, Medium, High
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
public SpaceHeaterPowerLevel PowerLevel = SpaceHeaterPowerLevel.Medium;

/// <summary>
/// Maximum target temperature the device can be set to
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
public float MaxTemperature = Atmospherics.T20C + 20;

/// <summary>
/// Minimal target temperature the device can be set to
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
public float MinTemperature = Atmospherics.T0C - 10;

/// <summary>
/// Coefficient of performance. Output power / input power.
/// Positive for heaters, negative for freezers.
/// </summary>
[DataField("heatingCoefficientOfPerformance")]
[ViewVariables(VVAccess.ReadWrite)]
public float HeatingCp = 1f;

[DataField("coolingCoefficientOfPerformance")]
[ViewVariables(VVAccess.ReadWrite)]
public float CoolingCp = -0.9f;

/// <summary>
/// The delta from the target temperature after which the space heater switch mode while in Auto. Value should account for the thermomachine temperature tolerance.
/// </summary>
[DataField]
[ViewVariables(VVAccess.ReadWrite)]
public float AutoModeSwitchThreshold = 0.8f;

/// <summary>
/// Current electrical power consumption, in watts, of the space heater at medium power level. Passed to the thermomachine component.
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
public float PowerConsumption = 3500f;
}
Loading

0 comments on commit 9884351

Please sign in to comment.