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 java.util.Iterator;
31  import net.sf.json.JSONObject;
32  
33  import net.sf.mbus4j.dataframes.datablocks.DataBlock;
34  import net.sf.mbus4j.json.JsonSerializeType;
35  
36  /**
37   *
38   * @author arnep@users.sourceforge.net
39   * @version $Id: GeneralApplicationError.java 104 2014-02-21 09:31:17Z arnep $
40   */
41  public class GeneralApplicationError implements LongFrame, PrimaryAddress {
42  
43      public static String RSP_UD_SUBTYPE = "general application error";
44  
45      @Override
46      public JSONObject toJSON(JsonSerializeType jsonSerializeType) {
47          JSONObject result = new JSONObject();
48          result.accumulate("controlCode", getControlCode());
49          result.accumulate("subType", RSP_UD_SUBTYPE);
50          if (jsonSerializeType.ALL == jsonSerializeType) {
51              result.accumulate("acd", isAcd());
52              result.accumulate("dfc", isDfc());
53              result.accumulate("address", address & 0xFF);
54              result.accumulate("error", error.getLabel());
55          }
56          return result;
57      }
58  
59      @Override
60      public void fromJSON(JSONObject json) {
61          acd = json.getBoolean("acd");
62          dfc = json.getBoolean("dfc");
63          address = (byte) json.getInt("address");
64          error = GeneralApplicationErrorEnum.fromLabel(json.getString("error"));
65      }
66  
67      public static enum GeneralApplicationErrorEnum {
68  
69          UNSPECIFIED_ERROR(0x00, "Unspecified error"),
70          UNIMPLEMENTED_CI_FIELD(0x01, "unimplemented ci-field"),
71          BUFFER_TOO_LONG(0x02, "buffer too long, truncated"),
72          TOO_MANX_RECORDS(0x03, "too manx records"),
73          REMATURE_END_OF_RECORDS(0x04, "premature end of records"),
74          MORE_THAN_10_DIFES(0x05, "more tham 10 DIFE's"),
75          MORE_THAN_10_VIFES(0x06, "more tham 10 VIFE's"),
76          RESERVED_0X07(0x07, "Reserved 0x07"),
77          APP_TOO_BUSY_FOR_HANDLING_READOUT_REQUEST(0x08, "application to busy for handling readout request"),
78          TOO_MANY_READOUTS(0x09, "too many readouts(for slaves with limited readouts per time");
79  
80          private static GeneralApplicationErrorEnum valueOf(int id) {
81              for (GeneralApplicationErrorEnum e : GeneralApplicationErrorEnum.values()) {
82                  if (e.id == id) {
83                      return e;
84                  }
85              }
86              return null;
87          }
88          public final byte id;
89          private final String label;
90  
91          private GeneralApplicationErrorEnum(int id, String label) {
92              this.id = (byte) id;
93              this.label = label;
94          }
95  
96          @Override
97          public String toString() {
98              return label;
99          }
100 
101         public String getLabel() {
102             return label;
103         }
104 
105         public static GeneralApplicationErrorEnum fromLabel(String label) {
106             for (GeneralApplicationErrorEnum value : values()) {
107                 if (value.label.equals(label)) {
108                     return value;
109                 }
110             }
111             return valueOf(label);
112         }
113     }
114     private boolean acd;
115     private boolean dfc;
116     private byte address;
117     private GeneralApplicationErrorEnum error;
118 
119     public GeneralApplicationError() {
120         super();
121     }
122 
123     public GeneralApplicationError(UserDataResponse old) {
124         this.acd = old.isAcd();
125         this.dfc = old.isDfc();
126         this.address = old.getAddress();
127     }
128 
129     @Override
130     public boolean addDataBlock(DataBlock dataBlock) {
131         throw new UnsupportedOperationException("Not supported yet.");
132     }
133 
134     /**
135      * @return the address
136      */
137     @Override
138     public byte getAddress() {
139         return address;
140     }
141 
142     @Override
143     public ControlCode getControlCode() {
144         return ControlCode.RSP_UD;
145     }
146 
147     /**
148      * @return the error
149      */
150     public GeneralApplicationErrorEnum getError() {
151         return error;
152     }
153 
154     @Override
155     public DataBlock getLastDataBlock() {
156         throw new UnsupportedOperationException("Not supported yet.");
157     }
158 
159     /**
160      * @return the acd
161      */
162     public boolean isAcd() {
163         return acd;
164     }
165 
166     /**
167      * @return the dfc
168      */
169     public boolean isDfc() {
170         return dfc;
171     }
172 
173     @Override
174     public Iterator<DataBlock> iterator() {
175         return new Iterator<DataBlock>() {
176 
177             @Override
178             public boolean hasNext() {
179                 return false;
180             }
181 
182             @Override
183             public DataBlock next() {
184                 throw new UnsupportedOperationException("Not supported yet.");
185             }
186 
187             @Override
188             public void remove() {
189                 throw new UnsupportedOperationException("Not supported yet.");
190             }
191         };
192     }
193 
194     @Override
195     public void replaceDataBlock(DataBlock oldDataBlock, DataBlock newDataBlock) {
196         throw new UnsupportedOperationException("Not supported yet.");
197     }
198 
199     /**
200      * @param acd the acd to set
201      */
202     public void setAcd(boolean acd) {
203         this.acd = acd;
204     }
205 
206     /**
207      * @param address the address to set
208      */
209     @Override
210     public void setAddress(byte address) {
211         this.address = address;
212     }
213 
214     /**
215      * @param dcf the dfc to set
216      */
217     public void setDfc(boolean dfc) {
218         this.dfc = dfc;
219     }
220 
221     public void setError(byte b) {
222         error = GeneralApplicationErrorEnum.valueOf(b);
223     }
224 
225     /**
226      * @param error the error to set
227      */
228     public void setError(GeneralApplicationErrorEnum error) {
229         this.error = error;
230     }
231 
232     @Override
233     public String toString() {
234         StringBuilder sb = new StringBuilder();
235         sb.append("control code = ").append(getControlCode()).append('\n');
236         sb.append("isAcd = ").append(isAcd()).append('\n');
237         sb.append("isDfc = ").append(isDfc()).append('\n');
238         sb.append(String.format("address = 0x%02X\n", address));
239         sb.append("error = ").append(error).append('\n');
240         return sb.toString();
241     }
242 }