Pebble AppMessage JS json / csv - > C

时间:2015-10-09 21:28:06

标签: c csv pebble-sdk

我正在试验Pebble SDK并希望从网上阅读太阳能数据。 我已经设法在JS中完全构建了一个应用程序。见linkenter image description here

当我在JS中发现一些限制时,我在C中重写了代码。 当通过JS接收json数据时,我通过字典将其发送给C. 您可以找到C版here

从网络收到的RAW数据:

  

[{HourNum:" 0:0",HourPower:" 0" },{HourNum:" 0:10",HourPower:   " 0" },{HourNum:" 0:20",HourPower:" 0"等等

" HourPower"过滤数据并通过Dictonary选项发送给C:

  

[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,59, 72,89,110,127,160,192,253,316,352,360,522,539,601,598,770,582,650,810,741,805,894,908,864,904,962,1016,1106,1853,1503,1311,1406,1444,1401,1409,1344,1622,1472,1382,1609,1766,3310,3093,2041,1496,2068,3302,3185,1358, 2760,2715,1285,994,1086,832,871,813,1707,1218,1218,239,203,170,132,70,53,45,39,39,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0]

两个挑战:

  • 1我想知道我是否收到了所有144个条目。

在日志中我只看到:

  

收到buffer_solarvalue:   [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,< -stops here

JS和C之间的数据是否有限? 在将数据从JS发送到C之前,我是否需要将数据拆分成多个部分?

  • 2我不知道如何处理在C中收到的数据。** 我已经阅读并尝试了几种可能性,尝试阅读原生json,现在尝试通过csv阅读它,但是如何?

最终目标是从网络上读取这些值并创建图表。 但首先我需要在C中正确读取值。

我是否正确我应该采用csv方式? 任何有过大规模经验的人都会#34; JS和C之间的数据集?

2 个答案:

答案 0 :(得分:0)

我无法在CloudPebble中使用strtok所以我编写了这个函数来推进指向下一个数字的指针

void skip_over_char(char **buffer, const char lookup) {
  while(((*buffer)[0] != lookup) && ((*buffer)[0] != '\0')) {
    (*buffer)++;
  }
  (*buffer)++; //skip over lookup character
}

然后,您可以通过这种方式将值添加到solar_values数组中。因此,您可以看到您确实收到了所有144个条目。

static int solar_values[144];
static int index;
static int received;
char *pch;

case KEY_SOLARVALUE :
  received = snprintf(buffer_solarvalue, sizeof(buffer_solarvalue), "%s", t->value->cstring);
  APP_LOG(APP_LOG_LEVEL_INFO , "Received buffer_solarvalue: %s", buffer_solarvalue);
  pch = buffer_solarvalue;
  skip_over_char(&pch, '[');
  index = 0;
  while (pch - buffer_solarvalue < received) {
    solar_values[index++] = atoi(pch);
    APP_LOG(APP_LOG_LEVEL_INFO, "solar_values[%3d] = %d", index - 1, solar_values[index-1]);
    skip_over_char(&pch, ',');
  }
  APP_LOG(APP_LOG_LEVEL_INFO , "Received %d values", index);
  break;  

答案 1 :(得分:0)

如果您想要阅读144值,首先要做的就是发送144值进行转换。您的数据仅包含138

  

[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,59, 72,89,110,127,160,192,253,316,352,360,522,539,601,598,770,582,650,810,741,805,894,908,864,904,962,1016,1106,1853,1503,1311,1406,1444,1401,1409,1344,1622,1472,1382,1609,1766,3310,3093,2041,1496,2068,3302,3185,1358, 2760,2715,1285,994,1086,832,871,813,1707,1218,1218,239,203,170,132,70,53,45,39,39,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0]

将csv数据转换为数字有很多选择。 strtokstrsep或我个人strtol。原因在于,对于数字转换,strtol(以及它的姐妹函数)提供了转换失败的最大程度的错误报告。

由于strtol (const char *nptr, char **endptr, int base);使用指向要转换的数字的指针(nptr)并更新指向刚刚转换的数字后面的下一个字符的结束指针(endptr) ,它非常适合走下由任何分隔符分隔的数字串,并将所有值转换为数字。

以下只是一个快速示例,说明如何使用它将“HourPower”数据转换为数组中包含的各个整数。出于示例的目的,您只需从stdin读取您的值。我提供了松散的错误检查,只是为了捕获一些基本的错误,否则会导致写入数组的结尾等等。它们是错误检查的例子,并不是详尽无遗的。

仔细看看并告诉我你是否有疑问:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>

#define NDATA 160
#define MAXC 1024

long xstrtol (char *p, char **ep, int base);

int main (void) {

    char line[MAXC] = {0};              /* line buffer for fgets    */
    char *p, *ep;                       /* pointers for strtol      */
    int array[NDATA] = {0};             /* array of values          */
    size_t idx = 0, nelem = 0;          /* indexes, number of ints  */

    /* read each line in file */
    while (fgets(line, MAXC, stdin))
    {
        p = ep = line;  /* initize pointer/end pointer      */

        /* convert each csv value to number, store in array */
        while (errno == 0)
        {
            /* skip delimiters/move pointer to next digit */
            while (*ep && *ep != '-' && (*ep < '0' || *ep > '9')) ep++;
            if (*ep)
                p = ep;
            else  /* break if end of string */
                break;

            array[idx++] = (int)xstrtol (p, &ep, 10); /* convert */

            if (idx == NDATA) { /* NDATA reached, conversion error */
                fprintf (stderr, "error: conversion exceeded 144.\n");
                return 1;
            }

        }

        if (idx != 144)   /* validate 144 values converted */
            fprintf (stderr, "warning: invalid conversion of '%zu' values.\n",
                    idx);

        nelem = idx;  /* save the final number of values (144) */
    }

    printf ("\n the converted json values are:\n\n");
    for (idx = 0; idx < nelem; idx++) {
        printf ("  array[%2zu] : %d\n", idx, array[idx]);
    }
    putchar ('\n');

    return 0;
}

/** a simple strtol implementation with error checking.
*  any failed conversion will cause program exit. Adjust
*  response to failed conversion as required.
*/
long xstrtol (char *p, char **ep, int base)
{
    errno = 0;

    long tmp = strtol (p, ep, base);

    /* Check for various possible errors */
    if ((errno == ERANGE && (tmp == LONG_MIN || tmp == LONG_MAX)) ||
        (errno != 0 && tmp == 0)) {
        perror ("strtol");
        exit (EXIT_FAILURE);
    }

    if (*ep == p) {
        fprintf (stderr, "No digits were found\n");
        exit (EXIT_FAILURE);
    }

    return tmp;
}

示例输出

$ ./bin/array_json_xstrtol <hpdata.txt
warning: invalid conversion of '138' values.

 the converted json values are:

  array[ 0] : 0
  array[ 1] : 0
  array[ 2] : 0
  array[ 3] : 0
  array[ 4] : 0
  array[ 5] : 0
  array[ 6] : 0
  array[ 7] : 0
  array[ 8] : 0
  array[ 9] : 0
  array[10] : 0
...
  array[47] : 59
  array[48] : 72
  array[49] : 89
  array[50] : 110
  array[51] : 127
  array[52] : 160
  array[53] : 192
  array[54] : 253
  array[55] : 316
  array[56] : 352
  array[57] : 360
  array[58] : 522
  array[59] : 539
...
  array[130] : 0
  array[131] : 0
  array[132] : 0
  array[133] : 0
  array[134] : 0
  array[135] : 0
  array[136] : 0
  array[137] : 0

hpdata.txt包含您的示例数据。