1 package net.sf.mbus4j.dataframes.datablocks;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
43
44
45 public class DateAndTimeDataBlock extends DataBlock {
46
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
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
85
86 public boolean isRes1() {
87 return res1;
88 }
89
90
91
92
93 public boolean isRes2() {
94 return res2;
95 }
96
97
98
99
100 public boolean isRes3() {
101 return res3;
102 }
103
104
105
106
107 public boolean isSummerTime() {
108 return summerTime;
109 }
110
111 public boolean isValid() {
112 return valid;
113 }
114
115
116
117
118 public void setRes1(boolean res1) {
119 this.res1 = res1;
120 }
121
122
123
124
125 public void setRes2(boolean res2) {
126 this.res2 = res2;
127 }
128
129
130
131
132 public void setRes3(boolean res3) {
133 this.res3 = res3;
134 }
135
136
137
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
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
237 } catch (ParseException ex) {
238 throw new RuntimeException(ex);
239 }
240
241 }
242 }