-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatusd.cpp
203 lines (158 loc) · 4.31 KB
/
statusd.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
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "ArduiPi_OLED_lib.h"
#include "Adafruit_GFX.h"
#include "ArduiPi_OLED.h"
#include <time.h>
#include <getopt.h>
#include <sys/time.h>
// Instantiate the display
ArduiPi_OLED display;
// Config Option
struct s_opts
{
int oled;
int verbose;
};
// default options values
s_opts opts = {
OLED_ADAFRUIT_I2C_128x64, // Default oled
false // Not verbose
};
static void init_oled()
{
// SPI change parameters to fit to your LCD
if (display.oled_is_spi_proto(opts.oled)) {
if (!display.init(OLED_SPI_DC, OLED_SPI_RESET, OLED_SPI_CS, opts.oled)) {
exit(EXIT_FAILURE);
}
} else {
if (!display.init(OLED_I2C_RESET, opts.oled)) {
exit(EXIT_FAILURE);
}
}
}
static void get_ip_address(char ip[16])
{
struct ifaddrs *ifaddr, *ifa;
int s;
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
return;
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL) {
continue;
}
s = getnameinfo(ifa->ifa_addr, sizeof (struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if ((0 == strcmp(ifa->ifa_name, "eth0")) && (ifa->ifa_addr->sa_family == AF_INET)) {
if (s != 0) {
printf("getnameinfo() failed: %s\n", gai_strerror(s));
return;
}
strcpy(ip, host);
freeifaddrs(ifaddr);
return;
}
}
freeifaddrs(ifaddr);
}
static bool read_internet_status()
{
return !system("nc -w 10 -z 10.8.0.1 22");
}
static int read_mails_received()
{
const char* filename = "/var/tmp/mails_received.txt";
int mails_received = 0;
struct stat buf;
if (stat(filename, &buf)) {
return -1;
}
if ((time(NULL) - buf.st_mtim.tv_sec) > (24*3600)) {
return -1; // Status file is unreasonably old
}
FILE* fp = fopen(filename, "r");
if (!fp) {
return -1;
}
fscanf(fp, "%d", &mails_received);
fclose(fp);
return mails_received;
}
static void show_status()
{
time_t now;
static time_t last_time;
struct tm * timeinfo;
double percent = 0;
struct timespec tp;
static bool up = true;
const double cycle = 2; // seconds
double half_cycle = cycle / 2.0;
static bool internet_up = false;
static int mails_received = -99;
char ip[16];
usleep(70000);
get_ip_address(ip);
display.clearDisplay();
display.setCursor(0, 0);
time(&now);
timeinfo = localtime(&now);
clock_gettime(CLOCK_REALTIME, &tp);
double secs_fraction = (double)(tp.tv_nsec) / 1000000000.0;
int where_in_cycle = tp.tv_sec % (int)cycle;
up = where_in_cycle < half_cycle;
double where_in_cycle_d = (double)where_in_cycle + secs_fraction;
if (up) {
percent = 100.0 * (where_in_cycle_d / half_cycle);
} else {
percent = 100.0 - 100.0 * ((double)(where_in_cycle_d - half_cycle) / half_cycle);
}
display.setTextSize(2);
display.printf("%2.2d:%2.2d:%2.2d\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
display.setTextSize(1);
// Only do "expensive" stuff every minute
if ((now - last_time) > 60) {
last_time = now;
internet_up = read_internet_status();
mails_received = read_mails_received();
}
display.printf("IP: %s\n", ip);
display.printf("Network %s\n", internet_up ? "UP" : "DOWN");
display.printf("%d mails today.\n", mails_received);
display.drawVerticalBargraph(121, 0, 6, (int16_t)display.height(), WHITE, (int)percent);
display.display();
}
static void close_display()
{
// Free PI GPIO ports
display.close();
}
// Free PI GPIO ports
int main(int argc, char **argv)
{
if (argc > 1 && !strcmp("-d", argv[1])) {
if (daemon(0, 0)) {
fprintf(stderr, "Daemon call failed.\n");
return 1;
}
}
atexit(close_display);
init_oled();
display.begin();
display.setTextColor(WHITE);
while (1) {
show_status();
}
return 0;
}