Skip to content

Commit ad62940

Browse files
committed
Allow omitting all optional SampledValue fields
According to the OCPP specification, all fields of the SampledValue class except for "value" are optional and may be omitted. However, the Java class implementation in this library initialized all fields but "phase" to their default values, and prevented resetting all but the enum fields to null. Most prominently, the "unit" field was initialized to "Wh" and could not be reset, preventing the generation of compliant messages for "Frequency" and "Power.Factor" measurands, for which the "unit" field needs to be omitted. Remove the initialization of fields to their default values from the class constructors, so that they are left as null and will be omitted when serializing the object. Exempt null references from the validations in the field setter methods, so that all fields can be set to null. Add returning the default value in the field getter methods in case the stored field value is null, where applicable. Most importantly, for the "unit" field, only replace null with "Wh" for "Energy" type measurands. This ensures the null is retained for the "Frequency" and "Power.Factor" measurands, in compliance with the OCPP specification.
1 parent 9ce7411 commit ad62940

File tree

1 file changed

+12
-27
lines changed

1 file changed

+12
-27
lines changed

ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/core/SampledValue.java

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,7 @@ public class SampledValue implements Validatable {
5353

5454
/** @deprecated use {@link #SampledValue(String)} to be sure to set required fields */
5555
@Deprecated
56-
public SampledValue() {
57-
try {
58-
setContext("Sample.Periodic");
59-
setFormat(ValueFormat.Raw);
60-
setMeasurand("Energy.Active.Import.Register");
61-
setLocation(Location.Outlet);
62-
setUnit("Wh");
63-
} catch (PropertyConstraintException ex) {
64-
logger.error("constructor of SampledValue failed", ex);
65-
}
66-
}
56+
public SampledValue() {}
6757

6858
/**
6959
* Handle required fields.
@@ -73,11 +63,6 @@ public SampledValue() {
7363
public SampledValue(String value) {
7464
try {
7565
setValue(value);
76-
setContext("Sample.Periodic");
77-
setFormat(ValueFormat.Raw);
78-
setMeasurand("Energy.Active.Import.Register");
79-
setLocation(Location.Outlet);
80-
setUnit("Wh");
8166
} catch (PropertyConstraintException ex) {
8267
logger.error("constructor of SampledValue failed", ex);
8368
}
@@ -117,7 +102,7 @@ public void setValue(String value) {
117102
* @return enum value for context.
118103
*/
119104
public String getContext() {
120-
return context;
105+
return context == null ? "Sample.Periodic" : context;
121106
}
122107

123108
/**
@@ -132,7 +117,7 @@ public String getContext() {
132117
// TODO: Change to enum, solve format issue and change exception message.
133118
@XmlElement
134119
public void setContext(String context) {
135-
if (!isValidContext(context)) {
120+
if (context != null && !isValidContext(context)) {
136121
throw new PropertyConstraintException(context, "context is not properly defined");
137122
}
138123

@@ -159,7 +144,7 @@ private boolean isValidContext(String context) {
159144
* @return the {@link ValueFormat}.
160145
*/
161146
public ValueFormat getFormat() {
162-
return format;
147+
return format == null ? ValueFormat.Raw : format;
163148
}
164149

165150
/**
@@ -179,7 +164,7 @@ public void setFormat(ValueFormat format) {
179164
*/
180165
@Deprecated
181166
public ValueFormat objFormat() {
182-
return format;
167+
return format == null ? ValueFormat.Raw : format;
183168
}
184169

185170
/**
@@ -188,7 +173,7 @@ public ValueFormat objFormat() {
188173
* @return enum value of measurand.
189174
*/
190175
public String getMeasurand() {
191-
return measurand;
176+
return measurand == null ? "Energy.Active.Import.Register" : measurand;
192177
}
193178

194179
/**
@@ -208,7 +193,7 @@ public String getMeasurand() {
208193
// TODO: Change to enum, solve format issue and change exception message.
209194
@XmlElement
210195
public void setMeasurand(String measurand) {
211-
if (!isValidMeasurand(measurand))
196+
if (measurand != null && !isValidMeasurand(measurand))
212197
throw new PropertyConstraintException(measurand, "measurand value is not properly defined");
213198

214199
this.measurand = measurand;
@@ -265,7 +250,7 @@ public String getPhase() {
265250
// TODO: Change to enum, solve format issue and change exception message.
266251
@XmlElement
267252
public void setPhase(String phase) {
268-
if (!isValidPhase(phase)) {
253+
if (phase != null && !isValidPhase(phase)) {
269254
throw new PropertyConstraintException(phase, "phase is not properly defined");
270255
}
271256

@@ -283,7 +268,7 @@ private boolean isValidPhase(String phase) {
283268
* @return the {@link Location}.
284269
*/
285270
public Location getLocation() {
286-
return location;
271+
return location == null ? Location.Outlet : location;
287272
}
288273

289274
/**
@@ -303,7 +288,7 @@ public void setLocation(Location location) {
303288
*/
304289
@Deprecated
305290
public Location objLocation() {
306-
return location;
291+
return location == null ? Location.Outlet : location;
307292
}
308293

309294
/**
@@ -312,7 +297,7 @@ public Location objLocation() {
312297
* @return Unit of Measure.
313298
*/
314299
public String getUnit() {
315-
return unit;
300+
return unit == null && getMeasurand().startsWith("Energy") ? "Wh" : unit;
316301
}
317302

318303
/**
@@ -328,7 +313,7 @@ public String getUnit() {
328313
// TODO: Change to enum, solve format issue and change exception message.
329314
@XmlElement
330315
public void setUnit(String unit) {
331-
if (!isValidUnit(unit)) {
316+
if (unit != null && !isValidUnit(unit)) {
332317
throw new PropertyConstraintException(unit, "unit is not properly defined");
333318
}
334319

0 commit comments

Comments
 (0)