-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetData.js
executable file
·138 lines (108 loc) · 3 KB
/
getData.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
import { readConfig, definedIp, definedApiKey, inMilliseconds } from './readConfig.js'
var configIncompleteText;
var currentMode;
var currentState = 0;
var currentTemp;
var heatTemp;
var coolTemp;
var currentHum;
var fan;
var fanState;
var message = '';
class GetData {
constructor() {
this.protocol = 'http';
this.count = 0;
this.interval = inMilliseconds;
this.updateInterval = setInterval(
this.init.bind( this ),
inMilliseconds
)
}
async retrieveData() {
await readConfig.init();
if ( this.interval != inMilliseconds ) {
clearInterval ( this.updateInterval );
this.updateInterval = setInterval(
this.init.bind( this ),
inMilliseconds
)
this.interval = inMilliseconds;
}
if ( definedIp == '' || definedIp === undefined || definedApiKey == '' || definedApiKey === undefined ) {
console.log( 'Missing Data. Please update config.' );
configIncompleteText = 'Config is incomplete. Click Settings above.';
}
else {
const result = await fetch( `${this.protocol}://${definedIp}/query/info` );
const json = await result.json();
this.count += 1;
console.log( json );
console.log( 'Data updated.' );
console.log( 'Mode is:', json.mode );
console.log( 'State is:', json.state );
console.log( 'Count is:', this.count );
if ( json.state != currentState ) {
if ( json.state == 1 ) { message = 'System is now heating.'; }
else if ( json.state == 2 ) { message = 'System is now cooling.'; }
else { message = 'System is now idle.'; }
console.log( message );
}
else {
message = '';
}
configIncompleteText = '';
currentMode = json.mode;
currentState = json.state;
currentTemp = json.spacetemp;
heatTemp = json.heattemp;
coolTemp = json.cooltemp;
currentHum = json.hum;
fan = json.fan;
fanState = json.fanstate;
}
}
async didRetrieveData() {
await this.retrieveData()
.catch( ( error ) => {
console.log( '' );
console.log( 'Something went wrong. Try updating config.' );
console.log( '' );
console.warn( error );
} );
}
async sendData() {
await this.didRetrieveData();
if ( message != '' ) {
const sendToApi = fetch( 'https://api.studiojq.io/notifications', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify( {
apikey: definedApiKey,
message: message
} )
} ).then( response => response.json() ).then( ( data ) => {
console.log( data.message );
configIncompleteText = data.configtext;
} );
}
}
async didSendData() {
await this.sendData()
.catch( ( error ) => {
console.log( '' );
console.log( 'Something went wrong. Check your config.' );
console.log( '' );
console.warn( error );
} );
}
async init() {
await this.didSendData();
}
}
await readConfig.init();
const getData = new GetData();
await getData.init();
export { configIncompleteText, currentMode, currentState, currentTemp, heatTemp, coolTemp, currentHum, fan, fanState, inMilliseconds };