View Javadoc
1   package net.sf.mbus4j.dataframes.datablocks;
2   
3   /*
4    * #%L
5    * mbus4j-core
6    * %%
7    * Copyright (C) 2009 - 2014 MBus4J
8    * %%
9    * mbus4j - Drivers for the M-Bus protocol - http://mbus4j.sourceforge.net/
10   * Copyright (C) 2009-2014, mbus4j.sf.net, and individual contributors as indicated
11   * by the @authors tag. See the copyright.txt in the distribution for a
12   * full listing of individual contributors.
13   * 
14   * This is free software; you can redistribute it and/or modify it
15   * under the terms of the GNU General Public License as
16   * published by the Free Software Foundation; either version 3 of
17   * the License, or (at your option) any later version.
18   * 
19   * This software is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22   * Lesser General Public License for more details.
23   * 
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this software; if not, write to the Free
26   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
27   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
28   * #L%
29   */
30  import net.sf.json.JSONObject;
31  import net.sf.mbus4j.dataframes.datablocks.dif.DataFieldCode;
32  import net.sf.mbus4j.dataframes.datablocks.dif.FunctionField;
33  import net.sf.mbus4j.dataframes.datablocks.vif.Vif;
34  import net.sf.mbus4j.dataframes.datablocks.vif.Vife;
35  
36  /**
37   *
38   * @author arnep@users.sourceforge.net
39   * @version $Id: IntegerDataBlock.java 104 2014-02-21 09:31:17Z arnep $
40   */
41  public class IntegerDataBlock extends DataBlock implements BcdValue {
42  
43      private int value;
44      private String bcdError;
45  
46      @Override
47      public boolean isBcdError() {
48          return bcdError != null;
49      }
50  
51      public IntegerDataBlock() {
52          super();
53      }
54  
55      @Deprecated
56      public IntegerDataBlock(DataFieldCode dif, FunctionField functionField, short subUnit, int tariff, long storageNumber, Vif vif, Vife... vifes) {
57          super(dif, functionField, subUnit, tariff, storageNumber, vif, vifes);
58      }
59  
60      @Deprecated
61      public IntegerDataBlock(DataFieldCode dif, Vif vif) {
62          super(dif, vif);
63      }
64  
65      /**
66       * @return the value
67       */
68      public int getValue() {
69          if (isBcdError()) {
70              throw new IllegalArgumentException("No value BCD Error: " + bcdError);
71          } else {
72              return value;
73          }
74      }
75  
76      @Override
77      public String getValueAsString() {
78          if (bcdError != null) {
79              return "BCD Error: " + bcdError;
80          }
81          switch (getDataFieldCode()) {
82              case _24_BIT_INTEGER:
83              case _32_BIT_INTEGER:
84                  return Integer.toString(value);
85              case _6_DIGIT_BCD:
86                  return String.format("%06d", value);
87              case _8_DIGIT_BCD:
88                  return String.format("%08d", value);
89              default:
90                  throw new RuntimeException("DIF not supported: " + getDataFieldCode());
91          }
92      }
93  
94      /**
95       * @param value the value to set
96       */
97      public void setValue(int value) {
98          this.value = value;
99          this.bcdError = null;
100     }
101 
102     @Override
103     protected void accumulateDatatoJSON(JSONObject json) {
104         if (!isBcdError()) {
105             json.accumulate("data", getValue());
106         } else {
107             JSONObject jsonBcdError = new JSONObject();
108             jsonBcdError.accumulate("bcdErrorCode", getBcdError());
109             json.accumulate("data", jsonBcdError);
110         }
111     }
112 
113     @Override
114     public void fromJSON(JSONObject json) {
115         super.fromJSON(json);
116         if (json.get("data") instanceof JSONObject) {
117             JSONObject data = json.getJSONObject("data");
118             if (data.containsKey("bcdErrorCode")) {
119                 bcdError = data.getString("bcdErrorCode");
120             } else {
121                 throw new IllegalArgumentException("Unknown value at data: " + data.toString(1));
122             }
123         } else {
124             setValue(json.getInt("data"));
125         }
126     }
127 
128     /**
129      * @return the bcdError
130      */
131     @Override
132     public String getBcdError() {
133         return bcdError;
134     }
135 
136     /**
137      * @param bcdError the bcdError to set
138      */
139     @Override
140     public void setBcdError(String bcdError) {
141         this.bcdError = formatBcdError(bcdError);
142         this.value = 0;
143     }
144 
145     @Override
146     public void setValue(String text) {
147         try {
148             value = Integer.parseInt(text);
149             bcdError = null;
150         } catch (NumberFormatException ex) {
151             value = 0;
152             bcdError = text;
153         }
154     }
155 
156     @Override
157     public boolean isBcd() {
158         return DataFieldCode._6_DIGIT_BCD.equals(getDataFieldCode()) || DataFieldCode._8_DIGIT_BCD.equals(getDataFieldCode());
159     }
160 }