优化,显示浮点数

This commit is contained in:
aixiao 2022-09-20 10:26:34 +08:00
parent f881a600d3
commit c0ff9cb920
4 changed files with 46 additions and 69 deletions

View File

@ -1,6 +1,6 @@
CROSS_COMPILE ?=
CC := $(CROSS_COMPILE)gcc
CFLAGS += -g -O2 -Wall -L../libini -I../libini -fno-builtin
CFLAGS += -g -Os -Wall -L../libini -I../libini -fno-builtin
LIB += -lini -static
BIN = reboot_temperature

View File

@ -1,6 +1,6 @@
# reboot_temperature
树莓派温度上限时重启或者关机, 第一时间保护硬件, 不处在温度上线过长时间
搭配qqMail(https://git.aixiao.me/aixiao/qqMail)实现QQ邮件发送告警
搭配libesmtp https://git.aixiao.me/aixiao/libesmtp 实现QQ邮件发送告警
## Help Information
@ -12,7 +12,5 @@
nice = -20 // 进程进程优先级 +19 - -20
off_power = 0 // 1关机 0重启
send_mail = "1605227279"; // 发送者QQ
mail_key = "dqqpbbbxoaziba"; // 发送者QQ密钥
recv_mail = "1605227279"; // 接收者QQ
recv_mail = "1605227279@qq.com"; // 接收者QQ

View File

@ -6,8 +6,6 @@ second = 60
nice = -20
off_power = 0
send_mail = "1605227279";
mail_key = "dqqpbbbxoazibafd";
recv_mail = "1605227279";
is_alert = 1
recv_mail = "1605227279@qq.com";

View File

@ -1,6 +1,6 @@
#include "reboot_temperature.h"
int get_temperature(char *path)
static float get_temperature(char *path)
{
char buffer[SIZE];
FILE *fp = NULL;
@ -11,10 +11,10 @@ int get_temperature(char *path)
while (fgets(buffer, SIZE, fp) != NULL) ;
fclose(fp);
return atoi(buffer) / 1000;
return atof(buffer) / (float)1000;
}
int error_log(int l_t, int c_t, char *log_file, char *send_mail, char *mail_key, char *recv_mail)
static int error_log(float l_t, int c_t, char *log_file, char *recv_mail, int _is, int _alert)
{
time_t tmpcal_ptr = 0;
struct tm *tmp_ptr = NULL;
@ -33,44 +33,37 @@ int error_log(int l_t, int c_t, char *log_file, char *send_mail, char *mail_key,
return -1;
// 写入日志文件FD
fprintf(fd, "%d.%d.%d %d:%d:%d %s %d %s %d\n", (1900 + tmp_ptr->tm_year), (1 + tmp_ptr->tm_mon), tmp_ptr->tm_mday, tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec, "树莓CPU温度", l_t, "达到可重启温度", c_t);
// 发送邮件字符串
sprintf(temperature, "%d.%d.%d %d:%d:%d %s %d %s %d\n", (1900 + tmp_ptr->tm_year), (1 + tmp_ptr->tm_mon), tmp_ptr->tm_mday, tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec, "树莓CPU温度", l_t, "达到可重启温度", c_t);
sprintf(buffer, "qqMail -l smtp.qq.com -p 25 -f %s -e %s -q NIUYULING -r %s@QQ.COM -n NIUYULING -s \"Raspberrypi Temperature\" -t \"%s\"", send_mail, mail_key, recv_mail, temperature);
//printf("%s\n", buffer);
fprintf(fd, "%d.%d.%d %d:%d:%d %s %f℃ %s %f℃\n", (1900 + tmp_ptr->tm_year), (1 + tmp_ptr->tm_mon), tmp_ptr->tm_mday, tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec, "Raspberry CPU temperature", l_t, "Restartable or shutdown temperature", (float)c_t);
if (_is == 1) {
if (_alert == 1) {
// 关机活重启时
sprintf(temperature, "%d.%d.%d %d:%d:%d %s %f℃ %s %f℃\n", (1900 + tmp_ptr->tm_year), (1 + tmp_ptr->tm_mon), tmp_ptr->tm_mday, tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec, "Raspberry CPU temperature", l_t, "Restartable or shutdown temperature",
(float)c_t);
sprintf(buffer, "email -r %s -s \"Raspberrypi Temperature\" -t \"%s\"", recv_mail, temperature);
fp = popen(buffer, "r");
pclose(fp);
fclose(fd);
fp = NULL;
fd = NULL;
return 0;
} else {
// 正常温度时
if (tmp_ptr->tm_min >= 10 && tmp_ptr->tm_min <= 12) {
sprintf(temperature, "%d.%d.%d %d:%d:%d %s:%f℃\n", (1900 + tmp_ptr->tm_year), (1 + tmp_ptr->tm_mon), tmp_ptr->tm_mday, tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec, "Raspberry CPU temperature", l_t);
sprintf(buffer, "email -r %s -s \"Raspberrypi Temperature\" -t \"%s\"", recv_mail, temperature);
fp = popen(buffer, "r");
pclose(fp);
fp = NULL;
}
}
int correct_log(int l_t, int c_t, char *log_file)
{
time_t tmpcal_ptr = 0;
struct tm *tmp_ptr = NULL;
FILE *fd = NULL;
}
time(&tmpcal_ptr);
tmp_ptr = localtime(&tmpcal_ptr);
fd = fopen(log_file, "a+");
if (fd == NULL)
return -1;
fprintf(fd, "%d.%d.%d %d:%d:%d %s %d %s %d\n", (1900 + tmp_ptr->tm_year), (1 + tmp_ptr->tm_mon), tmp_ptr->tm_mday, tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec, "树莓派CPU温度", l_t, "未达到重启或者关机温度", c_t);
fclose(fd);
fd = NULL;
return 0;
}
int get_executable_path(char *processdir, char *processname, int len)
static int get_executable_path(char *processdir, char *processname, int len)
{
char *filename;
@ -92,8 +85,7 @@ int main(int argc, char *argv[], char **env)
char log_file[SIZE];
char *inifile = "conf/config.ini";
char path[SIZE] = { 0 };
char send_mail[SIZE] = {0};
char mail_key[SIZE] = {0};
char recv_mail[SIZE] = { 0 };
char executable_filename[SIZE] = { 0 };
(void)get_executable_path(path, executable_filename, sizeof(path));
@ -101,48 +93,37 @@ int main(int argc, char *argv[], char **env)
memset(buffer, 0, SIZE);
memset(log_file, 0, SIZE);
memset(send_mail, 0 , SIZE);
memset(mail_key, 0, SIZE);
memset(recv_mail, 0, SIZE);
if (daemon(1, 1) == -1)
{
if (daemon(1, 1) == -1) {
perror("daemon");
exit(1);
}
if (-1 == (nice(getinikeyint("global", "nice", inifile)))) // 进程优先级
{
perror("nice");
}
while (1)
{
while (1) {
getinikeystring("global", "thermal_zone", inifile, buffer); // 获取thermal_zone路径
getinikeystring("global", "log_file", inifile, log_file); // 获取日志文件名
getinikeystring("global", "send_mail", inifile, send_mail); // 获取发送者QQ号
getinikeystring("global", "mail_key", inifile, mail_key); // 获取QQ邮箱授权码
getinikeystring("global", "recv_mail", inifile, recv_mail); // 获取接收者邮箱
if (get_temperature(buffer) >= getinikeyint("global", "temperature", inifile)) // 达到重启或者关机温度
{
sync();
if (getinikeyint("global", "off_power", inifile) == 1) {
error_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file, send_mail, mail_key, recv_mail);
error_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file, recv_mail, getinikeyint("global", "is_alert", inifile), 1);
return reboot(RB_POWER_OFF); // 关机
}
else
{
error_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file, send_mail, mail_key, recv_mail);
} else {
error_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file, recv_mail, getinikeyint("global", "is_alert", inifile), 1);
return reboot(RB_AUTOBOOT); // 重启
}
}
else // 未达到重启或者关机温度
} else // 未达到重启或者关机温度
{
correct_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file);
error_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file, recv_mail, getinikeyint("global", "is_alert", inifile), 0);
}
// 等待
sleep(getinikeyint("global", "second", inifile));
}