-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration-test.ts
115 lines (101 loc) · 3.91 KB
/
integration-test.ts
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
import { SDKFactory, Need, Mission, Message } from "dav-js";
import { NeedFilterParams, NeedParams, BidParams, MissionParams, StartingMessageParams, ChargingStartedMessageParams, ChargingCompleteMessageParams, StatusRequestMessageParams, ProviderStatusMessageParams, ChargingArrivalMessageParams, DroneStatusMessageParams } from "dav-js/dist/drone-charging";
import * as os from 'os';
import * as path from 'path';
import * as fs from 'fs';
import MessageParams from 'dav-js/dist/drone-charging/MessageParams';
const config = require('./env');
const wallet = JSON.parse(fs.readFileSync(path.join(os.homedir(), '.dav', 'wallet')).toString());
const identity = JSON.parse(fs.readFileSync(path.join(os.homedir(), '.dav', 'charger')).toString());
async function main() {
try {
const DAV = SDKFactory({
apiSeedUrls: config.apiSeedUrls,
kafkaSeedUrls: config.kafkaSeedUrls,
ethNodeUrl: wallet.nodeUrl
});
const charger = await DAV.getIdentity(identity.address);
console.log('Charger', charger);
const needs = await charger.needsForType<NeedParams>(new NeedFilterParams({
location: {
lat: 32.050382,
long: 34.766149
},
radius: 1000,
}));
needs.subscribe(handleBid, exitOnError);
console.log('Waiting for Needs...', needs.topic);
}
catch (err) {
exitOnError(err);
}
}
async function handleBid(need: Need<NeedParams>) {
try {
console.log('Need', need);
const bid = await need.createBid(new BidParams({
price: '1',
vehicleId: identity.address,
availableFrom: 1,
isCommitted: false,
entranceLocation: {
lat: 32.050382,
long: 34.766149
}
}));
console.log('Waiting on Bid', bid);
const commitmentRequests = await bid.commitmentRequests();
commitmentRequests.subscribe(commitmentRequest => {
console.log('CommitmentRequest', commitmentRequest);
commitmentRequest.confirm();
}, exitOnError);
const messages = await bid.messages();
messages.subscribe(message => {
console.log('Bid Message', message);
}, exitOnError);
const missions = await bid.missions();
missions.subscribe(handleMission, exitOnError);
}
catch (err) {
exitOnError(err);
}
}
async function handleMission(mission: Mission<MissionParams>) {
try {
console.log('Mission', mission);
const messages = await mission.messages();
messages.subscribe(message => handleMissionMessage(mission, message), exitOnError);
await mission.sendMessage(new StartingMessageParams({}));
await mission.sendMessage(new StatusRequestMessageParams({}));
}
catch (err) {
exitOnError(err);
}
}
async function handleMissionMessage(mission: Mission<MissionParams>, message: Message<MessageParams>) {
try {
if (message.params instanceof ChargingArrivalMessageParams) {
console.log('Mission Message', 'Charging Arrival');
await mission.sendMessage(new ChargingStartedMessageParams({}));
await mission.sendMessage(new ChargingCompleteMessageParams({}));
}
else if (message.params instanceof StatusRequestMessageParams) {
console.log('Mission Message', 'Status Request');
await mission.sendMessage(new ProviderStatusMessageParams({ finishEta: 1 }));
}
else if (message.params instanceof DroneStatusMessageParams) {
console.log('Mission Message', 'Drone Status', message.params);
}
else {
console.log('Mission Message', message);
}
}
catch (err) {
exitOnError(err);
}
}
main().then(() => { });
function exitOnError(err: any) {
console.error('Exiting: ', err);
process.exit(0);
}