-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
87 lines (65 loc) · 2.12 KB
/
extension.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
const vscode = require("vscode");
const fetch = require("node-fetch");
var config = vscode.workspace.getConfiguration("elektri-hind");
if (config.alignment == "left") {
var alignment = vscode.StatusBarAlignment.Left;
} else {
var alignment = vscode.StatusBarAlignment.Right;
}
var statusBarPriceItem = vscode.window.createStatusBarItem(alignment, config.alignmentPriority);
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
// Hourly reload
var nextDate = new Date();
if (nextDate.getMinutes() === 0) {
// Fetch if full hour
getData();
} else {
// Else get time of next full hour
nextDate.setHours(nextDate.getHours() + 1);
nextDate.setMinutes(0);
nextDate.setSeconds(0);
// Difference between now and the next full hour
var difference = nextDate - new Date();
// Fetch timeout until next full hour
setTimeout(getData, difference);
}
// Reload command
const disposable = vscode.commands.registerCommand("elektri-hind.reload", () => {
reloadStatusBarItem();
});
context.subscriptions.push(disposable);
// First fetch and creation
getData();
}
// This method is called when your extension is deactivated
function deactivate() {
statusBarPriceItem.dispose();
}
function setStatusBarItem(price) {
statusBarPriceItem.text = price.toFixed(config.decimalPointsStatusBar).toString() + " €/kWh";
statusBarPriceItem.tooltip = "Current electricity price (" + price.toFixed(config.decimalPointsTooltip).toString() + " €/kWh)";
statusBarPriceItem.show();
}
async function getData() {
const response = await fetch("https://dashboard.elering.ee/api/nps/price/ee/current?group=ee");
const data = await response.json();
setStatusBarItem(data.data[0].price / 1000, true);
}
function reloadStatusBarItem() {
statusBarPriceItem.dispose();
config = vscode.workspace.getConfiguration("elektri-hind");
if (config.alignment == "left") {
alignment = vscode.StatusBarAlignment.Left;
} else {
alignment = vscode.StatusBarAlignment.Right;
}
statusBarPriceItem = vscode.window.createStatusBarItem(alignment, config.alignmentPriority);
getData();
}
module.exports = {
activate,
deactivate,
};