Skip to content

Commit a72530d

Browse files
committed
Refactor command executor usage.
1 parent 83df913 commit a72530d

22 files changed

Lines changed: 192 additions & 90 deletions

File tree

‎App/HA4IoT.WebApp/cache.manifest‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CACHE MANIFEST
2-
# Version 1.17.169
2+
# Version 1.17.170
33

44
CACHE:
55

‎Controllers/Examples/HA4IoT.Controller.Main/Main/Rooms/KitchenConfiguration.cs‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace HA4IoT.Controller.Main.Main.Rooms
2222
{
2323
internal class KitchenConfiguration
2424
{
25+
private readonly ISystemEventsService _systemEventsService;
2526
private readonly IAreaRegistryService _areaService;
2627
private readonly IDeviceRegistryService _deviceService;
2728
private readonly CCToolsDeviceService _ccToolsBoardService;
@@ -64,6 +65,7 @@ public enum Kitchen
6465
}
6566

6667
public KitchenConfiguration(
68+
ISystemEventsService systemEventsService,
6769
IAreaRegistryService areaService,
6870
IDeviceRegistryService deviceService,
6971
CCToolsDeviceService ccToolsDeviceService,
@@ -72,6 +74,7 @@ public KitchenConfiguration(
7274
ActuatorFactory actuatorFactory,
7375
SensorFactory sensorFactory)
7476
{
77+
_systemEventsService = systemEventsService ?? throw new ArgumentNullException(nameof(systemEventsService));
7578
_areaService = areaService ?? throw new ArgumentNullException(nameof(areaService));
7679
_deviceService = deviceService ?? throw new ArgumentNullException(nameof(deviceService));
7780
_ccToolsBoardService = ccToolsDeviceService ?? throw new ArgumentNullException(nameof(ccToolsDeviceService));
@@ -118,6 +121,11 @@ public void Apply()
118121
_actuatorFactory.RegisterSocket(area, Kitchen.SocketCeiling1, hspe8[HSPE8Pin.GPIO3].WithInvertedState());
119122
_actuatorFactory.RegisterSocket(area, Kitchen.SocketCeiling2, hspe8[HSPE8Pin.GPIO4].WithInvertedState());
120123

124+
_systemEventsService.StartupCompleted += (s, e) =>
125+
{
126+
area.GetComponent(Kitchen.SocketCeiling1).TryTurnOn();
127+
};
128+
121129
_actuatorFactory.RegisterRollerShutter(area, Kitchen.RollerShutter, hsrel5[HSREL5Pin.Relay4], hsrel5[HSREL5Pin.Relay3]);
122130

123131
_sensorFactory.RegisterButton(area, Kitchen.ButtonKitchenette, input1.GetInput(11));
@@ -134,7 +142,12 @@ public void Apply()
134142
area.GetRollerShutter(Kitchen.RollerShutter).ConnectWith(
135143
area.GetButton(Kitchen.RollerShutterButtonUp), area.GetButton(Kitchen.RollerShutterButtonDown));
136144

137-
area.GetButton(Kitchen.RollerShutterButtonUp).PressedLongTrigger.Attach(() => area.GetComponent(Kitchen.LightKitchenette).TryTogglePowerState());
145+
area.GetButton(Kitchen.RollerShutterButtonUp).PressedLongTrigger.Attach(() =>
146+
{
147+
var light = area.GetComponent(Kitchen.LightKitchenette);
148+
light.TryTogglePowerState();
149+
light.TrySetColor(0D, 0D, 1D);
150+
});
138151

139152
_actuatorFactory.RegisterLogicalComponent(area, Kitchen.CombinedAutomaticLights)
140153
.WithComponent(area.GetLamp(Kitchen.LightCeilingWall))

‎Controllers/HA4IoT.Simulator/Controls/UIMotionDetectorAdapter.cs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ public class UIMotionDetectorAdapter : IMotionDetectorAdapter
1111

1212
public UIMotionDetectorAdapter(CheckBox checkBox)
1313
{
14-
if (checkBox == null) throw new ArgumentNullException(nameof(checkBox));
15-
16-
_checkBox = checkBox;
14+
_checkBox = checkBox ?? throw new ArgumentNullException(nameof(checkBox));
1715
}
1816

1917
public event EventHandler MotionDetectionBegin;
2018
public event EventHandler MotionDetectionEnd;
2119

20+
public void Refresh()
21+
{
22+
}
23+
2224
public void Connect()
2325
{
2426
_checkBox.Checked += (s, e) => OnMotionDetectionBegin();

‎Core/HA4IoT.Contracts/Adapters/IMotionDetectorAdapter.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ public interface IMotionDetectorAdapter
77
event EventHandler MotionDetectionBegin;
88

99
event EventHandler MotionDetectionEnd;
10+
11+
void Refresh();
1012
}
1113
}

‎Core/HA4IoT.Networking/Http/HttpClientSession.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ private string GenerateWebSocketAccept(HttpContext httpContext)
162162

163163
public void Dispose()
164164
{
165+
_httpRequestReader?.Dispose();
165166
_inputStream?.Dispose();
166167
_outputStream?.Dispose();
167168
_client?.Dispose();

‎Core/HA4IoT.Networking/Http/HttpRequestReader.cs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
namespace HA4IoT.Networking.Http
99
{
10-
public class HttpRequestReader
10+
public sealed class HttpRequestReader : IDisposable
1111
{
1212
private readonly MemoryStream _buffer = new MemoryStream();
13-
private readonly byte[] _chunkBuffer = new byte[64 * 1024]; // 64 KB
13+
private readonly byte[] _chunkBuffer = new byte[8 * 1024]; // 8 KB
1414
private readonly Stream _stream;
1515

1616
private HttpRequest _request;
@@ -197,5 +197,10 @@ private void Reset()
197197
_buffer.Position = 0;
198198
_buffer.SetLength(0);
199199
}
200+
201+
public void Dispose()
202+
{
203+
_buffer.Dispose();
204+
}
200205
}
201206
}

‎Core/HA4IoT.Tests/Mockups/Adapters/TestMotionDetectorAdapter.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ public class TestMotionDetectorAdapter : IMotionDetectorAdapter
88
public event EventHandler MotionDetectionBegin;
99
public event EventHandler MotionDetectionEnd;
1010

11+
public void Refresh()
12+
{
13+
}
14+
1115
public void Begin()
1216
{
1317
MotionDetectionBegin?.Invoke(this, EventArgs.Empty);

‎Core/HA4IoT/Actuators/Fans/Fan.cs‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,32 @@
77
using HA4IoT.Contracts.Components;
88
using HA4IoT.Contracts.Components.Features;
99
using HA4IoT.Contracts.Components.States;
10-
using HA4IoT.Contracts.Core;
1110
using HA4IoT.Contracts.Hardware;
1211
using HA4IoT.Contracts.Services.Settings;
1312

1413
namespace HA4IoT.Actuators.Fans
1514
{
1615
public class Fan : ComponentBase, IFan
1716
{
18-
private readonly ISettingsService _settingsService;
1917
private readonly object _syncRoot = new object();
18+
19+
private readonly CommandExecutor _commandExecutor = new CommandExecutor();
2020
private readonly IFanAdapter _adapter;
21+
private readonly ISettingsService _settingsService;
2122

2223
private int _currentLevel;
2324

2425
public Fan(string id, IFanAdapter adapter, ISettingsService settingsService) : base(id)
2526
{
2627
_adapter = adapter ?? throw new ArgumentNullException(nameof(adapter));
2728
_settingsService = settingsService ?? throw new ArgumentNullException(nameof(settingsService));
29+
30+
_commandExecutor.Register<ResetCommand>(c => ResetState());
31+
_commandExecutor.Register<TurnOffCommand>(c => SetLevelInternal(0));
32+
_commandExecutor.Register<TurnOnCommand>(c => SetLevelInternal(GetLevelFeature().DefaultActiveLevel));
33+
_commandExecutor.Register<SetLevelCommand>(c => SetLevelInternal(c.Level));
34+
_commandExecutor.Register<IncreaseLevelCommand>(c => SetLevelInternal(_currentLevel + 1));
35+
_commandExecutor.Register<DecreaseLevelCommand>(c => SetLevelInternal(_currentLevel - 1));
2836
}
2937

3038
public override IComponentFeatureStateCollection GetState()
@@ -53,19 +61,18 @@ public override void ExecuteCommand(ICommand command)
5361
{
5462
if (command == null) throw new ArgumentNullException(nameof(command));
5563

56-
var commandExecutor = new CommandExecutor();
57-
commandExecutor.Register<ResetCommand>(c => ResetState());
58-
commandExecutor.Register<TurnOffCommand>(c => SetLevelInternal(0));
59-
commandExecutor.Register<TurnOnCommand>(c => SetLevelInternal(GetLevelFeature().DefaultActiveLevel));
60-
commandExecutor.Register<SetLevelCommand>(c => SetLevelInternal(c.Level));
61-
commandExecutor.Register<IncreaseLevelCommand>(c => SetLevelInternal(_currentLevel + 1));
62-
commandExecutor.Register<DecreaseLevelCommand>(c => SetLevelInternal(_currentLevel - 1));
63-
commandExecutor.Execute(command);
64+
lock (_syncRoot)
65+
{
66+
_commandExecutor.Execute(command);
67+
}
6468
}
6569

6670
public void ResetState()
6771
{
68-
SetLevelInternal(0, true);
72+
lock (_syncRoot)
73+
{
74+
SetLevelInternal(0, true);
75+
}
6976
}
7077

7178
private LevelFeature GetLevelFeature()

‎Core/HA4IoT/Actuators/Lamps/Lamp.cs‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ namespace HA4IoT.Actuators.Lamps
1313
{
1414
public class Lamp : ComponentBase, ILamp
1515
{
16-
private readonly ILampAdapter _adapter;
16+
private readonly object _syncRoot = new object();
1717

18+
private readonly CommandExecutor _commandExecutor = new CommandExecutor();
19+
private readonly ILampAdapter _adapter;
20+
1821
private PowerStateValue _powerState = PowerStateValue.Off;
1922
private ColorState _colorState;
2023

@@ -27,6 +30,16 @@ public Lamp(string id, ILampAdapter adapter)
2730
{
2831
_colorState = new ColorState();
2932
}
33+
34+
_commandExecutor.Register<ResetCommand>(c => ResetState());
35+
_commandExecutor.Register<TurnOnCommand>(c => SetStateInternal(PowerStateValue.On, _colorState));
36+
_commandExecutor.Register<TurnOffCommand>(c => SetStateInternal(PowerStateValue.Off, _colorState));
37+
_commandExecutor.Register<TogglePowerStateCommand>(c => TogglePowerState());
38+
39+
if (_adapter.SupportsColor)
40+
{
41+
_commandExecutor.Register<SetColorCommand>(c => SetStateInternal(_powerState, GenerateColorState(c)));
42+
}
3043
}
3144

3245
public override IComponentFeatureStateCollection GetState()
@@ -59,23 +72,18 @@ public override void ExecuteCommand(ICommand command)
5972
{
6073
if (command == null) throw new ArgumentNullException(nameof(command));
6174

62-
var commandExecutor = new CommandExecutor();
63-
commandExecutor.Register<ResetCommand>(c => ResetState());
64-
commandExecutor.Register<TurnOnCommand>(c => SetStateInternal(PowerStateValue.On, _colorState));
65-
commandExecutor.Register<TurnOffCommand>(c => SetStateInternal(PowerStateValue.Off, _colorState));
66-
commandExecutor.Register<TogglePowerStateCommand>(c => TogglePowerState());
67-
68-
if (_adapter.SupportsColor)
75+
lock (_syncRoot)
6976
{
70-
commandExecutor.Register<SetColorCommand>(c => SetStateInternal(_powerState, GenerateColorState(c)));
77+
_commandExecutor.Execute(command);
7178
}
72-
73-
commandExecutor.Execute(command);
7479
}
7580

7681
public void ResetState()
7782
{
78-
SetStateInternal(PowerStateValue.Off, _colorState, true);
83+
lock (_syncRoot)
84+
{
85+
SetStateInternal(PowerStateValue.Off, _colorState, true);
86+
}
7987
}
8088

8189
private void TogglePowerState()

‎Core/HA4IoT/Actuators/RollerShutters/RollerShutter.cs‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace HA4IoT.Actuators.RollerShutters
1818
public class RollerShutter : ComponentBase, IRollerShutter
1919
{
2020
private readonly object _syncRoot = new object();
21+
private readonly CommandExecutor _commandExecutor = new CommandExecutor();
2122
private readonly IRollerShutterAdapter _adapter;
2223
private readonly Timeout _autoOffTimeout;
2324

@@ -42,6 +43,11 @@ public RollerShutter(
4243
timerService.Tick += (s, e) => TrackPosition(e);
4344

4445
settingsService.CreateSettingsMonitor<RollerShutterSettings>(this, s => Settings = s.NewSettings);
46+
47+
_commandExecutor.Register<MoveUpCommand>(c => MoveUp());
48+
_commandExecutor.Register<MoveDownCommand>(c => MoveDown());
49+
_commandExecutor.Register<TurnOffCommand>(c => Stop());
50+
_commandExecutor.Register<ResetCommand>(c => MoveUp(true));
4551
}
4652

4753
public RollerShutterSettings Settings { get; set; }
@@ -68,12 +74,7 @@ public override void ExecuteCommand(ICommand command)
6874

6975
lock (_syncRoot)
7076
{
71-
var commandExecutor = new CommandExecutor();
72-
commandExecutor.Register<MoveUpCommand>(c => MoveUp());
73-
commandExecutor.Register<MoveDownCommand>(c => MoveDown());
74-
commandExecutor.Register<TurnOffCommand>(c => Stop());
75-
commandExecutor.Register<ResetCommand>(c => MoveUp(true));
76-
commandExecutor.Execute(command);
77+
_commandExecutor.Execute(command);
7778
}
7879
}
7980

0 commit comments

Comments
 (0)