|
| 1 | +package eu.chargetime.ocpp.test; |
| 2 | + |
| 3 | +import eu.chargetime.ocpp.JSONServer; |
| 4 | +import eu.chargetime.ocpp.ServerEvents; |
| 5 | +import eu.chargetime.ocpp.feature.profile.ServerCoreEventHandler; |
| 6 | +import eu.chargetime.ocpp.feature.profile.ServerCoreProfile; |
| 7 | +import eu.chargetime.ocpp.model.Confirmation; |
| 8 | +import eu.chargetime.ocpp.model.Request; |
| 9 | +import eu.chargetime.ocpp.model.core.*; |
| 10 | + |
| 11 | +import java.util.Calendar; |
| 12 | + |
| 13 | +/* |
| 14 | + ChargeTime.eu - Java-OCA-OCPP |
| 15 | + Copyright (C) 2015-2016 Thomas Volden <[email protected]> |
| 16 | +
|
| 17 | + MIT License |
| 18 | +
|
| 19 | + Copyright (c) 2016 Thomas Volden |
| 20 | +
|
| 21 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 22 | + of this software and associated documentation files (the "Software"), to deal |
| 23 | + in the Software without restriction, including without limitation the rights |
| 24 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 25 | + copies of the Software, and to permit persons to whom the Software is |
| 26 | + furnished to do so, subject to the following conditions: |
| 27 | +
|
| 28 | + The above copyright notice and this permission notice shall be included in all |
| 29 | + copies or substantial portions of the Software. |
| 30 | +
|
| 31 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 32 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 33 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 34 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 35 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 36 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 37 | + SOFTWARE. |
| 38 | + */ |
| 39 | +public class JSONServerSample |
| 40 | +{ |
| 41 | + private JSONServer server; |
| 42 | + |
| 43 | + public void started() throws Exception |
| 44 | + { |
| 45 | + if (server != null) |
| 46 | + return; |
| 47 | + |
| 48 | + // The core profile is mandatory |
| 49 | + ServerCoreProfile core = new ServerCoreProfile(new ServerCoreEventHandler() { |
| 50 | + @Override |
| 51 | + public AuthorizeConfirmation handleAuthorizeRequest(int sessionIndex, AuthorizeRequest request) { |
| 52 | + |
| 53 | + System.out.println(request); |
| 54 | + // ... handle event |
| 55 | + |
| 56 | + return new AuthorizeConfirmation(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public BootNotificationConfirmation handleBootNotificationRequest(int sessionIndex, BootNotificationRequest request) { |
| 61 | + |
| 62 | + System.out.println(request); |
| 63 | + // ... handle event |
| 64 | + |
| 65 | + return null; // returning null means unsupported feature |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public DataTransferConfirmation handleDataTransferRequest(int sessionIndex, DataTransferRequest request) { |
| 70 | + |
| 71 | + System.out.println(request); |
| 72 | + // ... handle event |
| 73 | + |
| 74 | + return null; // returning null means unsupported feature |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public HeartbeatConfirmation handleHeartbeatRequest(int sessionIndex, HeartbeatRequest request) { |
| 79 | + |
| 80 | + System.out.println(request); |
| 81 | + // ... handle event |
| 82 | + |
| 83 | + return null; // returning null means unsupported feature |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public MeterValuesConfirmation handleMeterValuesRequest(int sessionIndex, MeterValuesRequest request) { |
| 88 | + |
| 89 | + System.out.println(request); |
| 90 | + // ... handle event |
| 91 | + |
| 92 | + return null; // returning null means unsupported feature |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public StartTransactionConfirmation handleStartTransactionRequest(int sessionIndex, StartTransactionRequest request) { |
| 97 | + |
| 98 | + System.out.println(request); |
| 99 | + // ... handle event |
| 100 | + |
| 101 | + return null; // returning null means unsupported feature |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public StatusNotificationConfirmation handleStatusNotificationRequest(int sessionIndex, StatusNotificationRequest request) { |
| 106 | + |
| 107 | + System.out.println(request); |
| 108 | + // ... handle event |
| 109 | + |
| 110 | + return null; // returning null means unsupported feature |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public StopTransactionConfirmation handleStopTransactionRequest(int sessionIndex, StopTransactionRequest request) { |
| 115 | + |
| 116 | + System.out.println(request); |
| 117 | + // ... handle event |
| 118 | + |
| 119 | + return null; // returning null means unsupported feature |
| 120 | + } |
| 121 | + }); |
| 122 | + |
| 123 | + server = new JSONServer(core); |
| 124 | + server.open("localhost", 8887, new ServerEvents() { |
| 125 | + |
| 126 | + @Override |
| 127 | + public void newSession(int sessionIndex) { |
| 128 | + |
| 129 | + // sessionIndex is used to send messages. |
| 130 | + System.out.println("New session " + sessionIndex); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void lostSession(int sessionIndex) { |
| 135 | + |
| 136 | + System.out.println("Session " + sessionIndex + " lost connection"); |
| 137 | + } |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + public void sendClearCacheRequest() throws Exception { |
| 142 | + |
| 143 | + // Use the feature profile to help create event |
| 144 | + ClearCacheRequest request = core.createClearCacheRequest(); |
| 145 | + |
| 146 | + // Server returns a promise which will be filled once it receives a confirmation. |
| 147 | + // Select the distination client with the sessionIndex integer. |
| 148 | + server.send(sessionIndex, request).whenComplete((confirmation, throwable) -> System.out.println(confirmation)); |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments