-
Notifications
You must be signed in to change notification settings - Fork 16
/
astroberry_system.cpp
393 lines (329 loc) · 11 KB
/
astroberry_system.cpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*******************************************************************************
Copyright(c) 2015-2020 Radek Kaczorek <rkaczorek AT gmail DOT com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*******************************************************************************/
#include <stdio.h>
#include <memory>
#include <string.h>
#include "config.h"
#include "astroberry_system.h"
#include <gpiod.h>
// We declare an auto pointer to IndiAstroberrySystem
std::unique_ptr<IndiAstroberrySystem> indiAstroberrySystem(new IndiAstroberrySystem());
void ISPoll(void *p);
void ISInit()
{
static int isInit = 0;
if (isInit == 1)
return;
if(indiAstroberrySystem.get() == 0)
{
isInit = 1;
indiAstroberrySystem.reset(new IndiAstroberrySystem());
}
}
void ISGetProperties(const char *dev)
{
ISInit();
indiAstroberrySystem->ISGetProperties(dev);
}
void ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int num)
{
ISInit();
indiAstroberrySystem->ISNewSwitch(dev, name, states, names, num);
}
void ISNewText( const char *dev, const char *name, char *texts[], char *names[], int num)
{
ISInit();
indiAstroberrySystem->ISNewText(dev, name, texts, names, num);
}
void ISNewNumber(const char *dev, const char *name, double values[], char *names[], int num)
{
ISInit();
indiAstroberrySystem->ISNewNumber(dev, name, values, names, num);
}
void ISNewBLOB (const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int num)
{
INDI_UNUSED(dev);
INDI_UNUSED(name);
INDI_UNUSED(sizes);
INDI_UNUSED(blobsizes);
INDI_UNUSED(blobs);
INDI_UNUSED(formats);
INDI_UNUSED(names);
INDI_UNUSED(num);
}
void ISSnoopDevice (XMLEle *root)
{
ISInit();
indiAstroberrySystem->ISSnoopDevice(root);
}
IndiAstroberrySystem::IndiAstroberrySystem()
{
setVersion(VERSION_MAJOR,VERSION_MINOR);
}
IndiAstroberrySystem::~IndiAstroberrySystem()
{
}
bool IndiAstroberrySystem::Connect()
{
SetTimer(1000);
IDMessage(getDeviceName(), "Astroberry System connected successfully.");
// Get basic system info
FILE* pipe;
char buffer[128];
//update Hardware
//https://www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/README.md
pipe = popen("cat /sys/firmware/devicetree/base/model", "r");
fgets(buffer, 128, pipe);
pclose(pipe);
IUSaveText(&SysInfoT[0], buffer);
//update CPU temp
pipe = popen("echo $(($(cat /sys/class/thermal/thermal_zone0/temp)/1000))", "r");
fgets(buffer, 128, pipe);
pclose(pipe);
IUSaveText(&SysInfoT[1], buffer);
//update uptime
pipe = popen("uptime|awk -F, '{print $1}'|awk -Fup '{print $2}'|xargs", "r");
fgets(buffer, 128, pipe);
pclose(pipe);
IUSaveText(&SysInfoT[2], buffer);
//update load
pipe = popen("uptime|awk -F, '{print $3\" /\"$4\" /\"$5}'|awk -F: '{print $2}'|xargs", "r");
fgets(buffer, 128, pipe);
pclose(pipe);
IUSaveText(&SysInfoT[3], buffer);
//update Hostname
pipe = popen("hostname", "r");
fgets(buffer, 128, pipe);
pclose(pipe);
IUSaveText(&SysInfoT[4], buffer);
//update Local IP
pipe = popen("hostname -I|awk -F' ' '{print $1}'|xargs", "r");
fgets(buffer, 128, pipe);
pclose(pipe);
IUSaveText(&SysInfoT[5], buffer);
//update Public IP
pipe = popen("wget -qO- http://ipecho.net/plain|xargs", "r");
fgets(buffer, 128, pipe);
pclose(pipe);
IUSaveText(&SysInfoT[6], buffer);
// Update client
IDSetText(&SysInfoTP, NULL);
return true;
}
bool IndiAstroberrySystem::Disconnect()
{
IDMessage(getDeviceName(), "Astroberry System disconnected successfully.");
return true;
}
void IndiAstroberrySystem::TimerHit()
{
if(isConnected())
{
// update time
struct tm *local_timeinfo;
static char ts[32];
time_t rawtime;
time(&rawtime);
local_timeinfo = localtime (&rawtime);
strftime(ts, 20, "%Y-%m-%dT%H:%M:%S", local_timeinfo);
IUSaveText(&SysTimeT[0], ts);
snprintf(ts, sizeof(ts), "%4.2f", (local_timeinfo->tm_gmtoff/3600.0));
IUSaveText(&SysTimeT[1], ts);
SysTimeTP.s = IPS_OK;
IDSetText(&SysTimeTP, NULL);
if (polling++ > 59)
{
FILE* pipe;
char buffer[128];
SysInfoTP.s = IPS_BUSY;
IDSetText(&SysInfoTP, NULL);
//update CPU temp
pipe = popen("echo $(($(cat /sys/class/thermal/thermal_zone0/temp)/1000))", "r");
fgets(buffer, 128, pipe);
pclose(pipe);
IUSaveText(&SysInfoT[1], buffer);
//update uptime
pipe = popen("uptime|awk -F, '{print $1}'|awk -Fup '{print $2}'|xargs", "r");
fgets(buffer, 128, pipe);
pclose(pipe);
IUSaveText(&SysInfoT[2], buffer);
//update load
pipe = popen("uptime|awk -F, '{print $3\" /\"$4\" /\"$5}'|awk -F: '{print $2}'|xargs", "r");
fgets(buffer, 128, pipe);
pclose(pipe);
IUSaveText(&SysInfoT[3], buffer);
SysInfoTP.s = IPS_OK;
IDSetText(&SysInfoTP, NULL);
polling = 0;
}
SetTimer(1000);
}
}
const char * IndiAstroberrySystem::getDefaultName()
{
return (char *)"Astroberry System";
}
bool IndiAstroberrySystem::initProperties()
{
// We init parent properties first
INDI::DefaultDevice::initProperties();
IUFillText(&SysTimeT[0],"LOCAL_TIME","Local Time",NULL);
IUFillText(&SysTimeT[1],"UTC_OFFSET","UTC Offset",NULL);
IUFillTextVector(&SysTimeTP,SysTimeT,2,getDeviceName(),"SYSTEM_TIME","System Time",MAIN_CONTROL_TAB,IP_RO,60,IPS_IDLE);
IUFillText(&SysInfoT[0],"HARDWARE","Hardware",NULL);
IUFillText(&SysInfoT[1],"CPU TEMP","CPU Temp (°C)",NULL);
IUFillText(&SysInfoT[2],"UPTIME","Uptime (hh:mm)",NULL);
IUFillText(&SysInfoT[3],"LOAD","Load (1 / 5 / 15 min.)",NULL);
IUFillText(&SysInfoT[4],"HOSTNAME","Hostname",NULL);
IUFillText(&SysInfoT[5],"LOCAL_IP","Local IP",NULL);
IUFillText(&SysInfoT[6],"PUBLIC_IP","Public IP",NULL);
IUFillTextVector(&SysInfoTP,SysInfoT,7,getDeviceName(),"SYSTEM_INFO","System Info",MAIN_CONTROL_TAB,IP_RO,60,IPS_IDLE);
IUFillSwitch(&SysControlS[0], "SYSCTRL_REBOOT", "Reboot", ISS_OFF);
IUFillSwitch(&SysControlS[1], "SYSCTRL_SHUTDOWN", "Shutdown", ISS_OFF);
IUFillSwitchVector(&SysControlSP, SysControlS, 2, getDeviceName(), "SYSCTRL", "System Ctrl", MAIN_CONTROL_TAB, IP_RW, ISR_1OFMANY, 0, IPS_IDLE);
IUFillSwitch(&SysOpConfirmS[0], "SYSOPCONFIRM_CONFIRM", "Yes", ISS_OFF);
IUFillSwitch(&SysOpConfirmS[1], "SYSOPCONFIRM_CANCEL", "No", ISS_OFF);
IUFillSwitchVector(&SysOpConfirmSP, SysOpConfirmS, 2, getDeviceName(), "SYSOPCONFIRM", "Continue?", MAIN_CONTROL_TAB, IP_RW, ISR_1OFMANY, 0, IPS_IDLE);
return true;
}
bool IndiAstroberrySystem::updateProperties()
{
// Call parent update properties first
INDI::DefaultDevice::updateProperties();
if (isConnected())
{
defineText(&SysTimeTP);
defineText(&SysInfoTP);
defineSwitch(&SysControlSP);
}
else
{
// We're disconnected
deleteProperty(SysTimeTP.name);
deleteProperty(SysInfoTP.name);
deleteProperty(SysControlSP.name);
}
return true;
}
void IndiAstroberrySystem::ISGetProperties(const char *dev)
{
INDI::DefaultDevice::ISGetProperties(dev);
}
bool IndiAstroberrySystem::ISNewNumber (const char *dev, const char *name, double values[], char *names[], int n)
{
return INDI::DefaultDevice::ISNewNumber(dev,name,values,names,n);
}
bool IndiAstroberrySystem::ISNewSwitch (const char *dev, const char *name, ISState *states, char *names[], int n)
{
// first we check if it's for our device
if (!strcmp(dev, getDeviceName()))
{
// handle system control
if (!strcmp(name, SysControlSP.name))
{
IUUpdateSwitch(&SysControlSP, states, names, n);
if ( SysControlS[0].s == ISS_ON )
{
DEBUG(INDI::Logger::DBG_SESSION, "Astroberry device is set to REBOOT. Confirm or Cancel operation.");
SysControlSP.s = IPS_BUSY;
IDSetSwitch(&SysControlSP, NULL);
// confirm switch
defineSwitch(&SysOpConfirmSP);
return true;
}
if ( SysControlS[1].s == ISS_ON )
{
DEBUG(INDI::Logger::DBG_SESSION, "Astroberry device is set to SHUT DOWN. Confirm or Cancel operation.");
SysControlSP.s = IPS_BUSY;
IDSetSwitch(&SysControlSP, NULL);
// confirm switch
defineSwitch(&SysOpConfirmSP);
return true;
}
}
// handle system control confirmation
if (!strcmp(name, SysOpConfirmSP.name))
{
IUUpdateSwitch(&SysOpConfirmSP, states, names, n);
if ( SysOpConfirmS[0].s == ISS_ON )
{
SysOpConfirmSP.s = IPS_IDLE;
IDSetSwitch(&SysOpConfirmSP, NULL);
SysOpConfirmS[0].s = ISS_OFF;
IDSetSwitch(&SysOpConfirmSP, NULL);
// execute system operation
if (SysControlS[0].s == ISS_ON)
{
DEBUG(INDI::Logger::DBG_SESSION, "System operation confirmed. System is going to REBOOT now");
FILE* pipe;
char buffer[512];
pipe = popen("sudo reboot", "r");
fgets(buffer, 512, pipe);
pclose(pipe);
DEBUGF(INDI::Logger::DBG_SESSION, "System output: %s", buffer);
}
if (SysControlS[1].s == ISS_ON)
{
DEBUG(INDI::Logger::DBG_SESSION, "System operation confirmed. System is going to SHUT DOWN now");
FILE* pipe;
char buffer[512];
pipe = popen("sudo poweroff", "r");
fgets(buffer, 512, pipe);
pclose(pipe);
DEBUGF(INDI::Logger::DBG_SESSION, "System output: %s", buffer);
}
// reset system control buttons
SysControlSP.s = IPS_IDLE;
IDSetSwitch(&SysControlSP, NULL);
SysControlS[0].s = ISS_OFF;
SysControlS[1].s = ISS_OFF;
IDSetSwitch(&SysControlSP, NULL);
deleteProperty(SysOpConfirmSP.name);
return true;
}
if ( SysOpConfirmS[1].s == ISS_ON )
{
DEBUG(INDI::Logger::DBG_SESSION, "System operation canceled.");
SysOpConfirmSP.s = IPS_IDLE;
IDSetSwitch(&SysOpConfirmSP, NULL);
SysOpConfirmS[1].s = ISS_OFF;
IDSetSwitch(&SysOpConfirmSP, NULL);
// reset system control buttons
SysControlSP.s = IPS_IDLE;
IDSetSwitch(&SysControlSP, NULL);
SysControlS[0].s = ISS_OFF;
SysControlS[1].s = ISS_OFF;
IDSetSwitch(&SysControlSP, NULL);
deleteProperty(SysOpConfirmSP.name);
return true;
}
}
}
return INDI::DefaultDevice::ISNewSwitch (dev, name, states, names, n);
}
bool IndiAstroberrySystem::ISNewText (const char *dev, const char *name, char *texts[], char *names[], int n)
{
return INDI::DefaultDevice::ISNewText (dev, name, texts, names, n);
}
bool IndiAstroberrySystem::ISNewBLOB (const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n)
{
return INDI::DefaultDevice::ISNewBLOB (dev, name, sizes, blobsizes, blobs, formats, names, n);
}
bool IndiAstroberrySystem::ISSnoopDevice(XMLEle *root)
{
return INDI::DefaultDevice::ISSnoopDevice(root);
}