View Javadoc
1   package net.sf.mbus4j.dataframes.datablocks;
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.text.DateFormat;
31  import java.text.ParseException;
32  import java.text.SimpleDateFormat;
33  import java.util.Date;
34  import net.sf.json.JSONObject;
35  
36  import net.sf.mbus4j.dataframes.datablocks.dif.DataFieldCode;
37  import net.sf.mbus4j.dataframes.datablocks.vif.Vif;
38  import net.sf.mbus4j.dataframes.datablocks.vif.Vife;
39  
40  /**
41   *
42   * @author arnep@users.sourceforge.net
43   * @version $Id: DateAndTimeDataBlock.java 162 2016-07-27 18:07:08Z arnep $
44   */
45  public class DateAndTimeDataBlock extends DataBlock {
46      // SimpleDateFormat is not thread-safe, so give one to each thread
47      private static final ThreadLocal<SimpleDateFormat> ISO_8601 = new ThreadLocal<SimpleDateFormat>(){
48          @Override
49          protected SimpleDateFormat initialValue()
50          {
51              return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
52          }
53      };
54  
55      private Date value;
56      private boolean valid;
57      private boolean summerTime;
58      private boolean res1;
59      private boolean res2;
60      private boolean res3;
61  
62      public DateAndTimeDataBlock() {
63          super();
64      }
65  
66      @Deprecated
67      public DateAndTimeDataBlock(Vif vif, Vife... vifes) {
68          super(DataFieldCode._32_BIT_INTEGER, vif, vifes);
69      }
70  
71      /**
72       * @return the value
73       */
74      public Date getValue() {
75          return value;
76      }
77  
78      @Override
79      public String getValueAsString() {
80          return ISO_8601.get().format(value);
81      }
82  
83      /**
84       * @return the res1
85       */
86      public boolean isRes1() {
87          return res1;
88      }
89  
90      /**
91       * @return the res2
92       */
93      public boolean isRes2() {
94          return res2;
95      }
96  
97      /**
98       * @return the res3
99       */
100     public boolean isRes3() {
101         return res3;
102     }
103 
104     /**
105      * @return the summerTime
106      */
107     public boolean isSummerTime() {
108         return summerTime;
109     }
110 
111     public boolean isValid() {
112         return valid;
113     }
114 
115     /**
116      * @param res1 the res1 to set
117      */
118     public void setRes1(boolean res1) {
119         this.res1 = res1;
120     }
121 
122     /**
123      * @param res2 the res2 to set
124      */
125     public void setRes2(boolean res2) {
126         this.res2 = res2;
127     }
128 
129     /**
130      * @param res3 the res3 to set
131      */
132     public void setRes3(boolean res3) {
133         this.res3 = res3;
134     }
135 
136     /**
137      * @param summerTime the summerTime to set
138      */
139     public void setSummerTime(boolean summerTime) {
140         this.summerTime = summerTime;
141     }
142 
143     public void setValid(boolean valid) {
144         this.valid = valid;
145     }
146 
147     /**
148      * @param value the value to set
149      */
150     public void setValue(Date value) {
151         this.value = value;
152     }
153 
154     @Override
155     public void toString(StringBuilder sb, String inset) {
156         if (getAction() != null) {
157             sb.append(inset).append("action = ").append(getAction()).append("\n");
158         }
159         sb.append(inset).append("dataType = ").append(getDataFieldCode()).append("\n");
160         if (getVif() != null) {
161             sb.append(inset).append("description = ").append(getParamDescr()).append("\n");
162             if (getUnitOfMeasurement() != null) {
163                 sb.append(inset).append("unit =");
164                 if (getExponent() != null) {
165 
166                     if (getExponent() > 0) {
167                         sb.append(" * 1");
168                         for (int i = 0; i < getExponent(); i++) {
169                             sb.append("0");
170                         }
171                     } else if (getExponent() < 0) {
172                         sb.append(" * 0.");
173                         for (int i = -1; i > getExponent(); i--) {
174                             sb.append("0");
175                         }
176                         sb.append("1");
177 
178                     }
179                 }
180                 sb.append(" [");
181                 if (getSiPrefix() != null) {
182                     sb.append(getSiPrefix());
183                 }
184                 sb.append(getUnitOfMeasurement()).append("]\n");
185             } else {
186                 sb.append("\n");
187             }
188             sb.append(inset).append("value = ").append(getValueAsString()).append("\n");
189         }
190         if (getFunctionField() != null) {
191             sb.append(inset).append("tariff = ").append(getTariff()).append('\n');
192             sb.append(inset).append("storageNumber = ").append(getStorageNumber()).append("\n");
193             sb.append(inset).append("functionField = ").append(getFunctionField()).append("\n");
194             sb.append(inset).append("subUnit = ").append(getSubUnit()).append("\n");
195         }
196         sb.append(inset).append("valid = ").append(valid).append("\n");
197         sb.append(inset).append("summertime = ").append(summerTime).append("\n");
198         sb.append(inset).append("res1 = ").append(res1).append("\n");
199         sb.append(inset).append("res2 = ").append(res2).append("\n");
200         sb.append(inset).append("res3 = ").append(res3).append("\n");
201     }
202 
203     @Override
204     protected void accumulateDatatoJSON(JSONObject json) {
205         JSONObject jsonValue = new JSONObject();
206         jsonValue.accumulate("timestamp", ISO_8601.get().format(value));
207         jsonValue.accumulate("summertime", summerTime);
208         jsonValue.accumulate("valid", valid);
209         jsonValue.accumulate("res1", res1);
210         jsonValue.accumulate("res2", res2);
211         jsonValue.accumulate("res3", res3);
212         json.accumulate("data", jsonValue);
213     }
214 
215     @Override
216     public void fromJSON(JSONObject json) {
217         super.fromJSON(json);
218         try {
219             JSONObject jsonValue = json.getJSONObject("data");
220             valid = jsonValue.getBoolean("valid");
221             summerTime = jsonValue.getBoolean("summertime");
222             res1 = jsonValue.getBoolean("res1");
223             res2 = jsonValue.getBoolean("res2");
224             res3 = jsonValue.getBoolean("res3");
225             value = ISO_8601.get().parse(jsonValue.getString("timestamp"));
226         } catch (ParseException ex) {
227             throw new RuntimeException(ex);
228         }
229     }
230 
231     @Override
232     public void setValue(String text) {
233         try {
234             value = DateFormat.getDateTimeInstance().parse(text);
235             valid = true;
236             //TODO summerTime
237         } catch (ParseException ex) {
238             throw new RuntimeException(ex);
239         }
240 
241     }
242 }