forked from sap-labs-france/ev-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChargeWorkflow.ts
More file actions
153 lines (147 loc) · 4.61 KB
/
ChargeWorkflow.ts
File metadata and controls
153 lines (147 loc) · 4.61 KB
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
import axios from 'axios';
class ChargeWorkflow {
baseUrl = 'http://df2.localhost:8080/v1';
token: string;
chargingStationId: string;
txnId: string = '103149113';
public async doLogin(email: string, password: string) {
let url = `${this.baseUrl}/auth/signin`;
let dataIn = {
email: email,
password: password,
acceptEula: true,
tenant: 'df2',
};
const { data, status } = await axios.post(url, dataIn, {
headers: {
Accept: 'application/json',
},
});
this.token = data.token;
}
public async getChargers() {
let url = `${this.baseUrl}/api/charging-stations?Limit=100`;
const { data, status } = await axios.get(url, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.token}`,
},
});
if (data.result) {
for (let chargePoint of data.result) {
console.log(`Vendor -> ${chargePoint.chargePointVendor}`);
console.log(`Charge Point ID -> ${chargePoint.id}`);
}
} else {
console.log('No charging station found');
}
}
public async startCharging() {
let url = `${this.baseUrl}/api/transactions/start`;
this.chargingStationId = 'CS-ABB-00001';
let dataIn = {
chargingStationID: this.chargingStationId,
args: {
tagID: 'TESTRFID',
visualTagID: 'TESTRFID',
connectorId: 1,
},
};
console.log(`Starting charging on ${this.chargingStationId}`);
const { data, status } = await axios.put(url, dataIn, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.token}`,
},
});
if (data.status) {
console.log(`Status of Txn ${data.status}`);
} else {
console.log('Starting charging failed');
}
}
public async getTxnIdFromCharger() {
let url = `${this.baseUrl}/api/charging-stations/${this.chargingStationId}?ProjectFields=`;
const { data, status } = await axios.get(url, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.token}`,
},
});
if (data.connectors) {
for (let connector of data.connectors) {
if (connector.id === '1') {
this.txnId = connector.currentTransactionID;
break;
}
}
if (this.txnId) {
console.log(`Txn Id -> ${this.txnId}`);
}
} else {
console.log('Finding charger details failed.');
}
}
public async getTxnDetails() {
let url = `${this.baseUrl}/api/transactions/${this.txnId}?WithCar=true&WithUser=true&WithTag=true`;
const { data, status } = await axios.get(url, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.token}`,
},
});
if (data.chargeBoxID && data.userID) {
console.log(`Charging in Charger ${data.chargeBoxID}`);
} else {
console.log('Finding charger details failed.');
}
}
public async stopTxn() {
let url = `${this.baseUrl}/api/transactions/${this.txnId}/stop`;
try {
const { data, status } = await axios.put(url, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.token}`,
},
});
if (data.status) {
console.log(`Status of Stop Txn ${data.status}`);
} else {
console.log('Finding charger details failed.');
}
} catch (e) {
console.error(e);
}
}
public async executeWorkflow() {
// User Login
console.log('##############################################');
console.log('USER LOGIN');
await this.doLogin('abc1@deepfleet.com', 'Password123@');
console.log('##############################################');
//Get all chargers
console.log('FIND CHARGERS');
console.log('##############################################');
await this.getChargers();
console.log('##############################################');
// Start transaction
console.log('##############################################');
console.log('START TXN');
await this.startCharging();
console.log('##############################################');
//Txn consumption
console.log('##############################################');
console.log('TXN DETAILS');
await this.getTxnIdFromCharger();
await this.getTxnDetails();
console.log('##############################################');
//Stop txn
console.log('##############################################');
console.log('STOPPING TXN');
await this.stopTxn();
console.log('##############################################');
}
}
const chargerFlow = new ChargeWorkflow();
chargerFlow.executeWorkflow();