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