Skip to content

Commit e1f82ad

Browse files
committed
Added MeterValues feature to core profile.
1 parent 7895305 commit e1f82ad

File tree

14 files changed

+1244
-21
lines changed

14 files changed

+1244
-21
lines changed

ocpp-common/src/test/java/eu/chargetime/ocpp/test/SessionTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ public void open_connectsViaCommunicator() {
137137
}
138138

139139
@Test
140-
public void onCall_unhandledCallback_callSendCallError() {
140+
public void onCall_unhandledCallback_callSendCallError() throws Exception {
141141
// Given
142142
String someId = "Some id";
143143
when(sessionEvents.handleRequest(any())).thenReturn(null);
144+
when(communicator.unpackPayload(any(), any())).thenReturn(new TestRequest());
144145

145146
// When
146147
eventHandler.onCall(someId, null, null);

ocpp-test/src/main/java/eu/chargetime/ocpp/test/FakeCentralSystem.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ public boolean hasReceivedAuthorizeRequest()
122122
return receivedMessage != null && "Authorize".equals(getAction());
123123
}
124124

125+
public boolean hasReceivedMeterValuesRequest() {
126+
return receivedMessage != null && "MeterValues".equals(getAction());
127+
}
128+
125129
private JSONObject getCallPayload()
126130
{
127131
return (JSONObject)receivedMessage[3];
@@ -214,11 +218,12 @@ public enum AvailabilityType {
214218
Inoperative, Operative
215219
}
216220

217-
public void sendChangeAvailability(int connectorId, AvailabilityType type)
221+
public void sendChangeAvailabilityRequest(int connectorId, AvailabilityType type)
218222
{
219223
String payload = "\"connectorId\":%d,\"type\":\"%s\"";
220224
sendRequest("ChangeAvailability", String.format(payload, connectorId, type));
221225
}
226+
222227
public enum RegistrationStatus {
223228
Accepted, Pending, Rejected
224229
}
@@ -228,6 +233,10 @@ public void sendHeartbeatConfirmation() {
228233
sendConfirmation(String.format(payload, now()));
229234
}
230235

236+
public void sendMeterValuesConfirmation() {
237+
sendConfirmation("");
238+
}
239+
231240
public void sendBootConfirmation(RegistrationStatus status) {
232241
String payload = "\"currentTime\": \"%s\", \"interval\": %d, \"status\": \"%s\"";
233242
sendConfirmation(String.format(payload, now(), 300, status));

ocpp-test/src/main/java/eu/chargetime/ocpp/test/FakeChargePoint.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package eu.chargetime.ocpp.test;
22

33
import eu.chargetime.ocpp.*;
4-
import eu.chargetime.ocpp.model.*;
54
import eu.chargetime.ocpp.feature.profile.ClientCoreEventHandler;
65
import eu.chargetime.ocpp.feature.profile.CoreProfile;
6+
import eu.chargetime.ocpp.model.*;
7+
8+
import java.util.Calendar;
79

810
import static org.hamcrest.MatcherAssert.assertThat;
9-
import static org.hamcrest.Matchers.equalTo;
10-
import static org.hamcrest.Matchers.instanceOf;
11-
import static org.hamcrest.Matchers.is;
11+
import static org.hamcrest.Matchers.*;
1212

1313
/**
1414
ChargeTime.eu - Java-OCA-OCPP
@@ -107,8 +107,12 @@ public void sendHeartbeatRequest() {
107107
}
108108

109109
public void sendMeterValuesRequest() {
110-
Request request = core.createMeterValuesRequest();
111-
send(request);
110+
try {
111+
Request request = core.createMeterValuesRequest(42, Calendar.getInstance(), "42");
112+
send(request);
113+
} catch (PropertyConstraintException ex) {
114+
ex.printStackTrace();
115+
}
112116
}
113117

114118
public void sendDataTransferRequest(String vendorId, String messageId, String data) {
@@ -146,6 +150,10 @@ public void hasReceivedHeartbeatConfirmation() {
146150
assertThat(receivedConfirmation, instanceOf(HeartbeatConfirmation.class));
147151
}
148152

153+
public void hasReceivedMeterValuesConfirmation() {
154+
assertThat(receivedConfirmation, instanceOf(MeterValuesConfirmation.class));
155+
}
156+
149157
public void disconnect() {
150158
client.disconnect();
151159
}

ocpp-v1_6/src/it/groovy/core_features/ChangeAvailability.groovy

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package core_features
22

33
import eu.chargetime.ocpp.test.FakeCentralSystem
44
import eu.chargetime.ocpp.test.FakeChargePoint
5-
import spock.lang.*
6-
import spock.util.concurrent.PollingConditions;
5+
import spock.lang.Shared
6+
import spock.lang.Specification
7+
import spock.util.concurrent.PollingConditions
78

89
class ChangeAvailability extends Specification
910
{
@@ -26,7 +27,7 @@ class ChangeAvailability extends Specification
2627
def "Central System sends a ChangeAvailability request and receives a response"() {
2728
def conditions = new PollingConditions(timeout: 1)
2829
when:
29-
centralSystem.sendChangeAvailability(0, FakeCentralSystem.AvailabilityType.Inoperative);
30+
centralSystem.sendChangeAvailabilityRequest(0, FakeCentralSystem.AvailabilityType.Inoperative);
3031

3132
then:
3233
conditions.eventually {

ocpp-v1_6/src/it/groovy/core_features/MeterValues.groovy

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ class MeterValues extends Specification
2929
when:
3030
chargePoint.sendMeterValuesRequest();
3131

32-
3332
then:
3433
conditions.eventually {
35-
centralSystem.hasReceivedMeterValues();
34+
centralSystem.hasReceivedMeterValuesRequest();
3635
}
3736

3837
when:
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package eu.chargetime.ocpp.feature;
2+
3+
import eu.chargetime.ocpp.feature.profile.Profile;
4+
import eu.chargetime.ocpp.model.Confirmation;
5+
import eu.chargetime.ocpp.model.MeterValuesConfirmation;
6+
import eu.chargetime.ocpp.model.MeterValuesRequest;
7+
import eu.chargetime.ocpp.model.Request;
8+
9+
/**
10+
* ChargeTime.eu - Java-OCA-OCPP
11+
* <p>
12+
* MIT License
13+
* <p>
14+
* Copyright (C) 2016 Thomas Volden <[email protected]>
15+
* <p>
16+
* Permission is hereby granted, free of charge, to any person obtaining a copy
17+
* of this software and associated documentation files (the "Software"), to deal
18+
* in the Software without restriction, including without limitation the rights
19+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20+
* copies of the Software, and to permit persons to whom the Software is
21+
* furnished to do so, subject to the following conditions:
22+
* <p>
23+
* The above copyright notice and this permission notice shall be included in all
24+
* copies or substantial portions of the Software.
25+
* <p>
26+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32+
* SOFTWARE.
33+
*/
34+
public class MeterValuesFeature extends Feature {
35+
public MeterValuesFeature(Profile ownerProfile) {
36+
super(ownerProfile);
37+
}
38+
39+
@Override
40+
public Class<? extends Request> getRequestType() {
41+
return MeterValuesRequest.class;
42+
}
43+
44+
@Override
45+
public Class<? extends Confirmation> getConfirmationType() {
46+
return MeterValuesConfirmation.class;
47+
}
48+
49+
@Override
50+
public String getAction() {
51+
return "MeterValues";
52+
}
53+
}

ocpp-v1_6/src/main/java/eu/chargetime/ocpp/feature/profile/CoreProfile.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import eu.chargetime.ocpp.model.*;
66

77
import java.util.ArrayList;
8+
import java.util.Calendar;
89

910
/**
1011
ChargeTime.eu - Java-OCA-OCPP
@@ -49,6 +50,7 @@ public CoreProfile(ClientCoreEventHandler handler) {
4950
features.add(new ClearCacheFeature(this));
5051
features.add(new DataTransferFeature(this));
5152
features.add(new HeartbeatFeature(this));
53+
features.add(new MeterValuesFeature(this));
5254
}
5355

5456
public AuthorizeRequest createAuthorizeRequest(String idToken) throws PropertyConstraintException {
@@ -67,8 +69,24 @@ public HeartbeatRequest createHeartbeatRequest() {
6769
return new HeartbeatRequest();
6870
}
6971

70-
public MeterValuesRequest createMeterValuesRequest() {
71-
return null;
72+
public MeterValuesRequest createMeterValuesRequest(Integer connectorId, Calendar timestamp, String value) throws PropertyConstraintException {
73+
SampledValue sampledValue = new SampledValue();
74+
sampledValue.setValue(value);
75+
return createMeterValuesRequest(connectorId, timestamp, sampledValue);
76+
}
77+
78+
public MeterValuesRequest createMeterValuesRequest(Integer connectorId, Calendar timestamp, SampledValue... sampledValues) throws PropertyConstraintException {
79+
MeterValue meterValue = new MeterValue();
80+
meterValue.setTimestamp(timestamp);
81+
meterValue.setSampledValue(sampledValues);
82+
return createMeterValuesRequest(connectorId, meterValue);
83+
}
84+
85+
public MeterValuesRequest createMeterValuesRequest(Integer connectorId, MeterValue... meterValues) throws PropertyConstraintException {
86+
MeterValuesRequest request = new MeterValuesRequest();
87+
request.setConnectorId(connectorId);
88+
request.setMeterValue(meterValues);
89+
return request;
7290
}
7391

7492
@Override

ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/MeterValue.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import eu.chargetime.ocpp.PropertyConstraintException;
44

5+
import java.text.SimpleDateFormat;
56
import java.util.Calendar;
7+
import java.util.TimeZone;
68

79
/**
810
* ChargeTime.eu - Java-OCA-OCPP
@@ -51,7 +53,13 @@ public void setTimestamp(Calendar timestamp) throws PropertyConstraintException
5153
this.timestamp = timestamp;
5254
}
5355

54-
public Calendar getTimestamp() {
56+
public String getTimestamp() {
57+
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
58+
formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
59+
return formatter.format(timestamp.getTime());
60+
}
61+
62+
public Calendar objTimestamp() {
5563
return timestamp;
5664
}
5765

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package eu.chargetime.ocpp.model;
2+
3+
/**
4+
* ChargeTime.eu - Java-OCA-OCPP
5+
* <p>
6+
* MIT License
7+
* <p>
8+
* Copyright (C) 2016 Thomas Volden <[email protected]>
9+
* <p>
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy
11+
* of this software and associated documentation files (the "Software"), to deal
12+
* in the Software without restriction, including without limitation the rights
13+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
* copies of the Software, and to permit persons to whom the Software is
15+
* furnished to do so, subject to the following conditions:
16+
* <p>
17+
* The above copyright notice and this permission notice shall be included in all
18+
* copies or substantial portions of the Software.
19+
* <p>
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26+
* SOFTWARE.
27+
*/
28+
public class MeterValuesConfirmation implements Confirmation {
29+
@Override
30+
public boolean validate() {
31+
return true;
32+
}
33+
}

0 commit comments

Comments
 (0)