-
Notifications
You must be signed in to change notification settings - Fork 0
/
gas_station.ts
executable file
·154 lines (133 loc) · 5.72 KB
/
gas_station.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
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
on({ id: 'telegram.0.communicate.request', change: 'any' }, function (obj) {
let regex:RegExp = /](FUEL|tanken)/i;
if(regex.test(obj.state.val) == false){
return
}
console.log("cmd:"+ obj.state.val);
var user_to_send:string = obj.state.val.substring(1, obj.state.val.indexOf("]"));
let commandList:string = (obj.state.val.substring(obj.state.val.indexOf("]") + 1, obj.state.val.length)).toLowerCase();
let commands: Array<string>= [];
let output:string = "";
let inline_keyboard = [];
let commandCount = (commandList.match(/;/g)||[]).length;
commands = commandList.split(";");
var main_call = "FUEL;";
switch(commands[0]){
case "tanken":
calc_average();
var list = [];
for (var i = 0; i < 10; i++) {
if (getState("tankerkoenig.0.stations." + i + ".status").val == "open") {
list.push(new Array(getState("tankerkoenig.0.stations." + i + ".name").val,
getState("tankerkoenig.0.stations." + i + ".e5.short").val));
}
}
list = list.sort(function (a, b) {
return a[1] - b[1];
});
if (list.length == 0) {
output += "Alle Tankstellen in der Liste sind aktuell geschlossen";
} else {
output += "<b>Aktuelle Spritpreise:</b>\n\n"
let onetime:boolean = false;
list.forEach(function (item) {
if(onetime == false && item[1] >getState('0_userdata.0.Tanken.avarage').val){
onetime = true;
output += "--- 7 Tage druchschnitt " + getState('0_userdata.0.Tanken.avarage').val.toFixed(3) +"€ ---\n";
}
output += item[0] + " : " + item[1] + "€\n";
});
if (onetime == false){
output += "--- 7 Tage druchschnitt " + getState('0_userdata.0.Tanken.avarage').val.toFixed(3) +"€ ---\n";
}
}
inline_keyboard = [
[{ text: "Grafik", callback_data: "FUEL;flot" }],
]
get_Image();
break;
case "fuel":
switch(commands[1]){
case "flot":
output += "Die Letzten 7 Tage (" + getState('0_userdata.0.Tanken.avarage').val.toFixed(3) + "€)"
sendTo('telegram', {
user: user_to_send,
text:"/opt/iobroker/node_modules/iobroker.phantomjs/fuel_e5.png",
//text:" http://127.0.0.1:8082/state/phantomjs.0.pictures.fuel_e5_png",
caption: output,
parse_mode: "HTML"
});
return;
}
break;
}
if (output != ""){
sendTo('telegram', {
user: user_to_send,
text: output,
parse_mode: "HTML",
reply_markup: {
inline_keyboard: inline_keyboard
}
});
}
});
function calc_average(){
var end = Date.now();
sendTo('history.0', 'getHistory', {
id: 'tankerkoenig.0.stations.cheapest.e5.short',
options: {
start: end - 604800000 ,
end: end,
aggregate: 'max'
}
}, function (result) {
var values = []
for (var i = 0; i < result.result.length; i++) {
if (typeof(result.result[i].val) === 'number'){
values.push(result.result[i].val);
//console.log(typeof(result.result[i].val) + " value " +result.result[i].val );
}
}
let sum = values.reduce((a,b)=>a+b);
let min = Math.min.apply(Math, values);
let avarege = sum/values.length;
setState('0_userdata.0.Tanken.avarage',avarege);
setState('0_userdata.0.Tanken.min',min);
});
}
function get_Image(){
let min = (getState("0_userdata.0.Tanken.min").val - 0.01).toString();
let url = getState('0_userdata.0.Tanken.flot_url').val
let url_1 = url.substr(0, url.indexOf("min%5D=")+7);
let temp = url.substr(url.indexOf("min%5D="), url.length);
let url_2 = temp.substr(temp.indexOf("&"), temp.length);
let url_full = url_1 + min + url_2;
//console.log("url "+ url);
sendTo('phantomjs.0', 'send', {
url: url_full,
output: 'fuel_e5.png', // default value
width: 800, // default value
height: 500, // default value
timeout: 1000, // default value
zoom: 1, // default value
// online : true,
'clip-top': 0, // default value
'clip-left': 0, // default value
'clip-width': 800, // default value is equal to width
'clip-height': 500, // default value is equal to height
'scroll-top': 0, // default value
'scroll-left': 0, // default value
online: false // default value
}, function (result) {
if (result.error) {
console.error(JSON.stringify(result.error));
}
if (result.stderr) {
console.error(result.stderr);
}
if (result.stdout) {
console.log(result.stdout);
}
});
}