View Javadoc
1   package net.sf.mbus4j.dataframes;
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.JSONArray;
31  import net.sf.json.JSONObject;
32  
33  import net.sf.mbus4j.dataframes.datablocks.DataBlock;
34  import net.sf.mbus4j.json.JSONFactory;
35  import net.sf.mbus4j.json.JsonSerializeType;
36  
37  import java.util.ArrayList;
38  import java.util.Iterator;
39  import java.util.List;
40  
41  /**
42   *
43   * @author arnep@users.sourceforge.net
44   * @version $Id: SendUserData.java 145 2015-04-29 18:09:27Z arnep $
45   */
46  public class SendUserData
47          implements LongFrame {
48  
49      public static final String SEND_USER_DATA_SUBTYPE = "send user data";
50      private final List<DataBlock> dataBlocks = new ArrayList<>();
51      private byte address;
52      private boolean fcb;
53  
54      public SendUserData(boolean fcb) {
55          this.fcb = fcb;
56      }
57  
58      public SendUserData() {
59      }
60  
61      @Override
62      public boolean addDataBlock(DataBlock dataBlock) {
63          return dataBlocks.add(dataBlock);
64      }
65  
66      @Override
67      public byte getAddress() {
68          return address;
69      }
70  
71      @Override
72      public ControlCode getControlCode() {
73          return ControlCode.SND_UD;
74      }
75  
76      @Override
77      public DataBlock getLastDataBlock() {
78          return dataBlocks.get(dataBlocks.size() - 1);
79      }
80  
81      public boolean isFcb() {
82          return fcb;
83      }
84  
85      @Override
86      public Iterator<DataBlock> iterator() {
87          return dataBlocks.iterator();
88      }
89  
90      @Override
91      public void replaceDataBlock(DataBlock oldDataBlock, DataBlock newDataBlock) {
92          final int pos = dataBlocks.indexOf(oldDataBlock);
93          dataBlocks.remove(pos);
94          dataBlocks.add(pos, newDataBlock);
95      }
96  
97      @Override
98      public void setAddress(byte address) {
99          this.address = address;
100     }
101 
102     public void setFcb(boolean fcb) {
103         this.fcb = fcb;
104     }
105 
106     @Override
107     public String toString() {
108         StringBuilder sb = new StringBuilder();
109         sb.append("control code = ").append(getControlCode()).append('\n');
110         sb.append("isFcb = ").append(isFcb()).append('\n');
111         sb.append(String.format("address = 0x%02X\n", address));
112 
113         for (int i = 0; i < dataBlocks.size(); i++) {
114             sb.append("datablock[").append(i).append("]:\n");
115             dataBlocks.get(i).toString(sb, "  ");
116         }
117 
118         return sb.toString();
119     }
120 
121     @Override
122     public JSONObject toJSON(JsonSerializeType jsonSerializeType) {
123         JSONObject result = new JSONObject();
124         result.accumulate("controlCode",
125                 getControlCode());
126         result.accumulate("subType", SEND_USER_DATA_SUBTYPE);
127 
128         if (JsonSerializeType.ALL == jsonSerializeType) {
129             result.accumulate("fcb",
130                     isFcb());
131             result.accumulate("address", address & 0xFF);
132         }
133 
134         JSONArray jsonDataBlocks = new JSONArray();
135 
136         for (DataBlock db : this) {
137             jsonDataBlocks.add(db.toJSON(jsonSerializeType));
138         }
139 
140         result.accumulate("dataBlocks", jsonDataBlocks);
141 
142         return result;
143     }
144 
145     @Override
146     public void fromJSON(JSONObject json) {
147         fcb = json.getBoolean("fcb");
148         address = (byte) json.getInt("address");
149 
150         JSONFactory.readDataBlocks(this, json);
151 
152     }
153 }