-
Notifications
You must be signed in to change notification settings - Fork 1
/
chargebox.html
397 lines (341 loc) · 12.8 KB
/
chargebox.html
1
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<html>
<head>
<style>
body {
background: #000;
color: white;
font-family: Arial;
}
button, input, select {
display: block;
width: 100%;
margin-bottom: 5px;
cursor: pointer;
}
button {
background: #369;
color: #fff;
padding: 5px 0;
border: none;
}
ul {
background: #000;
color: #eee;
}
#red, #green, #blue {
min-width: 10px;
}
#red {
background-color: red;
}
#green {
background-color: green;
}
#blue {
background-color: blue;
}
#yellow {
background-color: yellow;
}
</style>
</head>
<body>
<h1>Simple OCPP 1.6 Chargebox Simulator</h1>
<select style="display:none;"><option value="">OCPP-J1.6</option></select>
<label>Central Station</label><input id="CP" type="text" placeholder="Id Station" value="ws://Localhost:9000/CP_1"/>
<label>Tag</label><input id="TAG" type="text" placeholder="Tag" value="B4A63CDF"/>
<h2>Actions</h2>
<button id="connect">Connect</button>
<button id="send">Authorize</button>
<button id="start">Start Transaction</button>
<button id="stop">Stop Transaction</button>
<button id="heartbeat">Heartbeat</button>
<label>Meter value</label><input id="metervalue" type="text" placeholder="Meter value" value="1"/>
<button id="mv">Send Meter Values</button>
<button id="status">Status Notification</button>
<button id="data_transfer">Data Tranfer</button>
<div>
<span class="indicator" id="red">____</span>
<span class="indicator" id="green">____</span>
<span class="indicator" id="blue">____</span>
<span class="indicator" id="yellow">____</span>
</div>
<ul id="console"></ul>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script>
var c = 0;
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var id = randomId();
var _websocket = null;
var connector_locked = false;
function formatDate(date) {
var day = String(date.getDate());
if (day.length <2){
day = ('0' + day.slice(-2));
}
var monthIndex = String(date.getMonth()+1);
if (monthIndex.length <2){
monthIndex = ('0' + monthIndex.slice(-2));
}
var year = date.getFullYear();
var h = date.getHours();
var m = String(date.getMinutes());
var s = String(date.getSeconds());
if (h.length <2){
h = ('0' + h.slice(-2));
}
if (m.length <2){
m = ('0' + m.slice(-2));
}
if (s.length <2){
s = ('0' + s.slice(-2));
}
return year + '-' + monthIndex + '-' + day+"T"+h+":"+m+":"+s+"Z";
}
function randomId() {
id = "";
for (var i = 0; i < 36; i++) {
id += possible.charAt(Math.floor(Math.random() * possible.length));
}
return id;
}
function wsConnect() {
var wsurl = $('select').val();
var CP = $('#CP').val();
if (_websocket) {
$('#red').show();
_websocket.close(3001);
} else {
_websocket = new WebSocket(wsurl + "" + CP, ["ocpp1.6", "ocpp1.5"]);
_websocket.onopen = function (authorizationData) {
sessionStorage.setItem('LastAction', "BootNotification");
$('#yellow').show();
BootNotification();
$('#connect').text('Disconnect').css('background', 'green');
};
_websocket.onmessage = function (msg) {
c++;
var ddata = (JSON.parse(msg.data));
if(c==1){
var hb_interval = handleData(ddata);
sessionStorage.setItem("Configuration",hb_interval);
startHB(hb_interval*1000);
}
if (ddata[0] === 3) {
la = getLastAction();
if (la == "startTransaction"){
ddata = ddata[2];
logMsg("Data exchange successful!");
logMsg(ddata)
var array = $.map(ddata, function (value, index) {
return [value];
});
var TransactionId = (array[0]);
sessionStorage.setItem('TransactionId', TransactionId);
logMsg("Response: " + JSON.stringify(ddata));
}else{
logMsg("Response: " + JSON.stringify(ddata[2]));
}
} else if ((JSON.parse(msg.data))[0] === 4) {
logMsg("Data exchange failed - JSON is not accepted!");
} else if ((JSON.parse(msg.data))[0] === 2) {
logMsg((JSON.parse(msg.data))[2]);
id = (JSON.parse(msg.data))[1];
switch (ddata[2]) {
case "Reset":
//Reset type SOFT, HARD
var ResetS = JSON.stringify([3, id, {"status": "Accepted"}]);
_websocket.send(ResetS);
location.reload();
break;
case "RemoteStopTransaction":
//TransactionID
var remStp = JSON.stringify([3, id, {"status": "Accepted"}]);
_websocket.send(remStp);
var stop_id = (JSON.parse(msg.data)[3].transactionId);
stopTransaction(stop_id);
$('.indicator').hide();
$('#yellow').show();
break;
case "RemoteStartTransaction":
//Need to get idTag, connectorId (map - ddata[3])
var remStrt = JSON.stringify([3, id, {"status": "Accepted"}]);
_websocket.send(remStrt);
startTransaction();
break;
case "UnlockConnector": /////////ERROR!!!!!!!!
//connectorId
var UC = JSON.stringify([3, id, {"status": "Accepted"}]);
_websocket.send(UC);
// connector_locked = false;
// $('.indicator').hide();
//$('#yellow').show();
//logMsg("Connector status changed to: "+connector_locked);
break;
default:
var error = JSON.stringify([4, id]);
_websocket.send(error);
break;
}
}
};
_websocket.onclose = function (evt) {
$('#connect').text('Connect').css('background', '#369');
if (evt.code == 3001) {
logMsg('ws closed');
_websocket = null;
} else {
logMsg('ws connection error: ' + evt.code);
$('#console').html("");
_websocket = null;
wsConnect();
}
};
_websocket.onerror = function (evt) {
if (_websocket.readyState == 1) {
$('#red').show();
logMsg('ws normal error: ' + evt.type);
}
};
}
}
function logMsg(err) {
console.log(err);
$('#console').append('<li>' + err + '</li>');
}
function Authorize(){
sessionStorage.setItem('LastAction', "Authorize");
var Auth = JSON.stringify([2, id, "Authorize", {"idTag": $("#TAG").val()}]);
_websocket.send(Auth);
}
function startTransaction(){
sessionStorage.setItem('LastAction', "startTransaction");
$('.indicator').hide();
$('#green').show();
connector_locked = true;
logMsg("Connector status changed to: " + connector_locked);
var strtT = JSON.stringify([2, id, "StartTransaction", {
"connectorId": 2,
"idTag": $("#TAG").val(),
"timestamp": formatDate(new Date()),
"meterStart": parseInt(document.getElementById("metervalue").value),
"reservationId": 0
}]);
_websocket.send(strtT);
}
function stopTransaction(transaction_id = false){
sessionStorage.setItem('LastAction', "stopTransaction");
transaction_id == false ? ssid = sessionStorage.getItem('TransactionId') : ssid = transaction_id;
$('.indicator').hide();
connector_locked = false;
logMsg("Connector status changed to: " + connector_locked);
$('#yellow').show();
var stpT = JSON.stringify([2, id, "StopTransaction",{
"transactionId": parseInt(ssid),
"idTag": $("#TAG").val(),
"timestamp": formatDate(new Date()),
"meterStop": parseInt(document.getElementById("metervalue").value)
}]);
_websocket.send(stpT);
}
function handleData(data, request = false){
var lastAction = getLastAction();
if(lastAction = "BootNotification"){
data = data[2];
heartbeat_interval = data.interval;
return heartbeat_interval;
}else if(lastAction = "StartTransaction"){
return "StartTransaction";
}else if (1==2){
alert("else");
}
}
function getLastAction(){
var LastAction = sessionStorage.getItem("LastAction");
return LastAction;
}
function BootNotification(){
var BN = JSON.stringify([2, id, "BootNotification", {
"chargePointVendor": "AVT-Company",
"chargePointModel": "AVT-Express",
"chargePointSerialNumber": "avt.001.13.1",
"chargeBoxSerialNumber": "avt.001.13.1.01",
"firmwareVersion": "0.9.87",
"iccid": "",
"imsi": "",
"meterType": "AVT NQC-ACDC",
"meterSerialNumber": "avt.001.13.1.01"
}]);
logMsg('ws connected');
_websocket.send(BN);
}
function startHB(interval){
logMsg("Setting heartbeat interval to "+interval);
setInterval(send_heartbeat,interval);
}
function send_heartbeat() {
sessionStorage.setItem('LastAction', "Heartbeat");
var HB = JSON.stringify([2, id, "Heartbeat", {}]);
_websocket.send(HB);
}
$( document ).ready(function() {
$('.indicator').hide();
$('#red').show();
//bind controls
$('#connect').click(function () {
$('.indicator').hide();
$('#console').html("");
wsConnect();
});
$('#send').click(function () {
Authorize();
});
$('#start').click(function () {
startTransaction();
});
$('#stop').click(function () {
stopTransaction();
});
$('#mv').click(function () {
sessionStorage.setItem('LastAction', "MeterValues");
var val = $("#metervalue").val();
var MV = JSON.stringify([2, id, "MeterValues", {"connectorId": 1, "transactionId": ssid, "meterValue": [{"timestamp": formatDate(new Date()), "sampledValue": [{"value": val}]}]}]);
_websocket.send(MV);
});
$('#heartbeat').click(function () {
send_heartbeat();
});
$('#status').click(function () {
sessionStorage.setItem('LastAction', "StatusNotification");
var SN = JSON.stringify([2, id, "StatusNotification", {
"connectorId": 2,
"status": "Available",
"errorCode": "NoError",
"info": "",
"timestamp": formatDate(new Date()),
"vendorId": "",
"vendorErrorCode": ""
}]);
_websocket.send(SN);
});
$('#data_transfer').click(function () {
sessionStorage.setItem('LastAction', "DataTransfer");
var DT = JSON.stringify([2, id, "DataTransfer", {
"vendorId": "rus.avt.cp",
"messageId": "GetChargeInstruction",
"data": ""
}]);
_websocket.send(DT);
});
$('#connect').on('change', function () {
if (_websocket) {
_websocket.close(3001);
}
});
});
</script>
</body>
</html>