Skip to content

Commit 36885a0

Browse files
committed
Added connect / disconnect client events.
1 parent 665c2ea commit 36885a0

File tree

6 files changed

+90
-6
lines changed

6 files changed

+90
-6
lines changed

ocpp-common/src/main/java/eu/chargetime/ocpp/Client.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,17 @@ public Client(Session session) {
6161
*
6262
* @param uri url and port of the server
6363
*/
64-
public void connect(String uri)
64+
public void connect(String uri) {
65+
this.connect(uri, null);
66+
}
67+
68+
/**
69+
* Connect to server
70+
*
71+
* @param uri url and port of the server
72+
* @param events client events for connect/disconnect
73+
*/
74+
public void connect(String uri, ClientEvents events)
6575
{
6676
session.open(uri, new SessionEvents() {
6777
@Override
@@ -94,12 +104,14 @@ public void handleError(String uniqueId, String errorCode, String errorDescripti
94104

95105
@Override
96106
public void handleConnectionClosed() {
97-
107+
if (events != null)
108+
events.connectionClosed();
98109
}
99110

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

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

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

33
import eu.chargetime.ocpp.Client;
4+
import eu.chargetime.ocpp.ClientEvents;
45
import eu.chargetime.ocpp.Session;
56
import eu.chargetime.ocpp.SessionEvents;
67
import eu.chargetime.ocpp.feature.Feature;
@@ -87,6 +88,34 @@ public void connect_connects() {
8788
verify(session, times(1)).open(eq(someUrl), anyObject());
8889
}
8990

91+
@Test
92+
public void connect_connectionOpenedEvent() {
93+
// Given
94+
ClientEvents events = mock(ClientEvents.class);
95+
client.connect(null, events);
96+
97+
// When
98+
this.eventHandler.handleConnectionOpened();
99+
100+
// Then
101+
verify(events, times(1)).connectionOpened();
102+
verify(events, never()).connectionClosed();
103+
}
104+
105+
@Test
106+
public void connect_connectionClosedEvent() {
107+
// Given
108+
ClientEvents events = mock(ClientEvents.class);
109+
client.connect(null, events);
110+
111+
// When
112+
this.eventHandler.handleConnectionClosed();
113+
114+
// Then
115+
verify(events, times(1)).connectionClosed();
116+
verify(events, never()).connectionOpened();
117+
}
118+
90119
@Test
91120
public void send_aMessage_isCommunicated() throws Exception {
92121
// When

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,17 @@ public UnlockConnectorConfirmation handleUnlockConnectorRequest(UnlockConnectorR
131131

132132
public void connect() {
133133
try {
134-
client.connect(url);
134+
client.connect(url, new ClientEvents() {
135+
@Override
136+
public void connectionOpened() {
137+
138+
}
139+
140+
@Override
141+
public void connectionClosed() {
142+
143+
}
144+
});
135145
} catch (Exception ex) {
136146
ex.printStackTrace();
137147
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public boolean validate() {
5050
valid &= chargingProfileId != null;
5151
valid &= stackLevel >= 0;
5252
valid &= chargingProfilePurpose != null;
53-
valid &= transactionId == null || "TxProfile".equals(chargingProfilePurpose);
53+
valid &= transactionId == null || chargingProfilePurpose == ChargingProfilePurposeType.TxProfile;
5454
valid &= chargingProfileKind != null;
5555
valid &= chargingSchedule != null && chargingSchedule.validate();
5656
return valid;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public boolean validate() {
5050

5151
if (chargingProfile != null) {
5252
valid &= chargingProfile.validate();
53-
valid &= "TxProfile".equals(chargingProfile.getChargingProfilePurpose());
53+
valid &= chargingProfile.getChargingProfilePurpose() == ChargingProfilePurposeType.TxProfile;
5454
}
5555
return valid;
5656
}

0 commit comments

Comments
 (0)