|
| 1 | +// ZWaveDotNet Copyright (C) 2024 |
| 2 | +// |
| 3 | +// This program is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU Affero General Public License as published by |
| 5 | +// the Free Software Foundation, either version 3 of the License, or any later version. |
| 6 | +// This program is distributed in the hope that it will be useful, |
| 7 | +// but WITHOUT ANY WARRANTY, without even the implied warranty of |
| 8 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 9 | +// See the GNU Affero General Public License for more details. |
| 10 | +// You should have received a copy of the GNU Affero General Public License |
| 11 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 12 | + |
| 13 | +using Serilog; |
| 14 | +using System.Collections; |
| 15 | +using ZWaveDotNet.CommandClasses.Enums; |
| 16 | +using ZWaveDotNet.CommandClassReports; |
| 17 | +using ZWaveDotNet.CommandClassReports.Enums; |
| 18 | +using ZWaveDotNet.Entities; |
| 19 | +using ZWaveDotNet.Enums; |
| 20 | +using ZWaveDotNet.SerialAPI; |
| 21 | + |
| 22 | +namespace ZWaveDotNet.CommandClasses |
| 23 | +{ |
| 24 | + /// <summary> |
| 25 | + /// The Protection Command Class version 1 used to protect a device against unintentional control by e.g. a child. |
| 26 | + /// </summary> |
| 27 | + [CCVersion(CommandClass.Protection, 1)] |
| 28 | + public class Protection : CommandClassBase |
| 29 | + { |
| 30 | + public event CommandClassEvent<ProtectionReport>? Report; |
| 31 | + |
| 32 | + enum ProtectionCommand : byte |
| 33 | + { |
| 34 | + Set = 0x1, |
| 35 | + Get = 0x2, |
| 36 | + Report = 0x3, |
| 37 | + SupportedGet = 0x4, |
| 38 | + SupportedReport = 0x5, |
| 39 | + ECSet = 0x6, |
| 40 | + ECGet = 0x7, |
| 41 | + ECReport = 0x8, |
| 42 | + TimeoutSet = 0x9, |
| 43 | + TimeoutGet = 0x10, |
| 44 | + TimeoutReport = 0x11, |
| 45 | + } |
| 46 | + |
| 47 | + public Protection(Node node, byte endpoint) : base(node, endpoint, CommandClass.Protection) { } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// <b>Version 1</b>: Request the protection state from a device. |
| 51 | + /// </summary> |
| 52 | + /// <param name="cancellationToken"></param> |
| 53 | + /// <returns></returns> |
| 54 | + public async Task<ProtectionReport> Get(CancellationToken cancellationToken = default) |
| 55 | + { |
| 56 | + ReportMessage response = await SendReceive(ProtectionCommand.Get, ProtectionCommand.Report, cancellationToken); |
| 57 | + return new ProtectionReport(response.Payload.Span); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// <b>Version 1</b>: Set the protection state in a device. |
| 62 | + /// </summary> |
| 63 | + /// <param name="protectionState"></param> |
| 64 | + /// <param name="cancellationToken"></param> |
| 65 | + /// <returns></returns> |
| 66 | + public async Task Set(LocalProtectionState protectionState, CancellationToken cancellationToken = default) |
| 67 | + { |
| 68 | + await SendCommand(ProtectionCommand.Set, cancellationToken, (byte)protectionState); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// <b>Version 2</b>: Set the protection state in a device. |
| 73 | + /// </summary> |
| 74 | + /// <param name="protectionState"></param> |
| 75 | + /// <param name="cancellationToken"></param> |
| 76 | + /// <returns></returns> |
| 77 | + public async Task Set(LocalProtectionState localProtection, RFProtectionState remoteProtection, CancellationToken cancellationToken = default) |
| 78 | + { |
| 79 | + await SendCommand(ProtectionCommand.Set, cancellationToken, (byte)localProtection, (byte)remoteProtection); |
| 80 | + } |
| 81 | + |
| 82 | + protected override async Task<SupervisionStatus> Handle(ReportMessage message) |
| 83 | + { |
| 84 | + if (message.Command == (byte)ProtectionCommand.Report) |
| 85 | + { |
| 86 | + ProtectionReport rpt = new ProtectionReport(message.Payload.Span); |
| 87 | + await FireEvent(Report, rpt); |
| 88 | + return SupervisionStatus.Success; |
| 89 | + } |
| 90 | + return SupervisionStatus.NoSupport; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments