-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration-test.ts
124 lines (108 loc) · 3.99 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
116
117
118
119
120
121
122
123
124
import { SDKFactory, Bid, Mission } from "dav-js";
import { NeedParams, MissionParams, BidParams, ChargingArrivalMessageParams, StatusRequestMessageParams, DroneStatusMessageParams, StartingMessageParams, ChargingStartedMessageParams, ProviderStatusMessageParams, ChargingCompleteMessageParams } from "dav-js/dist/drone-charging";
import * as os from 'os';
import * as path from 'path';
import * as fs from 'fs';
import * as util from 'util';
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', 'drone')).toString());
async function main() {
try {
const DAV = SDKFactory({
apiSeedUrls: config.apiSeedUrls,
kafkaSeedUrls: config.kafkaSeedUrls,
ethNodeUrl: wallet.nodeUrl
});
const drone = await DAV.getIdentity(identity.address);
console.log('Drone', drone);
const need = await drone.publishNeed(new NeedParams({
location: {
lat: 32.050382,
long: 34.766149
}
}));
const bids = await need.bids<BidParams>();
bids.subscribe(handleBid, exitOnError);
console.log('Waiting for Bids...', bids.topic);
}
catch (err) {
exitOnError(err);
}
}
async function handleBid(bid: Bid<BidParams>): Promise<void> {
try {
console.log('Bid', bid);
const messages = await bid.messages();
messages.subscribe(message => {
console.log('Bid Message', message);
}, exitOnError);
const missions = await bid.missions();
missions.subscribe(mission => {
console.log('Bid Mission', mission);
}, exitOnError);
const commitmentConfirmation = await bid.requestCommitment();
console.log('CommitmentConfirmation', commitmentConfirmation);
const mission = await bid.accept(new MissionParams({
}), wallet.private);
await handleMission(mission);
}
catch (err) {
exitOnError(err);
}
}
async function handleMission(mission: Mission<MissionParams>) {
try {
console.log('Mission', mission);
const messages = await mission.messages();
messages.subscribe(async message => {
try {
if (message.params instanceof StartingMessageParams) {
console.log('Mission Message', 'Starting');
try {
const signTransactionReceipt = await mission.signContract(wallet.address, wallet.private);
console.log('Sign Transaction Receipt', signTransactionReceipt);
} catch (err) {
console.log(util.inspect(err));
}
await mission.sendMessage(new ChargingArrivalMessageParams({}));
}
else if (message.params instanceof ChargingStartedMessageParams) {
console.log('Mission Message', 'Charging Started');
await mission.sendMessage(new StatusRequestMessageParams({}));
}
else if (message.params instanceof ChargingCompleteMessageParams) {
console.log('Mission Message', 'Charging Complete');
try {
const finalizeTransactionReceipt = await mission.finalizeMission(wallet.address, wallet.private);
console.log('Finalize Transaction Receipt', finalizeTransactionReceipt);
} catch (err) {
console.log(util.inspect(err));
}
process.exit(0);
}
else if (message.params instanceof StatusRequestMessageParams) {
console.log('Mission Message', 'Status Request');
await mission.sendMessage(new DroneStatusMessageParams({ location: { lat: 1, long: 1 } }));
}
else if (message.params instanceof ProviderStatusMessageParams) {
console.log('Mission Message', 'Provider Status', message.params);
}
else {
console.log('Mission Message', message);
}
}
catch (err) {
exitOnError(err);
}
}, exitOnError);
}
catch (err) {
exitOnError(err);
}
}
main().then(() => { });
function exitOnError(err: any) {
console.error('Exiting: ', err);
process.exit(0);
}