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: SynchronizeAction.java 145 2015-04-29 18:09:27Z arnep $
38   */
39  public class SynchronizeAction
40          implements ControlFrame {
41  
42      public static final String SEND_USER_DATA_SUBTYPE = "synchronize action";
43      private byte address;
44      private boolean fcb;
45  
46      public SynchronizeAction(SendUserData old) {
47          this.address = old.getAddress();
48          this.fcb = old.isFcb();
49      }
50  
51      public SynchronizeAction() {
52      }
53  
54      @Override
55      public byte getAddress() {
56          return address;
57      }
58  
59      @Override
60      public ControlCode getControlCode() {
61          return ControlCode.SND_UD;
62      }
63  
64      /**
65       * @return the fcb
66       */
67      public boolean isFcb() {
68          return fcb;
69      }
70  
71      @Override
72      public void setAddress(byte address) {
73          this.address = address;
74      }
75  
76      /**
77       * @param fcb the fcb to set
78       */
79      public void setFcb(boolean fcb) {
80          this.fcb = fcb;
81      }
82  
83      @Override
84      public String toString() {
85          StringBuilder sb = new StringBuilder();
86          sb.append("control code = ").append(getControlCode()).append('\n');
87          sb.append("isFcb = ").append(isFcb()).append('\n');
88          sb.append(String.format("address = 0x%02X\n", address));
89  
90          return sb.toString();
91      }
92  
93      @Override
94      public JSONObject toJSON(JsonSerializeType jsonSerializeType) {
95          JSONObject result = new JSONObject();
96          result.accumulate("controlCode",
97                  getControlCode());
98          result.accumulate("subType", SEND_USER_DATA_SUBTYPE);
99  
100         if (JsonSerializeType.ALL == jsonSerializeType) {
101             result.accumulate("fcb",
102                     isFcb());
103             result.accumulate("address", address & 0xFF);
104         }
105 
106         return result;
107     }
108 
109     @Override
110     public void fromJSON(JSONObject json) {
111         fcb = json.getBoolean("fcb");
112         address = (byte) json.getInt("address");
113     }
114 }