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.vif.Vif;
33  
34  /**
35   *
36   * @author arnep@users.sourceforge.net
37   * @version $Id: ShortDataBlock.java 104 2014-02-21 09:31:17Z arnep $
38   */
39  public class ShortDataBlock extends DataBlock implements BcdValue {
40  
41      private short value;
42      private String bcdError;
43  
44      @Override
45      public boolean isBcdError() {
46          return bcdError != null;
47      }
48  
49      public ShortDataBlock() {
50          super();
51      }
52  
53      @Deprecated
54      public ShortDataBlock(DataFieldCode dif, Vif vif) {
55          super(dif, vif);
56      }
57  
58      /**
59       * @return the value
60       */
61      public short getValue() {
62          if (isBcdError()) {
63              throw new IllegalArgumentException("No value BCD Error: " + bcdError);
64          } else {
65              return value;
66          }
67      }
68  
69      @Override
70      public String getValueAsString() {
71          if (bcdError != null) {
72              return bcdError;
73          }
74          switch (getDataFieldCode()) {
75              case _16_BIT_INTEGER:
76                  return Short.toString(value);
77              case _4_DIGIT_BCD:
78                  return String.format("%04d", value);
79              default:
80                  throw new RuntimeException("DIF not supported: " + getDataFieldCode());
81          }
82      }
83  
84      /**
85       * @param value the value to set
86       */
87      public void setValue(short value) {
88          this.value = value;
89          this.bcdError = null;
90      }
91  
92      @Override
93      protected void accumulateDatatoJSON(JSONObject json) {
94          if (!isBcdError()) {
95              json.accumulate("data", getValue());
96          } else {
97              JSONObject jsonBcdError = new JSONObject();
98              jsonBcdError.accumulate("bcdErrorCode", getBcdError());
99              json.accumulate("data", jsonBcdError);
100         }
101     }
102 
103     @Override
104     public void fromJSON(JSONObject json) {
105         super.fromJSON(json);
106         if (json.get("data") instanceof JSONObject) {
107             JSONObject data = json.getJSONObject("data");
108             if (data.containsKey("bcdErrorCode")) {
109                 bcdError = data.getString("bcdErrorCode");
110             } else {
111                 throw new IllegalArgumentException("Unknown value at data: " + data.toString(1));
112             }
113         } else {
114             setValue((short) json.getInt("data"));
115         }
116     }
117 
118     /**
119      * @return the bcdError
120      */
121     @Override
122     public String getBcdError() {
123         return bcdError;
124     }
125 
126     /**
127      * @param bcdError the bcdError to set
128      */
129     @Override
130     public void setBcdError(String bcdError) {
131         this.bcdError = formatBcdError(bcdError);
132         this.value = 0;
133     }
134 
135     @Override
136     public void setValue(String text) {
137         try {
138             value = Short.parseShort(text);
139             bcdError = null;
140         } catch (NumberFormatException ex) {
141             value = 0;
142             bcdError = text;
143         }
144     }
145 
146     @Override
147     public boolean isBcd() {
148         return DataFieldCode._4_DIGIT_BCD.equals(getDataFieldCode());
149     }
150 }