]> git.basschouten.com Git - openhab-addons.git/blob
fa0299dbd1c147e6427e59e41092aaedb14246d3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.modbus.studer.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.core.library.types.OnOffType;
18
19 /**
20  * The {@link StuderParser} class with helper method
21  * and possible values for mode and state
22  *
23  * @author Giovanni Mirulla - Initial contribution
24  */
25 @NonNullByDefault
26 public class StuderParser {
27     public enum ModeXtender {
28         INVALID(0),
29         INVERTER(1),
30         CHARGER(2),
31         BOOST(3),
32         INJECTION(4),
33         UNKNOWN(-1);
34
35         private final int code;
36
37         ModeXtender(int code) {
38             this.code = code;
39         }
40
41         public int code() {
42             return this.code;
43         }
44     }
45
46     public static ModeXtender getModeXtenderByCode(int code) {
47         switch (code) {
48             case 0:
49                 return ModeXtender.INVALID;
50             case 1:
51                 return ModeXtender.INVERTER;
52             case 2:
53                 return ModeXtender.CHARGER;
54             case 3:
55                 return ModeXtender.BOOST;
56             case 4:
57                 return ModeXtender.INJECTION;
58             default:
59                 return ModeXtender.UNKNOWN;
60         }
61     }
62
63     public static OnOffType getStateByCode(int code) {
64         switch (code) {
65             case 0:
66                 return OnOffType.OFF;
67             case 1:
68                 return OnOffType.ON;
69             default:
70                 return OnOffType.OFF;
71         }
72     }
73
74     public enum VTType {
75         VT80(0),
76         VT65(1),
77         UNKNOWN(-1);
78
79         private final int code;
80
81         VTType(int code) {
82             this.code = code;
83         }
84
85         public int code() {
86             return this.code;
87         }
88     }
89
90     public static VTType getVTTypeByCode(int code) {
91         switch (code) {
92             case 0:
93                 return VTType.VT80;
94             case 1:
95                 return VTType.VT65;
96             default:
97                 return VTType.UNKNOWN;
98         }
99     }
100
101     public enum VTMode {
102         NIGHT(0),
103         STARTUP(1),
104         CHARGER(3),
105         SECURITY(5),
106         OFF(6),
107         CHARGE(8),
108         CHARGEV(9),
109         CHARGEI(10),
110         CHARGET(11),
111         CHIBSP(12),
112         UNKNOWN(-1);
113
114         private final int code;
115
116         VTMode(int code) {
117             this.code = code;
118         }
119
120         public int code() {
121             return this.code;
122         }
123     }
124
125     public static VTMode getVTModeByCode(int code) {
126         switch (code) {
127             case 0:
128                 return VTMode.NIGHT;
129             case 1:
130                 return VTMode.STARTUP;
131             case 3:
132                 return VTMode.CHARGER;
133             case 5:
134                 return VTMode.SECURITY;
135             case 6:
136                 return VTMode.OFF;
137             case 8:
138                 return VTMode.CHARGE;
139             case 9:
140                 return VTMode.CHARGEV;
141             case 10:
142                 return VTMode.CHARGEI;
143             case 11:
144                 return VTMode.CHARGET;
145             case 12:
146                 return VTMode.CHIBSP;
147             default:
148                 return VTMode.UNKNOWN;
149         }
150     }
151
152     public enum VSMode {
153         NIGHT(0),
154         SECURITY(1),
155         OFF(2),
156         CHARGE(3),
157         CHARGEV(4),
158         CHARGEI(5),
159         CHARGEP(6),
160         CHARGEIPV(7),
161         CHARGET(8),
162         CHIBSP(10),
163         UNKNOWN(-1);
164
165         private final int code;
166
167         VSMode(int code) {
168             this.code = code;
169         }
170
171         public int code() {
172             return this.code;
173         }
174     }
175
176     public static VSMode getVSModeByCode(int code) {
177         switch (code) {
178             case 0:
179                 return VSMode.NIGHT;
180             case 1:
181                 return VSMode.SECURITY;
182             case 2:
183                 return VSMode.OFF;
184             case 3:
185                 return VSMode.CHARGE;
186             case 4:
187                 return VSMode.CHARGEV;
188             case 5:
189                 return VSMode.CHARGEI;
190             case 6:
191                 return VSMode.CHARGEP;
192             case 7:
193                 return VSMode.CHARGEIPV;
194             case 8:
195                 return VSMode.CHARGET;
196             case 10:
197                 return VSMode.CHIBSP;
198             default:
199                 return VSMode.UNKNOWN;
200         }
201     }
202
203     /**
204      * Convert an hex string to float
205      *
206      * @param hex string to convert from
207      * @return the converted float
208      */
209     public @Nullable Float hexToFloat(String hex) {
210         String t = hex.replace(" ", "");
211         float f = Float.intBitsToFloat((int) Long.parseLong(t, 16));
212         if (Float.isNaN(f)) {
213             return null;
214         } else {
215             return f;
216         }
217     }
218 }