-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPiholestatsApp.qml
150 lines (130 loc) · 4.97 KB
/
PiholestatsApp.qml
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
import QtQuick 2.1
import qb.components 1.0
import qb.base 1.0;
import FileIO 1.0
App {
id: root
property url trayUrl : "PiholestatsTray.qml";
property url thumbnailIcon: "qrc:/tsc/pihole_logo_40x40.png"
property url menuScreenUrl : "PiholestatsSettings.qml"
property url piholeScreenUrl : "PiholestatsScreen.qml"
property url piholeTileUrl : "PiholestatsTile.qml"
property PiholestatsSettings piholeSettings
property PiholestatsScreen piholeScreen
property bool dialogShown : false //shown when changes have been. Shown only once.
property SystrayIcon piholeTray
property bool showAppIcon : true
property bool firstTimeShown : true
property variant piholeConfigJSON
property bool piholeDataRead: false
// app settings
property string connectionPath
property string ipadres
property string poortnummer : "80"
property int refreshrate : 60 // interval to retrieve data
property string authtoken
//data vars
property string tmp_ads_blocked_today
property string tmp_ads_percentage_today
// user settings from config file
property variant userSettingsJSON : {
'connectionPath': [],
'ShowTrayIcon': "",
'refreshrate': "",
'authtoken': ""
}
// location of settings file
FileIO {
id: userSettingsFile
source: "file:///mnt/data/tsc/piholestats.userSettings.json"
}
function init() {
registry.registerWidget("systrayIcon", trayUrl, this, "piholeTray");
registry.registerWidget("screen", piholeScreenUrl, this, "piholeScreen");
registry.registerWidget("screen", menuScreenUrl, this, "piholeSettings");
registry.registerWidget("menuItem", null, this, null, {objectName: "AppMenuItem", label: qsTr("Pi-Hole"), image: thumbnailIcon, screenUrl: menuScreenUrl, weight: 120});
registry.registerWidget("tile", piholeTileUrl, this, null, {thumbLabel: "PiHole", thumbIcon: thumbnailIcon, thumbCategory: "general", thumbWeight: 30, baseTileWeight: 10, thumbIconVAlignment: "center"});
}
//this function needs to be started after the app is booted.
Component.onCompleted: {
// read user settings
try {
userSettingsJSON = JSON.parse(userSettingsFile.read());
showAppIcon = (userSettingsJSON['ShowTrayIcon'] == "yes") ? true : false
connectionPath = userSettingsJSON['connectionPath'];
var splitVar = connectionPath.split(":")
ipadres = splitVar[0];
poortnummer = splitVar[1];
if (poortnummer.length < 2) poortnummer = "80";
refreshrate = userSettingsJSON['refreshrate'];
authtoken = userSettingsJSON['authtoken'];
} catch(e) {
}
refreshScreen();
}
// refresh screen
function refreshScreen() {
piholeDataRead = false;
readPiHolePHPData();
}
// save user settings
function saveSettings(){
connectionPath = ipadres + ":" + poortnummer;
var tmpUserSettingsJSON = {
"connectionPath" : ipadres + ":" + poortnummer,
"refreshrate" : refreshrate,
"authtoken" : authtoken,
"ShowTrayIcon" : (showAppIcon) ? "yes" : "no"
}
var doc3 = new XMLHttpRequest();
doc3.open("PUT", "file:///mnt/data/tsc/piholestats.userSettings.json");
doc3.send(JSON.stringify(tmpUserSettingsJSON));
}
// read json file
function readPiHolePHPData() {
// console.log("*****PiHole connectionPath:" + connectionPath);
if ( connectionPath.length > 4 ) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "http://"+connectionPath+"/admin/api.php", true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status === 200) {
// console.log("*****PiHole response:" + xmlhttp.responseText);
// saveJSON(xmlhttp.responseText);
piholeConfigJSON = JSON.parse(xmlhttp.responseText);
tmp_ads_blocked_today = piholeConfigJSON['ads_blocked_today'];
// console.log("*****PiHole tmp_ads_blocked_today: " + tmp_ads_blocked_today);
// last tmp_ads_percentage_today = piholeConfigJSON['ads_percentage_today'];
tmp_ads_percentage_today = Math.round(piholeConfigJSON['ads_percentage_today']) + " %";
// console.log("*****PiHole tmp_ads_percentage_today: " + tmp_ads_percentage_today);
} else {
tmp_ads_blocked_today = "server incorrect";
// console.log("*****PiHole tmp_ads_blocked_today: "+ tmp_ads_blocked_today);
tmp_ads_percentage_today = "server incorrect";
// console.log("*****PiHole tmp_ads_percentage_today: "+ tmp_ads_percentage_today);
}
}
}
} else {
tmp_ads_blocked_today = "empty settings";
// console.log("*****PiHole tmp_ads_blocked_today: "+ tmp_ads_blocked_today);
tmp_ads_percentage_today = "empty settings";
// console.log("*****PiHole tmp_ads_percentage_today: "+ tmp_ads_percentage_today);
}
xmlhttp.send();
}
// save json data in json file. Optional, see readPiHolePHPData
function saveJSON(text) {
var doc3 = new XMLHttpRequest();
doc3.open("PUT", "file:///var/volatile/tmp/pihole_retrieved_data.json");
doc3.send(text);
}
// Timer in s * 1000
Timer {
id: datetimeTimer
interval: refreshrate * 1000;
running: false
repeat: true
onTriggered: refreshScreen()
}
}