2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.teleinfo.internal.data;
15 import java.io.Serializable;
16 import java.util.EnumMap;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.teleinfo.internal.reader.io.serialport.InvalidFrameException;
22 import org.openhab.binding.teleinfo.internal.reader.io.serialport.Label;
25 * The {@link Frame} class defines common attributes for any Teleinfo frames.
27 * @author Nicolas SIBERIL - Initial contribution
30 public class Frame implements Serializable {
32 private static final long serialVersionUID = -1934715078822532494L;
34 private Map<Label, String> labelToValues = new EnumMap<>(Label.class);
36 public void put(Label label, String value) {
37 labelToValues.put(label, value);
40 public @Nullable String get(Label label) {
41 return labelToValues.get(label);
44 public @Nullable Integer getAsInt(Label label) {
45 String value = labelToValues.get(label);
47 return Integer.parseInt(value);
53 // default constructor
56 public FrameType getType() throws InvalidFrameException {
57 Phase phase = getPhase();
58 Pricing pricing = getPricing();
59 Evolution evolution = getEvolution();
66 return FrameType.CBEMM_ICC_BASE;
68 return FrameType.CBEMM_ICC_EJP;
70 return FrameType.CBEMM_ICC_HC;
72 return FrameType.CBEMM_ICC_TEMPO;
74 return FrameType.UNKNOWN;
79 return FrameType.CBEMM_BASE;
81 return FrameType.CBEMM_EJP;
83 return FrameType.CBEMM_HC;
85 return FrameType.CBEMM_TEMPO;
87 return FrameType.UNKNOWN;
90 return FrameType.UNKNOWN;
95 return FrameType.CBETM_SHORT;
99 return FrameType.CBETM_LONG_BASE;
101 return FrameType.CBETM_LONG_EJP;
103 return FrameType.CBETM_LONG_HC;
105 return FrameType.CBETM_LONG_TEMPO;
107 return FrameType.UNKNOWN;
111 return FrameType.UNKNOWN;
115 public Phase getPhase() throws InvalidFrameException {
116 if (labelToValues.containsKey(Label.IINST)) {
117 return Phase.ONE_PHASED;
118 } else if (labelToValues.containsKey(Label.IINST1)) {
119 return Phase.THREE_PHASED;
121 throw new InvalidFrameException();
124 public boolean isShortFrame() {
125 return !labelToValues.containsKey(Label.ISOUSC);
128 public Evolution getEvolution() {
129 if (labelToValues.containsKey(Label.PAPP)) {
130 return Evolution.ICC;
132 return Evolution.NONE;
135 public Pricing getPricing() throws InvalidFrameException {
136 String optarif = labelToValues.get(Label.OPTARIF);
137 if (optarif == null) {
138 throw new InvalidFrameException();
148 if (optarif.matches("BBR.")) {
149 return Pricing.TEMPO;
151 throw new InvalidFrameException();
155 public void clear() {
156 labelToValues.clear();
159 public Map<Label, String> getLabelToValues() {
160 return labelToValues;
163 private char getProgrammeChar() {
164 String optarif = labelToValues.get(Label.OPTARIF);
165 if (optarif == null) {
166 throw new IllegalStateException("No OPTARIF field in frame");
168 return optarif.charAt(3);
171 public String getProgrammeCircuit1() {
172 char program = getProgrammeChar();
173 return convertProgrammeCircuit1(program);
176 public String getProgrammeCircuit2() {
177 char program = getProgrammeChar();
178 return convertProgrammeCircuit2(program);
181 private String convertProgrammeCircuit1(char value) {
182 String prgCircuit1 = computeProgrammeCircuitBinaryValue(value).substring(3, 5);
183 switch (prgCircuit1) {
191 final String error = String.format("Programme circuit 1 '%s' is unknown", prgCircuit1);
192 throw new IllegalStateException(error);
196 private String convertProgrammeCircuit2(char value) {
197 String prgCircuit2 = computeProgrammeCircuitBinaryValue(value).substring(5, 8);
198 switch (prgCircuit2) {
216 final String error = String.format("Programme circuit 2 '%s' is unknown", prgCircuit2);
217 throw new IllegalStateException(error);
221 private String computeProgrammeCircuitBinaryValue(char value) {
222 return String.format("%8s", Integer.toBinaryString(value)).replace(' ', '0');