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  
32  import net.sf.mbus4j.json.JsonSerializeType;
33  
34  /**
35   *
36   * @author arnep@users.sourceforge.net
37   * @version $Id: SetBaudrate.java 145 2015-04-29 18:09:27Z arnep $
38   */
39  public class SetBaudrate
40          implements ControlFrame {
41  
42      public static final String SEND_USER_DATA_SUBTYPE = "set baudrate";
43      private byte address;
44      private boolean fcb;
45      private int baudrate;
46  
47      public SetBaudrate(SendUserData old, int baudrate) {
48          this.address = old.getAddress();
49          this.fcb = old.isFcb();
50          this.baudrate = baudrate;
51      }
52  
53      public SetBaudrate() {
54      }
55  
56      @Override
57      public byte getAddress() {
58          return address;
59      }
60  
61      /**
62       * @return the baudrate
63       */
64      public int getBaudrate() {
65          return baudrate;
66      }
67  
68      @Override
69      public ControlCode getControlCode() {
70          return ControlCode.SND_UD;
71      }
72  
73      /**
74       * @return the fcb
75       */
76      public boolean isFcb() {
77          return fcb;
78      }
79  
80      @Override
81      public void setAddress(byte address) {
82          this.address = address;
83      }
84  
85      /**
86       * @param baudrate the baudrate to set
87       */
88      public void setBaudrate(int baudrate) {
89          this.baudrate = baudrate;
90      }
91  
92      /**
93       * @param fcb the fcb to set
94       */
95      public void setFcb(boolean fcb) {
96          this.fcb = fcb;
97      }
98  
99      @Override
100     public String toString() {
101         StringBuilder sb = new StringBuilder();
102         sb.append("control code = ").append(getControlCode()).append('\n');
103         sb.append("isFcb = ").append(isFcb()).append('\n');
104         sb.append(String.format("address = 0x%02X\n", address));
105         sb.append(String.format("baudrate = %d\n", baudrate));
106 
107         return sb.toString();
108     }
109 
110     @Override
111     public JSONObject toJSON(JsonSerializeType jsonSerializeType) {
112         JSONObject result = new JSONObject();
113         result.accumulate("controlCode",
114                 getControlCode());
115         result.accumulate("subType", SEND_USER_DATA_SUBTYPE);
116 
117         if (jsonSerializeType.ALL == jsonSerializeType) {
118             result.accumulate("fcb",
119                     isFcb());
120             result.accumulate("address", address & 0xFF);
121             result.accumulate("baudrate", baudrate);
122         }
123 
124         return result;
125     }
126 
127     @Override
128     public void fromJSON(JSONObject json) {
129         fcb = json.getBoolean("fcb");
130         address = (byte) json.getInt("address");
131         baudrate = json.getInt("baudrate");
132     }
133 }