-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBLG.js
207 lines (201 loc) · 7.4 KB
/
BLG.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
/**
* StromDAO Business Object: DirectBLG
* =========================================
* Directly charge for Energy delivery to an account within StromDAO Energy Blockchain.
*
* DirectCharging gets used together with DirectConnections.
*
* In theory account A and account B are exchanging energy metered by meter_point C
*
* DirectCharging takes several DirectConnections and runs charging as soon as method chargeAll is called.
*
* @author Thorsten Zoerner thorsten.zoerner@stromdao.de
*
*/
var request = require('request');
this.blg=function(obj_or_address) {
if(typeof obj_or_address == "undefined") obj_or_address=parent.options.contracts["StromDAO-BO.sol_DirectBalancingGroup"];
var p1 = new Promise(function(resolve, reject) {
var instance=parent._objInstance(obj_or_address,'StromDAO-BO.sol_DirectBalancingGroup');
parent._saveLabel('BLG',obj_or_address);
instance.addFeedIn=function(account,meter_point,cost_per_energy,cost_per_day) {
var p2 = new Promise(function(resolve2, reject2) {
instance.obj.addFeedIn(account,meter_point,cost_per_energy,cost_per_day).then(function(o) {
parent._waitForTransactionKeepRef(o,resolve2);
});
});
return p2;
};
instance.addFeedOut=function(account,meter_point,cost_per_energy,cost_per_day) {
var p2 = new Promise(function(resolve2, reject2) {
instance.obj.addFeedOut(account,meter_point,cost_per_energy,cost_per_day).then(function(o) {
parent._waitForTransactionKeepRef(o,resolve2);
});
});
return p2;
};
instance.setFixedEnergyCost=function(cost_per_energy) {
var p2 = new Promise(function(resolve2, reject2) {
instance.obj.setFixedEnergyCost(cost_per_energy).then(function(o) {
parent._waitForTransactionKeepRef(o,resolve2);
});
});
return p2;
};
instance.charge=function() {
var p2 = new Promise(function(resolve2, reject2) {
instance.obj.charge({gasLimit:5120000,gasPrice:0}).then(function(o) {
parent._waitForTransactionKeepRef(o,resolve2);
});
});
return p2;
};
instance.emptyConnections=function() {
var p2 = new Promise(function(resolve2, reject2) {
instance.obj.emptyConnections().then(function(o) {
parent._waitForTransactionKeepRef(o,resolve2);
});
});
return p2;
};
instance.stromkontoDelta=function() {
var p2 = new Promise(function(resolve2, reject2) {
instance.obj.stromkontoDelta().then(function(o) {
parent._saveLabel('BAL',o[0]);
resolve2(o[0]);
});
});
return p2;
}
instance.feedIn=function(idx) {
var p2 = new Promise(function(resolve2, reject2) {
instance.obj.feedIn(idx).then(function(o) {
parent._saveLabel('CON',o[0]);
resolve2(o[0]);
});
});
return p2;
}
instance.feedOut=function(idx) {
var p2 = new Promise(function(resolve2, reject2) {
instance.obj.feedOut(idx).then(function(o) {
parent._saveLabel('CON',o[0]);
resolve2(o[0]);
});
});
return p2;
}
instance.stromkontoIn=function() {
var p2 = new Promise(function(resolve2, reject2) {
instance.obj.stromkontoIn().then(function(o) {
parent._saveLabel('BAL',o[0]);
resolve2(o[0]);
});
});
return p2;
}
instance.stromkontoOut=function() {
var p2 = new Promise(function(resolve2, reject2) {
instance.obj.stromkontoOut().then(function(o) {
parent._saveLabel('BAL',o[0]);
resolve2(o[0]);
});
});
return p2;
}
//cnt_feedin
instance.cnt_feedin=function() {
var p2 = new Promise(function(resolve2, reject2) {
instance.obj.cnt_feedin().then(function(o) {
resolve2(o[0]*1);
});
});
return p2;
}
instance.cnt_feedout=function() {
var p2 = new Promise(function(resolve2, reject2) {
instance.obj.cnt_feedout().then(function(o) {
resolve2(o[0]*1);
});
});
return p2;
}
instance.balancesheets=function(idx) {
var p2 = new Promise(function(resolve2, reject2) {
// Save as it is unmutbale
var stored = parent.storage.getItemSync(obj_or_address+"_"+idx);
if((typeof stored == "undefined")||(stored==null)) {
instance.obj.balancesheets(idx).then(function(o) {
o.idx=idx;
parent._saveLabel('BAL',o.balanceIn);
parent._saveLabel('BAL',o.balanceOut);
parent.storage.setItemSync(obj_or_address+"_"+idx,JSON.stringify(o));
resolve2(o);
});
} else {
var o = JSON.parse(stored);
o.blockNumber=parent._utils.bigNumberify("0x"+o.blockNumber._bn);
resolve2(o);
}
});
return p2;
}
instance.setCostPerEnergy=function(connection,cost_per_energy) {
var p2 = new Promise(function(resolve2, reject2) {
instance.obj.setCostPerEnergy(connection,cost_per_energy).then(function(o) {
parent._waitForTransactionKeepRef(o,resolve2);
});
});
return p2;
}
instance.setAccountInfo=function(account,data_obj) {
var p2 = new Promise(function(resolve2, reject2) {
request.post("http://l2.stromdao.de:8001/put", {form:data_obj}).on('data', function(data) {
var multihash=data.toString();
parent.stringstoragefactory().then( function(ssf) {
ssf.build(multihash).then(function(adr) {
console.log("Adr",adr,"Multihash",multihash,"Account",account);
instance.obj.setAccountInfo(account,adr).then(function(o) {
parent._waitForTransactionKeepRef(o,resolve2);
});
});
});
});
});
return p2;
}
instance.accountInfo=function(account) {
var p2 = new Promise(function(resolve2, reject2) {
instance.obj.accountInfo(account).then(function(o) {
parent.stringstorage(o[0]).then(function(ss) {
ss.str().then(function(multihash) {
request.post("http://l2.stromdao.de:8001/get",{form:{key:multihash}}).on('data', function(d) {
string = d.toString();
resolve2(string);
});;
});
});
});
});
return p2;
}
instance.setCostPerDay=function(connection,cost_per_day) {
var p2 = new Promise(function(resolve2, reject2) {
instance.obj.setCostPerDay(connection,cost_per_day).then(function(o) {
parent._waitForTransactionKeepRef(o,resolve2);
});
});
return p2;
}
instance.balancesheets_cnt=function() {
var p2 = new Promise(function(resolve2, reject2) {
instance.obj.balancesheets_cnt().then(function(o) {
resolve2(o[0]);
});
});
return p2;
}
resolve(instance);
});
return p1;
};