-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHubV1Controller.cs
More file actions
206 lines (171 loc) · 7.64 KB
/
HubV1Controller.cs
File metadata and controls
206 lines (171 loc) · 7.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Options;
using OpenShock.Common.Authentication;
using OpenShock.Common.Extensions;
using OpenShock.Common.Hubs;
using OpenShock.Common.Models;
using OpenShock.Common.Services.Ota;
using OpenShock.LiveControlGateway.LifetimeManager;
using OpenShock.LiveControlGateway.Options;
using OpenShock.Serialization.Deprecated.DoNotUse.V1;
using OpenShock.Serialization.Types;
using Semver;
using Serilog;
namespace OpenShock.LiveControlGateway.Controllers;
/// <summary>
/// Communication with the hubs aka ESP-32 microcontrollers
/// </summary>
[ApiController]
[ApiVersion("1")]
[Route("/{version:apiVersion}/ws/device")]
[Authorize(AuthenticationSchemes = OpenShockAuthSchemas.HubToken)]
public sealed class HubV1Controller : HubControllerBase<HubToGatewayMessage, GatewayToHubMessage>
{
private readonly IHubContext<UserHub, IUserHub> _userHubContext;
/// <summary>
/// DI
/// </summary>
/// <param name="hubLifetimeManager"></param>
/// <param name="userHubContext"></param>
/// <param name="serviceProvider"></param>
/// <param name="options"></param>
/// <param name="logger"></param>
public HubV1Controller(
HubLifetimeManager hubLifetimeManager,
IHubContext<UserHub, IUserHub> userHubContext,
IServiceProvider serviceProvider,
IOptions<LcgOptions> options,
ILogger<HubV1Controller> logger
)
: base(HubToGatewayMessage.Serializer, GatewayToHubMessage.Serializer, hubLifetimeManager, serviceProvider, options, logger)
{
_userHubContext = userHubContext;
}
private OtaUpdateStatus? _lastStatus;
private IUserHub HcOwner => _userHubContext.Clients.User(CurrentHub.Owner.ToString());
/// <inheritdoc />
protected override async Task<bool> Handle(HubToGatewayMessage data)
{
if(!data.Payload.HasValue) return false;
var payload = data.Payload.Value;
await using var scope = ServiceProvider.CreateAsyncScope();
var otaService = scope.ServiceProvider.GetRequiredService<IOtaService>();
Logger.LogTrace("Received payload [{Kind}] from hub [{HubId}]", payload.Kind, CurrentHub.Id);
switch (payload.Kind)
{
case HubToGatewayMessagePayload.ItemKind.KeepAlive:
if (!await SelfOnline(payload.KeepAlive.Uptime))
{
return false;
}
break;
case HubToGatewayMessagePayload.ItemKind.OtaInstallStarted:
_lastStatus = OtaUpdateStatus.Started;
await HcOwner.OtaInstallStarted(
CurrentHub.Id,
payload.OtaInstallStarted.UpdateId,
payload.OtaInstallStarted.Version!.ToSemVersion());
await otaService.Started(
CurrentHub.Id,
payload.OtaInstallStarted.UpdateId,
payload.OtaInstallStarted.Version!.ToSemVersion());
break;
case HubToGatewayMessagePayload.ItemKind.OtaInstallProgress:
await HcOwner.OtaInstallProgress(
CurrentHub.Id,
payload.OtaInstallProgress.UpdateId,
payload.OtaInstallProgress.Task,
payload.OtaInstallProgress.Progress);
if (_lastStatus == OtaUpdateStatus.Started)
{
_lastStatus = OtaUpdateStatus.Running;
await otaService.Progress(CurrentHub.Id, payload.OtaInstallProgress.UpdateId);
}
break;
case HubToGatewayMessagePayload.ItemKind.OtaInstallFailed:
await HcOwner.OtaInstallFailed(
CurrentHub.Id,
payload.OtaInstallFailed.UpdateId,
payload.OtaInstallFailed.Fatal,
payload.OtaInstallFailed.Message!);
await otaService.Error(CurrentHub.Id, payload.OtaInstallFailed.UpdateId,
payload.OtaInstallFailed.Fatal, payload.OtaInstallFailed.Message!);
_lastStatus = OtaUpdateStatus.Error;
break;
case HubToGatewayMessagePayload.ItemKind.BootStatus:
if (payload.BootStatus.BootType == FirmwareBootType.NewFirmware)
{
await HcOwner.OtaInstallSucceeded(
CurrentHub.Id, payload.BootStatus.OtaUpdateId);
await otaService.Success(CurrentHub.Id, payload.BootStatus.OtaUpdateId);
_lastStatus = OtaUpdateStatus.Finished;
break;
}
if (payload.BootStatus.BootType == FirmwareBootType.Rollback)
{
await HcOwner.OtaRollback(
CurrentHub.Id, payload.BootStatus.OtaUpdateId);
await otaService.Error(CurrentHub.Id, payload.BootStatus.OtaUpdateId, false,
"Hub booted with firmware rollback");
_lastStatus = OtaUpdateStatus.Error;
break;
}
if (payload.BootStatus.BootType == FirmwareBootType.Normal)
{
if (payload.BootStatus.OtaUpdateId == 0) break;
var unfinished = await otaService.UpdateUnfinished(CurrentHub.Id,
payload.BootStatus.OtaUpdateId);
if (!unfinished) break;
Log.Warning("OTA update unfinished, rolling back");
await HcOwner.OtaRollback(
CurrentHub.Id, payload.BootStatus.OtaUpdateId);
await otaService.Error(CurrentHub.Id, payload.BootStatus.OtaUpdateId, false,
"Hub booted with normal boot, update seems unfinished");
_lastStatus = OtaUpdateStatus.Error;
}
break;
case HubToGatewayMessagePayload.ItemKind.NONE:
default:
Logger.LogWarning("Payload kind not defined [{Kind}]", payload.Kind);
return false;
}
return true;
}
/// <inheritdoc />
public override ValueTask Control(List<OpenShock.Serialization.Gateway.ShockerCommand> controlCommands)
=> QueueMessage(new GatewayToHubMessage
{
Payload = new GatewayToHubMessagePayload(new ShockerCommandList
{
Commands = controlCommands.Select(x => new ShockerCommand()
{
Duration = x.Duration,
Type = x.Type,
Id = x.Model == Serialization.Types.ShockerModelType.Petrainer998DR ? (ushort)(x.Id >> 1) : x.Id, // Fix for old hubs, their ids was serialized wrongly in the RFTransmitter, the V1 endpoint is being phased out, so this wont stay here forever
Intensity = x.Intensity,
Model = x.Model
}).ToList()
})
});
/// <inheritdoc />
public override ValueTask CaptivePortal(bool enable)
=> QueueMessage(new GatewayToHubMessage
{
Payload = new GatewayToHubMessagePayload(new CaptivePortalConfig()
{
Enabled = enable
})
});
/// <inheritdoc />
public override ValueTask OtaInstall(SemVersion version)
=> QueueMessage(new GatewayToHubMessage
{
Payload = new GatewayToHubMessagePayload(new OtaInstall
{
Version = version.ToSemVer()
})
});
}