logPackTools.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include <sys/time.h>
  2. #include <sys/timeb.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/shm.h>
  11. #include <sys/mman.h>
  12. #include <linux/wireless.h>
  13. #include <arpa/inet.h>
  14. #include <netinet/in.h>
  15. #include <unistd.h>
  16. #include <stdarg.h>
  17. #include <stdio.h> /*標準輸入輸出定義*/
  18. #include <stdlib.h> /*標準函數庫定義*/
  19. #include <unistd.h> /*Unix 標準函數定義*/
  20. #include <fcntl.h> /*檔控制定義*/
  21. #include <termios.h> /*PPSIX 終端控制定義*/
  22. #include <errno.h> /*錯誤號定義*/
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include "../Projects/define.h"
  29. #include <math.h>
  30. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  31. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  32. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  33. #define Debug
  34. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  35. #define PASS 1
  36. #define FAIL -1
  37. #define SystemLogMessage
  38. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  39. struct StatusCodeData *ShmStatusCodeData;
  40. void trim(char *s);
  41. int mystrcmp(char *p1, char *p2);
  42. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  43. void split(char **arr, char *str, const char *del);
  44. #ifdef SystemLogMessage
  45. int StoreLogMsg(const char *fmt, ...) {
  46. char Buf[4096 + 256];
  47. char buffer[4096];
  48. time_t CurrentTime;
  49. struct tm *tm;
  50. va_list args;
  51. va_start(args, fmt);
  52. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  53. va_end(args);
  54. memset(Buf, 0, sizeof(Buf));
  55. CurrentTime = time(NULL);
  56. tm = localtime(&CurrentTime);
  57. sprintf(Buf,
  58. "echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  59. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,
  60. tm->tm_min, tm->tm_sec, buffer, tm->tm_year + 1900, tm->tm_mon + 1);
  61. system(Buf);
  62. #ifdef Debug
  63. printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year + 1900,
  64. tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
  65. buffer);
  66. #endif
  67. return rc;
  68. }
  69. #endif
  70. int DiffTimeb(struct timeb ST, struct timeb ET) {
  71. //return milli-second
  72. unsigned int StartTime, StopTime;
  73. StartTime = (unsigned int) ST.time;
  74. StopTime = (unsigned int) ET.time;
  75. return (StopTime - StartTime) * 1000 + ET.millitm - ST.millitm;
  76. }
  77. //=================================
  78. // Common routine
  79. //=================================
  80. void trim(char *s) {
  81. int i = 0, j, k, l = 0;
  82. while ((s[i] == ' ') || (s[i] == '\t') || (s[i] == '\n'))
  83. i++;
  84. j = strlen(s) - 1;
  85. while ((s[j] == ' ') || (s[j] == '\t') || (s[j] == '\n'))
  86. j--;
  87. if (i == 0 && j == strlen(s) - 1) {
  88. } else if (i == 0)
  89. s[j + 1] = '\0';
  90. else {
  91. for (k = i; k <= j; k++)
  92. s[l++] = s[k];
  93. s[l] = '\0';
  94. }
  95. }
  96. int mystrcmp(char *p1, char *p2) {
  97. while (*p1 == *p2) {
  98. if (*p1 == '\0' || *p2 == '\0')
  99. break;
  100. p1++;
  101. p2++;
  102. }
  103. if (*p1 == '\0' && *p2 == '\0')
  104. return (PASS);
  105. else
  106. return (FAIL);
  107. }
  108. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt) {
  109. strncpy(dest, src + start, cnt);
  110. dest[cnt] = 0;
  111. }
  112. void split(char **arr, char *str, const char *del) {
  113. char *s = strtok(str, del);
  114. while (s != NULL) {
  115. *arr++ = s;
  116. s = strtok(NULL, del);
  117. }
  118. }
  119. int ConnectorType(char* connector){
  120. int result;
  121. if(strcmp(connector, "0") == 0){
  122. result= 0;
  123. }
  124. else if(strcmp(connector, "1") == 0 || strcmp(connector, "2") == 0 || strcmp(connector, "3") == 0 || strcmp(connector, "4") == 0 || strcmp(connector, "U") == 0 || strcmp(connector, "E") == 0){
  125. result= 1;//CCS
  126. }
  127. else if(strcmp(connector, "5") == 0 || strcmp(connector, "6") == 0 || strcmp(connector, "G") == 0){
  128. result= 2;//GB
  129. }
  130. else if(strcmp(connector, "J") == 0){
  131. result= 3;//CHAdeMO
  132. }
  133. else{
  134. }
  135. return result;
  136. }
  137. int ModelType(char* type,char* network){
  138. int result=0;
  139. if(strcmp(type, "A") == 0){
  140. if(strcmp(network, "0") == 0 || strcmp(network, "R") == 0 || strcmp(network, "B") == 0){
  141. result=0;
  142. }
  143. else if(strcmp(network, "E") == 0 || strcmp(network, "W") == 0 || strcmp(network, "T") == 0 || strcmp(network, "U") == 0){
  144. result=1;
  145. }
  146. }
  147. else if(strcmp(type, "D") == 0){
  148. result=2;
  149. }
  150. return result;
  151. }
  152. //==========================================
  153. // Init all share memory
  154. //==========================================
  155. int InitShareMemory() {
  156. int result = PASS;
  157. int MeterSMId;
  158. //creat ShmSysConfigAndInfo
  159. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey,
  160. sizeof(struct SysConfigAndInfo), 0777)) < 0) {
  161. #ifdef SystemLogMessage
  162. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  163. #endif
  164. result = FAIL;
  165. } else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0))
  166. == (void *) -1) {
  167. #ifdef SystemLogMessage
  168. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  169. #endif
  170. result = FAIL;
  171. } else {
  172. }
  173. //creat ShmStatusCodeData
  174. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData),
  175. 0777)) < 0) {
  176. #ifdef SystemLogMessage
  177. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  178. #endif
  179. result = FAIL;
  180. } else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1) {
  181. #ifdef SystemLogMessage
  182. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  183. #endif
  184. result = FAIL;
  185. } else {
  186. }
  187. return result;
  188. }
  189. //================================================
  190. // Main process
  191. //================================================
  192. int main(int argc, char *argv[]) {
  193. if (InitShareMemory() == FAIL) {
  194. #ifdef SystemLogMessage
  195. DEBUG_ERROR("InitShareMemory NG\n");
  196. #endif
  197. if (ShmStatusCodeData != NULL) {
  198. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  199. }
  200. sleep(5);
  201. return 0;
  202. }
  203. if(strcmp(argv[1], "log") == 0){
  204. unsigned char SerialNo[64];;
  205. memcpy(SerialNo,ShmSysConfigAndInfo->SysConfig.SerialNumber,ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  206. printf("%s", SerialNo);
  207. int year,month,i,ty,tm,cnt;
  208. char ym[7] ;
  209. char pwd[70];
  210. char cmd[100];
  211. time_t tt = time(0); //獲取當前時間
  212. struct tm *pst = localtime(&tt); //把time_t類型轉換為struct tm類型
  213. year = pst->tm_year + 1900;
  214. month = pst->tm_mon + 1;
  215. if((argc == 3) && isdigit(*argv[2])){
  216. cnt=atoi(argv[2]);
  217. }
  218. else{
  219. cnt=6;
  220. }
  221. for(i=0;i<cnt;i++){
  222. if(month-i<1){
  223. tm=month-i+12;
  224. ty=year-1;
  225. }
  226. else{
  227. tm=month-i;
  228. ty=year;
  229. }
  230. sprintf(ym,"%04d-%02d",ty,tm);
  231. sprintf(pwd,"%04d%02d%s",year,month,SerialNo);
  232. sprintf(cmd,"zip --password %s \t /mnt/%04d-%02d.zip \t /Storage/ChargeLog/*%04d*%02d*\n",pwd,year,month,ty,tm);
  233. system(cmd);
  234. sprintf(cmd,"zip --password %s \t /mnt/%04d-%02d.zip \t /Storage/EventLog/*%04d*%02d*\n",pwd,year,month,ty,tm);
  235. system(cmd);
  236. sprintf(cmd,"zip --password %s \t /mnt/%04d-%02d.zip \t /Storage/SystemLog/*%04d*%02d*\n",pwd,year,month,ty,tm);
  237. system(cmd);
  238. printf("Log packing is done!\n");
  239. // zip --password "'.date('Ym').$SerialNumber.'" -r /var/www/log.zip /Storage/
  240. }
  241. }
  242. }