common.c 29 KB

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