forked from opennetty/opennetty-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenNettyCoordinator.cs
More file actions
2031 lines (1803 loc) · 122 KB
/
Copy pathOpenNettyCoordinator.cs
File metadata and controls
2031 lines (1803 loc) · 122 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/opennetty/opennetty-core for more information concerning
* the license and the contributors participating to this project.
*/
using System.Globalization;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using Microsoft.Extensions.Logging;
using static OpenNetty.OpenNettyEvents;
namespace OpenNetty;
/// <summary>
/// Contains the logic necessary to infer high-level events from incoming
/// and outgoing notifications dispatched by the OpenNetty pipeline.
/// </summary>
public sealed class OpenNettyCoordinator : IOpenNettyHandler
{
private readonly OpenNettyController _controller;
private readonly OpenNettyEvents _events;
private readonly ILogger<OpenNettyCoordinator> _logger;
private readonly OpenNettyManager _manager;
private readonly IOpenNettyPipeline _pipeline;
/// <summary>
/// Creates a new instance of the <see cref="OpenNettyCoordinator"/> class.
/// </summary>
/// <param name="controller">The OpenNetty controller.</param>
/// <param name="events">The OpenNetty events.</param>
/// <param name="logger">The OpenNetty logger.</param>
/// <param name="manager">The OpenNetty manager.</param>
/// <param name="pipeline">The OpenNetty pipeline.</param>
public OpenNettyCoordinator(
OpenNettyController controller,
OpenNettyEvents events,
ILogger<OpenNettyCoordinator> logger,
OpenNettyManager manager,
IOpenNettyPipeline pipeline)
{
_controller = controller ?? throw new ArgumentNullException(nameof(controller));
_events = events ?? throw new ArgumentNullException(nameof(events));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_manager = manager ?? throw new ArgumentNullException(nameof(manager));
_pipeline = pipeline ?? throw new ArgumentNullException(nameof(pipeline));
}
/// <inheritdoc/>
async ValueTask<IAsyncDisposable> IOpenNettyHandler.SubscribeAsync() => StableCompositeAsyncDisposable.Create(
[
// Note: this event handler is responsible for monitoring incoming and outgoing frames to detect state
// changes affecting - directly or indirectly (e.g via a Nitoo PnL scenario) - registered endpoints.
await _pipeline.SelectMany(static notification => notification switch
{
OpenNettyNotifications.MessageReceived {
Session.Type: OpenNettySessionType.Generic,
Message: {
Protocol: OpenNettyProtocol.Nitoo,
Type : OpenNettyMessageType.BusCommand or
OpenNettyMessageType.DimensionRead or
OpenNettyMessageType.DimensionSet,
Address : not null,
Mode : OpenNettyMode.Broadcast } message }
=> AsyncObservable.Return<(OpenNettyNotification Notification, OpenNettyMessage Message)>((notification, message)),
OpenNettyNotifications.MessageReceived {
Session.Type: OpenNettySessionType.Event,
Message: {
Protocol: OpenNettyProtocol.Scs,
Type : OpenNettyMessageType.BusCommand or
OpenNettyMessageType.DimensionRead or
OpenNettyMessageType.DimensionSet,
Address : not null } message }
=> AsyncObservable.Return<(OpenNettyNotification Notification, OpenNettyMessage Message)>((notification, message)),
OpenNettyNotifications.MessageReceived {
Session.Type: OpenNettySessionType.Generic,
Message: {
Protocol: OpenNettyProtocol.Zigbee,
Type : OpenNettyMessageType.BusCommand or
OpenNettyMessageType.DimensionRead or
OpenNettyMessageType.DimensionSet,
Address : not null } message }
=> AsyncObservable.Return<(OpenNettyNotification Notification, OpenNettyMessage Message)>((notification, message)),
// Note: unlike SCS (and Zigbee devices when the supervision mode is enabled), Nitoo devices
// never report back state changes to the OpenWebNet gateway when the change originates from
// the gateway itself. To ensure events are correctly reported, the outgoing Nitoo BUS COMMAND
// and DIMENSION SET messages that have been acknowledged by the gateway (and optionally validated
// by the remote device using a special "VALID ACTION" BUS COMMAND message) are monitored here.
OpenNettyNotifications.MessageSent {
Session.Type: OpenNettySessionType.Generic,
Message: {
Protocol: OpenNettyProtocol.Nitoo,
Type : OpenNettyMessageType.BusCommand or OpenNettyMessageType.DimensionSet,
Address : not null } message }
=> AsyncObservable.Return<(OpenNettyNotification Notification, OpenNettyMessage Message)>((notification, message)),
_ => AsyncObservable.Empty<(OpenNettyNotification Notification, OpenNettyMessage Message)>()
})
.Do(async arguments =>
{
// Important: to ensure the order of events is preserved, this RX event handler processes incoming and outgoing
// messages sequentially. As such, it is critical that this asynchronous method completes as quickly as possible.
switch (arguments)
{
// The switch state and the brightness level of an endpoint can be inferred from 6 types of messages:
//
// - For Nitoo devices, from an incoming or outgoing "OFF" or "ON" BUS COMMAND message.
// - For SCS/Zigbee devices, from an incoming "OFF" or "ON" - parameterized or not - BUS COMMAND message.
// - For SCS/Zigbee devices, from an incoming "ON%" BUS COMMAND message.
// - For SCS/Zigbee devices, from an incoming "DIMMER SPEED LEVEL" or "DIMMER STATUS" DIMENSION READ message.
// - For Nitoo devices, from an incoming "UNIT DESCRIPTION" DIMENSION READ message.
// - For Nitoo devices, from an outgoing "DIMMER SPEED LEVEL" DIMENSION SET message.
case (OpenNettyNotification notification,
OpenNettyMessage { Protocol: OpenNettyProtocol.Nitoo,
Type : OpenNettyMessageType.BusCommand,
Command : OpenNettyCommand command,
Address : OpenNettyAddress address,
Mode : OpenNettyMode mode })
when command == OpenNettyCommands.Lighting.Off || command == OpenNettyCommands.Lighting.On:
{
// Ignore the message if the corresponding endpoint couldn't be resolved or if it
// was received by a different gateway than the one associated with the endpoint.
var endpoint = await _manager.FindEndpointByAddressAsync(address);
if (endpoint is null || (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway))
{
return;
}
// If the message was received and was emitted by the source using
// a broadcast transmission, it is considered as an ON/OFF scenario.
if (notification is OpenNettyNotifications.MessageReceived && mode is OpenNettyMode.Broadcast &&
endpoint.HasCapability(OpenNettyCapabilities.OnOffScenario))
{
if (command == OpenNettyCommands.Lighting.On)
{
await _events.PublishAsync(new OnScenarioReportedEventArgs(endpoint));
}
else
{
await _events.PublishAsync(new OffScenarioReportedEventArgs(endpoint));
}
}
List<Task> tasks = [];
// Note: outgoing ON/OFF commands sent using broadcast or multicast transmission
// generally don't affect the local output of a Nitoo lighting device. As such,
// the switch state/brightness of the endpoint is only reported if the message was
// received (e.g by a different unit on the same device) or was sent in unicast.
if (mode is OpenNettyMode.Unicast || notification is OpenNettyNotifications.MessageReceived)
{
if (endpoint.HasCapability(OpenNettyCapabilities.OnOffSwitchState) &&
endpoint.GetStringSetting(OpenNettySettings.ActuatorType) is null or OpenNettySettings.ActuatorTypes.Lighting)
{
tasks.Add(ReportStateAsync(endpoint, CancellationToken.None).AsTask());
}
if (endpoint is { Protocol: OpenNettyProtocol.Nitoo, Unit.Definition.AssociatedUnitId: byte unit })
{
tasks.Add(Task.Run(async () =>
{
var endpoint = await _manager.FindEndpointByAddressAsync(OpenNettyAddress.FromNitooAddress(
OpenNettyAddress.ToNitooAddress(address).Identifier, unit));
if (endpoint is not null &&
endpoint.HasCapability(OpenNettyCapabilities.OnOffSwitchState) &&
endpoint.GetStringSetting(OpenNettySettings.ActuatorType) is null or OpenNettySettings.ActuatorTypes.Lighting)
{
await ReportStateAsync(endpoint, CancellationToken.None);
}
}));
}
}
if (mode is OpenNettyMode.Broadcast or OpenNettyMode.Multicast && endpoint is { Unit.Scenarios: [_, ..] scenarios })
{
var endpoints = scenarios.ToAsyncEnumerable()
.Where(static scenario => scenario.FunctionCode is < 105)
.SelectAwait(scenario => _manager.FindEndpointByNameAsync(scenario.EndpointName))
.Where(static endpoint => endpoint is { Protocol: OpenNettyProtocol.Nitoo, Unit: OpenNettyUnit })
.OfType<OpenNettyEndpoint>()
.Where(static endpoint => endpoint.HasCapability(OpenNettyCapabilities.OnOffSwitchState))
.Where(static endpoint => endpoint.GetStringSetting(OpenNettySettings.ActuatorType) is
null or OpenNettySettings.ActuatorTypes.Lighting);
tasks.Add(Parallel.ForEachAsync(endpoints, ReportStateAsync));
}
await Task.WhenAll(tasks);
async ValueTask ReportStateAsync(OpenNettyEndpoint endpoint, CancellationToken cancellationToken)
{
if (command == OpenNettyCommands.Lighting.On)
{
await _events.PublishAsync(new SwitchStateReportedEventArgs(endpoint,
OpenNettyModels.Lighting.SwitchState.On), cancellationToken);
// Note: if the endpoint was configured to use the push-button mode, dispatch an OFF state
// event immediately after switching it on (or receiving a notification indicating it was
// switched on), as Nitoo devices using this mode don't automatically report the OFF state.
if (endpoint.GetStringSetting(OpenNettySettings.SwitchMode) is OpenNettySettings.SwitchModes.PushButton)
{
await _events.PublishAsync(new SwitchStateReportedEventArgs(endpoint,
OpenNettyModels.Lighting.SwitchState.Off), cancellationToken);
}
}
else
{
await _events.PublishAsync(new SwitchStateReportedEventArgs(endpoint,
OpenNettyModels.Lighting.SwitchState.Off), cancellationToken);
}
// Note: for Nitoo devices supporting dimming, an ON command always changes the brightness to 100%.
if (command == OpenNettyCommands.Lighting.On && (endpoint.HasCapability(OpenNettyCapabilities.BasicDimmingState) ||
endpoint.HasCapability(OpenNettyCapabilities.AdvancedDimmingState)))
{
await _events.PublishAsync(new BrightnessReportedEventArgs(endpoint, 100), cancellationToken);
}
}
break;
}
case (OpenNettyNotifications.MessageReceived,
OpenNettyMessage { Protocol: OpenNettyProtocol.Scs or OpenNettyProtocol.Zigbee,
Type : OpenNettyMessageType.BusCommand,
Command : OpenNettyCommand command,
Address : OpenNettyAddress address })
// Note: ON/OFF BUS COMMAND frames can be parameterized.
when command.Category == OpenNettyCategories.Lighting &&
(command.Value == OpenNettyCommands.Lighting.On.Value ||
command.Value == OpenNettyCommands.Lighting.Off.Value):
{
await Parallel.ForEachAsync(_manager.FindEndpointsByAddressAsync(address), async (endpoint, cancellationToken) =>
{
// Ignore the message if it was received by a different gateway than the one associated with the endpoint.
if (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway)
{
return;
}
// SCS devices configured to use the PUL mode never react to area and general commands.
if (address.Type is OpenNettyAddressType.ScsLightPointArea or OpenNettyAddressType.ScsLightPointGeneral &&
endpoint.GetStringSetting(OpenNettySettings.SwitchMode) is OpenNettySettings.SwitchModes.PushButton)
{
return;
}
if (endpoint.HasCapability(OpenNettyCapabilities.OnOffSwitchState) &&
endpoint.GetStringSetting(OpenNettySettings.ActuatorType) is null or OpenNettySettings.ActuatorTypes.Lighting)
{
await _events.PublishAsync(new SwitchStateReportedEventArgs(endpoint,
command.Value == OpenNettyCommands.Lighting.On.Value ?
OpenNettyModels.Lighting.SwitchState.On :
OpenNettyModels.Lighting.SwitchState.Off), cancellationToken);
}
});
break;
}
case (OpenNettyNotifications.MessageReceived,
OpenNettyMessage { Protocol: OpenNettyProtocol.Scs or OpenNettyProtocol.Zigbee,
Type : OpenNettyMessageType.BusCommand,
Command : OpenNettyCommand command,
Address : OpenNettyAddress address })
when command == OpenNettyCommands.Lighting.On20 ||
command == OpenNettyCommands.Lighting.On30 ||
command == OpenNettyCommands.Lighting.On40 ||
command == OpenNettyCommands.Lighting.On50 ||
command == OpenNettyCommands.Lighting.On60 ||
command == OpenNettyCommands.Lighting.On70 ||
command == OpenNettyCommands.Lighting.On80 ||
command == OpenNettyCommands.Lighting.On90 ||
command == OpenNettyCommands.Lighting.On100:
{
await Parallel.ForEachAsync(_manager.FindEndpointsByAddressAsync(address), async (endpoint, cancellationToken) =>
{
// Ignore the message if it was received by a different gateway than the one associated with the endpoint.
if (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway)
{
return;
}
// SCS devices configured to use the PUL mode never react to area and general commands.
if (address.Type is OpenNettyAddressType.ScsLightPointArea or OpenNettyAddressType.ScsLightPointGeneral &&
endpoint.GetStringSetting(OpenNettySettings.SwitchMode) is OpenNettySettings.SwitchModes.PushButton)
{
return;
}
if (endpoint.GetStringSetting(OpenNettySettings.ActuatorType) is not (null or OpenNettySettings.ActuatorTypes.Lighting))
{
return;
}
if (endpoint.HasCapability(OpenNettyCapabilities.OnOffSwitchState))
{
await _events.PublishAsync(new SwitchStateReportedEventArgs(endpoint,
OpenNettyModels.Lighting.SwitchState.On), cancellationToken);
}
// Note: brightness level changes reported via ON% BUS COMMAND frames are deliberately
// ignored for endpoints that support advanced dimming, as this method often gives very
// imprecise results that are inconsistent with the brightness level retrieved using a
// "DIMMER LEVEL SPEED" or "DIMMER STATUS" DIMENSION REQUEST frame (e.g when setting the
// brightness to 30%, a F418U2 SCS dimmer correctly reports the "130" value when using
// a DIMENSION REQUEST but returns "5" (50%) when using a STATUS REQUEST). To avoid that,
// a specialized event handler is responsible for monitoring ON% BUS COMMAND frames and
// retrieving the exact brightness level using a "DIMMER LEVEL SPEED" DIMENSION REQUEST.
if (endpoint.HasCapability(OpenNettyCapabilities.BasicDimmingState) &&
!endpoint.HasCapability(OpenNettyCapabilities.AdvancedDimmingState))
{
await _events.PublishAsync(new BrightnessReportedEventArgs(endpoint, (byte)
(command == OpenNettyCommands.Lighting.On20 ? 20 :
command == OpenNettyCommands.Lighting.On30 ? 30 :
command == OpenNettyCommands.Lighting.On40 ? 40 :
command == OpenNettyCommands.Lighting.On50 ? 50 :
command == OpenNettyCommands.Lighting.On60 ? 60 :
command == OpenNettyCommands.Lighting.On70 ? 70 :
command == OpenNettyCommands.Lighting.On80 ? 80 :
command == OpenNettyCommands.Lighting.On90 ? 90 : 100)), cancellationToken);
}
});
break;
}
case (OpenNettyNotifications.MessageReceived,
OpenNettyMessage { Protocol : OpenNettyProtocol.Scs or OpenNettyProtocol.Zigbee,
Type : OpenNettyMessageType.DimensionRead,
Address : OpenNettyAddress address,
Dimension: OpenNettyDimension dimension,
Values : [{ Length: > 0 } value, ..] })
when dimension == OpenNettyDimensions.Lighting.DimmerLevelSpeed ||
dimension == OpenNettyDimensions.Lighting.DimmerStatus:
{
await Parallel.ForEachAsync(_manager.FindEndpointsByAddressAsync(address), async (endpoint, cancellationToken) =>
{
// Ignore the message if it was received by a different gateway than the one associated with the endpoint.
if (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway)
{
return;
}
// SCS devices configured to use the PUL mode never react to area and general commands.
if (address.Type is OpenNettyAddressType.ScsLightPointArea or OpenNettyAddressType.ScsLightPointGeneral &&
endpoint.GetStringSetting(OpenNettySettings.SwitchMode) is OpenNettySettings.SwitchModes.PushButton)
{
return;
}
if (endpoint.GetStringSetting(OpenNettySettings.ActuatorType) is not (null or OpenNettySettings.ActuatorTypes.Lighting))
{
return;
}
var level = (byte) (byte.Parse(value, CultureInfo.InvariantCulture) - 100);
if (endpoint.HasCapability(OpenNettyCapabilities.OnOffSwitchState))
{
await _events.PublishAsync(new SwitchStateReportedEventArgs(endpoint, level is not 0 ?
OpenNettyModels.Lighting.SwitchState.On :
OpenNettyModels.Lighting.SwitchState.Off), cancellationToken);
}
// Note: the special brightness level "0" always indicates that the output is switched off.
// To avoid overriding the last known level (which is typically restored by SCS devices when
// receiving an ON command), the brightness level is only reported if it's higher than zero.
if (level is not 0 && (endpoint.HasCapability(OpenNettyCapabilities.BasicDimmingState) ||
endpoint.HasCapability(OpenNettyCapabilities.AdvancedDimmingState)))
{
await _events.PublishAsync(new BrightnessReportedEventArgs(endpoint, level), cancellationToken);
}
});
break;
}
case (OpenNettyNotifications.MessageReceived,
OpenNettyMessage { Protocol : OpenNettyProtocol.Nitoo,
Type : OpenNettyMessageType.DimensionRead,
Address : OpenNettyAddress address,
Dimension: OpenNettyDimension dimension,
Values : ["129", { Length: > 0 } value] })
when dimension == OpenNettyDimensions.Diagnostics.UnitDescription:
{
// Ignore the message if the corresponding endpoint couldn't be resolved or if it
// was received by a different gateway than the one associated with the endpoint.
var endpoint = await _manager.FindEndpointByAddressAsync(address);
if (endpoint is null || (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway))
{
return;
}
if (endpoint.HasCapability(OpenNettyCapabilities.OnOffSwitchState) &&
endpoint.GetStringSetting(OpenNettySettings.ActuatorType) is null or OpenNettySettings.ActuatorTypes.Lighting)
{
await _events.PublishAsync(new SwitchStateReportedEventArgs(endpoint, value is "128" or "129" or "130" ?
OpenNettyModels.Lighting.SwitchState.On :
OpenNettyModels.Lighting.SwitchState.Off));
}
break;
}
case (OpenNettyNotifications.MessageReceived,
OpenNettyMessage { Protocol : OpenNettyProtocol.Nitoo,
Type : OpenNettyMessageType.DimensionRead,
Address : OpenNettyAddress address,
Dimension: OpenNettyDimension dimension,
Values : ["143", { Length: > 0 } value, ..] })
when dimension == OpenNettyDimensions.Diagnostics.UnitDescription:
{
// Ignore the message if the corresponding endpoint couldn't be resolved or if it
// was received by a different gateway than the one associated with the endpoint.
var endpoint = await _manager.FindEndpointByAddressAsync(address);
if (endpoint is null || (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway))
{
return;
}
var level = byte.Parse(value, CultureInfo.InvariantCulture);
if (endpoint.GetStringSetting(OpenNettySettings.ActuatorType) is not (null or OpenNettySettings.ActuatorTypes.Lighting))
{
return;
}
if (endpoint.HasCapability(OpenNettyCapabilities.OnOffSwitchState))
{
await _events.PublishAsync(new SwitchStateReportedEventArgs(endpoint, level is not 0 ?
OpenNettyModels.Lighting.SwitchState.On :
OpenNettyModels.Lighting.SwitchState.Off));
}
// Note: the special brightness level "0" always indicates that the output is switched off.
// While Nitoo devices normally don't restore the last known brightness level, the brightness
// level is only reported if it's higher than zero for consistency with MyHome/SCS devices.
if (level is not 0 && (endpoint.HasCapability(OpenNettyCapabilities.BasicDimmingState) ||
endpoint.HasCapability(OpenNettyCapabilities.AdvancedDimmingState)))
{
await _events.PublishAsync(new BrightnessReportedEventArgs(endpoint, level));
}
break;
}
// Note: outgoing dimmer level commands sent using broadcast or multicast transmission
// generally don't affect the local output of a Nitoo lighting device. As such, the switch
// state/brightness of the endpoint is only reported if the message was sent in unicast.
case (OpenNettyNotifications.MessageReceived or OpenNettyNotifications.MessageSent,
OpenNettyMessage { Protocol : OpenNettyProtocol.Nitoo,
Type : OpenNettyMessageType.DimensionSet,
Address : OpenNettyAddress address,
Mode : OpenNettyMode.Unicast,
Dimension: OpenNettyDimension dimension,
Values : [{ Length: > 0 } value, ..] })
when dimension == OpenNettyDimensions.Lighting.DimmerLevelSpeed:
{
// Ignore the message if the corresponding endpoint couldn't be resolved or if it
// was received by a different gateway than the one associated with the endpoint.
var endpoint = await _manager.FindEndpointByAddressAsync(address);
if (endpoint is null || (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway))
{
return;
}
if (endpoint.GetStringSetting(OpenNettySettings.ActuatorType) is not (null or OpenNettySettings.ActuatorTypes.Lighting))
{
return;
}
var level = (byte) Math.Round(decimal.Parse(value, CultureInfo.InvariantCulture), MidpointRounding.AwayFromZero);
if (endpoint.HasCapability(OpenNettyCapabilities.OnOffSwitchState))
{
await _events.PublishAsync(new SwitchStateReportedEventArgs(endpoint, level is not 0 ?
OpenNettyModels.Lighting.SwitchState.On :
OpenNettyModels.Lighting.SwitchState.Off));
}
// Note: the special brightness level "0" always indicates that the output is switched off.
// While Nitoo devices normally don't restore the last known brightness level, the brightness
// level is only reported if it's higher than zero for consistency with MyHome/SCS devices.
if (level is not 0 && (endpoint.HasCapability(OpenNettyCapabilities.BasicDimmingState) ||
endpoint.HasCapability(OpenNettyCapabilities.AdvancedDimmingState)))
{
await _events.PublishAsync(new BrightnessReportedEventArgs(endpoint, level));
}
break;
}
case (OpenNettyNotifications.MessageReceived,
OpenNettyMessage { Protocol: OpenNettyProtocol.Zigbee,
Type : OpenNettyMessageType.BusCommand,
Command : OpenNettyCommand command,
Address : OpenNettyAddress address })
when command == OpenNettyCommands.Lighting.Toggle:
{
// Ignore the message if the corresponding endpoint couldn't be resolved or if it
// was received by a different gateway than the one associated with the endpoint.
var endpoint = await _manager.FindEndpointByAddressAsync(address);
if (endpoint is null || (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway))
{
return;
}
if (endpoint.HasCapability(OpenNettyCapabilities.ToggleScenario))
{
await _events.PublishAsync(new ToggleScenarioReportedEventArgs(endpoint));
}
break;
}
// The shutter state and the position of an endpoint can be inferred from 4 types of messages:
//
// - For Nitoo devices, from an incoming or outgoing "STOP", "UP" or "DOWN" BUS COMMAND message.
// - For SCS/Zigbee devices, from an incoming "STOP", "UP" or "DOWN" BUS COMMAND message.
// - For SCS/Zigbee devices, from an incoming "SHUTTER STATUS" DIMENSION READ message.
// - For Nitoo devices, from an incoming "UNIT DESCRIPTION" DIMENSION READ message.
case (OpenNettyNotification notification,
OpenNettyMessage { Protocol: OpenNettyProtocol.Nitoo,
Type : OpenNettyMessageType.BusCommand,
Command : OpenNettyCommand command,
Address : OpenNettyAddress address,
Mode : OpenNettyMode mode })
when command == OpenNettyCommands.Automation.Stop ||
command == OpenNettyCommands.Automation.Up ||
command == OpenNettyCommands.Automation.Down:
{
// Ignore the message if the corresponding endpoint couldn't be resolved or if it
// was received by a different gateway than the one associated with the endpoint.
var endpoint = await _manager.FindEndpointByAddressAsync(address);
if (endpoint is null || (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway))
{
return;
}
// If the message was received and was emitted by the source using a
// broadcast transmission, it is considered as an STOP/UP/DOWN scenario.
if (notification is OpenNettyNotifications.MessageReceived && mode is OpenNettyMode.Broadcast &&
endpoint.HasCapability(OpenNettyCapabilities.StopUpDownScenario))
{
if (command == OpenNettyCommands.Automation.Stop)
{
await _events.PublishAsync(new ShutterStopScenarioReportedEventArgs(endpoint));
}
else if (command == OpenNettyCommands.Automation.Up)
{
await _events.PublishAsync(new ShutterUpScenarioReportedEventArgs(endpoint));
}
else
{
await _events.PublishAsync(new ShutterDownScenarioReportedEventArgs(endpoint));
}
}
List<Task> tasks = [];
// Note: outgoing STOP/UP/DOWN commands sent using broadcast or multicast transmission
// generally don't affect the local output of a Nitoo automation device. As such,
// the shutter state/position of the endpoint is only reported if the message was
// received (e.g by a different unit on the same device) or was sent in unicast.
if (mode is OpenNettyMode.Unicast || notification is OpenNettyNotifications.MessageReceived)
{
if (endpoint.HasCapability(OpenNettyCapabilities.BasicShutterState) &&
endpoint.GetStringSetting(OpenNettySettings.ActuatorType) is null or OpenNettySettings.ActuatorTypes.Automation)
{
tasks.Add(ReportStateAsync(endpoint, CancellationToken.None).AsTask());
}
if (endpoint is { Protocol: OpenNettyProtocol.Nitoo, Unit.Definition.AssociatedUnitId: byte unit })
{
tasks.Add(Task.Run(async () =>
{
var endpoint = await _manager.FindEndpointByAddressAsync(OpenNettyAddress.FromNitooAddress(
OpenNettyAddress.ToNitooAddress(address).Identifier, unit));
if (endpoint is not null &&
endpoint.HasCapability(OpenNettyCapabilities.BasicShutterState) &&
endpoint.GetStringSetting(OpenNettySettings.ActuatorType) is null or OpenNettySettings.ActuatorTypes.Automation)
{
await ReportStateAsync(endpoint, CancellationToken.None);
}
}));
}
}
if (mode is OpenNettyMode.Broadcast or OpenNettyMode.Multicast && endpoint is { Unit.Scenarios: [_, ..] scenarios })
{
var endpoints = scenarios.ToAsyncEnumerable()
.Where(static scenario => scenario.FunctionCode is < 110 or 111 or 112)
.SelectAwait(scenario => _manager.FindEndpointByNameAsync(scenario.EndpointName))
.Where(static endpoint => endpoint is { Protocol: OpenNettyProtocol.Nitoo, Unit: OpenNettyUnit })
.OfType<OpenNettyEndpoint>()
.Where(static endpoint => endpoint.HasCapability(OpenNettyCapabilities.BasicShutterState));
tasks.Add(Parallel.ForEachAsync(endpoints, ReportStateAsync));
}
await Task.WhenAll(tasks);
async ValueTask ReportStateAsync(OpenNettyEndpoint endpoint, CancellationToken cancellationToken)
=> await _events.PublishAsync(new ShutterStateReportedEventArgs(endpoint,
command == OpenNettyCommands.Automation.Stop ? OpenNettyModels.Automation.ShutterState.Stopped :
command == OpenNettyCommands.Automation.Up ? OpenNettyModels.Automation.ShutterState.Opening :
command == OpenNettyCommands.Automation.Down ? OpenNettyModels.Automation.ShutterState.Closing :
throw new InvalidDataException(SR.GetResourceString(SR.ID0075))), cancellationToken);
break;
}
case (OpenNettyNotifications.MessageReceived,
OpenNettyMessage { Protocol: OpenNettyProtocol.Scs or OpenNettyProtocol.Zigbee,
Type : OpenNettyMessageType.BusCommand,
Command : OpenNettyCommand command,
Address : OpenNettyAddress address })
when command == OpenNettyCommands.Automation.Stop ||
command == OpenNettyCommands.Automation.Up ||
command == OpenNettyCommands.Automation.Down:
{
await Parallel.ForEachAsync(_manager.FindEndpointsByAddressAsync(address), async (endpoint, cancellationToken) =>
{
// Ignore the message if it was received by a different gateway than the one associated with the endpoint.
if (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway)
{
return;
}
if (endpoint is null || !endpoint.HasCapability(OpenNettyCapabilities.BasicShutterState) ||
endpoint.GetStringSetting(OpenNettySettings.ActuatorType) is not (null or OpenNettySettings.ActuatorTypes.Automation))
{
return;
}
// Note: the STOP command is only reported if the endpoint isn't an advanced shutter.
if (command != OpenNettyCommands.Automation.Stop || !endpoint.HasCapability(OpenNettyCapabilities.AdvancedShutterState))
{
await _events.PublishAsync(new ShutterStateReportedEventArgs(endpoint,
command == OpenNettyCommands.Automation.Stop ? OpenNettyModels.Automation.ShutterState.Stopped :
command == OpenNettyCommands.Automation.Up ? OpenNettyModels.Automation.ShutterState.Opening :
command == OpenNettyCommands.Automation.Down ? OpenNettyModels.Automation.ShutterState.Closing :
throw new InvalidDataException(SR.GetResourceString(SR.ID0075))), cancellationToken);
}
});
break;
}
case (OpenNettyNotifications.MessageReceived,
OpenNettyMessage { Protocol : OpenNettyProtocol.Scs or OpenNettyProtocol.Zigbee,
Type : OpenNettyMessageType.DimensionRead,
Address : OpenNettyAddress address,
Dimension: OpenNettyDimension dimension,
Values : [{ Length: > 0 } status, { Length: > 0 } position, ..] })
when dimension == OpenNettyDimensions.Automation.ShutterStatus:
{
await Parallel.ForEachAsync(_manager.FindEndpointsByAddressAsync(address), async (endpoint, cancellationToken) =>
{
// Ignore the message if it was received by a different gateway than the one associated with the endpoint.
if (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway)
{
return;
}
if (endpoint.HasCapability(OpenNettyCapabilities.AdvancedShutterState) &&
endpoint.GetStringSetting(OpenNettySettings.ActuatorType) is not (null or OpenNettySettings.ActuatorTypes.Automation))
{
await _events.PublishAsync(new ShutterStateReportedEventArgs(endpoint,
(status, byte.Parse(position, CultureInfo.InvariantCulture)) switch
{
("10", 0 ) => OpenNettyModels.Automation.ShutterState.Closed,
("10", >= 1 and <= 100) => OpenNettyModels.Automation.ShutterState.Open,
("10", _ ) => OpenNettyModels.Automation.ShutterState.Stopped,
("11" or "13", _) => OpenNettyModels.Automation.ShutterState.Opening,
("12" or "14", _) => OpenNettyModels.Automation.ShutterState.Closing,
_ => throw new InvalidDataException(SR.GetResourceString(SR.ID0075))
}), cancellationToken);
await _events.PublishAsync(new ShutterPositionReportedEventArgs(endpoint,
byte.Parse(position, CultureInfo.InvariantCulture)), cancellationToken);
}
});
break;
}
case (OpenNettyNotifications.MessageReceived,
OpenNettyMessage { Protocol : OpenNettyProtocol.Nitoo,
Type : OpenNettyMessageType.DimensionRead,
Address : OpenNettyAddress address,
Dimension: OpenNettyDimension dimension,
Values : ["139", { Length: > 0 } value, ..] })
when dimension == OpenNettyDimensions.Diagnostics.UnitDescription:
{
// Ignore the message if the corresponding endpoint couldn't be resolved or if it
// was received by a different gateway than the one associated with the endpoint.
var endpoint = await _manager.FindEndpointByAddressAsync(address);
if (endpoint is null || (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway))
{
return;
}
if (endpoint.HasCapability(OpenNettyCapabilities.BasicShutterState) &&
endpoint.GetStringSetting(OpenNettySettings.ActuatorType) is not (null or OpenNettySettings.ActuatorTypes.Automation))
{
await _events.PublishAsync(new ShutterStateReportedEventArgs(endpoint, value switch
{
"100" or "102" => OpenNettyModels.Automation.ShutterState.Opening,
"0" or "103" => OpenNettyModels.Automation.ShutterState.Closing,
_ => OpenNettyModels.Automation.ShutterState.Stopped
}));
}
break;
}
case (OpenNettyNotifications.MessageReceived,
OpenNettyMessage { Protocol : OpenNettyProtocol.Nitoo,
Type : OpenNettyMessageType.DimensionRead,
Address : OpenNettyAddress address,
Dimension: OpenNettyDimension dimension,
Values : [{ Length: > 0 }, { Length: > 0 }, { Length: > 0 }] values })
when dimension == OpenNettyDimensions.TemperatureControl.SmartMeterIndexes:
{
// Ignore the message if the corresponding endpoint couldn't be resolved or if it
// was received by a different gateway than the one associated with the endpoint.
var endpoint = await _manager.FindEndpointByAddressAsync(address);
if (endpoint is null || (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway))
{
return;
}
if (endpoint.HasCapability(OpenNettyCapabilities.SmartMeterIndexes))
{
await _events.PublishAsync(new SmartMeterIndexesReportedEventArgs(endpoint,
OpenNettyModels.TemperatureControl.SmartMeterIndexes.CreateFromDimensionValues(values)));
}
break;
}
case (OpenNettyNotifications.MessageReceived,
OpenNettyMessage { Protocol : OpenNettyProtocol.Nitoo,
Type : OpenNettyMessageType.DimensionRead,
Address : OpenNettyAddress address,
Dimension: OpenNettyDimension dimension,
Values : [{ Length: > 0 } value] })
when dimension == OpenNettyDimensions.TemperatureControl.SmartMeterRateType:
{
// Ignore the message if the corresponding endpoint couldn't be resolved or if it
// was received by a different gateway than the one associated with the endpoint.
var endpoint = await _manager.FindEndpointByAddressAsync(address);
if (endpoint is null || (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway))
{
return;
}
if (endpoint.HasCapability(OpenNettyCapabilities.SmartMeterInformation))
{
await _events.PublishAsync(new SmartMeterRateTypeReportedEventArgs(endpoint, value switch
{
"2" => OpenNettyModels.TemperatureControl.SmartMeterRateType.OffPeak,
"3" => OpenNettyModels.TemperatureControl.SmartMeterRateType.Peak,
_ => throw new InvalidDataException(SR.GetResourceString(SR.ID0075))
}));
}
break;
}
case (OpenNettyNotifications.MessageReceived,
OpenNettyMessage { Protocol : OpenNettyProtocol.Nitoo,
Type : OpenNettyMessageType.DimensionRead,
Address : OpenNettyAddress address,
Dimension: OpenNettyDimension dimension,
Values : ["7", { Length: > 0 } value] })
when dimension == OpenNettyDimensions.Diagnostics.UnitDescription:
{
// Ignore the message if the corresponding endpoint couldn't be resolved or if it
// was received by a different gateway than the one associated with the endpoint.
var endpoint = await _manager.FindEndpointByAddressAsync(address);
if (endpoint is null || (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway))
{
return;
}
if (endpoint.HasCapability(OpenNettyCapabilities.SmartMeterInformation))
{
await _events.PublishAsync(new SmartMeterRateTypeReportedEventArgs(endpoint, value switch
{
"32" or "33" => OpenNettyModels.TemperatureControl.SmartMeterRateType.OffPeak,
"48" or "49" => OpenNettyModels.TemperatureControl.SmartMeterRateType.Peak,
_ => throw new InvalidDataException(SR.GetResourceString(SR.ID0075))
}));
await _events.PublishAsync(new SmartMeterPowerCutModeReportedEventArgs(endpoint, value is "33" or "49"));
}
break;
}
case (OpenNettyNotifications.MessageReceived,
OpenNettyMessage { Protocol : OpenNettyProtocol.Nitoo,
Type : OpenNettyMessageType.DimensionRead,
Address : OpenNettyAddress address,
Dimension: OpenNettyDimension dimension,
Values : ["133", { Length: > 0 } value] })
when dimension == OpenNettyDimensions.Diagnostics.UnitDescription:
{
// Ignore the message if the corresponding endpoint couldn't be resolved or if it
// was received by a different gateway than the one associated with the endpoint.
var endpoint = await _manager.FindEndpointByAddressAsync(address);
if (endpoint is null || (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway))
{
return;
}
if (endpoint.HasCapability(OpenNettyCapabilities.WaterHeating))
{
switch (value)
{
// Note: the value "0" is ambiguous and appears to be used to represent two cases:
//
// - When the user selected the "automatic" mode but hot water is not currently
// being produced (e.g because the off-peak signal was not yet received).
//
// - When no hot water is produced because the user selected the "forced off" mode.
//
// Since the value is ambiguous, it is not possible to reliably determine the actual
// water heating mode: in this case, the mode is not immediately reported and an another
// event handler is responsible for reporting it when the off-peak signal is received.
case "0":
await _events.PublishAsync(new WaterHeaterStateReportedEventArgs(endpoint,
OpenNettyModels.TemperatureControl.WaterHeaterState.Idle));
break;
case "32":
await _events.PublishAsync(new WaterHeaterSetpointModeReportedEventArgs(endpoint,
OpenNettyModels.TemperatureControl.WaterHeaterMode.Automatic));
await _events.PublishAsync(new WaterHeaterStateReportedEventArgs(endpoint,
OpenNettyModels.TemperatureControl.WaterHeaterState.Idle));
break;
case "1" or "33":
await _events.PublishAsync(new WaterHeaterSetpointModeReportedEventArgs(endpoint,
OpenNettyModels.TemperatureControl.WaterHeaterMode.Automatic));
await _events.PublishAsync(new WaterHeaterStateReportedEventArgs(endpoint,
OpenNettyModels.TemperatureControl.WaterHeaterState.Heating));
break;
case "17":
await _events.PublishAsync(new WaterHeaterSetpointModeReportedEventArgs(endpoint,
OpenNettyModels.TemperatureControl.WaterHeaterMode.ForcedOn));
await _events.PublishAsync(new WaterHeaterStateReportedEventArgs(endpoint,
OpenNettyModels.TemperatureControl.WaterHeaterState.Heating));
break;
}
}
break;
}
case (OpenNettyNotifications.MessageSent,
OpenNettyMessage { Protocol : OpenNettyProtocol.Nitoo,
Type : OpenNettyMessageType.DimensionSet,
Address : OpenNettyAddress address,
Mode : OpenNettyMode mode,
Dimension: OpenNettyDimension dimension,
Values : [{ Length: > 0 } value, ..] })
when dimension == OpenNettyDimensions.TemperatureControl.WaterHeatingMode:
{
// Ignore the message if the corresponding endpoint couldn't be resolved or if it
// was received by a different gateway than the one associated with the endpoint.
var endpoint = await _manager.FindEndpointByAddressAsync(address);
if (endpoint is null || (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway))
{
return;
}
List<Task> tasks = [];
if (endpoint.HasCapability(OpenNettyCapabilities.WaterHeating))
{
tasks.Add(ReportSetpointModeAsync(endpoint, CancellationToken.None).AsTask());
}
if (endpoint is { Unit.Definition.AssociatedUnitId: byte unit })
{
tasks.Add(Task.Run(async () =>
{
var endpoint = await _manager.FindEndpointByAddressAsync(OpenNettyAddress.FromNitooAddress(
OpenNettyAddress.ToNitooAddress(address).Identifier, unit));
if (endpoint is not null && endpoint.HasCapability(OpenNettyCapabilities.WaterHeating))
{
await ReportSetpointModeAsync(endpoint, CancellationToken.None);
}
}));
}
if (mode is OpenNettyMode.Broadcast or OpenNettyMode.Multicast && endpoint is { Unit.Scenarios: [_, ..] scenarios })
{
var endpoints = scenarios.ToAsyncEnumerable()
.Where(static scenario => scenario.FunctionCode is 255)
.SelectAwait(scenario => _manager.FindEndpointByNameAsync(scenario.EndpointName))
.Where(static endpoint => endpoint is { Protocol: OpenNettyProtocol.Nitoo, Unit: OpenNettyUnit })
.OfType<OpenNettyEndpoint>()
.Where(static endpoint => endpoint.HasCapability(OpenNettyCapabilities.WaterHeating));
tasks.Add(Parallel.ForEachAsync(endpoints, ReportSetpointModeAsync));
}
await Task.WhenAll(tasks);
async ValueTask ReportSetpointModeAsync(OpenNettyEndpoint endpoint, CancellationToken cancellationToken) =>
await _events.PublishAsync(new WaterHeaterSetpointModeReportedEventArgs(endpoint, value switch
{
"0" => OpenNettyModels.TemperatureControl.WaterHeaterMode.ForcedOff,
"1" => OpenNettyModels.TemperatureControl.WaterHeaterMode.ForcedOn,
"2" => OpenNettyModels.TemperatureControl.WaterHeaterMode.Automatic,
_ => throw new InvalidDataException(SR.GetResourceString(SR.ID0075))
}), cancellationToken);
break;
}
// The partial state of a pilot wire device can be inferred from 3 types of incoming messages:
//
// - From a "UNIT DESCRIPTION" DIMENSION READ message.
// - From a parameterized "WIRE PILOT SETPOINT MODE" BUS COMMAND message.
// - From a parameterized "WIRE PILOT DEROGATION MODE" BUS COMMAND message.
// - From a "CANCEL WIRE PILOT DEROGATION" BUS COMMAND message.
case (OpenNettyNotifications.MessageReceived,
OpenNettyMessage { Protocol : OpenNettyProtocol.Nitoo,
Type : OpenNettyMessageType.DimensionRead,
Address : OpenNettyAddress address,
Dimension: OpenNettyDimension dimension,
Values : ["6" or "132", { Length: > 0 } value] })
when dimension == OpenNettyDimensions.Diagnostics.UnitDescription:
{
// Ignore the message if the corresponding endpoint couldn't be resolved or if it
// was received by a different gateway than the one associated with the endpoint.
var endpoint = await _manager.FindEndpointByAddressAsync(address);
if (endpoint is null || (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway))
{
return;
}
if (endpoint.HasCapability(OpenNettyCapabilities.PilotWireHeating))
{
var configuration = OpenNettyModels.TemperatureControl.PilotWireConfiguration.CreateFromUnitDescription([value]);
if (configuration.IsDerogationActive)
{
await _events.PublishAsync(new PilotWireDerogationModeReportedEventArgs(endpoint, configuration.Mode, configuration.DerogationDuration));
}
else
{
await _events.PublishAsync(new PilotWireSetpointModeReportedEventArgs(endpoint, configuration.Mode));
await _events.PublishAsync(new PilotWireDerogationModeReportedEventArgs(endpoint, null, null));
}
}
break;
}
case (OpenNettyNotification notification,
OpenNettyMessage { Protocol : OpenNettyProtocol.Nitoo,
Type : OpenNettyMessageType.BusCommand,
Command : OpenNettyCommand command,
Address : OpenNettyAddress address,
Mode : OpenNettyMode mode })
when command.Category == OpenNettyCategories.TemperatureControl &&
command.Value == OpenNettyCommands.TemperatureControl.WirePilotSetpointMode.Value &&
command.Parameters is [{ Length: > 0 } value]:
{
// Ignore the message if the corresponding endpoint couldn't be resolved or if it
// was received by a different gateway than the one associated with the endpoint.
var endpoint = await _manager.FindEndpointByAddressAsync(address);
if (endpoint is null || (endpoint.Gateway is not null && arguments.Notification.Gateway != endpoint.Gateway))
{
return;
}
List<Task> tasks = [];
if (endpoint.HasCapability(OpenNettyCapabilities.PilotWireHeating))
{
tasks.Add(ReportSetpointModeAsync(endpoint, CancellationToken.None).AsTask());
}
if (endpoint is { Unit.Definition.AssociatedUnitId: byte unit })
{
tasks.Add(Task.Run(async () =>
{