PageRenderTime 67ms CodeModel.GetById 41ms RepoModel.GetById 1ms app.codeStats 0ms

/metnet.c

https://github.com/Zozz/zozzfox
C | 172 lines | 139 code | 17 blank | 16 comment | 19 complexity | 638fc2f52e600e66695fa726474536d0 MD5 | raw file
  1. /*
  2. * metnet.c
  3. *
  4. * Created on: May 29, 2010
  5. * Author: zoli
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11. #include <unistd.h>
  12. #include <time.h>
  13. #include <sys/shm.h>
  14. #include <sys/socket.h>
  15. #include <arpa/inet.h>
  16. #include <syslog.h>
  17. #include <signal.h>
  18. #include <netdb.h>
  19. #include "wmrs200log.h"
  20. #define STRLEN_MSG 1018 // length of whole message
  21. static wmrs_t *w;
  22. static char dt[20], key[33];
  23. static char msg[1023] =
  24. "POST /code/api/obs_auto.php HTTP/1.1\r\n"
  25. "Host: metnet.hu\r\n"
  26. "Content-Type: multipart/form-data; boundary=--------06j410213928046\r\n"
  27. "Content-Length: 870\r\n\r\n"
  28. "----------06j410213928046\r\n"
  29. "Content-Disposition: form-data; name=\"xmlfile\"; filename=\"wsdata.xml\"\r\n"
  30. "Content-Type: text/xml\r\n\r\n"
  31. "<?xml version=\"1.0\" encoding=\"ISO-8859-2\" standalone=\"yes\"?>\r\n"
  32. " <?generator program=\"WMRS200log 1.0\"?>\r\n"
  33. " <?verzio verzio=\"1.0\" ?>\r\n"
  34. " <adatok>\r\n"
  35. " <muszer>ZWMRS</muszer>\r\n"
  36. ;
  37. /* Convert string to uppercase */
  38. static void strupr(char *s)
  39. {
  40. while(*s){
  41. *s = toupper(*s);
  42. s++;
  43. }
  44. return;
  45. }
  46. // free resources on exit
  47. static void cleanup(int dummy)
  48. {
  49. shmdt(w);
  50. exit(EXIT_SUCCESS);
  51. }
  52. int main(int argc, char *argv[])
  53. {
  54. unsigned int interval = 300; // update interval in seconds
  55. const int hlen = strlen(msg); // length of static part
  56. int shmid, sd, pos;
  57. FILE *fc;
  58. time_t t;
  59. struct tm *tmp;
  60. struct sockaddr_in saddr;
  61. struct hostent *host;
  62. char keygen[] = "echo -n 'ZWMRSCE323B11B2E18FC9C1DB1DA870BACAA82010-05-29 21:14:10' | md5sum >/var/key"; //sector
  63. char * const dtpos = strstr(keygen, "2010-"); // position of date/time
  64. openlog(argv[0], 0, 0);
  65. syslog(LOG_INFO, "starting");
  66. if(argc > 1){
  67. if(sscanf(argv[1], "%u", &interval) != 1){
  68. printf("Usage: %s [n]\n n: update interval in seconds\n", argv[0]);
  69. exit(EXIT_SUCCESS);
  70. }
  71. if(interval < 60) interval = 60;
  72. }
  73. /* read the station identifier */
  74. if((fc = fopen("/mnt/1/statid", "r")) != NULL){
  75. fgets(&keygen[9], 38, fc);
  76. fclose(fc);
  77. }
  78. else{
  79. syslog(LOG_ERR, "statid: %m");
  80. exit(EXIT_FAILURE);
  81. }
  82. /* create shared memory for WMRS communication */
  83. if((shmid = shmget(1962, sizeof(wmrs_t), IPC_CREAT | 0666)) < 0){
  84. syslog(LOG_ERR, "shmget: %m");
  85. exit(EXIT_FAILURE);
  86. }
  87. if((w = shmat(shmid, NULL, SHM_RDONLY)) == (void *)-1){
  88. syslog(LOG_ERR, "shmat: %m");
  89. exit(EXIT_FAILURE);
  90. }
  91. signal(SIGTERM, cleanup);
  92. for(; ; sleep(interval)){
  93. /* connect to metnet.hu */
  94. if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
  95. syslog(LOG_ERR, "socket: %m");
  96. continue;
  97. }
  98. memset(&saddr, 0, sizeof(saddr)); // clear struct
  99. saddr.sin_family = AF_INET;
  100. saddr.sin_port = htons(80);
  101. host = gethostbyname("metnet.hu");
  102. if(host != NULL){
  103. char *s, *d;
  104. s = host->h_addr_list[0];
  105. d = (char *)&saddr.sin_addr;
  106. for(pos = 0; pos < sizeof(saddr.sin_addr); pos++){
  107. *d++ = *s++;
  108. }
  109. }
  110. if(connect(sd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0){
  111. syslog(LOG_ERR, "connect: %m");
  112. close(sd);
  113. continue;
  114. }
  115. /* generate key */
  116. t = time(NULL);
  117. tmp = localtime(&t);
  118. strftime(dt, sizeof(dt), "%F %T", tmp);
  119. memcpy(dtpos, dt, 19); // replace date/time part
  120. system(keygen);
  121. key[0] = '\0';
  122. if((fc = fopen("/var/key", "r")) != NULL){
  123. fgets(key, 33, fc);
  124. fclose(fc);
  125. }
  126. strupr(key);
  127. /* build variable part of message */
  128. pos = hlen; // end of static part
  129. pos += sprintf(&msg[pos], " <kulcs>%s</kulcs>\r\n", key);
  130. pos += sprintf(&msg[pos], " <meres>%s</meres>\r\n", dt);
  131. pos += sprintf(&msg[pos], " <homerseklet>%5.1f</homerseklet>\r\n", w->s[1].temp);
  132. pos += sprintf(&msg[pos], " <relativlegnyomas>%4d</relativlegnyomas>\r\n", w->relP);
  133. pos += sprintf(&msg[pos], " <legnedvesseg>%3d</legnedvesseg>\r\n", w->s[1].rh);
  134. if(w->s[1].dew > 50.0) // in case of very low temp (below -10) the dew value is wrong (819.2)
  135. pos += sprintf(&msg[pos], " <harmatpont> 0.0</harmatpont>\r\n");
  136. else
  137. pos += sprintf(&msg[pos], " <harmatpont>%5.1f</harmatpont>\r\n", w->s[1].dew);
  138. pos += sprintf(&msg[pos], " <szelsebesseg>%5.1f</szelsebesseg>\r\n", w->wind);
  139. pos += sprintf(&msg[pos], " <szelirany>%3d</szelirany>\r\n", w->windDir);
  140. pos += sprintf(&msg[pos], " <szellokes>%5.1f</szellokes>\r\n", w->gust);
  141. pos += sprintf(&msg[pos], " <csapadek1ora>%5.1f</csapadek1ora>\r\n", w->prec1);
  142. pos += sprintf(&msg[pos], " <csapadek24ora>%5.1f</csapadek24ora>\r\n", w->prec24);
  143. pos += sprintf(&msg[pos], " <csapadekosszes>%7.1f</csapadekosszes>\r\n", w->precTot);
  144. strcpy(&msg[pos], " </adatok>\r\n----------06j410213928046--\r\n");
  145. // printf("%d\n", strlen(msg)); // print whole length
  146. if(send(sd, msg, STRLEN_MSG, 0) < 0){
  147. syslog(LOG_ERR, "send: %m");
  148. }
  149. while(recv(sd, &msg[hlen], 512, 0) > 1){
  150. // puts(&msg[hlen]);
  151. }
  152. close(sd);
  153. }
  154. }
  155. /* SDG */