Forráskód Böngészése

Merge pull request #51 from jancoow/Twitch

Twitch
Janco Kock 9 éve
szülő
commit
9f2874be07
10 módosított fájl, 166 hozzáadás és 18 törlés
  1. 71 7
      contentparser.c
  2. 2 0
      contentparser.h
  3. 24 3
      displayHandler.c
  4. 5 0
      displayHandler.h
  5. 9 2
      jsmn.c
  6. 1 1
      jsmn.h
  7. 13 2
      main.c
  8. 3 3
      network.c
  9. 18 0
      twitch.c
  10. 20 0
      twitch.h

+ 71 - 7
contentparser.c

@@ -8,7 +8,9 @@
 #include "mp3stream.h"
 #include "rtc.h"
 #include "alarm.h"
+#include "displayHandler.h"
 #include "vs10xx.h"
+#include "twitch.h"
 
 void parseAlarmJson(char* content){
     int r;
@@ -63,16 +65,15 @@ void parseAlarmJson(char* content){
             }else if (jsoneq(content, &token[i], "port") == 0) {
                 port = getIntegerToken(content, &token[i + 1]);
             }else if (jsoneq(content, &token[i], "ip") == 0) {
-                getStringToken(content, &token[i + 1], ip);
+                getStringToken(content, &token[i + 1], ip, 24);
             }else if (jsoneq(content, &token[i], "url") == 0) {
-                getStringToken(content, &token[i + 1], url);
+                getStringToken(content, &token[i + 1], url, 24);
             }else if (jsoneq(content, &token[i], "name") == 0) {
-                getStringToken(content, &token[i + 1], name);
+                getStringToken(content, &token[i + 1], name, 16);
             }else if (jsoneq(content, &token[i], "oo") == 0) {
                 oo = getIntegerToken(content, &token[i + 1]);
             }else if (jsoneq(content, &token[i], "st") == 0) {
                 st = getIntegerToken(content, &token[i + 1]);
-                i+=2;
             }
         }
 
@@ -133,8 +134,8 @@ void parseCommandQue(char* content){
                 u_short port = getIntegerToken(content, &token[i + 9]);
                 char url[24];
                 char ip[24];
-                getStringToken(content, &token[i + 7], url);
-                getStringToken(content, &token[i + 5], ip);
+                getStringToken(content, &token[i + 7], url, 24);
+                getStringToken(content, &token[i + 5], ip, 24);
                 bool success = connectToStream(ip, port, url);
                 if (success == true){
                     play();
@@ -149,6 +150,69 @@ void parseCommandQue(char* content){
 
 void parsetimezone(char* content)
 {
-    int timezone = atoi(content);
+    int timezone = atoi(content); //parsing string to int (only works when everything is int)
     setTimeZone(timezone);
 }
+
+void parseTwitch(char* content) {
+    if (!strcmp("null", content)) {
+        printf("Nobody is streaming");
+        return;
+    }
+    int r;
+    int i;
+    jsmn_parser p;
+    jsmntok_t token[20]; /* We expect no more than 20 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);
+    }
+
+    char name[20];
+    char title[20];
+    char game[20];
+    int date;
+    memset(name, 0, 20);
+    memset(title, 0, 20);
+    memset(game, 0, 20);
+
+    for (i = 1; i < r; i++) {
+        if (jsoneq(content, &token[i], "Name") == 0) {
+            getStringToken(content, &token[i + 1], name, 20);
+            i++;
+        }
+        else if (jsoneq(content, &token[i], "Title") == 0) {
+            getStringToken(content, &token[i + 1], title, 20);
+            i++;
+        }
+        else if (jsoneq(content, &token[i], "Game") == 0) {
+            getStringToken(content, &token[i + 1], game, 20);
+            i++;
+        }
+        else if (jsoneq(content, &token[i], "Date") == 0) {
+            date = getIntegerToken(content, &token[i + 1]);
+            i++;
+        }
+    }
+    printf("%d", date);
+    if(streamid != date)
+    {
+        strcpy(data.title, title);
+        strcpy(data.game, game);
+        strcpy(data.name, name);
+        printf("%s - %s - %s", name, title, game);
+        streamid = date;
+        setCurrentDisplay(DISPLAY_Twitch, 100);
+    }
+}
+//void TwitterParser(char* content)
+//{
+//    char tweet = atoi(content);
+//    printf("%d", tweet);
+//    displayTwitter(1,tweet);
+//}

+ 2 - 0
contentparser.h

@@ -7,5 +7,7 @@
 void parseAlarmJson(char* content);
 void parseCommandQue(char* content);
 void parsetimezone(char* content);
+void parseTwitch(char* content);
+void TwitterParser(char* content);
 
 #endif //CONTENTPARSER_H

+ 24 - 3
displayHandler.c

@@ -13,6 +13,7 @@
 #include "rtc.h"
 #include "alarm.h"
 #include "network.h"
+#include "twitch.h"
 
 struct _tm lastDisplayTime;
 viewDisplays currentViewDisplay;
@@ -41,6 +42,8 @@ void refreshScreen(){
         displayVolume();
     }else if(currentViewDisplay == DISPLAY_Alarm){
         displayAlarm(getRunningAlarmID());
+    }else if(currentViewDisplay == DISPLAY_Twitch){
+        displayTwitch(data.name, data.title, data.game);
     }
 }
 
@@ -123,7 +126,6 @@ void displayAlarm(char idx)
 void displayVolume()
 {
     u_char pos = getVolume();
-    ClearLcd();
     int i;
     LcdArrayLineOne("     Volume     ", 16);
 
@@ -131,10 +133,29 @@ void displayVolume()
 
     for(i = 0; i < 17; i++)
     {
-        characters[i] = 0xFF;
+        if(i < pos) {
+            characters[i] = 0xFF;
+        }else {
+            characters[i] = ' ';
+        }
     }
-    LcdArrayLineTwo(characters,pos);
+    LcdArrayLineTwo(characters,16);
 }
 
+void displayTwitter(int lineNumber,char text[])
+{
+    ClearLcd();
+    int i;
 
+    if (lineNumber > -1 && lineNumber < 2){
+        (*write_display_ptr[lineNumber])(text,strlen(text));
+    }
+}
 
+void displayTwitch(char name[], char title[], char game[])
+    {
+    ClearLcd();
+    LcdArrayLineOne(name, strlen(name));
+    LcdArrayLineTwo("Streaming", 9);
+    LcdBackLight(LCD_BACKLIGHT_ON);
+}

+ 5 - 0
displayHandler.h

@@ -4,6 +4,8 @@
 
 #ifndef MUTLI_OS_BUILD_DISPLAYHANDLER_H
 #define MUTLI_OS_BUILD_DISPLAYHANDLER_H
+#include "ntp.h"
+
 
 #include <time.h>
 #include "alarm.h"
@@ -20,5 +22,8 @@ void refreshScreen(void);
 void displayDateTime(void);
 void displayAlarm(char idx);
 void displayVolume();
+void displayTwitter(int lineNumber,char text[]);
+void displayTwitch(char name[], char title[], char game[]);
+
 
 #endif //MUTLI_OS_BUILD_DISPLAYHANDLER_H

+ 9 - 2
jsmn.c

@@ -1,6 +1,8 @@
 #include "jsmn.h"
 #include <stdio.h>
 #include <string.h>
+#include <assert.h>
+
 
 /**
  * Allocates a fresh unused token from the token pull.
@@ -334,6 +336,11 @@ int getIntegerToken(const char *json, jsmntok_t *tok){
 /**
  * Get the value of the token in a char[] format.
  */
-void getStringToken(const char *json, jsmntok_t *tok, char *res){
-	sprintf(res, "%.*s", tok->end - tok->start, json + tok->start);
+void getStringToken(const char *json, jsmntok_t *tok, char *res, char maxlength){
+	if((tok->end - tok->start) < maxlength - 1){
+		sprintf(res, "%.*s", tok->end - tok->start, json + tok->start);
+	}else{
+		printf("ERROR: String to large! output string length: %d - Input string length: %d - String: %.*s \n", maxlength - 1, (tok->end - tok->start), tok->end - tok->start, json + tok->start);
+		res[0] = '\0';
+	};
 }

+ 1 - 1
jsmn.h

@@ -75,7 +75,7 @@ int getIntegerToken(const char *json, jsmntok_t *tok);
 /**
  * Get the value of the token in a string format.
  */
-void getStringToken(const char *json, jsmntok_t *tok, char* res);
+void getStringToken(const char *json, jsmntok_t *tok, char* res, char maxlength);
 
 /**
  * Run JSON parser. It parses a JSON data string into and array of tokens, each describing

+ 13 - 2
main.c

@@ -217,7 +217,8 @@ THREAD(AlarmSync, arg)
     }
 
     NtpSync();
-
+    int dayCounter;
+    dayCounter = 0;
     for(;;)
     {
         if((initialized == true) && (hasNetworkConnection() == true))
@@ -226,12 +227,20 @@ THREAD(AlarmSync, arg)
             char url[49];
             sprintf(url, "/getAlarmen.php?radiomac=%s&tz=%d", getMacAdress(), getTimeZone());
             httpGet(url, parseAlarmJson);
+            char url2[43];
+            sprintf(url2, "/getTwitch.php?radiomac=%s", getMacAdress());
+            httpGet(url2, parseTwitch);
             isAlarmSyncing = false;
-
             //Command que (Telegram) sync
             sprintf(url, "%s%s", "/getCommands.php?radiomac=", getMacAdress());
             httpGet(url, parseCommandQue);
         }
+        if(dayCounter > 28800 && (hasNetworkConnection() == true))
+        {
+            NtpSync();
+            dayCounter = 0;
+        }
+        dayCounter++;
         NutSleep(3000);
     }
     NutThreadExit();
@@ -317,6 +326,8 @@ int main(void)
                 }else if(KbGetKey() == KEY_UP){
                     setCurrentDisplay(DISPLAY_Volume, 5);
                     volumeUp();
+                }else{
+                    setCurrentDisplay(DISPLAY_DateTime, 5);
                 }
             }
         }

+ 3 - 3
network.c

@@ -54,7 +54,7 @@ char* getMacAdress(){
 }
 
 void httpGet(char address[], void (*parser)(char*)){
-    u_long rx_to = 3000;
+    u_long rx_to = 10000;
     isReceiving = true;
     printf("\n\n #-- HTTP get -- #\n");
 
@@ -66,7 +66,7 @@ void httpGet(char address[], void (*parser)(char*)){
     int t = 0;
 
     if(content == 0){
-        printf("Can't calloc memory\n");
+        printf("Can't malloc memory\n");
     }else if (NutTcpConnect(sock, inet_addr("62.195.226.247"), 80)) {
         printf("Can't connect to server\n");
     }else if (NutTcpSetSockOpt(sock, SO_RCVTIMEO, &rx_to, sizeof(rx_to))){
@@ -99,8 +99,8 @@ void httpGet(char address[], void (*parser)(char*)){
         content[t] = '\0';
         printf("\nContent size: %d, Content: %s \n", t, content);
         parser(content);
-        free(content);
     }
+    free(content);
     NutTcpCloseSocket(sock);
     isReceiving = false;
 }

+ 18 - 0
twitch.c

@@ -0,0 +1,18 @@
+//
+// Created by aares on 30-3-2016.
+//
+
+#include "twitch.h"
+
+int streamid = 0;
+struct streamdata data;
+
+void setID(int id)
+{
+    streamid = id;
+}
+
+int getID()
+{
+    return streamid;
+}

+ 20 - 0
twitch.h

@@ -0,0 +1,20 @@
+//
+// Created by aares on 30-3-2016.
+//
+
+#ifndef INTERNETRADIO_TWITCH_H
+#define INTERNETRADIO_TWITCH_H
+
+#endif //INTERNETRADIO_TWITCH_H
+
+void setID(int id);
+int getID(void);
+int streamid;
+struct streamdata data;
+
+struct streamdata
+{
+    char title[20];
+    char name[20];
+    char game[20];
+};