common.c 27 KB

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