common.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. #include "common.h"
  2. #include "elog.h"
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/sysinfo.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <net/if.h>
  9. #include <arpa/inet.h>
  10. #include <sys/vfs.h>
  11. #include <sys/ioctl.h>
  12. #include <linux/rtc.h>
  13. #undef LOG_TAG
  14. #define LOG_TAG "main"
  15. GlobalDeviceManager __globalDeviceManage={0};
  16. GlobalDeviceManager __globalDeviceManage2={0};
  17. int ntp_sync_flag=0;
  18. unsigned long __globalSxIdx;
  19. _User_Log _GlobalUserLoginInfo;
  20. UserInfo _GlobalUserInfoNew;
  21. char _GlobalUserToken[64];
  22. MenuAuthority _GlobalMenuAuthority;
  23. NTPManager _globalNTPManager;
  24. void printf_global_device_info(GlobalDeviceInfo* _globalDeviceInfo)
  25. {
  26. log_d("******global device info********* ");
  27. log_d("product.id:%d ", _globalDeviceInfo->product.id);
  28. log_d("product.number:%s ", _globalDeviceInfo->product.number);
  29. log_d("product.name:%s ", _globalDeviceInfo->product.name);
  30. log_d("product.ype:%d ", _globalDeviceInfo->product.type);
  31. log_d("product.pwr_type:%d ", _globalDeviceInfo->product.pwr_type);
  32. log_d("product.version:%s ", _globalDeviceInfo->product.version);
  33. log_d("product.count:%d ", _globalDeviceInfo->product.count);
  34. log_d("product.samp_time:%d ", _globalDeviceInfo->product.samp_time);
  35. log_d("product.save_time:%d ", _globalDeviceInfo->product.save_time);
  36. log_d("cascade.mode:%d ", _globalDeviceInfo->cascade.mode);
  37. log_d("cascade.addr:%d ", _globalDeviceInfo->cascade.addr);
  38. log_d("cascade.baudrate:%d ", _globalDeviceInfo->cascade.baudrate);
  39. log_d("relay.path:%s ", _globalDeviceInfo->relay.path);
  40. log_d("******************************** ");
  41. }
  42. void printf_global_power_manage_info(GlobalPowerManger* _globalPowerManger)
  43. {
  44. printf("******global power manage info********* ");
  45. printf("product_id:%d ", _globalPowerManger->product_id);
  46. printf("product_ch_id:%d ", _globalPowerManger->product_ch_id);
  47. printf("product_ch_name:%s ", _globalPowerManger->product_ch_name);
  48. printf("product_ch_type:%d ", _globalPowerManger->product_ch_type);
  49. printf("product_ch_status:%d ", _globalPowerManger->product_ch_status);
  50. printf("product_ch_NF_status:%d ", _globalPowerManger->product_ch_NF_status);
  51. printf("product_ch_start_delay:%d ", _globalPowerManger->product_ch_start_delay);
  52. printf("product_ch_stop_delay:%d ", _globalPowerManger->product_ch_stop_delay);
  53. printf("product_vol_upper_enable:%d ", _globalPowerManger->global_over_manager->product_vol_upper_enable);
  54. printf("product_vol_upper_threshold:%0.2f ", _globalPowerManger->global_over_manager->product_vol_upper_threshold);
  55. printf("product_vol_over_upper_threshold_ctrl:%d ", _globalPowerManger->global_over_manager->product_vol_over_upper_threshold_ctrl);
  56. printf("product_vol_over_upper_threshold_para:%d ", _globalPowerManger->global_over_manager->product_vol_over_upper_threshold_ctrl_para);
  57. printf("product_vol_lower_enable:%d ", _globalPowerManger->global_over_manager->product_vol_lower_enable);
  58. printf("product_vol_lower_threshold:%0.2f ", _globalPowerManger->global_over_manager->product_vol_lower_threshold);
  59. printf("product_vol_over_lower_threshold_ctrl:%d ", _globalPowerManger->global_over_manager->product_vol_over_lower_threshold_ctrl);
  60. printf("product_vol_over_lower_threshold_ctrl_para:%d ", _globalPowerManger->global_over_manager->product_vol_over_lower_threshold_ctrl_para);
  61. printf("product_cur_upper_enable:%d ", _globalPowerManger->global_over_manager->product_cur_upper_enable);
  62. printf("product_cur_upper_threshold:%0.2f ", _globalPowerManger->global_over_manager->product_cur_upper_threshold);
  63. printf("product_cur_over_upper_threshold_ctrl:%d ", _globalPowerManger->global_over_manager->product_cur_over_upper_threshold_ctrl);
  64. printf("product_cur_over_upper_threshold_ctrl_para:%d ", _globalPowerManger->global_over_manager->product_cur_over_upper_threshold_ctrl_para);
  65. printf("product_pwr_upper_enable:%d ", _globalPowerManger->global_over_manager->product_pwr_upper_enable);
  66. printf("product_pwr_upper_threshold:%0.2f ", _globalPowerManger->global_over_manager->product_pwr_upper_threshold);
  67. printf("product_pwr_over_upper_threshold_ctrl:%d ", _globalPowerManger->global_over_manager->product_pwr_over_upper_threshold_ctrl);
  68. printf("product_pwr_over_upper_threshold_ctrl_para:%d ", _globalPowerManger->global_over_manager->product_pwr_over_upper_threshold_ctrl_para);
  69. printf("product_pwrcon_upper_enable:%d ", _globalPowerManger->global_over_manager->product_pwrcon_upper_enable);
  70. printf("product_pwrcon_upper_threshold:%0.2f ", _globalPowerManger->global_over_manager->product_pwrcon_upper_threshold);
  71. printf("product_pwrcon_over_upper_threshold_ctrl:%d ", _globalPowerManger->global_over_manager->product_pwrcon_over_upper_threshold_ctrl);
  72. printf("product_pwrcon_over_upper_threshold_ctrl_para:%d ", _globalPowerManger->global_over_manager->product_pwrcon_over_upper_threshold_ctrl_para);
  73. printf("******************************** ");
  74. }
  75. //打印电源信息
  76. void printf_power_info(GlobalPowerInfo* _globalPowerInfo)
  77. {
  78. struct list_head *pos;
  79. GlobalPowerInfo* _temp;
  80. int i = 0;
  81. list_for_each_entry(_temp, &_globalPowerInfo->list, list)
  82. {
  83. printf("i:%d ", i++);
  84. printf("samp_time:%s ", _temp->samp_time);
  85. printf("product_id:%d ", _temp->product_id);
  86. }
  87. }
  88. void INIT_LIST_HEAD(struct list_head *list)
  89. {
  90. list->next = list->prev = list;
  91. }
  92. /**
  93. * EasyLogger demo
  94. */
  95. // void test_elog(void) {
  96. // uint8_t buf[256]= {0};
  97. // int i = 0;
  98. // for (i = 0; i < sizeof(buf); i++)
  99. // {
  100. // buf[i] = i;
  101. // }
  102. // while(true) {
  103. // /* test log output for all level */
  104. // log_a("Hello EasyLogger!");
  105. // log_e("Hello EasyLogger!");
  106. // log_w("Hello EasyLogger!");
  107. // log_i("Hello EasyLogger!");
  108. // log_d("Hello EasyLogger!");
  109. // log_v("Hello EasyLogger!");
  110. // // elog_raw("Hello EasyLogger!");
  111. // elog_hexdump("test", 16, buf, sizeof(buf));
  112. // sleep(5);
  113. // }
  114. // }
  115. #if(1)
  116. void initLogger(void)
  117. {
  118. setbuf(stdout, NULL);
  119. elog_init();
  120. elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL);
  121. elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
  122. elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
  123. elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
  124. elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
  125. elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
  126. elog_set_text_color_enabled(true);
  127. elog_start();
  128. log_i("Smart Pdu Application Running!");
  129. }
  130. #endif
  131. /// @brief 获取系统mem大小
  132. /// @param totalSize
  133. /// @return
  134. int getTotalMemorySize(unsigned int* totalSize)
  135. {
  136. struct sysinfo memInfo;
  137. if (sysinfo(&memInfo) != -1) {
  138. *totalSize = memInfo.totalram;
  139. // printf("Total RAM size: %lu bytes\n", memInfo.totalram);
  140. // printf("Free RAM size: %lu bytes\n", memInfo.freeram);
  141. }
  142. else
  143. {
  144. *totalSize = 0 ;
  145. return -1;
  146. }
  147. return 0;
  148. }
  149. /// @brief 返回设备IP地址
  150. /// @param dev 网口名
  151. /// @return
  152. char* getLocalIpAddress(char* dev)
  153. {
  154. int fd;
  155. struct ifreq ifr;
  156. static char ip_addr[64];
  157. fd = socket(AF_INET, SOCK_DGRAM, 0);
  158. ifr.ifr_addr.sa_family = AF_INET;
  159. strncpy(ifr.ifr_name, dev, IFNAMSIZ-1);
  160. ioctl(fd, SIOCGIFADDR, &ifr);
  161. close(fd);
  162. char *p=inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
  163. if(!p) {
  164. return NULL;
  165. }
  166. strcpy(ip_addr, p);
  167. return ip_addr;
  168. }
  169. // 获取MAC地址的函数
  170. char* get_mac_address(const char *iface_name)
  171. {
  172. int fd;
  173. struct ifreq ifr;
  174. unsigned char *mac;
  175. static char mac_str[32];
  176. snprintf(mac_str, sizeof(mac_str), "%02d:%02d:%02d:%02d:%02d:%02d",
  177. 0, 0, 0, 0, 0, 0);
  178. // 创建一个socket
  179. if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  180. perror("socket error");
  181. return mac_str;
  182. }
  183. // 设置接口名称
  184. strncpy(ifr.ifr_name, iface_name, IFNAMSIZ - 1);
  185. // 获取MAC地址
  186. if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) {
  187. perror("ioctl error");
  188. close(fd);
  189. return mac_str;
  190. }
  191. // ifr.ifr_hwaddr.sa_data是一个包含硬件地址的字节数组
  192. mac = (unsigned char *)ifr.ifr_hwaddr.sa_data;
  193. // 将MAC地址转换为字符串格式
  194. snprintf(mac_str, sizeof(mac_str), "%02X:%02X:%02X:%02X:%02X:%02X",
  195. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  196. // 关闭socket
  197. close(fd);
  198. return mac_str;
  199. }
  200. /// @brief 获取磁盘使用率
  201. /// @param reused
  202. void get_disk_occupy(char ** reused)
  203. {
  204. const unsigned short MAXBUFSIZE = 1024;
  205. char currentDirectoryPath[ MAXBUFSIZE ];
  206. getcwd(currentDirectoryPath, MAXBUFSIZE);
  207. //printf("当前目录:%s\n",currentDirectoryPath);
  208. char cmd[50] = "df ";
  209. strcat(cmd, currentDirectoryPath);
  210. //printf("%s\n",cmd);
  211. char buffer[MAXBUFSIZE];
  212. FILE* pipe = popen(cmd, "r");
  213. char fileSys[20];
  214. char blocks[20];
  215. char used[20];
  216. char free[20];
  217. char percent[10];
  218. char moment[20];
  219. if (!pipe)
  220. {
  221. return;
  222. }
  223. if (fgets(buffer, sizeof(buffer), pipe) != NULL)
  224. {
  225. sscanf(buffer, "%s %s %s %s %s %s", fileSys, blocks, used, free, percent, moment);
  226. }
  227. if (fgets(buffer, sizeof(buffer), pipe) != NULL)
  228. {
  229. sscanf(buffer, "%s %s %s %s %s %s", fileSys, blocks, used, free, percent, moment);
  230. }
  231. //printf("desk used:%s\n",percent);
  232. strcpy(*reused, percent);
  233. return;
  234. }
  235. static int set_rtc(time_t t)
  236. {
  237. int r=0,fd;
  238. struct rtc_time *rtc;
  239. struct tm *tm = gmtime(&t);
  240. fd = open("/dev/rtc0", O_RDONLY);
  241. if (fd < 0) {
  242. perror("open rtc failed\n");
  243. return -1;
  244. }
  245. rtc = (struct rtc_time*)tm;
  246. if (ioctl(fd, RTC_SET_TIME, rtc) < 0) {
  247. perror("ioctl RTC_SET_TIME failed\n");
  248. r = -1;
  249. }
  250. else {
  251. printf("set rtc ok!\n");
  252. }
  253. close(fd);
  254. return r;
  255. }
  256. #define MY_MINUTE 60
  257. #define MY_HOUR (60*MY_MINUTE)
  258. #define MY_DAY (24*MY_HOUR)
  259. #define MY_YEAR (365*MY_DAY)
  260. static int my_month[12] = {
  261. 0,
  262. MY_DAY*(31),
  263. MY_DAY*(31+29),
  264. MY_DAY*(31+29+31),
  265. MY_DAY*(31+29+31+30),
  266. MY_DAY*(31+29+31+30+31),
  267. MY_DAY*(31+29+31+30+31+30),
  268. MY_DAY*(31+29+31+30+31+30+31),
  269. MY_DAY*(31+29+31+30+31+30+31+31),
  270. MY_DAY*(31+29+31+30+31+30+31+31+30),
  271. MY_DAY*(31+29+31+30+31+30+31+31+30+31),
  272. MY_DAY*(31+29+31+30+31+30+31+31+30+31+30)
  273. };
  274. time_t mktime2(struct tm *t, int zone)
  275. {
  276. time_t res,year;
  277. year = t->tm_year - 70;
  278. /* magic offsets (y+1) needed to get leapyears right.*/
  279. res = MY_YEAR*year + MY_DAY*((year+1)/4);
  280. res += my_month[t->tm_mon];
  281. /* and (y+2) here. If it wasn't a leap-year, we have to adjust */
  282. if (t->tm_mon>1 && ((year+2)%4))
  283. res -= MY_DAY;
  284. res += MY_DAY*(t->tm_mday-1);
  285. res += MY_HOUR*t->tm_hour;
  286. res += MY_MINUTE*t->tm_min;
  287. res += t->tm_sec;
  288. res -= zone*3600;
  289. return res;
  290. }
  291. int set_time(struct tm *t, int zone)
  292. {
  293. if(!t) {
  294. return -1;
  295. }
  296. t->tm_year -= 1900;
  297. t->tm_mon -= 1;
  298. time_t utc = mktime2(t, zone);
  299. return stime(&utc);
  300. }
  301. int set_utctime(time_t t)
  302. {
  303. return stime(&t);
  304. }
  305. int set_zone(int zone)
  306. {
  307. char buf[256];
  308. #if 0
  309. switch(zone) {
  310. case -11:
  311. sprintf(buf, "ln -sf /usr/share/zoneinfo/Pacific/Midway /etc/localtime");
  312. break;
  313. case -10:
  314. sprintf(buf, "ln -sf /usr/share/zoneinfo/HST /etc/localtime");
  315. break;
  316. case -9:
  317. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Adak /etc/localtime");
  318. break;
  319. case -8:
  320. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Boise /etc/localtime");
  321. break;
  322. case -7:
  323. sprintf(buf, "ln -sf /usr/share/zoneinfo/MST /etc/localtime");
  324. break;
  325. case -6:
  326. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Chicago /etc/localtime");
  327. break;
  328. case -5:
  329. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Detroit /etc/localtime");
  330. break;
  331. case -4:
  332. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Halifax /etc/localtime");
  333. break;
  334. case -3:
  335. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Maceio /etc/localtime");
  336. break;
  337. case -2:
  338. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Noronha /etc/localtime");
  339. break;
  340. case -1:
  341. sprintf(buf, "ln -sf /usr/share/zoneinfo/America/Scoresbysund /etc/localtime");
  342. break;
  343. case 0:
  344. sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/London /etc/localtime");
  345. break;
  346. case 1:
  347. sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/Paris /etc/localtime");
  348. break;
  349. case 2:
  350. sprintf(buf, "ln -sf /usr/share/zoneinfo/EET /etc/localtime");
  351. break;
  352. case 3:
  353. sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/Moscow /etc/localtime");
  354. break;
  355. case 4:
  356. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Amman /etc/localtime");
  357. break;
  358. case 5:
  359. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Karachi /etc/localtime");
  360. break;
  361. case 6:
  362. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Dhaka /etc/localtime");
  363. break;
  364. case 7:
  365. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Bangkok /etc/localtime");
  366. break;
  367. case 8:
  368. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Chongqing /etc/localtime");
  369. break;
  370. case 9:
  371. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime");
  372. break;
  373. case 10:
  374. sprintf(buf, "ln -sf /usr/share/zoneinfo/Australia/Sydney /etc/localtime");
  375. break;
  376. case 11:
  377. sprintf(buf, "ln -sf /usr/share/zoneinfo/Asia/Sakhalin /etc/localtime");
  378. break;
  379. case 12:
  380. sprintf(buf, "ln -sf /usr/share/zoneinfo/Pacific/Fiji /etc/localtime");
  381. break;
  382. default:
  383. return -1;
  384. }
  385. #else
  386. sprintf(buf, "ln -sf /usr/share/zoneinfo/Etc/GMT%s%d /etc/localtime", (zone<0)?"+":"-", abs(zone));
  387. #endif
  388. log_i("set_zone: %s", buf);
  389. return system(buf);
  390. }
  391. void print_time(char *s, struct tm *tm)
  392. {
  393. printf("___%s___, %04d-%02d-%02d %02d:%02d:%02d\n", s, tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  394. }
  395. void print_utc(char *s, time_t t)
  396. {
  397. struct tm *tm=gmtime(&t);
  398. print_time(s, tm);
  399. }
  400. int set_time_zone(struct tm *t, int zone)
  401. {
  402. int r;
  403. if(!t) {
  404. return -1;
  405. }
  406. r = set_zone(zone);
  407. if(r) {
  408. log_e("__1__ set_zone failed\n");
  409. }
  410. print_time("111", t);
  411. r = set_time(t, zone);
  412. if(r) {
  413. log_e("__1__ set_time failed\n");
  414. }
  415. print_utc("222", time(NULL));
  416. return r;
  417. }
  418. int set_utctime_zone(time_t t, int zone)
  419. {
  420. int r;
  421. r = set_zone(zone);
  422. if(r) {
  423. log_e("__2__ set_zone failed\n");
  424. }
  425. r = set_utctime(t);
  426. if(r) {
  427. log_e("__2__ set_time failed\n");
  428. }
  429. return r;
  430. }
  431. int get_time_str(char *s, int len, time_t t)
  432. {
  433. struct tm *tm=localtime(&t);
  434. return snprintf(s, len, "%04d-%02d-%02d %02d:%02d:%02d.000",tm->tm_year,tm->tm_mon,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
  435. }
  436. char* getUserNamebyToken(char* Token)
  437. {
  438. char* userName = NULL;
  439. if (Token==NULL)
  440. {
  441. return NULL;
  442. }
  443. _User_Log *_UserInfoTemp = NULL;
  444. list_for_each_entry(_UserInfoTemp, &_GlobalUserLoginInfo.list, list)
  445. {
  446. if (_UserInfoTemp == NULL)
  447. break;
  448. if (strcmp(_UserInfoTemp->user_token, Token) == 0)
  449. {
  450. userName=_UserInfoTemp->user_name;
  451. }
  452. }
  453. return userName;
  454. }
  455. int is_process_running(const char *process_name)
  456. {
  457. DIR *dir;
  458. struct dirent *entry;
  459. char path[1035]; // /proc/[pid]/cmdline 的最大路径长度(/proc/ + PID(最多5位) + /cmdline + \0)
  460. char buffer[4096]; // 假设cmdline不会超过这个长度
  461. FILE *file;
  462. dir = opendir("/proc");
  463. if (dir == NULL) {
  464. perror("opendir");
  465. return -1;
  466. }
  467. while ((entry = readdir(dir)) != NULL) {
  468. // 忽略非数字目录(即非进程目录)
  469. if (!isdigit(entry->d_name[0])) {
  470. continue;
  471. }
  472. // 构造cmdline文件的完整路径
  473. snprintf(path, sizeof(path), "/proc/%s/cmdline", entry->d_name);
  474. // 打开cmdline文件
  475. file = fopen(path, "r");
  476. if (file == NULL) {
  477. continue; // 忽略无法打开的文件
  478. }
  479. // 读取cmdline文件的内容
  480. if (fgets(buffer, sizeof(buffer), file) != NULL) {
  481. // 去除cmdline中的空字符(进程名与参数间用空字符分隔)
  482. size_t len = strlen(buffer);
  483. while (len > 0 && buffer[len - 1] == '\0') {
  484. buffer[--len] = '\0';
  485. }
  486. // 比较进程名(这里假设process_name是完整的命令行,但你可以修改逻辑来仅比较进程名)
  487. if (strstr(buffer, process_name) != NULL) {
  488. fclose(file);
  489. closedir(dir);
  490. return 1; // 进程已启动
  491. }
  492. }
  493. fclose(file);
  494. }
  495. closedir(dir);
  496. return 0; // 进程未启动
  497. }
  498. bool CheckLoginStatus(char* Token)
  499. {
  500. //return true;//先不处理401授权错误,待前端处理后再加 20240625 lyz
  501. // 没有用户登录
  502. int nLoginNum=0;
  503. _User_Log *_UserInfoTemp = NULL;
  504. list_for_each_entry(_UserInfoTemp, &_GlobalUserLoginInfo.list, list)
  505. {
  506. if (_UserInfoTemp == NULL)
  507. break;
  508. nLoginNum++;
  509. }
  510. if (nLoginNum==0)
  511. {
  512. return false;
  513. }
  514. // 根据Token查询不到登录的用户
  515. if (Token != NULL && strcmp(Token, "") != 0 && getUserNamebyToken(Token) == NULL)
  516. {
  517. return false;
  518. }
  519. return true;
  520. // return __globalDeviceManage._globalDevInfo.login_status;
  521. }
  522. // 辅助函数:判断是否是闰年
  523. int is_leap_year(int year) {
  524. return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
  525. }
  526. // 辅助函数:获取某月的天数
  527. int days_in_month(int year, int month) {
  528. int days_per_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  529. if (month == 2 && is_leap_year(year)) return 29;
  530. return days_per_month[month];
  531. }
  532. // 函数:增加一天到日期
  533. void add_day_to_date(char *date_str, int *year, int *month, int *day) {
  534. (*day)++;
  535. if (*day > days_in_month(*year, *month)) {
  536. (*day) = 1;
  537. (*month)++;
  538. if (*month > 12) {
  539. (*month) = 1;
  540. (*year)++;
  541. }
  542. }
  543. sprintf(date_str, "%04d-%02d-%02d", *year, *month, *day);
  544. }
  545. void SetMenuAuthority(int RoleID)
  546. {
  547. //超级管理员
  548. if (RoleID == 1)
  549. {
  550. _GlobalMenuAuthority.PowerEnabled = true;
  551. _GlobalMenuAuthority.SensorEnabled = true;
  552. _GlobalMenuAuthority.AlarmEnabled = true;
  553. _GlobalMenuAuthority.LogEnabled = true;
  554. _GlobalMenuAuthority.UserInfoEnabled = true;
  555. _GlobalMenuAuthority.CommEnabled = true;
  556. _GlobalMenuAuthority.ServiceEnabled = true;
  557. _GlobalMenuAuthority.MaintenanceEnabled = true;
  558. // 1、电源监控菜单
  559. _GlobalMenuAuthority.MonitorallEnabled = true;
  560. _GlobalMenuAuthority.ChannelstatusEnabled = true;
  561. _GlobalMenuAuthority.SwitchtatusEnabled = true;
  562. _GlobalMenuAuthority.MonitorhistoryEnabled = true;
  563. // 2、传感器管理菜单
  564. _GlobalMenuAuthority.SensorStatusEnabled = true;
  565. _GlobalMenuAuthority.SensorHistorylEnabled = true;
  566. // 3、告警管理菜单
  567. _GlobalMenuAuthority.XtjgEnabled = true;
  568. _GlobalMenuAuthority.YzszEnabled = true;
  569. // 4、日志信息菜单
  570. _GlobalMenuAuthority.CzrzEnabled = true;
  571. _GlobalMenuAuthority.YxrzEnabled = true;
  572. // 5、用户管理菜单
  573. _GlobalMenuAuthority.UserEnabled = true;
  574. _GlobalMenuAuthority.RoleEnabled = false;
  575. _GlobalMenuAuthority.MenuEnabled = false;
  576. // 6、通信管理菜单
  577. _GlobalMenuAuthority.WkszEnabled = true;
  578. _GlobalMenuAuthority.RsszEnabled = true;
  579. // 7、服务管理菜单
  580. _GlobalMenuAuthority.SmtpszEnabled = true;
  581. _GlobalMenuAuthority.SnmpszEnabled = true;
  582. _GlobalMenuAuthority.MqszEnabled = true;
  583. _GlobalMenuAuthority.NtpszEnabled = true;
  584. _GlobalMenuAuthority.DsszEnabled = true;
  585. _GlobalMenuAuthority.SxszEnabled = true;
  586. _GlobalMenuAuthority.FzszEnabled = true;
  587. // 8、设备维护菜单
  588. _GlobalMenuAuthority.SbcqEnabled = true;
  589. _GlobalMenuAuthority.GjsjEnabled = true;
  590. _GlobalMenuAuthority.HhccszEnabled = true;
  591. _GlobalMenuAuthority.PzdrdcEnabled = true;
  592. _GlobalMenuAuthority.ShfwEnabled = true;
  593. _GlobalMenuAuthority.GysbEnabled = true;
  594. }
  595. // 管理员
  596. else if (RoleID == 2)
  597. {
  598. _GlobalMenuAuthority.PowerEnabled = true;
  599. _GlobalMenuAuthority.SensorEnabled = true;
  600. _GlobalMenuAuthority.AlarmEnabled = true;
  601. _GlobalMenuAuthority.LogEnabled = true;
  602. _GlobalMenuAuthority.UserInfoEnabled = true;
  603. _GlobalMenuAuthority.CommEnabled = true;
  604. _GlobalMenuAuthority.ServiceEnabled = true;
  605. _GlobalMenuAuthority.MaintenanceEnabled = true;
  606. // 1、电源监控菜单
  607. _GlobalMenuAuthority.MonitorallEnabled = true;
  608. _GlobalMenuAuthority.ChannelstatusEnabled = true;
  609. _GlobalMenuAuthority.SwitchtatusEnabled = false;
  610. _GlobalMenuAuthority.MonitorhistoryEnabled = true;
  611. // 2、传感器管理菜单
  612. _GlobalMenuAuthority.SensorStatusEnabled = true;
  613. _GlobalMenuAuthority.SensorHistorylEnabled = true;
  614. // 3、告警管理菜单
  615. _GlobalMenuAuthority.XtjgEnabled = true;
  616. _GlobalMenuAuthority.YzszEnabled = true;
  617. // 4、日志信息菜单
  618. _GlobalMenuAuthority.CzrzEnabled = false;
  619. _GlobalMenuAuthority.YxrzEnabled = true;
  620. // 5、用户管理菜单
  621. _GlobalMenuAuthority.UserEnabled = true;
  622. _GlobalMenuAuthority.RoleEnabled = false;
  623. _GlobalMenuAuthority.MenuEnabled = false;
  624. // 6、通信管理菜单
  625. _GlobalMenuAuthority.WkszEnabled = false;
  626. _GlobalMenuAuthority.RsszEnabled = true;
  627. // 7、服务管理菜单
  628. _GlobalMenuAuthority.SmtpszEnabled = false;
  629. _GlobalMenuAuthority.SnmpszEnabled = false;
  630. _GlobalMenuAuthority.MqszEnabled = false;
  631. _GlobalMenuAuthority.NtpszEnabled = true;
  632. _GlobalMenuAuthority.DsszEnabled = true;
  633. _GlobalMenuAuthority.SxszEnabled = true;
  634. _GlobalMenuAuthority.FzszEnabled = true;
  635. // 8、设备维护菜单
  636. _GlobalMenuAuthority.SbcqEnabled = true;
  637. _GlobalMenuAuthority.GjsjEnabled = true;
  638. _GlobalMenuAuthority.HhccszEnabled = false;
  639. _GlobalMenuAuthority.PzdrdcEnabled = true;
  640. _GlobalMenuAuthority.ShfwEnabled = false;
  641. _GlobalMenuAuthority.GysbEnabled = true;
  642. }
  643. //普通用户
  644. else
  645. {
  646. _GlobalMenuAuthority.PowerEnabled = true;
  647. _GlobalMenuAuthority.SensorEnabled = true;
  648. _GlobalMenuAuthority.AlarmEnabled = false;
  649. _GlobalMenuAuthority.LogEnabled = false;
  650. _GlobalMenuAuthority.UserInfoEnabled = false;
  651. _GlobalMenuAuthority.CommEnabled = false;
  652. _GlobalMenuAuthority.ServiceEnabled = false;
  653. _GlobalMenuAuthority.MaintenanceEnabled = false;
  654. // 1、电源监控菜单
  655. _GlobalMenuAuthority.MonitorallEnabled = true;
  656. _GlobalMenuAuthority.ChannelstatusEnabled = true;
  657. _GlobalMenuAuthority.SwitchtatusEnabled = false;
  658. _GlobalMenuAuthority.MonitorhistoryEnabled = false;
  659. // 2、传感器管理菜单
  660. _GlobalMenuAuthority.SensorStatusEnabled = true;
  661. _GlobalMenuAuthority.SensorHistorylEnabled = false;
  662. // 3、告警管理菜单
  663. _GlobalMenuAuthority.XtjgEnabled = false;
  664. _GlobalMenuAuthority.YzszEnabled = false;
  665. // 4、日志信息菜单
  666. _GlobalMenuAuthority.CzrzEnabled = false;
  667. _GlobalMenuAuthority.YxrzEnabled = false;
  668. // 5、用户管理菜单
  669. _GlobalMenuAuthority.UserEnabled = false;
  670. _GlobalMenuAuthority.RoleEnabled = false;
  671. _GlobalMenuAuthority.MenuEnabled = false;
  672. // 6、通信管理菜单
  673. _GlobalMenuAuthority.WkszEnabled = false;
  674. _GlobalMenuAuthority.RsszEnabled = false;
  675. // 7、服务管理菜单
  676. _GlobalMenuAuthority.SmtpszEnabled = false;
  677. _GlobalMenuAuthority.SnmpszEnabled = false;
  678. _GlobalMenuAuthority.MqszEnabled = false;
  679. _GlobalMenuAuthority.NtpszEnabled = false;
  680. _GlobalMenuAuthority.DsszEnabled = false;
  681. _GlobalMenuAuthority.SxszEnabled = false;
  682. _GlobalMenuAuthority.FzszEnabled = false;
  683. // 8、设备维护菜单
  684. _GlobalMenuAuthority.SbcqEnabled = false;
  685. _GlobalMenuAuthority.GjsjEnabled = false;
  686. _GlobalMenuAuthority.HhccszEnabled = false;
  687. _GlobalMenuAuthority.PzdrdcEnabled = false;
  688. _GlobalMenuAuthority.ShfwEnabled = false;
  689. _GlobalMenuAuthority.GysbEnabled = false;
  690. }
  691. }