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.JSONObject;
31  import net.sf.mbus4j.json.JSONFactory;
32  import net.sf.mbus4j.json.JsonSerializeType;
33  
34  /**
35   *
36   * @author arnep@users.sourceforge.net
37   * @version $Id: RequestClassXData.java 104 2014-02-21 09:31:17Z arnep $
38   */
39  public class RequestClassXData implements ShortFrame {
40  
41      private byte address;
42      private boolean fcb;
43      private boolean fcv;
44      private ControlCode controlCode;
45  
46      public RequestClassXData(boolean fcb, boolean fcv, ControlCode controlCode) {
47          this.fcb = fcb;
48          this.fcv = fcv;
49          this.controlCode = controlCode;
50      }
51  
52      public RequestClassXData(ControlCode controlCode) {
53          this(false, true, controlCode);
54      }
55  
56      public RequestClassXData(ControlCode controlCode, byte address) {
57          this(false, true, controlCode);
58          this.address = address;
59      }
60  
61      @Override
62      public byte getAddress() {
63          return address;
64      }
65  
66      @Override
67      public ControlCode getControlCode() {
68          return controlCode;
69      }
70  
71      @Override
72      public boolean isFcb() {
73          return fcb;
74      }
75  
76      @Override
77      public boolean isFcv() {
78          return fcv;
79      }
80  
81      @Override
82      public void setAddress(byte address) {
83          this.address = address;
84      }
85  
86      @Override
87      public void setFcb(boolean fcb) {
88          this.fcb = fcb;
89      }
90  
91      @Override
92      public void setFcv(boolean fcv) {
93          this.fcv = fcv;
94      }
95  
96      @Override
97      public String toString() {
98          StringBuilder sb = new StringBuilder();
99          sb.append("control code = ").append(getControlCode()).append('\n');
100         sb.append("isFcb = ").append(isFcb()).append('\n');
101         sb.append("isFcv = ").append(isFcv()).append('\n');
102         sb.append(String.format("address = 0x%02X\n", address));
103         return sb.toString();
104     }
105 
106     @Override
107     public JSONObject toJSON(JsonSerializeType jsonSerializeType) {
108         JSONObject result = new JSONObject();
109         result.accumulate("controlCode", getControlCode().getLabel());
110         if (JsonSerializeType.ALL == jsonSerializeType) {
111             result.accumulate("fcb", isFcb());
112             result.accumulate("fcv", isFcv());
113             result.accumulate("address", JSONFactory.encodeHexByte(getAddress()));
114         }
115         return result;
116     }
117 
118     @Override
119     public void fromJSON(JSONObject json) {
120         setFcb(JSONFactory.getBoolean(json, "fcb", false));
121         setFcv(JSONFactory.getBoolean(json, "fcv", false));
122         setAddress(JSONFactory.decodeHexByte(json, "address", (byte) 0));
123     }
124 }