#include "common.h" #include "elog.h" #include #include #include #include #include #include #include #include #undef LOG_TAG #define LOG_TAG "main" GlobalDeviceManager __globalDeviceManage; GlobalDeviceManager __globalDeviceManage2; unsigned long __globalSxIdx; NTPClient* globalNTPclient; _User_Log _GlobalUserLoginInfo; UserInfo _GlobalUserInfoNew; char _GlobalUserToken[64]; MenuAuthority _GlobalMenuAuthority; 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_type:%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("product_modbus_type:%d ", _globalDeviceInfo->_gmodbus_info.product_modbus_type); log_d("product_modbus_baud:%d ", _globalDeviceInfo->_gmodbus_info.product_modbus_baud); log_d("product_modbus_addr:%d ", _globalDeviceInfo->_gmodbus_info.product_modbus_addr); log_d("product_relay_port:%s ", _globalDeviceInfo->_modbus_info.product_relay_ctrl_port); 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 strdup(mac_str); } // 设置接口名称 strncpy(ifr.ifr_name, iface_name, IFNAMSIZ - 1); // 获取MAC地址 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) { perror("ioctl error"); close(fd); return strdup(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); // 成功返回0 return strdup(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; } int set_local_timeandzone(struct tm* t, int zone) { char buf[128]; struct timeval tv; switch(zone) { case -11: //Midway 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: //Adak sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Adak /etc/localtime"); break; case -8: //Los Angeles sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Boise /etc/localtime"); break; case -7: //MST sprintf(buf, "ln -sf /usr/share/zoneinfo/MST /etc/localtime"); break; case -6: //Chicago sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Chicago /etc/localtime"); break; case -5: //Detroit sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Detroit /etc/localtime"); break; case -4: //Halifax sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Halifax /etc/localtime"); break; case -3: //Maceio sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Maceio /etc/localtime"); break; case -2: //Noronha sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Noronha /etc/localtime"); break; case -1: //Scoresbysund sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Scoresbysund /etc/localtime"); break; case 0: //Mexico_City sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Mexico_City /etc/localtime"); break; case 1: //Belgrade sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/Belgrade /etc/localtime"); break; case 2: break; case 3: break; case 4: break; case 5: break; case 6: break; case 7: break; case 8: sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Chongqing /etc/localtime"); break; case 9: break; case 10: break; case 11: break; case 12: break; } system("ln -sf /usr/share/zoneinfo/UTC /etc/localtime"); //t += zone * 3600; //struct tm* ptm = localtime(&t); memset(buf, 0, sizeof(buf)); //sprintf(buf, "echo \"Hello\""); sprintf(buf, "date -s \"%04d-%02d-%02d %02d:%02d:%02d\" && hwclock -w -f /dev/rtc0 ", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); log_i("%s",buf); system(buf); return 0; } 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; } 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; _GlobalMenuAuthority.DsszEnabled = true; _GlobalMenuAuthority.SxszEnabled = true; _GlobalMenuAuthority.FzszEnabled = true; // 8、设备维护菜单 _GlobalMenuAuthority.SbcqEnabled = true; _GlobalMenuAuthority.GjsjEnabled = 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; _GlobalMenuAuthority.DsszEnabled = true; _GlobalMenuAuthority.SxszEnabled = true; _GlobalMenuAuthority.FzszEnabled = true; // 8、设备维护菜单 _GlobalMenuAuthority.SbcqEnabled = true; _GlobalMenuAuthority.GjsjEnabled = 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.HhccszEnabled = false; _GlobalMenuAuthority.PzdrdcEnabled = false; _GlobalMenuAuthority.ShfwEnabled = false; _GlobalMenuAuthority.GysbEnabled = false; } }