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.dataframes.datablocks.DataBlock;
33  import net.sf.mbus4j.json.JSONFactory;
34  import net.sf.mbus4j.json.JsonSerializeType;
35  
36  import java.util.Iterator;
37  
38  /**
39   *
40   * @author arnep@users.sourceforge.net
41   * @version $Id: ApplicationReset.java 145 2015-04-29 18:09:27Z arnep $
42   */
43  public class ApplicationReset
44          implements LongFrame {
45  
46      public static final String SEND_USER_DATA_SUBTYPE = "application reset";
47  
48      public ApplicationReset() {
49      }
50  
51      @Override
52      public JSONObject toJSON(JsonSerializeType jsonSerializeType) {
53          JSONObject result = new JSONObject();
54          result.accumulate("controlCode",
55                  getControlCode());
56          result.accumulate("subType", SEND_USER_DATA_SUBTYPE);
57          result.accumulate("telegramType",
58                  getTelegramType().getLabel());
59          result.accumulate("subTelegram",
60                  getSubTelegram());
61  
62          if (JsonSerializeType.ALL == jsonSerializeType) {
63              result.accumulate("fcb",
64                      isFcb());
65              result.accumulate("address",
66                      JSONFactory.encodeHexByte(address));
67          }
68  
69          return result;
70      }
71  
72      @Override
73      public void fromJSON(JSONObject json) {
74          fcb = JSONFactory.getBoolean(json, "fcb", false);
75          address = JSONFactory.decodeHexByte(json, "address", (byte) 0);
76          telegramType = TelegramType.fromLabel(json.getString("telegramType"));
77          subTelegram = json.getInt("subTelegram");
78      }
79  
80      public static enum TelegramType {
81  
82          ALL(0x00, "All"),
83          USER_DATA(0x10, "User data"),
84          SIMPLE_BILLING(0x20, "Simple billing"),
85          ENHANCED_BILLING(0x30, "Enhanced billing"),
86          MULTI_TARIFF_BILLING(0x40, "Multi tariff billing"),
87          INSTANCIOUS_VALUES(0x50, "Instanious values"),
88          LOAD_MANAGEMENT_VALUES_FOR_MANAGEMENT(0x60, "Load management values for management"),
89          RESERVED_0x70(0x70, "Reserved 0x07"),
90          INSTALLATION_AND_STARTUP(0x80, "Installation and startup"),
91          TESTING(0x90, "Testing"),
92          CALIBRATION(0xA0, "Calibration"),
93          MANUFACTURING(0xB0, "Manufacturing"),
94          DEVELOPMENT(0xC0, "Development"),
95          SELFTEST(0xD0, "Selftest"),
96          RESERVED_0xE0(0xE0, "Reserved 0xE0"),
97          RESERVED_0xF0(0xF0, "Reserved 0xF0");
98  
99          public static TelegramType valueOf(int value) {
100             for (TelegramType c : TelegramType.values()) {
101                 if (c.id == value) {
102                     return c;
103                 }
104             }
105 
106             return null;
107         }
108 
109         final public byte id;
110         final public String label;
111 
112         TelegramType(int id, String label) {
113             this.id = (byte) id;
114             this.label = label;
115         }
116 
117         @Override
118         public String toString() {
119             return label;
120         }
121 
122         public String getLabel() {
123             return label;
124         }
125 
126         public static TelegramType fromLabel(String label) {
127             for (TelegramType value : values()) {
128                 if (value.label.equals(label)) {
129                     return value;
130                 }
131             }
132 
133             return valueOf(label);
134         }
135     }
136 
137     private byte address;
138     private boolean fcb;
139     private TelegramType telegramType;
140     private int subTelegram;
141 
142     public ApplicationReset(SendUserData old) {
143         this.address = old.getAddress();
144         this.setFcb(old.isFcb());
145     }
146 
147     public ApplicationReset(TelegramType telegramType, int subTelegram) {
148         this.telegramType = telegramType;
149         this.subTelegram = subTelegram;
150     }
151 
152     @Override
153     public boolean addDataBlock(DataBlock dataBlock) {
154         throw new UnsupportedOperationException("Not supported yet.");
155     }
156 
157     @Override
158     public byte getAddress() {
159         return address;
160     }
161 
162     @Override
163     public ControlCode getControlCode() {
164         return ControlCode.SND_UD;
165     }
166 
167     @Override
168     public DataBlock getLastDataBlock() {
169         throw new UnsupportedOperationException("Not supported yet.");
170     }
171 
172     /**
173      * @return the subTelegram
174      */
175     public int getSubTelegram() {
176         return subTelegram;
177     }
178 
179     /**
180      * @return the subcode
181      */
182     public TelegramType getTelegramType() {
183         return telegramType;
184     }
185 
186     public boolean isFcb() {
187         return fcb;
188     }
189 
190     @Override
191     public Iterator<DataBlock> iterator() {
192         return new Iterator<DataBlock>() {
193             @Override
194             public boolean hasNext() {
195                 return false;
196             }
197 
198             @Override
199             public DataBlock next() {
200                 throw new UnsupportedOperationException("Not supported yet.");
201             }
202 
203             @Override
204             public void remove() {
205                 throw new UnsupportedOperationException("Not supported yet.");
206             }
207         };
208     }
209 
210     @Override
211     public void replaceDataBlock(DataBlock oldDataBlock, DataBlock newDataBlock) {
212         throw new UnsupportedOperationException("Not supported yet.");
213     }
214 
215     @Override
216     public void setAddress(byte address) {
217         this.address = address;
218     }
219 
220     public void setFcb(boolean fcb) {
221         this.fcb = fcb;
222     }
223 
224     /**
225      * @param subTelegram the subTelegram to set
226      */
227     public void setSubTelegram(int subTelegram) {
228         this.subTelegram = subTelegram;
229     }
230 
231     /**
232      * @param subcode the subcode to set
233      */
234     public void setTelegramType(TelegramType telegramType) {
235         this.telegramType = telegramType;
236     }
237 
238     /**
239      * @param subcode the subcode to set
240      */
241     public void setTelegramTypeAndSubTelegram(int value) {
242         this.telegramType = TelegramType.valueOf(value & 0xF0);
243         this.subTelegram = value & 0x0F;
244     }
245 
246     @Override
247     public String toString() {
248         StringBuilder sb = new StringBuilder();
249         sb.append("control code = ").append(getControlCode()).append('\n');
250         sb.append("isFcb = ").append(isFcb()).append('\n');
251         sb.append(String.format("address = 0x%02X\n", address));
252         sb.append("telegramType = ").append(getTelegramType()).append('\n');
253         sb.append("subTelegram = ").append(getSubTelegram()).append('\n');
254 
255         return sb.toString();
256     }
257 
258     @Override
259     public boolean equals(Object o) {
260         if (o instanceof ApplicationReset) {
261             ApplicationReset other = (ApplicationReset) o;
262 
263             return (getTelegramType() == other.getTelegramType())
264                     && (getSubTelegram() == other.getSubTelegram());
265         } else {
266             return false;
267         }
268     }
269 
270 }