-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
136 lines (122 loc) · 5.22 KB
/
main.js
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
const P1Reader = require('p1-reader');
const BigchainOrm = require('bigchaindb-orm').default;
const OrmObject = require('bigchaindb-orm/dist/node/ormobject').default;
// const Connection = require('bigchaindb-orm/dist/node/connection').default;
const Connection = require('bigchaindb-driver/dist/node/connection').default;
const BigchainDriver = require('bigchaindb-driver');
const bip39 = require('bip39');
const axios = require('axios');
class OehuReadAndWrite {
constructor(opts) {
this.network = opts.network;
this.deviceId = opts.deviceId;
this.phrase = opts.phrase;
this.appId = opts.appId;
this.appKey = opts.appKey;
this.debug = opts.debug;
this.emulator = opts.emulator;
this.transactionsApi = 'https://api.oehu.org/transactions?raw=true&deviceId=' + this.deviceId; //add limit
// bug in bigchainDriver: https://github.com/bigchaindb/js-bigchaindb-driver/issues/268. Needs to be looked
// at later. ATM make a change to a node-modules file. See link above.
this.seed = bip39.mnemonicToSeed(this.phrase).slice(0, 32);
this.keypair = new BigchainDriver.Ed25519Keypair(this.seed);
this.lastReading = 0;
this.p1Reader = new P1Reader({debug: this.debug, emulator: this.emulator});
this.p1Reader.on('reading', (data) => this.p1OnRead(data));
this.p1Reader.on('error', (err) => this.p1OnError(err));
}
async p1OnRead(data) {
if (Date.now() - this.lastReading > 10000) { //every 10 seconds
console.log('Reading and uploading');
const reading = {
lastUpdate: Date.now(),
electricityReceived: {
total: data.electricity.received.tariff1.reading + data.electricity.received.tariff2.reading,
tariff1: data.electricity.received.tariff1.reading,
tariff2: data.electricity.received.tariff2.reading
},
electricityDelivered: {
total: data.electricity.delivered.tariff1.reading + data.electricity.delivered.tariff2.reading,
tariff1: data.electricity.delivered.tariff1.reading,
tariff2: data.electricity.delivered.tariff2.reading
},
gasReceived: data.gas.reading
};
console.log(reading);
let result = this.setupBigchainTransaction(reading);
this.lastReading = Date.now();
}
}
p1OnError(err) {
console.log('Error while reading: ' + err);
}
async setupBigchainTransaction(reading) {
//Create connection
let connection = new Connection(this.network, {
app_id: this.appId,
app_key: this.appKey
});
//Get latest transaction of asset
let tx = await axios.get(this.transactionsApi)
.then(function (response) {
return response.data[0];
})
.catch(function (error) {
console.log(error);
});
//Create transfer transaction, sign and send to Bigchain
this.createTransferTransaction(connection, tx, reading);
}
async createTransferTransaction(connection, tx, reading) {
let newAssetTransaction;
try {
newAssetTransaction = await connection.transferTransaction(
tx,
this.keypair.publicKey,
this.keypair.privateKey,
this.keypair.publicKey,
this.merge_options(tx.metadata.metadata, reading)
);
console.log(newAssetTransaction);
} catch (e) {
console.log(e);
}
// Same result :(
// try {
// const txTransfer = BigchainDriver.Transaction.makeTransferTransaction(
// [{ 'tx': tx, 'output_index': 0 }],
// [BigchainDriver.Transaction.makeOutput(BigchainDriver.Transaction.makeEd25519Condition(this.keypair.publicKey))],
// this.merge_options(tx.metadata.metadata, reading),
// );
//
// const txTransferSigned = BigchainDriver.Transaction.signTransaction(txTransfer, this.keypair.privateKey);
//
// console.log('____txTransferSigned_____');
// console.log(txTransferSigned);
// console.log('____/txTransferSigned_____');
//
// console.log('Send it');
// let promise = connection.postTransactionCommit(txTransferSigned)
// .then((res) => console.log(res))
// .catch((err) => console.log(err));
//
// } catch (error) {
// return Promise.reject(error)
// }
}
merge_options(obj1, obj2) {
let obj3 = {};
for (let attrname in obj1) { obj3[attrname] = obj1[attrname]; }
for (let attrname in obj2) { obj3[attrname] = obj2[attrname]; }
return obj3;
}
}
let test = new OehuReadAndWrite({
network: "http://188.166.15.225:9984/api/v1/",
deviceId: "id:3b959424:devices:beda4fb7-c2f9-4cb3-a479-4c8038535c74",
phrase: "penalty shine inner milk early answer ceiling twin spot blush width brick",
appId: "3b959424",
appKey: "30c12a0e15343d705a7e7ccb6d75f1c0",
debug: true,
emulator: true
});