-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
412 lines (360 loc) · 18.4 KB
/
index.html
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" type="text/css" href="main.css">
<script src="./node_modules/web3/dist/web3.min.js"></script>
<script src="abi.js"></script>
</head>
<body onload="loadOperation()">
<div class="container">
<h1>PoC Energia</h1>
<div id="operations">
<a href="data.html">Consultar información de la Blockchain</a>
<label for="selectOperation" class="col-lg-2 control-label">Operacion a ejecutar</label>
<select id="operationsSelect" name="operationsSelect" onchange="loadOperation()">
<!--
<option value="addUser">Anadir usuario</option>
-->
<option value="produceEnergy">Producir energia</option>
<option value="purchaseEnergy">Comprar energia</option>
<option value="consumeEnergy">Consumir energia</option>
<option value="setPrize">Establecer precio</option>
</select>
</div>
<div class="operation">
<div id="users">
<label for="userAddress" class="col-lg-2 control-label">Introducir cuenta</label>
<input id="userAddress" type="text">
<label for="userName" class="col-lg-2 control-label">Nombre del usuario</label>
<input id="userName" type="text">
<label for="userProfile" class="col-lg-2 control-label">Perfil del usuario</label>
<select id="userProfiles" name="userProfiles">
<option selected value="producer">Productor</option>
<option value="marketer">Comercializador</option>
<option value="client">Cliente</option>
</select>
</div>
<div id="producedEner">
<label for="producedEnerQuantity" class="col-lg-2 control-label">Cantidad de energía producica</label>
<input id="producedEnerQuantity" type="text">
<label for="producedEnerDate" class="col-lg-2 control-label">Fecha</label>
<input id="producedEnerDate" type="date">
</div>
<div id="purchasedEner">
<label for="purchasedEnerQuantity" class="col-lg-2 control-label">Cantidad de energía comprada</label>
<input id="purchasedEnerQuantity" type="text">
<!--
<label for="purchasedValue" class="col-lg-2 control-label">Precio de compra</label>
<input id="purchasedValue" type="text">
-->
<label for="productorAccount" class="col-lg-2 control-label">Dirección del productor</label>
<input id="productorAccount" type="text">
<label for="purchasedEnerDate" class="col-lg-2 control-label">Fecha</label>
<input id="purchasedEnerDate" type="date">
</div>
<div id="consumedEner">
<label for="consumedEnerQuantity" class="col-lg-2 control-label">Cantidad de energía consumida</label>
<input id="consumedEnerQuantity" type="text">
<label for="consumedConcept" class="col-lg-2 control-label">Concepto (razón del consumo)</label>
<input id="consumedConcept" type="text">
<!--
<label for="consumedValue" class="col-lg-2 control-label">Precio de la energía consumida</label>
<input id="consumedValue" type="text">
-->
<label for="distributorAccount" class="col-lg-2 control-label">Dirección del distribuidor</label>
<input id="distributorAccount" type="text">
<label for="consumedEnerDate" class="col-lg-2 control-label">Fecha</label>
<input id="consumedEnerDate" type="date">
<label for="consumedEnerTime" class="col-lg-2 control-label">Hora</label>
<input id="consumedEnerTime" type="time">
<!--
input id="consumedEnerDate" type="datetime-local">
-->
</div>
<div id="setPrize">
<label for="prize" class="col-lg-2 control-label">Precio de la energía</label>
<input id="prize" type="text">
</div>
</div>
<div>
<button id="button">Ejecutar operación</button>
</div>
<div id="accounts">
<label for="selectAccount" class="col-lg-2 control-label">Cuenta que ejecutará la operación</label>
<select id="accountsSelect" name="accountsSelect"/>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script>
/*
** CONFIGURACION INICIAL
*/
//Definicion del nodo / proveedor de servicios
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:7545"));
}
/*COGEMOS TODAS LAS CUENTAS DE NUESTRO NODO
accountNum = web3.eth.accounts.length;
select = document.getElementById('accountsSelect');
for (i = 0; i < accountNum; i++) {
opt = document.createElement('option');
opt.value = i;
opt.innerHTML = web3.eth.accounts[i];
select.appendChild(opt);
}*/
//Definimos cuenta con la que vamos a trabajar
web3.eth.defaultAccount = web3.eth.accounts[0];
//web3.eth.defaultAccount.gas = '3';
var PoCEnergyContract = web3.eth.contract(abiArray);
//var PoCEnergyContract = web3.eth.contract(abiArray, {gas: 30});
//DEFINICION DE LA DIRECCION DEL SMART CONTRACT
var PoCEnergy = PoCEnergyContract.at('0xe725fa3dca9632aa6aa74625dcf4c4dd07e0e2d3');
console.log(PoCEnergy);
/*
** FUNCIONES DE NUESTRO CLIENTE
*/
//Cargamos los usuarios de la Blockchain
PoCEnergy.getUsers(function(error, result){
console.log("getUsers");
if(!error){
select = document.getElementById('accountsSelect');
for (i = 0; i < result.length; i++) {
var direccionAccountBck = result[i].toString();
PoCEnergy.getUser(direccionAccountBck, function(error2, result2){
if(!error2){
opt = document.createElement('option');
opt.value = result2[0].toString();
opt.innerHTML = result2[1].toString() + " - " + result2[2].toString() + " - " + result2[0].toString();
//opt.value = direccionAccountBck;
//opt.innerHTML = result2[1].toString() + " - " + result2[0].toString();
select.appendChild(opt);
}else{
console.error("getUsers.PoCEnergy.getUser Error: " + error2);
}
});
}
}else{
console.error("getUsers.PoCEnergy.getUsers Error: " + error);
}
});
//Operaciones PRECIO
function setBCHPrize(){
console.log("setBCHPrize");
//Obtenemos la dirección del usuario que esta llamando a la Blockchain
select = document.getElementById("accountsSelect");
direccionAccountBck = select.options[select.selectedIndex].value;
console.log(direccionAccountBck);
//Obtenemos información sobre el usuario llamando a la Blockchain
PoCEnergy.getUser(direccionAccountBck, function(error2, result2){
if(!error2){
//Comprobamos si es un productor
if(result2[2].toString() == "producer"){
console.log("setBCHPrize.PoCEnergy.getUser: OK");
//Establecemos la cuenta de usuario como la que va a llamar a la Blockchain
web3.eth.defaultAccount = result2[0];
//Llamada a la Blockchain
PoCEnergy.setPrize($("#prize").val(), function(error3, result3){
if(!error3){
console.log("setBCHPrize.PoCEnergy.setPrize result: " + result3);
}else{
console.error("setBCHPrize.PoCEnergy.setPrize Error: " + error3);
}
});
}else{
alert("No eres un productor de energía, no puedes establecer su precio");
}
}else{
console.error("setBCHPrize.PoCEnergy.getUser Error: " + error2);
}
});
}
//Operaciones PRODUCCION DE ENERGIA
function setBCHProducedEnergy(){
//Obtenemos la dirección del usuario que esta llamando a la Blockchain
console.log("setBCHProducedEnergy");
select = document.getElementById("accountsSelect");
direccionAccountBck = select.options[select.selectedIndex].value;
console.log(direccionAccountBck);
//Obtenemos información sobre el usuario llamando a la Blockchain
PoCEnergy.getUser(direccionAccountBck, function(error2, result2){
if(!error2){
//Comprobamos si es un productor
if(result2[2].toString() == "producer"){
console.log("setBCHProducedEnergy.PoCEnergy.getUser: OK");
//Establecemos la cuenta de usuario como la que va a llamar a la Blockchain
web3.eth.defaultAccount = result2[0];
//gas2 = new BigNumber(105695);
var dateNumber = new Date($("#producedEnerDate").val()).getTime();
//alert(dateNumber);
//Llamada a la Blockchain
PoCEnergy.setProducedEnergy($("#producedEnerQuantity").val(), dateNumber, {gas: 90823}, function(error3, result3){
//PoCEnergy.setProducedEnergy($("#producedEnerQuantity").val(), $("#producedEnerDate").val(), function(error3, result3){
if(!error3){
//alert("result3" + result3);
console.log("setBCHProducedEnergy.PoCEnergy.setProducedEnergy result: " + result3);
}else{
//alert("error3" + error3);
console.error("setBCHProducedEnergy.PoCEnergy.setProducedEnergy Error: " + error3);
}
});
}else{
alert("No eres un productor de energía, no puedes producir energía");
}
}else{
console.error("setBCHProducedEnergy.PoCEnergy.getUser Error: " + error2);
}
});
}
//Operaciones COMPRA DE ENERGIA
function setBCHPurchasedEnergy(){
//Obtenemos la dirección del usuario que esta llamando a la Blockchain
console.log("setBCHPurchasedEnergy");
select = document.getElementById("accountsSelect");
direccionAccountBck = select.options[select.selectedIndex].value;
console.log(direccionAccountBck);
//Obtenemos información sobre el usuario llamando a la Blockchain
PoCEnergy.getUser(direccionAccountBck, function(error2, result2){
if(!error2){
//Comprobamos si es un distributor
if(result2[2].toString() == "marketer"){
console.log("setBCHPurchasedEnergy.PocEnergy.getUser: OK");
//Establecemos la cuenta de usuario como la que va a llamar a la Blockchain
web3.eth.defaultAccount = result2[0];
var dateNumber = new Date($("#purchasedEnerDate").val()).getTime();
//Llamada a la Blockchain
PoCEnergy.setPurchasedEnergy($("#productorAccount").val(),$("#purchasedEnerQuantity").val(),
dateNumber, {gas: 105695}, function(error3, result3){
if(!error3){
console.log("setBCHPurchasedEnergy.PoCEnergy.setPurchasedEnergy result: " + result3);
}else{
console.error("setBCHPurchasedEnergy.PoCEnergy.setPurchasedEnergy Error: " + error3);
}
});
}else{
alert("No eres un distribuidir de energía, no puedes comprar energía");
}
}else{
console.error("setBCHPurchasedEnergy.PoCEnergy.getUser Error: " + error2);
}
});
}
//Operaciones CONSUMO DE ENERGIA
function setBCHConsumedEnergy(){
//Obtenemos la dirección del usuario que esta llamando a la Blockchain
console.log("setBCHConsumedEnergy");
select = document.getElementById("accountsSelect");
direccionAccountBck = select.options[select.selectedIndex].value;
console.log(direccionAccountBck);
//Obtenemos información sobre el usuario llamando a la Blockchain
PoCEnergy.getUser(direccionAccountBck, function(error2, result2){
if(!error2){
//Comprobamos si es un cliente
if(result2[2].toString() == "client"){
console.log("setBCHConsumedEnergy.PoCEnergy.getUser: OK");
//Establecemos la cuenta de usuario como la que va a llamar a la Blockchain
web3.eth.defaultAccount = result2[0];
var time = $("#consumedEnerTime").val().split(":");
var date = new Date($("#consumedEnerDate").val());
date.setTime(date.getTime() + (time[0]*60*60*1000) + (time[1]*60*1000));
//Llamada a la Blockchain
PoCEnergy.setConsumedEnergy($("#distributorAccount").val(), $("#consumedEnerQuantity").val(),
$("#consumedConcept").val(), date, {gas: 105695}, function(error3, result3){
if(!error3){
console.log("setBCHConsumedEnergy.PoCEnergy.setConsumedEnergy result: " + result3);
}else{
console.error("setBCHConsumedEnergy.PoCEnergy.setConsumedEnergy Error: " + error3);
}
});
}else{
alert("No eres un cliente, por lo que no puedes consumir energía");
}
}else{
console.error("setBCHConsumedEnergy.PoCEnergy.getUser Error: " + error2);
}
});
}
//Boton click que llama a la correspondiente función
$("#button").click(function() {
//alert("Buton");
myselect = document.getElementById("operationsSelect");
operation = myselect.options[myselect.selectedIndex].value;
if (operation=="setPrize"){
setBCHPrize();
}else if (operation=="produceEnergy"){
setBCHProducedEnergy();
}else if (operation=="purchaseEnergy"){
setBCHPurchasedEnergy();
}else if (operation=="consumeEnergy"){
setBCHConsumedEnergy();
}
/*else if(operation=="addUser"){
alert("Añadir usuario");
myselect = document.getElementById("userProfiles");
profile = myselect.options[myselect.selectedIndex].value;
alert($("#userAddress").val() + " - " + $("#userName").val() + " - " + profile);
PoCEnergy.setUser($("#userAddress").val(), $("#userName").val(), profile);
}*/
});
//TODO: TRATAMIENTO DE LOS BLOQUES
/*var filter = web3.eth.filter('latest');
filter.watch(function(error, result){
if (error) {
alert("tratarBloques - error");
console.log(error);
return;
}else{
alert("tratarBloques - bien");
var block = web3.eth.getBlock(result, true);
console.log('block #' + block.number);
console.dir(block.transactions);
}
//for (var index = 0; index < block.transactions.length; index++) {
// var t = block.transactions[index];
//}
});
filter.watch(function(error, result){
if (error) {
alert("Errorrrrrr");
return;
}else{
console.log("Rrrrrresultado: " + result);
alert("Resultado: " + result);
}
});
var i = 1;
//while (i<108) {
i++;
var block = web3.eth.getBlock(i, true);
for (var index = 0; index < block.transactions.length; index++) {
var t = block.transactions[index];
alert(t.from);
}
web3.eth.getBlock(i, function(error, result){
if(!error && result!=null){
if (result.transactions.length!=0){
console.log("Bloque: " + i + " - transacciones: " + result.transactions.length);
var t = result.transactions[0];
alert(t);
// Decode from
alert(t.from);
}
}
else{
alert("error");
console.error(error);
}
});
//}
*/
</script>
<!--<script src="mainWeb3.js"></script>-->
<script src="main.js"></script>
</body>
</html>