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.json.JSONFactory;
32  
33  /**
34   *
35   * @author arnep@users.sourceforge.net
36   * @version $Id: RawDataBlock.java 104 2014-02-21 09:31:17Z arnep $
37   */
38  public class RawDataBlock extends DataBlock {
39  
40      private byte[] value;
41  
42      public RawDataBlock() {
43          super();
44      }
45  
46      /**
47       * @return the value
48       */
49      public byte[] getValue() {
50          return value;
51      }
52  
53      @Override
54      public String getValueAsString() {
55          StringBuilder sb = new StringBuilder();
56          if (value != null) {
57              for (byte b : value) {
58                  sb.append(String.format("%02X", b));
59              }
60          }
61          return sb.toString();
62      }
63  
64      /**
65       * @param value the value to set
66       */
67      public void setValue(byte[] value) {
68          this.value = value;
69      }
70  
71      public void setValue(int... value) {
72          this.value = new byte[value.length];
73          for (int i = 0; i < value.length; i++) {
74              this.value[i] = (byte) value[i];
75          }
76      }
77  
78      @Override
79      public void toString(StringBuilder sb, String inset) {
80          sb.append(inset).append("dataType = ").append(getDataFieldCode()).append("\n");
81          sb.append(inset).append("value = ").append(getValueAsString()).append("\n");
82      }
83  
84      @Override
85      protected void accumulateDatatoJSON(JSONObject json) {
86          if (getValue() != null) {
87              json.accumulate("data", JSONFactory.encodeHexByteArray(getValue()));
88          }
89      }
90  
91      @Override
92      public void fromJSON(JSONObject json) {
93          super.fromJSON(json);
94          if (json.containsKey("data")) {
95              value = JSONFactory.decodeHexByteArray(json.getString("data"));
96          } else {
97              value = null;
98          }
99      }
100 
101     @Override
102     public void setValue(String text) {
103         value = new byte[text.length() / 2];
104         for (int i = 0; i < text.length() / 2; i++) {
105             value[i] = (byte) Integer.parseInt(text.substring(i * 2, i * 2 + 1), 16);
106         }
107 
108     }
109 }