Selaa lähdekoodia

Merge pull request #41 from jancoow/contentparser

Added contentparser file for easy adding web content parser.
Janco Kock 9 vuotta sitten
vanhempi
commit
8ce1d97916
8 muutettua tiedostoa jossa 142 lisäystä ja 124 poistoa
  1. 117 0
      contentparser.c
  2. 10 0
      contentparser.h
  3. 2 3
      main.c
  4. 5 114
      network.c
  5. 1 3
      network.h
  6. 5 4
      ntp.c
  7. 1 0
      ntp.h
  8. 1 0
      vs10xx.c

+ 117 - 0
contentparser.c

@@ -0,0 +1,117 @@
+//
+// Created by janco on 25-3-16.
+//
+#include "contentparser.h"
+#include "ntp.h"
+#include "network.h"
+#include "jsmn.h"
+#include "rtc.h"
+#include "alarm.h"
+
+void parseAlarmJson(char* content){
+    int r;
+    int i;
+    jsmn_parser p;
+    jsmntok_t token[150]; /* We expect no more than 128 tokens */
+
+    jsmn_init(&p);
+    r = jsmn_parse(&p, content, strlen(content), token, sizeof(token)/sizeof(token[0]));
+    if (r <= 0) {
+        printf("Failed to parse JSON: %d \n", r);
+        return;
+    }else{
+        printf("Aantal tokens found: %d \n", r);
+    }
+
+    int start = 1;
+    int usedAlarms[maxAlarms()];
+    int j;
+    for(j = 0; j < maxAlarms(); j++){
+        usedAlarms[j] = 0;
+    }
+    for(i = 1; i < r; i++)
+    {
+        struct _tm time = GetRTCTime();
+        u_short port;
+        char url[24];
+        char ip[24];
+        char name[16];
+        char st;
+        memset(url, 0, 24);
+        memset(ip, 0, 24);
+        memset(name, 0, 16);
+
+        int id;
+        for (i = i; !((i + start) % 26 == 0); i++) {
+            if (jsoneq(content, &token[i], "YYYY") == 0) {
+                time.tm_year= getIntegerToken(content, &token[i + 1]) - 1900;
+                i++;
+            }else if (jsoneq(content, &token[i], "MM") == 0) {
+                time.tm_mon=  getIntegerToken(content, &token[i + 1]) - 1;
+                i++;
+            }else if (jsoneq(content, &token[i], "DD") == 0) {
+                time.tm_mday =  getIntegerToken(content, &token[i + 1]);
+                i++;
+            }else if (jsoneq(content, &token[i], "hh") == 0) {
+                time.tm_hour = 	getIntegerToken(content, &token[i + 1]);
+                i++;
+            }else if (jsoneq(content, &token[i], "mm") == 0) {
+                time.tm_min = getIntegerToken(content, &token[i + 1]);
+                i++;
+            }else if (jsoneq(content, &token[i], "ss") == 0) {
+                time.tm_sec = getIntegerToken(content, &token[i + 1]);
+                i++;
+            }else if (jsoneq(content, &token[i], "id") == 0) {
+                id = getIntegerToken(content, &token[i + 1]);
+                i++;
+            }else if (jsoneq(content, &token[i], "port") == 0) {
+                port = getIntegerToken(content, &token[i + 1]);
+                i++;
+            }else if (jsoneq(content, &token[i], "ip") == 0) {
+                getStringToken(content, &token[i + 1], ip);
+                i++;
+            }else if (jsoneq(content, &token[i], "url") == 0) {
+                getStringToken(content, &token[i + 1], url);
+                i++;
+            }else if (jsoneq(content, &token[i], "name") == 0) {
+                getStringToken(content, &token[i + 1], name);
+                i++;
+            }else if (jsoneq(content, &token[i], "st") == 0) {
+                st = getIntegerToken(content, &token[i + 1]);
+                i++;
+            }
+        }
+        start = 0;
+
+        int idx = alarmExist(id);
+        if(idx == -1){
+            printf("New alarm found!\n");
+            printf("Alarm time is: %02d:%02d:%02d\n", time.tm_hour, time.tm_min, time.tm_sec);
+            printf("Alarm date is: %02d.%02d.%02d\n", time.tm_mday, (time.tm_mon + 1), (time.tm_year + 1900));
+            printf("Alarm stream data is: %s:%d%s\n", ip, port, url);
+            printf("Alarm id and name and st is: %d %s %d\n\n", id, name, st);
+
+            //zoek naar een vrije plaats in de alarm array
+            for(j = 0; j < maxAlarms(); j++){
+                if(usedAlarms[j] == 0){ //Dit is een lege plaats, hier kunnen we ons nieuwe alarm plaatsen
+                    setAlarm(time, name, ip, port, url, st, id, j);
+                    usedAlarms[j] = 1;
+                    j = 10;
+                }
+            }
+        }else{
+            usedAlarms[idx] = 1; //Alarm bestaat al, dus we houden deze plaats vrij voor dat alarm
+        }
+    }
+    for(j = 0; j < maxAlarms(); j++){ //Alle overige plaatsen, die wij niet gezet hebben, verwijderen.
+        if(usedAlarms[j] == 0){
+            deleteAlarm(j);
+        };
+    }
+}
+
+void parsetimezone(char* content)
+{
+    int timezone = atoi(content);
+    printf("%d", timezone);
+}

+ 10 - 0
contentparser.h

@@ -0,0 +1,10 @@
+//
+// Created by janco on 25-3-16.
+//
+#ifndef CONTENTPARSER_H
+#define CONTENTPARSER_H
+
+void parseAlarmJson(char* content);
+void parsetimezone(char* content);
+
+#endif //CONTENTPARSER_H

+ 2 - 3
main.c

@@ -48,6 +48,7 @@
 #include "alarm.h"
 #include "ntp.h"
 #include "httpstream.h"
+#include "contentparser.h"
 
 /*-------------------------------------------------------------------------*/
 /* local routines (prototyping)                                            */
@@ -228,9 +229,7 @@ THREAD(AlarmSync, arg)
             isAlarmSyncing = 1;
             char url[43];
             sprintf(url, "%s%s", "/getAlarmen.php?radiomac=", getMacAdress());
-            char* content = httpGet(url);
-            parseAlarmJson(content);
-            free(content);
+            httpGet(url, parseAlarmJson);
             isAlarmSyncing = 0;
         }
         NutSleep(3000);

+ 5 - 114
network.c

@@ -26,6 +26,7 @@
 #include "jsmn.h"
 #include "rtc.h"
 #include "alarm.h"
+#include "contentparser.h"
 
 bool isReceiving;
 bool hasNetwork;
@@ -49,7 +50,7 @@ char* getMacAdress(){
     ether_ntoa(confnet.cdn_mac);
 }
 
-char* httpGet(char address[]){
+void httpGet(char address[], void (*parser)(char*)){
     u_long rx_to = 3000;
     isReceiving = true;
     printf("\n\n #-- HTTP get -- #\n");
@@ -97,121 +98,11 @@ char* httpGet(char address[]){
 
     content[t] = '\0';
     printf("\nContent size: %d, Content: %s \n", t, content);
-    isReceiving = false;
-    return content;
-}
-
-int getTimeZone()
-{
-    char* content = httpGet("/gettimezone.php");
-    int timezone = atoi(content);
+    parser(content);
     free(content);
-    printf("%d", timezone);
-    return timezone;
-}
-
-void parseAlarmJson(char* content){
-    int r;
-    int i;
-    jsmn_parser p;
-    jsmntok_t token[150]; /* We expect no more than 128 tokens */
-
-    jsmn_init(&p);
-    r = jsmn_parse(&p, content, strlen(content), token, sizeof(token)/sizeof(token[0]));
-    if (r <= 0) {
-        printf("Failed to parse JSON: %d \n", r);
-        return;
-    }else{
-        printf("Aantal tokens found: %d \n", r);
-    }
-
-
 
-    int start = 1;
-    int usedAlarms[maxAlarms()];
-    int j;
-    for(j = 0; j < maxAlarms(); j++){
-        usedAlarms[j] = 0;
-    }
-    for(i = 1; i < r; i++)
-    {
-        struct _tm time = GetRTCTime();
-        u_short port;
-        char url[24];
-        char ip[24];
-        char name[16];
-        char st;
-		memset(url, 0, 24);
-		memset(ip, 0, 24);
-		memset(name, 0, 16);
-        
-		int id;
-        for (i = i; !((i + start) % 26 == 0); i++) {
-            if (jsoneq(content, &token[i], "YYYY") == 0) {
-                time.tm_year= getIntegerToken(content, &token[i + 1]) - 1900;
-                i++;
-            }else if (jsoneq(content, &token[i], "MM") == 0) {
-                time.tm_mon=  getIntegerToken(content, &token[i + 1]) - 1;
-                i++;
-            }else if (jsoneq(content, &token[i], "DD") == 0) {
-                time.tm_mday =  getIntegerToken(content, &token[i + 1]);
-                i++;
-            }else if (jsoneq(content, &token[i], "hh") == 0) {
-                time.tm_hour = 	getIntegerToken(content, &token[i + 1]);
-                i++;
-            }else if (jsoneq(content, &token[i], "mm") == 0) {
-                time.tm_min = getIntegerToken(content, &token[i + 1]);
-                i++;
-            }else if (jsoneq(content, &token[i], "ss") == 0) {
-                time.tm_sec = getIntegerToken(content, &token[i + 1]);
-                i++;
-            }else if (jsoneq(content, &token[i], "id") == 0) {
-                id = getIntegerToken(content, &token[i + 1]);
-                i++;
-            }else if (jsoneq(content, &token[i], "port") == 0) {
-                port = getIntegerToken(content, &token[i + 1]);
-                i++;
-            }else if (jsoneq(content, &token[i], "ip") == 0) {
-                getStringToken(content, &token[i + 1], ip);
-                i++;
-            }else if (jsoneq(content, &token[i], "url") == 0) {
-                getStringToken(content, &token[i + 1], url);
-                i++;
-            }else if (jsoneq(content, &token[i], "name") == 0) {
-                getStringToken(content, &token[i + 1], name);
-                i++;
-            }else if (jsoneq(content, &token[i], "st") == 0) {
-                st = getIntegerToken(content, &token[i + 1]);
-                i++;
-            }
-        }
-        start = 0;
-
-        int idx = alarmExist(id);
-        if(idx == -1){
-            printf("New alarm found!\n");
-            printf("Alarm time is: %02d:%02d:%02d\n", time.tm_hour, time.tm_min, time.tm_sec);
-            printf("Alarm date is: %02d.%02d.%02d\n", time.tm_mday, (time.tm_mon + 1), (time.tm_year + 1900));
-            printf("Alarm stream data is: %s:%d%s\n", ip, port, url);
-            printf("Alarm id and name and st is: %d %s %d\n\n", id, name, st);
-
-            //zoek naar een vrije plaats in de alarm array
-            for(j = 0; j < maxAlarms(); j++){
-                if(usedAlarms[j] == 0){ //Dit is een lege plaats, hier kunnen we ons nieuwe alarm plaatsen
-                    setAlarm(time, name, ip, port, url, st, id, j);
-                    usedAlarms[j] = 1;
-                    j = 10;
-                }
-            }
-        }else{
-            usedAlarms[idx] = 1; //Alarm bestaat al, dus we houden deze plaats vrij voor dat alarm
-        }
-    }
-    for(j = 0; j < maxAlarms(); j++){ //Alle overige plaatsen, die wij niet gezet hebben, verwijderen.
-        if(usedAlarms[j] == 0){
-            deleteAlarm(j);
-        };
-    }
+    isReceiving = false;
+    return content;
 }
 
 bool NetworkIsReceiving(void){

+ 1 - 3
network.h

@@ -8,9 +8,7 @@
 //bool hasNetworkConnection(void);
 //bool NetworkIsReceiving(void);
 extern void NetworkInit(void);
-char* httpGet(char address[]);
+extern void httpGet(char address[], void (*parser)(char*));
 char* getMacAdress();
-void parseAlarmJson(char* content);
-int getTimeZone();
 
 #endif /* _Network_H */

+ 5 - 4
ntp.c

@@ -20,6 +20,7 @@
 
 #include "log.h"
 #include "ntp.h"
+#include "contentparser.h"
 
 int TIME_ZONE = 1;
 #define LOG_MODULE  LOG_NTP_MODULE
@@ -102,7 +103,7 @@ bool NtpTimeIsValid(void){
 void NtpSync(void){
     /* Ophalen van pool.ntp.org */
     isSyncing = true;
-    _timezone = -getTimeZone() * 3600;
+    httpGet("/gettimezone.php", parsetimezone);
     printf(TIME_ZONE);
     NutDelay(100);
     //puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
@@ -142,6 +143,6 @@ void NtpWriteTimeToEeprom(tm time_struct){
     NutDelay(100);
 }
 
-//unsigned long TmStructToEpoch(tm tm_struct){
-//
-//}
+void setTimeZone(int timezone){
+    _timezone = -timezone * 3600;
+}

+ 1 - 0
ntp.h

@@ -10,6 +10,7 @@ typedef enum {false, true} bool;
 extern bool NtpIsSyncing(void);
 extern void NtpInit(void);
 extern void NtpSync(void);
+extern void setTimeZone(int timezone);
 extern bool NtpTimeIsValid(void);
 
 void NtpCheckValidTime(void);

+ 1 - 0
vs10xx.c

@@ -793,6 +793,7 @@ int VsSetVolume(u_char left, u_char right)
     if(right < 0){
         right = 0;
     }
+    printf("%d %d", left, right);
 
     ief = VsPlayerInterrupts(0);