| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953 |
- #include "common.h"
- #include "elog.h"
- #include <unistd.h>
- #include <time.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <sys/sysinfo.h>
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <net/if.h>
- #include <arpa/inet.h>
- #include <sys/vfs.h>
- #include <sys/ioctl.h>
- #include <linux/rtc.h>
- #include "cfg.h"
- #include "sys.h"
- #undef LOG_TAG
- #define LOG_TAG "main"
- GlobalDeviceManager __globalDeviceManage={0};
- GlobalDeviceManager __globalDeviceManage2={0};
- int ntp_sync_flag=0;
- unsigned long __globalSxIdx;
- _User_Log _GlobalUserLoginInfo;
- UserInfo _GlobalUserInfoNew;
- char _GlobalUserToken[64];
- MenuAuthority _GlobalMenuAuthority;
- NTPManager _globalNTPManager;
- SNMPManager _globalSNMPManager;
- void printf_global_device_info(GlobalDeviceInfo* _globalDeviceInfo)
- {
- log_d("******global device info********* ");
- log_d("product.id:%d ", _globalDeviceInfo->product.id);
- log_d("product.number:%s ", _globalDeviceInfo->product.number);
- log_d("product.name:%s ", _globalDeviceInfo->product.name);
- log_d("product.ype:%d ", _globalDeviceInfo->product.type);
- log_d("product.pwr_type:%d ", _globalDeviceInfo->product.pwr_type);
- log_d("product.version:%s ", _globalDeviceInfo->product.version);
- log_d("product.count:%d ", _globalDeviceInfo->product.count);
- log_d("product.samp_time:%d ", _globalDeviceInfo->product.samp_time);
- log_d("product.save_time:%d ", _globalDeviceInfo->product.save_time);
- log_d("cascade.mode:%d ", _globalDeviceInfo->cascade.mode);
- log_d("cascade.addr:%d ", _globalDeviceInfo->cascade.addr);
- log_d("cascade.baudrate:%d ", _globalDeviceInfo->cascade.baudrate);
- log_d("relay.path:%s ", _globalDeviceInfo->relay.path);
- log_d("******************************** ");
- }
- void printf_global_power_manage_info(GlobalPowerManger* _globalPowerManger)
- {
- printf("******global power manage info********* ");
- printf("product_id:%d ", _globalPowerManger->product_id);
- printf("product_ch_id:%d ", _globalPowerManger->product_ch_id);
- printf("product_ch_name:%s ", _globalPowerManger->product_ch_name);
- printf("product_ch_type:%d ", _globalPowerManger->product_ch_type);
- printf("product_ch_status:%d ", _globalPowerManger->product_ch_status);
- printf("product_ch_NF_status:%d ", _globalPowerManger->product_ch_NF_status);
- printf("product_ch_start_delay:%d ", _globalPowerManger->product_ch_start_delay);
- printf("product_ch_stop_delay:%d ", _globalPowerManger->product_ch_stop_delay);
- printf("product_vol_upper_enable:%d ", _globalPowerManger->global_over_manager.product_vol_upper_enable);
- printf("product_vol_upper_threshold:%0.2f ", _globalPowerManger->global_over_manager.product_vol_upper_threshold);
- printf("product_vol_over_upper_threshold_ctrl:%d ", _globalPowerManger->global_over_manager.product_vol_over_upper_threshold_ctrl);
- printf("product_vol_over_upper_threshold_para:%d ", _globalPowerManger->global_over_manager.product_vol_over_upper_threshold_ctrl_para);
- printf("product_vol_lower_enable:%d ", _globalPowerManger->global_over_manager.product_vol_lower_enable);
- printf("product_vol_lower_threshold:%0.2f ", _globalPowerManger->global_over_manager.product_vol_lower_threshold);
- printf("product_vol_over_lower_threshold_ctrl:%d ", _globalPowerManger->global_over_manager.product_vol_over_lower_threshold_ctrl);
- printf("product_vol_over_lower_threshold_ctrl_para:%d ", _globalPowerManger->global_over_manager.product_vol_over_lower_threshold_ctrl_para);
- printf("product_cur_upper_enable:%d ", _globalPowerManger->global_over_manager.product_cur_upper_enable);
- printf("product_cur_upper_threshold:%0.2f ", _globalPowerManger->global_over_manager.product_cur_upper_threshold);
- printf("product_cur_over_upper_threshold_ctrl:%d ", _globalPowerManger->global_over_manager.product_cur_over_upper_threshold_ctrl);
- printf("product_cur_over_upper_threshold_ctrl_para:%d ", _globalPowerManger->global_over_manager.product_cur_over_upper_threshold_ctrl_para);
- printf("product_pwr_upper_enable:%d ", _globalPowerManger->global_over_manager.product_pwr_upper_enable);
- printf("product_pwr_upper_threshold:%0.2f ", _globalPowerManger->global_over_manager.product_pwr_upper_threshold);
- printf("product_pwr_over_upper_threshold_ctrl:%d ", _globalPowerManger->global_over_manager.product_pwr_over_upper_threshold_ctrl);
- printf("product_pwr_over_upper_threshold_ctrl_para:%d ", _globalPowerManger->global_over_manager.product_pwr_over_upper_threshold_ctrl_para);
- printf("product_pwrcon_upper_enable:%d ", _globalPowerManger->global_over_manager.product_pwrcon_upper_enable);
- printf("product_pwrcon_upper_threshold:%0.2f ", _globalPowerManger->global_over_manager.product_pwrcon_upper_threshold);
- printf("product_pwrcon_over_upper_threshold_ctrl:%d ", _globalPowerManger->global_over_manager.product_pwrcon_over_upper_threshold_ctrl);
- printf("product_pwrcon_over_upper_threshold_ctrl_para:%d ", _globalPowerManger->global_over_manager.product_pwrcon_over_upper_threshold_ctrl_para);
- printf("******************************** ");
- }
- //打印电源信息
- void printf_power_info(GlobalPowerInfo* _globalPowerInfo)
- {
- struct list_head *pos;
- GlobalPowerInfo* _temp;
- int i = 0;
- list_for_each_entry(_temp, &_globalPowerInfo->list, list)
- {
- printf("i:%d ", i++);
- printf("samp_time:%s ", _temp->samp_time);
- printf("product_id:%d ", _temp->product_id);
- }
- }
- void INIT_LIST_HEAD(struct list_head *list)
- {
- list->next = list->prev = list;
- }
- /**
- * EasyLogger demo
- */
- // void test_elog(void) {
- // uint8_t buf[256]= {0};
- // int i = 0;
- // for (i = 0; i < sizeof(buf); i++)
- // {
- // buf[i] = i;
- // }
- // while(true) {
- // /* test log output for all level */
- // log_a("Hello EasyLogger!");
- // log_e("Hello EasyLogger!");
- // log_w("Hello EasyLogger!");
- // log_i("Hello EasyLogger!");
- // log_d("Hello EasyLogger!");
- // log_v("Hello EasyLogger!");
- // // elog_raw("Hello EasyLogger!");
- // elog_hexdump("test", 16, buf, sizeof(buf));
- // sleep(5);
- // }
- // }
- #if(1)
- void initLogger(void)
- {
- setbuf(stdout, NULL);
- elog_init();
- elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL);
- elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
- elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
- elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
- elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
- elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
- elog_set_text_color_enabled(true);
- elog_start();
- log_i("Smart Pdu Application Running!");
- }
- #endif
- /// @brief 获取系统mem大小
- /// @param totalSize
- /// @return
- int getTotalMemorySize(unsigned int* totalSize)
- {
- struct sysinfo memInfo;
-
- if (sysinfo(&memInfo) != -1) {
- *totalSize = memInfo.totalram;
- // printf("Total RAM size: %lu bytes\n", memInfo.totalram);
- // printf("Free RAM size: %lu bytes\n", memInfo.freeram);
- }
- else
- {
- *totalSize = 0 ;
- return -1;
- }
- return 0;
- }
- /// @brief 返回设备IP地址
- /// @param dev 网口名
- /// @return
- char* getLocalIpAddress(char* dev)
- {
- int fd;
- struct ifreq ifr;
- static char ip_addr[64];
- fd = socket(AF_INET, SOCK_DGRAM, 0);
- ifr.ifr_addr.sa_family = AF_INET;
- strncpy(ifr.ifr_name, dev, IFNAMSIZ-1);
-
- ioctl(fd, SIOCGIFADDR, &ifr);
- close(fd);
- char *p=inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
- if(!p) {
- return NULL;
- }
- strcpy(ip_addr, p);
- return ip_addr;
- }
- // 获取MAC地址的函数
- char* get_mac_address(const char *iface_name)
- {
- int fd;
- struct ifreq ifr;
- unsigned char *mac;
- static char mac_str[32];
- snprintf(mac_str, sizeof(mac_str), "%02d:%02d:%02d:%02d:%02d:%02d",
- 0, 0, 0, 0, 0, 0);
- // 创建一个socket
- if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
- perror("socket error");
- return mac_str;
- }
-
- // 设置接口名称
- strncpy(ifr.ifr_name, iface_name, IFNAMSIZ - 1);
-
- // 获取MAC地址
- if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) {
- perror("ioctl error");
- close(fd);
- return mac_str;
- }
- // ifr.ifr_hwaddr.sa_data是一个包含硬件地址的字节数组
- mac = (unsigned char *)ifr.ifr_hwaddr.sa_data;
- // 将MAC地址转换为字符串格式
- snprintf(mac_str, sizeof(mac_str), "%02X:%02X:%02X:%02X:%02X:%02X",
- mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
-
- // 关闭socket
- close(fd);
-
- return mac_str;
- }
- /// @brief 获取磁盘使用率
- /// @param reused
- void get_disk_occupy(char ** reused)
- {
- const unsigned short MAXBUFSIZE = 1024;
- char currentDirectoryPath[ MAXBUFSIZE ];
- getcwd(currentDirectoryPath, MAXBUFSIZE);
- //printf("当前目录:%s\n",currentDirectoryPath);
- char cmd[50] = "df ";
- strcat(cmd, currentDirectoryPath);
- //printf("%s\n",cmd);
-
- char buffer[MAXBUFSIZE];
- FILE* pipe = popen(cmd, "r");
- char fileSys[20];
- char blocks[20];
- char used[20];
- char free[20];
- char percent[10];
- char moment[20];
- if (!pipe)
- {
- return;
- }
- if (fgets(buffer, sizeof(buffer), pipe) != NULL)
- {
- sscanf(buffer, "%s %s %s %s %s %s", fileSys, blocks, used, free, percent, moment);
- }
- if (fgets(buffer, sizeof(buffer), pipe) != NULL)
- {
- sscanf(buffer, "%s %s %s %s %s %s", fileSys, blocks, used, free, percent, moment);
- }
- //printf("desk used:%s\n",percent);
- strcpy(*reused, percent);
- return;
- }
- static int set_rtc(time_t t)
- {
- int r=0,fd;
- // #if (CHIP_TYPE == CHIP_T113s)
- // system("hwclock -w -f /dev/rtc0");
- // #else
-
- struct rtc_time *rtc;
- struct tm *tm = gmtime(&t);
- fd = open("/dev/rtc0", O_RDONLY);
- if (fd < 0) {
- perror("open rtc failed\n");
- return -1;
- }
-
- rtc = (struct rtc_time*)tm;
- if (ioctl(fd, RTC_SET_TIME, rtc) < 0) {
- perror("ioctl RTC_SET_TIME failed\n");
- r = -1;
- }
- else {
- printf("set rtc ok!\n");
- }
- close(fd);
- //#endif
- return r;
- }
- #define MY_MINUTE 60
- #define MY_HOUR (60*MY_MINUTE)
- #define MY_DAY (24*MY_HOUR)
- #define MY_YEAR (365*MY_DAY)
- static int my_month[12] = {
- 0,
- MY_DAY*(31),
- MY_DAY*(31+29),
- MY_DAY*(31+29+31),
- MY_DAY*(31+29+31+30),
- MY_DAY*(31+29+31+30+31),
- MY_DAY*(31+29+31+30+31+30),
- MY_DAY*(31+29+31+30+31+30+31),
- MY_DAY*(31+29+31+30+31+30+31+31),
- MY_DAY*(31+29+31+30+31+30+31+31+30),
- MY_DAY*(31+29+31+30+31+30+31+31+30+31),
- MY_DAY*(31+29+31+30+31+30+31+31+30+31+30)
- };
- time_t mktime2(struct tm *t, int tzone)
- {
- time_t res,year;
- year = t->tm_year - 70;
- /* magic offsets (y+1) needed to get leapyears right.*/
- res = MY_YEAR*year + MY_DAY*((year+1)/4);
- res += my_month[t->tm_mon];
- /* and (y+2) here. If it wasn't a leap-year, we have to adjust */
- if (t->tm_mon>1 && ((year+2)%4))
- res -= MY_DAY;
- res += MY_DAY*(t->tm_mday-1);
- res += MY_HOUR*t->tm_hour;
- res += MY_MINUTE*t->tm_min;
- res += t->tm_sec;
- res -= tzone*3600;
- return res;
- }
- time_t mktime0(void)
- {
- struct tm t;
- t.tm_year = 70;
- t.tm_mon = 0;
- t.tm_mday = 1;
- t.tm_hour = 0;
- t.tm_min = 0;
- t.tm_sec = 0;
- return mktime(&t);
- }
- int set_time(struct tm *t, int tzone)
- {
- if(!t) {
- return -1;
- }
- t->tm_year -= 1900;
- t->tm_mon -= 1;
- time_t utc = mktime2(t, tzone);
- stime(&utc);
- set_rtc(utc);
- return 0;
- }
- int set_utctime(time_t t)
- {
- stime(&t);
- set_rtc(t);
- #if (CHIP_TYPE == CHIP_T113s)
- sleep(1);
- system("hwclock --show");
-
- system("hwclock -s");
- #endif
- return 0;
- }
- int get_tzone(void)
- {
- int tz = 0;
- #if (CHIP_TYPE == CHIP_T113s)
- time_t t1,t2;
- struct tm *tm_local,*tm_utc;
- time(&t1);
- t2=t1;
- tm_local = localtime(&t1);
- t1=mktime(tm_local);
- tm_utc = gmtime(&t2);
- t2=mktime(tm_utc);
- tz = (t1 - t2)/3600;
- #else
-
- time_t now = time(NULL);
- struct tm local_time = *localtime(&now);
- time_t utc = mktime(&local_time);
- tz = (now - utc)/3600;
- #endif
- return tz;
- }
- int set_tzone(int tzone)
- {
- int r,tz;
- char buf[256];
- tz = get_tzone();
- #if 0
- switch(zone) {
- case -11:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/Pacific/Midway /etc/localtime");
- break;
- case -10:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/HST /etc/localtime");
- break;
- case -9:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Adak /etc/localtime");
- break;
- case -8:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Boise /etc/localtime");
- break;
- case -7:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/MST /etc/localtime");
- break;
- case -6:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Chicago /etc/localtime");
- break;
- case -5:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Detroit /etc/localtime");
- break;
- case -4:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Halifax /etc/localtime");
- break;
- case -3:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Maceio /etc/localtime");
- break;
- case -2:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Noronha /etc/localtime");
- break;
- case -1:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Scoresbysund /etc/localtime");
- break;
- case 0:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/London /etc/localtime");
- break;
- case 1:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/Paris /etc/localtime");
- break;
- case 2:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/EET /etc/localtime");
- break;
- case 3:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/Moscow /etc/localtime");
- break;
- case 4:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Amman /etc/localtime");
- break;
- case 5:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Karachi /etc/localtime");
- break;
- case 6:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Dhaka /etc/localtime");
- break;
- case 7:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Bangkok /etc/localtime");
- break;
- case 8:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Chongqing /etc/localtime");
- break;
- case 9:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime");
- break;
- case 10:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/Australia/Sydney /etc/localtime");
- break;
- case 11:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Sakhalin /etc/localtime");
- break;
- case 12:
- sprintf(buf, "ln -sf /usr/share/zoneinfo/Pacific/Fiji /etc/localtime");
- break;
- default:
- return -1;
- }
- #else
- #if (CHIP_TYPE == CHIP_T113s)
- sprintf(buf, "ln -sf /usr/share/zoneinfo/GMT%s%d /etc/localtime", (tzone<0)?"+":"-", abs(tzone));
- #else
- sprintf(buf, "ln -sf /usr/share/zoneinfo/Etc/GMT%s%d /etc/localtime", (tzone<0)?"+":"-", abs(tzone));
- #endif
- #endif
- log_i("set_zone: %s", buf);
- system(buf);
- if(tz!=tzone) {
- printf("tz %d tzone %d\n",tz,tzone);
- sys_restart_app();
- }
-
- return 0;
- }
- void print_time(char *s, struct tm *tm)
- {
- printf("___%s___, %04d-%02d-%02d %02d:%02d:%02d\n", s, tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
- }
- void print_utc(char *s, time_t t)
- {
- struct tm *tm=gmtime(&t);
- tm->tm_year += 1900;
- tm->tm_mon += 1;
- print_time(s, tm);
- }
- int set_time_zone(struct tm *t, int zone)
- {
- int r;
- if(!t) {
- return -1;
- }
- r = set_time(t, zone);
- if(r) {
- log_e("__1__ set_time failed\n");
- }
- r = set_tzone(zone);
- if(r) {
- log_e("__1__ set_tzone failed\n");
- }
- return r;
- }
- int set_utctime_zone(time_t t, int zone)
- {
- int r;
- r = set_utctime(t);
- if(r) {
- log_e("__2__ set_time failed\n");
- }
- r = set_tzone(zone);
- if(r) {
- log_e("__2__ set_zone failed\n");
- }
-
- return r;
- }
- int get_time_str(char *s, int len, time_t t)
- {
- struct tm *tm=localtime(&t);
- return snprintf(s, len, "%04d-%02d-%02d %02d:%02d:%02d.000",tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
- }
- int get_time_str2(char *s, int len, time_t t)
- {
- struct tm *tm=localtime(&t);
- return snprintf(s, len, "%04d%02d%02d%02d%02d%02d", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
- }
- int get_curtime_str(char *s, int len)
- {
- time_t t=time(NULL);
- struct tm *tm=localtime(&t);
- return snprintf(s, len, "%04d-%02d-%02d %02d:%02d:%02d", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
- }
- int get_curtime_str2(char *s, int len)
- {
- time_t t=time(NULL);
- struct tm *tm=localtime(&t);
- return snprintf(s, len, "%02d.%02d %02d:%02d:%02d", tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
- }
- char* getUserNamebyToken(char* Token)
- {
- char* userName = NULL;
- if (Token==NULL)
- {
- return NULL;
- }
-
- _User_Log *_UserInfoTemp = NULL;
- list_for_each_entry(_UserInfoTemp, &_GlobalUserLoginInfo.list, list)
- {
- if (_UserInfoTemp == NULL)
- break;
- if (strcmp(_UserInfoTemp->user_token, Token) == 0)
- {
- userName=_UserInfoTemp->user_name;
- }
- }
- return userName;
- }
- typedef struct {
- uint16_t year;
- uint8_t mon;
- uint8_t day;
- uint8_t hour;
- uint8_t min;
- uint8_t sec;
- }datetime_t;
- int get_bldtime(char *buf, int len)
- {
- char mon[10]={0};
- datetime_t dt;
- char *mon2[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
- sscanf(__DATE__, "%s %hhu %hu", mon,&dt.day,&dt.year);
- sscanf(__TIME__, "%hhu:%hhu:%hhu", &dt.hour,&dt.min,&dt.sec);
- for(int i=0;i<12;i++) {
- if(strcmp(mon, mon2[i])==0) {
- dt.mon = i+1; break;
- }
- }
- return snprintf(buf,len,"%04hu/%02hhu/%02hhu %02hhu:%02hhu:%02hhu",dt.year,dt.mon,dt.day,dt.hour,dt.min,dt.sec);
- }
- int get_bldtime2(char *dst, char *ori)
- {
- char mon[10]={0};
- datetime_t dt;
- char *mon2[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
- sscanf(ori, "%s %hhu %hu %hhu:%hhu:%hhu", mon,&dt.day,&dt.year,&dt.hour,&dt.min,&dt.sec);
- for(int i=0;i<12;i++) {
- if(strcmp(mon, mon2[i])==0) {
- dt.mon = i+1; break;
- }
- }
- return sprintf(dst,"%04hu/%02hhu/%02hhu %02hhu:%02hhu:%02hhu",dt.year,dt.mon,dt.day,dt.hour,dt.min,dt.sec);
- }
- int is_process_running(const char *process_name)
- {
- DIR *dir;
- struct dirent *entry;
- char path[1035]; // /proc/[pid]/cmdline 的最大路径长度(/proc/ + PID(最多5位) + /cmdline + \0)
- char buffer[4096]; // 假设cmdline不会超过这个长度
- FILE *file;
-
- dir = opendir("/proc");
- if (dir == NULL) {
- perror("opendir");
- return -1;
- }
-
- while ((entry = readdir(dir)) != NULL) {
- // 忽略非数字目录(即非进程目录)
- if (!isdigit(entry->d_name[0])) {
- continue;
- }
-
- // 构造cmdline文件的完整路径
- snprintf(path, sizeof(path), "/proc/%s/cmdline", entry->d_name);
-
- // 打开cmdline文件
- file = fopen(path, "r");
- if (file == NULL) {
- continue; // 忽略无法打开的文件
- }
-
- // 读取cmdline文件的内容
- if (fgets(buffer, sizeof(buffer), file) != NULL) {
- // 去除cmdline中的空字符(进程名与参数间用空字符分隔)
- size_t len = strlen(buffer);
- while (len > 0 && buffer[len - 1] == '\0') {
- buffer[--len] = '\0';
- }
-
- // 比较进程名(这里假设process_name是完整的命令行,但你可以修改逻辑来仅比较进程名)
- if (strstr(buffer, process_name) != NULL) {
- fclose(file);
- closedir(dir);
- return 1; // 进程已启动
- }
- }
-
- fclose(file);
- }
-
- closedir(dir);
- return 0; // 进程未启动
- }
- bool CheckLoginStatus(char* Token)
- {
- //return true;//先不处理401授权错误,待前端处理后再加 20240625 lyz
-
- // 没有用户登录
- int nLoginNum=0;
- _User_Log *_UserInfoTemp = NULL;
- list_for_each_entry(_UserInfoTemp, &_GlobalUserLoginInfo.list, list)
- {
- if (_UserInfoTemp == NULL)
- break;
- nLoginNum++;
- }
- if (nLoginNum==0)
- {
- return false;
- }
- // 根据Token查询不到登录的用户
- if (Token != NULL && strcmp(Token, "") != 0 && getUserNamebyToken(Token) == NULL)
- {
- return false;
- }
- return true;
- // return __globalDeviceManage._globalDevInfo.login_status;
- }
- // 辅助函数:判断是否是闰年
- int is_leap_year(int year) {
- return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
- }
-
- // 辅助函数:获取某月的天数
- int days_in_month(int year, int month) {
- int days_per_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- if (month == 2 && is_leap_year(year)) return 29;
- return days_per_month[month];
- }
-
- // 函数:增加一天到日期
- void add_day_to_date(char *date_str, int *year, int *month, int *day) {
- (*day)++;
- if (*day > days_in_month(*year, *month)) {
- (*day) = 1;
- (*month)++;
- if (*month > 12) {
- (*month) = 1;
- (*year)++;
- }
- }
- sprintf(date_str, "%04d-%02d-%02d", *year, *month, *day);
- }
- void SetMenuAuthority(int RoleID)
- {
- //超级管理员
- if (RoleID == 1)
- {
- _GlobalMenuAuthority.PowerEnabled = true;
- _GlobalMenuAuthority.SensorEnabled = true;
- _GlobalMenuAuthority.AlarmEnabled = true;
- _GlobalMenuAuthority.LogEnabled = true;
- _GlobalMenuAuthority.UserInfoEnabled = true;
- _GlobalMenuAuthority.CommEnabled = true;
- _GlobalMenuAuthority.ServiceEnabled = true;
- _GlobalMenuAuthority.MaintenanceEnabled = true;
- // 1、电源监控菜单
- _GlobalMenuAuthority.MonitorallEnabled = true;
- _GlobalMenuAuthority.ChannelstatusEnabled = true;
- _GlobalMenuAuthority.SwitchtatusEnabled = true;
- _GlobalMenuAuthority.MonitorhistoryEnabled = true;
- // 2、传感器管理菜单
- _GlobalMenuAuthority.SensorStatusEnabled = true;
- _GlobalMenuAuthority.SensorHistorylEnabled = true;
- // 3、告警管理菜单
- _GlobalMenuAuthority.XtjgEnabled = true;
- _GlobalMenuAuthority.YzszEnabled = true;
- // 4、日志信息菜单
- _GlobalMenuAuthority.CzrzEnabled = true;
- _GlobalMenuAuthority.YxrzEnabled = true;
- // 5、用户管理菜单
- _GlobalMenuAuthority.UserEnabled = true;
- _GlobalMenuAuthority.RoleEnabled = false;
- _GlobalMenuAuthority.MenuEnabled = false;
- // 6、通信管理菜单
- _GlobalMenuAuthority.WkszEnabled = true;
- _GlobalMenuAuthority.RsszEnabled = true;
- // 7、服务管理菜单
- _GlobalMenuAuthority.SmtpszEnabled = true;
- _GlobalMenuAuthority.SnmpszEnabled = true;
- _GlobalMenuAuthority.MqszEnabled = true;
- _GlobalMenuAuthority.NtpszEnabled = true;
- #ifndef NOT_USE_CONTROL
- _GlobalMenuAuthority.DsszEnabled = true;
- _GlobalMenuAuthority.SxszEnabled = true;
- _GlobalMenuAuthority.FzszEnabled = true;
- #else
- _GlobalMenuAuthority.DsszEnabled = false;
- _GlobalMenuAuthority.SxszEnabled = false;
- _GlobalMenuAuthority.FzszEnabled = false;
- #endif
- // 8、设备维护菜单
- _GlobalMenuAuthority.SbcqEnabled = true;
- #ifndef NOT_USE_CONTROL
- _GlobalMenuAuthority.GjsjEnabled = true;
- #else
- _GlobalMenuAuthority.GjsjEnabled = false;
- #endif
- _GlobalMenuAuthority.KzbsjEnabled = true;
- _GlobalMenuAuthority.HhccszEnabled = true;
- _GlobalMenuAuthority.PzdrdcEnabled = true;
- _GlobalMenuAuthority.ShfwEnabled = true;
- _GlobalMenuAuthority.GysbEnabled = true;
- }
- // 管理员
- else if (RoleID == 2)
- {
- _GlobalMenuAuthority.PowerEnabled = true;
- _GlobalMenuAuthority.SensorEnabled = true;
- _GlobalMenuAuthority.AlarmEnabled = true;
- _GlobalMenuAuthority.LogEnabled = true;
- _GlobalMenuAuthority.UserInfoEnabled = true;
- _GlobalMenuAuthority.CommEnabled = true;
- _GlobalMenuAuthority.ServiceEnabled = true;
- _GlobalMenuAuthority.MaintenanceEnabled = true;
- // 1、电源监控菜单
- _GlobalMenuAuthority.MonitorallEnabled = true;
- _GlobalMenuAuthority.ChannelstatusEnabled = true;
- _GlobalMenuAuthority.SwitchtatusEnabled = false;
- _GlobalMenuAuthority.MonitorhistoryEnabled = true;
- // 2、传感器管理菜单
- _GlobalMenuAuthority.SensorStatusEnabled = true;
- _GlobalMenuAuthority.SensorHistorylEnabled = true;
- // 3、告警管理菜单
- _GlobalMenuAuthority.XtjgEnabled = true;
- _GlobalMenuAuthority.YzszEnabled = true;
- // 4、日志信息菜单
- _GlobalMenuAuthority.CzrzEnabled = false;
- _GlobalMenuAuthority.YxrzEnabled = true;
- // 5、用户管理菜单
- _GlobalMenuAuthority.UserEnabled = true;
- _GlobalMenuAuthority.RoleEnabled = false;
- _GlobalMenuAuthority.MenuEnabled = false;
- // 6、通信管理菜单
- _GlobalMenuAuthority.WkszEnabled = false;
- _GlobalMenuAuthority.RsszEnabled = true;
- // 7、服务管理菜单
- _GlobalMenuAuthority.SmtpszEnabled = false;
- _GlobalMenuAuthority.SnmpszEnabled = false;
- _GlobalMenuAuthority.MqszEnabled = false;
- _GlobalMenuAuthority.NtpszEnabled = true;
- #ifndef NOT_USE_CONTROL
- _GlobalMenuAuthority.DsszEnabled = true;
- _GlobalMenuAuthority.SxszEnabled = true;
- _GlobalMenuAuthority.FzszEnabled = true;
- #else
- _GlobalMenuAuthority.DsszEnabled = false;
- _GlobalMenuAuthority.SxszEnabled = false;
- _GlobalMenuAuthority.FzszEnabled = false;
- #endif
- // 8、设备维护菜单
- _GlobalMenuAuthority.SbcqEnabled = true;
- #ifndef NOT_USE_CONTROL
- _GlobalMenuAuthority.GjsjEnabled = true;
- #else
- _GlobalMenuAuthority.GjsjEnabled = false;
- #endif
- _GlobalMenuAuthority.KzbsjEnabled = true;
- _GlobalMenuAuthority.HhccszEnabled = false;
- _GlobalMenuAuthority.PzdrdcEnabled = true;
- _GlobalMenuAuthority.ShfwEnabled = false;
- _GlobalMenuAuthority.GysbEnabled = true;
- }
- //普通用户
- else
- {
- _GlobalMenuAuthority.PowerEnabled = true;
- _GlobalMenuAuthority.SensorEnabled = true;
- _GlobalMenuAuthority.AlarmEnabled = false;
- _GlobalMenuAuthority.LogEnabled = false;
- _GlobalMenuAuthority.UserInfoEnabled = false;
- _GlobalMenuAuthority.CommEnabled = false;
- _GlobalMenuAuthority.ServiceEnabled = false;
- _GlobalMenuAuthority.MaintenanceEnabled = false;
- // 1、电源监控菜单
- _GlobalMenuAuthority.MonitorallEnabled = true;
- _GlobalMenuAuthority.ChannelstatusEnabled = true;
- _GlobalMenuAuthority.SwitchtatusEnabled = false;
- _GlobalMenuAuthority.MonitorhistoryEnabled = false;
- // 2、传感器管理菜单
- _GlobalMenuAuthority.SensorStatusEnabled = true;
- _GlobalMenuAuthority.SensorHistorylEnabled = false;
- // 3、告警管理菜单
- _GlobalMenuAuthority.XtjgEnabled = false;
- _GlobalMenuAuthority.YzszEnabled = false;
- // 4、日志信息菜单
- _GlobalMenuAuthority.CzrzEnabled = false;
- _GlobalMenuAuthority.YxrzEnabled = false;
- // 5、用户管理菜单
- _GlobalMenuAuthority.UserEnabled = false;
- _GlobalMenuAuthority.RoleEnabled = false;
- _GlobalMenuAuthority.MenuEnabled = false;
- // 6、通信管理菜单
- _GlobalMenuAuthority.WkszEnabled = false;
- _GlobalMenuAuthority.RsszEnabled = false;
- // 7、服务管理菜单
- _GlobalMenuAuthority.SmtpszEnabled = false;
- _GlobalMenuAuthority.SnmpszEnabled = false;
- _GlobalMenuAuthority.MqszEnabled = false;
- _GlobalMenuAuthority.NtpszEnabled = false;
- _GlobalMenuAuthority.DsszEnabled = false;
- _GlobalMenuAuthority.SxszEnabled = false;
- _GlobalMenuAuthority.FzszEnabled = false;
- // 8、设备维护菜单
- _GlobalMenuAuthority.SbcqEnabled = false;
- _GlobalMenuAuthority.GjsjEnabled = false;
- _GlobalMenuAuthority.KzbsjEnabled = false;
- _GlobalMenuAuthority.HhccszEnabled = false;
- _GlobalMenuAuthority.PzdrdcEnabled = false;
- _GlobalMenuAuthority.ShfwEnabled = false;
- _GlobalMenuAuthority.GysbEnabled = false;
- }
-
- }
|