This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
producer.js
355 lines (330 loc) · 9.71 KB
/
producer.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
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
/* @file producer.js
* @authors Saurabh Gupta [[email protected]]
* Awadhut Thube [[email protected]]
* Jheel Nagaria [[email protected]]
* Ashish Kamble [[email protected]]
*/
// Instances //
/* web3.js is a collection of libraries that allow you to interact with a
* local or remote ethereum node using HTTP, IPC or WebSocket.
*/
var web3 = require('web3');
var Web3 = require('web3');
var web3 = new Web3();
var web3Admin = require('web3admin')
// Node.js web application framework
var express = require('express');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var other_servers = require('socket.io-client'); // This is a client connecting to the SERVER 2 (MAIN SERVER)
var main_server = other_servers.connect('http://localhost:4000', {reconnect: true});
// Setup Serial Connection with Arduino Nano to control relay
// var arduino_serial_port = require("serialport");
// var serialport = new SerialPort('/dev/cu.usbmodem1421',{parser: SerialPort.parsers.Readline('\n')});
var SerialPort = require('serialport');
var Readline = SerialPort.parsers.Readline;
var arduino_serial_port = new SerialPort('/dev/cu.usbmodem1421');
var parser = new Readline('\n');
arduino_serial_port.pipe(parser);
// Serial Port Setup for Energy Meter Reading
var serialPort = require('serialport');
var meter_serial_port = new serialPort('/dev/cu.usbserial', {
baudRate: 115200,
parser: new serialPort.parsers.Readline('\n\r')
});
// Variables Declaration
var meter_reading_string = ""; var value_meter;
var producer_address; var consumer_address;
var ether_per_token = 0; var accepted_bid;
var accept_deal_flag = 0; var block_deal_flag = 1;
var energy_tokens;
var pending_tx_list = []
var energy_KWH = 0; var prev_energy_KWH = 0; var difference = 0;
var producer; var consumer;
// Smart Contract for generation of Virtual Energy Tokens and Automate transactions
var abi = [
{
"constant": false,
"inputs": [
{
"name": "_account",
"type": "address"
}
],
"name": "token_balance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "address"
}
],
"name": "balances",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_account",
"type": "address"
}
],
"name": "eth_balance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_account",
"type": "address"
},
{"name": "amount",
"type": "uint256"
}
],
"name": "send_eth",
"outputs": [],
"payable": true,
"stateMutability": "payable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_account",
"type": "address"
},
{"name": "amount",
"type": "uint256"
}
],
"name": "update_tokens",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
]
// Contract Address obtained after deploying contract from 'Remix Solidity Compiler'
// This is the token generation and ether sending contract
var contract_address = "0xbccc53572694ea920a4bf3070b4780a7892855a2" ;
// Contract Object Creation at Contract Address
var obj = web3.eth.contract(abi).at("0xbccc53572694ea920a4bf3070b4780a7892855a2");
// Web3 setup
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
web3.eth.defaultAccount = web3.eth.accounts[0];
web3Admin.extend(web3);
// Energy Meter Serial Port
meter_serial_port.on('open', function()
{
console.log("Energy Meter Serial Port Open\n")
setInterval(function()
{
// Request to read energy consumption data from the meter
meter_serial_port.write(Buffer.from('SHOW=\r\n'), function(error)
{
// Search for 'KWH'(Power consumption reading) in the data returned by the meter
KWH_index = meter_reading_string.indexOf("KWH", 0);
// Our required reading is at index 8 and 9 from the 'KWH' substring start index
value_meter = meter_reading_string.substring(KWH_index + 8, KWH_index + 9);
energy_KWH = 1 + Number(value_meter);
// Reset String
meter_reading_string = "";
// Check for writing error
if (error)
{
return console.log('Error on writing to Meter: ', error.message);
}
});
}, 5000)
// Read energy consumption data from the meter
meter_serial_port.on('data', function (data)
{
meter_reading_string = meter_reading_string + data.toString();
});
});
// Arduino Nano Serial Port
arduino_serial_port.on('open', function()
{
console.log("Arduino Serial Port Open\n")
main_server.on('connect', function(){ });
//Close Connection
main_server.on('close',function(data)
{
console.log("Closing Connection");
arduino_serial_port.write('2');
accept_deal_flag = 0;
block_deal_flag = 1;
});
// Accept Bid
// PoC - Active only for 1st Deal
main_server.on('accept_deal_0',function(data)
{
producer_address = data.producer_address;
consumer_address = data.consumer_address;
accepted_bid = data.bid;
// Toggle Relay Connection to stop producer from charging battery
if(consumer_address == web3.eth.accounts[0])
{
arduino_serial_port.write('1');
accept_deal_flag = 1 ;
}
});
//Accept request for Sharing Energy Tokens of Producers for Display on Marketplace
main_server.on('req_tokens_0', function(data)
{
if(data == web3.eth.accounts[4])
{
main_server.emit('display_tokens_0', energy_tokens);
}
});
main_server.on('req_tokens_1', function(data)
{
if(data == web3.eth.accounts[4])
{
main_server.emit('display_tokens_1', energy_tokens);
}
});
main_server.on('req_tokens_2', function(data)
{
if(data == web3.eth.accounts[4])
{
main_server.emit('display_tokens_2', energy_tokens);
}
});
main_server.on('req_tokens_3', function(data)
{
if(data == web3.eth.accounts[4])
{
main_server.emit('display_tokens_3', energy_tokens);
}
});
main_server.on('req_tokens_4', function(data)
{
if(data == web3.eth.accounts[4])
{
main_server.emit('display_tokens_4', energy_tokens);
}
});
main_server.on('req_tokens_5', function(data)
{
if(data == web3.eth.accounts[4])
{
main_server.emit('display_tokens_5', energy_tokens);
}
});
// Login to Personal Account using Passphrase
app.use(express.static('public'));
app.get('/', function(req, res)
{
res.sendfile('login_page.html');
io.once('connection', function (socket)
{
socket.on('check_passphrase', function (data)
{
// Unlock Ethereum Account [0] and send its boolean output
var unlock_result = web3.personal.unlockAccount(web3.eth.accounts[0], data, 100000);
socket.emit('unlock_ethereum_account_result', unlock_result);
});
});
});
//Wallet Functions
app.get('/enter_wallet', function(req, res)
{
res.sendfile('wallet.html');
io.on('connection', function (socket)
{
// Start Mining
socket.on('startmine', function(socket)
{
var Mine = web3.miner.start();
});
// Stop Mining
socket.on('stopmine', function(socket)
{
var StopMine = web3.miner.stop();
});
// Do a Basic Transaction
socket.on('basic_tx', function(data)
{
from_address = web3.eth.accounts[0];
to_address = data.add;
value = data.val;
var send = web3.eth.sendTransaction({from : from_address, to: to_address , value:value})
});
setInterval(function()
{
difference= energy_KWH - prev_energy_KWH;
// Get Account Balance
var balance = web3.eth.getBalance(web3.eth.accounts[4]);
socket.emit('pending_tx_list', {tx_1:pending_tx_list[0], tx_2:pending_tx_list[1], tx_3:pending_tx_list[2]});
socket.emit('energy_token_balance', {energy:energy_KWH, tok:energy_tokens, bal:balance});
// If increment in Energy supplied to battery, increase energy tokens
if(difference != 0)
{
obj.update_tokens(web3.eth.accounts[4], difference);
prev_energy_KWH = energy_KWH;
}
// Get list of pending transactions
pending_tx_list = web3.eth.getBlock("pending").transactions;
// Get Token balance from Smart Contract Object
energy_tokens = obj.token_balance.call(web3.eth.accounts[4]);
}, 1000);
});
});
// Check if a bid has been accepted
// If yes, then register the account addresses and initiate realtime
// transactions through the smart contract
setInterval(function()
{
if (accept_deal_flag == 1 && block_deal_flag == 1)
{
// Register account addresses and purchase value
producer = producer_address;
consumer = consumer_address;
ether_per_token = accepted_bid;
// Toggle flag
block_deal_flag = 2;
}
if (accept_deal_flag == 1)
{
obj.send_eth(producer, ether_per_token, {from:consumer, to:contract_address, value:ether_per_token});
}
}, 8000);
});
// HTTP SERVER
http.listen(3000, function()
{
console.log('listening on *:3000');
});