Exemplo n.º 1
0
int main()
{
  m2x_context *ctx = NULL;
  m2x_response response;

  const char *from = "2015-01-01T01:00:00.000Z";
  const char *end = "2016-01-01T01:00:00.000Z";

  char buf[2048];

  ctx = m2x_open(M2X_KEY);

  sprintf(buf, "{\"from\": \"%s\", \"end\": \"%s\"}", from, end);

  printf("Deleting location hisory from: %s end: %s for device: %s\n", from, end, device_id);

  response = m2x_device_delete_location_history(ctx, device_id, buf);
  
  printf("Status code: %d\n", response.status);
  
  if (m2x_is_success(&response) && response.raw) {
    printf("%s\n", response.raw);
  }

  m2x_release_response(&response);
  m2x_close(ctx);
  
  return 0;
}
Exemplo n.º 2
0
/*******************************************************************************
 **
 ** Function         create_device
 **
 ** Description     Create  Device, 
 **                 Hardcoded 
 **                  to add reading from configuration file. 
 **
 ** Returns          void
 **
 *******************************************************************************/
static int create_device(void)
{
    char *master_key;
    char fname[50];
    FILE *device_fp; 
    printf("Creating M2X device \n");
 
    sprintf(fname, "/etc/m2x/dev_%s", name);
    device_fp = fopen (fname, "r");
    if (device_fp) 
    {
        fscanf(device_fp, "%s", device_id);
        APPL_TRACE_INFO("Device '%s' Exists. Device ID:%s\n",name, device_id );
        fclose(device_fp);
        return 2;
    } 
    else 
    {
        /* 1.3 Create a Device */
        sprintf(buf, "{\"name\": \"%s\", \"description\": \"%s\", \"tags\": \"%s\", \"visibility\": \"%s\"}",
          name, description, tags, visibility);
        APPL_TRACE_INFO("Create device: %s\n", buf);
        response = m2x_device_create(ctx, buf);
        APPL_TRACE_INFO("Status code: %d\n", response.status);

        if (m2x_is_success(&response) && response.raw) 
        {
            APPL_TRACE_INFO("%s\n", response.raw);
            APPL_TRACE_INFO("Feed ID    : %s\n", json_object_get_string(json_value_get_object(
                response.json), "id"));

            memcpy(device_id, json_object_get_string(json_value_get_object(response.json), "id"),
                        sizeof(device_id));
            APPL_TRACE_INFO("%s\n", &device_id);
            
            /* Store the Device Id in the file */
            /*sprintf(fname, "/etc/m2x/dev_%s", name);*/
            device_fp = fopen (fname, "w+");
            if (device_fp) {  
                fprintf(device_fp, "%s", device_id);
                fclose(device_fp);
            } else
            { 
                printf("create_device: Failed to open file");
            }  
            device_count++;
        } 
       
        /* Release the response so that same struct is used for next response */
        m2x_release_response(&response);
        /* Sleep for about a min, since it takes ~40 sec to create the device */
        sleep(45);
        return 1;
    }
}
Exemplo n.º 3
0
static int create_trigger(void)
{    
    /* 3.0 Create a Trigger */
    memset(&buf, 0, sizeof(buf));
    sprintf(buf, "{\"stream\": \"%s\", \"name\": \"High Temperature\", \"condition\": \">\", \"value\": \"75\", \"callback_url\": \"http://requestb.in/1dsaa5u1\", \"status\": \"enabled\",  \"send_location\": \"true\"  }",
          stream_name);
    response = m2x_device_trigger_create(ctx, &device_id, buf);
    if (m2x_is_success(&response) && response.raw) {
      printf("%s\n", response.raw);
    }
    sleep(5);
}
Exemplo n.º 4
0
int main()
{
  m2x_context *ctx = NULL;
  char buf[2048];
  m2x_response response;

  ctx = m2x_open(M2X_KEY);

  sprintf(buf, "{\"name\": \"%s\", \"description\": \"%s\", \"tags\": \"%s\", \"visibility\": \"%s\"}",
          name, description, tags, visibility);
  printf("Update device %s: %s\n", feed_id, buf);
  response = m2x_device_update(ctx, feed_id, buf);
  printf("Status code: %d\n", response.status);
  if (m2x_is_success(&response) && response.raw) {
    printf("%s\n", response.raw);
  }
  m2x_release_response(&response);
  m2x_close(ctx);
  return 0;
}
Exemplo n.º 5
0
int main()
{
  m2x_context ctx;
  struct json_token *arr = NULL, *tok = NULL, *tok2 = NULL;
  int i, len;
  m2x_response response;
  char buf[40];

  m2x_open(M2X_KEY, &ctx);
  response = m2x_device_list(&ctx, "");
  printf("Response Status Code: %d\n", response.status);
  if (m2x_is_success(&response)) {
    arr = (struct json_token *) response.data;
    printf("Total: %d\n", parse_integer(find_json_token(arr, "body.total")));
    printf("Pages: %d\n", parse_integer(find_json_token(arr, "body.pages")));
    printf("Limit: %d\n\n", parse_integer(find_json_token(arr, "body.limit")));
    tok = find_json_token(arr, "body.devices");
    if (tok) {
      i = 0;
      while (1) {
        sprintf(buf, "[%d].id", i);
        tok2 = find_json_token(tok, buf);
        if (!tok2) { break; }
        print_token_as_string("Device ID: ", tok2);
        sprintf(buf, "[%d].name", i);
        tok2 = find_json_token(tok, buf);
        print_token_as_string("Device Name: ", tok2);
        printf("\n");
        i++;
      }
    }
  }
  m2x_release_response(&ctx, &response);
  m2x_close(&ctx);
  return 0;
}
Exemplo n.º 6
0
static int post_to_m2x(int length, char *input_val)
{

    int data_length = length - 8; 
    int is_ok;
    unsigned char UUID[2];
    unsigned char BDA[6];
    unsigned char *SENSOR_DATA;
    unsigned char *tmp_val = input_val;
    unsigned int  uuid_is=0;
    short value, x, y, z;
   

/* Receive a message from client */

/*
Sample Data for Sensors 

Humidity      org.bluetooth.characteristic.humidity 0x2A6F  Adopted 
Pressure      org.bluetooth.characteristic.pressure 0x2A6D  Adopted
Temperature   org.bluetooth.characteristic.temperature  0x2A6E  Adopted
Magnetic Flux Density - 3D org.bluetooth.characteristic.magnetic_flux_density_3D    0x2AA1  Adopted 

     AABBBBCCCCCCDD(DDDD)
     AA - Length of data (2)
     BBBB - UUID (2)
     CCCCCC - BDA (6)
     DD(DDDD) - Value (2 or 6 bytes)
     interpret_data(length, buf)


*/ 

/*
     AABBBBCCCCCCDD(DDDD)
     AA - Length of data (2)
     BB BB - UUID (4)
     CC CC CC CC CC CC- BDA (6)
     DD DD (DD DD DD DD) - Value (2 or 6 bytes)

    e.g. 10 or 14
    0a 00 6f 2a 45 20 01 18 10 00 e6 01
    0a 00 6d 2a 45 20 01 18 10 00 95 27 
    0a 00 6e 2a 45 20 01 18 10 00 e9 00 
    0a 00 6f 2a 45 20 01 18 10 00 e6 01 
    0e 00 a0 bb 45 20 01 18 10 00 00 00 00 00 00 00
    0e 00 a1 bb 45 20 01 18 10 00 c8 00 58 ff 00 00 
    
*/  
   memcpy(UUID, tmp_val, 2);
   uuid_is =  UUID[0] | UUID[1]<<8;

   printf("uuid_is = %d, length=%\n", uuid_is, length, data_length);
   
   memcpy(BDA, &tmp_val[2], 6);   
   printf("BDA = %s", BDA);

   if ((data_length == 2) || (data_length ==6)) {
       /*memcpy(SENSOR_DATA, &tmp_val[8], data_length);*/
       SENSOR_DATA=&tmp_val[8];
       /*SENSOR_DATA[data_length]= "\n";*/
      printf("Sensor_data: %x %x", SENSOR_DATA[0], SENSOR_DATA[1]);

     switch(data_length)
     {
        case 2:
	     STREAM_TO_UINT16(value, SENSOR_DATA);
             printf("Sensor_data: %x ",value);
             break;
        case 6:
	     STREAM_TO_UINT16(value, SENSOR_DATA);
	     STREAM_TO_UINT16(x, SENSOR_DATA);
	     STREAM_TO_UINT16(y, SENSOR_DATA);
	     STREAM_TO_UINT16(z, SENSOR_DATA);
      printf("Sensor_data: x=%x y=%x z=%x", x,y,z);
       x = x & 0x000F;
       z = y & 0x00F0;
       z = y & 0x0F00;
        
             break;
     }

   }
   else{
       return;
   }
    /* Reading the first two bytes for the length */ 
    switch(uuid_is) {
        case UUID_CHARACTERISTIC_HUMIDITY: 
            stream_id= HUMIDITY;
            break;
        case UUID_CHARACTERISTIC_TEMPERATURE: 
            stream_id= TEMPERATURE;
            break;
        case UUID_CHARACTERISTIC_PRESSURE: 
            stream_id= PRESSURE;
            break;
        case UUID_CHARACTERISTIC_FLUX: 
            stream_id= MAGNETO_FLUX;
            break;
        case UUID_CHARACTERISTIC_ACCELEROMETER: 
             stream_id= ACCL;
            break;
        case UUID_CHARACTERISTIC_GYROSCOPE: 
             stream_id= GYRO;
            break;
        }
        
    memset(&buf, 0, sizeof(buf));  
    printf("\nReceived Sensor reading from :");
    switch(stream_id) {
        case HUMIDITY:
            printf("HUMIDITY");
            sprintf(buf, "{ \"value\": \"%d.%d\" }", value/10,value%10);        
        break;
        case TEMPERATURE:
            printf("TEMPERATURE");           
            sprintf(buf, "{ \"value\": \"%d\" }", value/10, value%10);        
        break;
        case PRESSURE:
            printf("PRESSURE");
            sprintf(buf, "{ \"value\": \"%d\" }", value);        
        break;

        case GYRO:
            printf("GYRO");
            sprintf(buf, "{ \"value\": \"%x%x%x\" }", x,y,z );        
        break;
        case ACCL:
            printf("ACCL");
            sprintf(buf, "{ \"value\": \"%x%x%x\" }", x,y,z );        
        break;
        case MAGNETO_FLUX:
            printf("MAGNETO");
            sprintf(buf, "{ \"value\": \"%x%x%x\" }", x,y,z );        
        break;
        default:
        
        break;
    }        
    printf("(%d)\n", stream_id);
    
    

    /*{ "value": x }*/

    printf("buf: %s \n", buf);    
#ifdef RUN_M2X
    response = m2x_device_stream_value_update(ctx, &device_id, stream_name_list[stream_id], buf);
    if (m2x_is_success(&response) && response.raw) {
        printf("%s\n", response.raw);
    }
    else {
        /* Error in updating value */
    }
        
    /* Release the response so that same struct is used for next response */
    m2x_release_response(&response);
  /*  sleep(5);*/
#endif
    
}
Exemplo n.º 7
0
/*******************************************************************************
 **
 ** Function         initialize_setup
 **
 ** Description     Create  streams and trigger
 **                  Can be hardcoded 
 **                  to add reading from configuration file. 
 **
 ** Returns          void
 **
 *******************************************************************************/
static int create_streams(void)
{

    FILE *streams_fp;
    char fname[10];
    char key_from_file[100];
    int stream_len=0;
    int device_present;
    int stream_present[MAX_STREAM];
    int j;
   
    printf("Creating M2X Streams \n");
    memset(stream_present, 0, MAX_STREAM);
    /* Get the list of streams from the M2X Server and validate that all the Streams are present */
      response = m2x_device_streams(ctx, device_id);
      printf("Response code: %d\n", response.status);
      if (m2x_is_success(&response)) {
        JSON_Array *streams = json_object_get_array(json_value_get_object(
            response.json), "streams");
        size_t i;
        for (i = 0; i < json_array_get_count(streams); i++) {
          JSON_Object *json_stream = json_array_get_object(streams, i);
          const char *stream_name = json_object_get_string(json_stream, "name");
          for (j=0; j< MAX_STREAM; j++)
          {
                if (strncmp(stream_name, stream_name_list[j],strlen(stream_name)))
                {
                  stream_present[j] = 1; /* Stream already present*/
                } 
          }
          printf("Name       : %s\n", stream_name);
          printf("\n");
        }
      }
      m2x_release_response(&response);

    /* Create a Stream */
    for (i=0; i < MAX_STREAM; i++)
    {
        if (stream_present[i])
            continue;   
        printf("Creating %d M2X Stream :\"%s\" of size=%d  \n", i, stream_name_list[i], sizeof(stream_name_list[i]));
        memset(&buf, 0, sizeof(buf));
        stream_len= strlen(stream_name_list[i]);
	    stream_len = (stream_len<MAX_STREAM_NAME)?stream_len:MAX_STREAM_NAME;
        memcpy(stream_name, stream_name_list[i], stream_len+1); 
        /*stream_name[stream_len]='\n';*/
        APPL_TRACE_INFO("\nBefore m2x_device_stream_create device_id: %s    |\n", &device_id);
        APPL_TRACE_INFO("\nBefore m2x_device_stream_create stream_name: %s  |\n", stream_name);
        
        switch(i) {
            case HUMIDITY:
                sprintf(buf, "{ \"unit\": { \"label\": \"percentage\", \"symbol\": \"%%\" }, \"type\": \"numeric\" }");
            break;        
            case TEMPERATURE:
                sprintf(buf, "{ \"unit\": { \"label\": \"celsius\", \"symbol\": \"C\" }, \"type\": \"numeric\" }");
            break;        
            case PRESSURE:
                sprintf(buf, "{ \"unit\": { \"label\": \"pascal\", \"symbol\": \"Pa\" }, \"type\": \"numeric\" }");
            break;        
            
            case GYRO:
                sprintf(buf, "{ \"unit\": { \"label\": \"point\", \"symbol\": \"p\" }, \"type\": \"numeric\" }");
            break;        
            case ACCL:
                sprintf(buf, "{ \"unit\": { \"label\": \"point\", \"symbol\": \"p\" }, \"type\": \"numeric\" }");
            break;        
            case MAGNETO_FLUX:
                sprintf(buf, "{ \"unit\": { \"label\": \"point\", \"symbol\": \"p\" }, \"type\": \"numeric\" }");
            break;
        }
        
        response = m2x_device_stream_create(ctx, &device_id, stream_name, buf);        
        if (m2x_is_success(&response) && response.raw) {
        APPL_TRACE_INFO("%s\n", response.raw);
        }

        /* Store the Device Id in the file */
        sprintf(fname, "/tmp/m2x/stream_%s", stream_name);
        streams_fp = fopen (fname, "w+");
        fprintf(streams_fp, "%s", stream_name);
        fclose(streams_fp);

        /* Release the response so that same struct is used for next response */
        m2x_release_response(&response);
        /* Sleep for about a min, since it takes ~40 sec to create the device */
        sleep(5);
    }
    
    
}
Exemplo n.º 8
0
int m2x_fetch_pending_commands(m2x_context *ctx, const char *dev_id)
{
  m2x_response response;
  m2x_command *command;
  struct json_token *arr = NULL, *tok = NULL;
  char command_ids[M2X_COMMAND_QUEUE_LENGTH + 1][M2X_ID_MAX_LENGTH + 1];
  char token_path[30];
  int idx = 0;
  int inserted_any = 0;

  /* Fetch the summaries of the next outstanding commands, if any. */
  response = m2x_device_commands(ctx, dev_id, M2X__FETCH_COMMANDS_PARAMS);
  if (!m2x_is_success(&response))
    return 0;
  arr = (struct json_token *) response.data;

  /* Capture the command id string from each command summary in the list. */
  for (idx = 0; idx < M2X_COMMAND_QUEUE_LENGTH; idx++) {
    /* Get the id token at the token path. */
    sprintf(token_path, "body.commands[%i].id", idx);
    tok = find_json_token(arr, token_path);
    if (!tok)
      break;

    /* Store the command id string from the token. */
    command_ids[idx][0] = '\0';
    strncat(command_ids[idx], tok->ptr, tok->len);
  }
  /* Add a terminator as the last element of the command_ids array. */
  command_ids[idx][0] = '\0';

  /* Clean up. */
  m2x_release_response(ctx, &response);

  /* Use the command ids to fetch and store each detailed representation. */
  for (idx = 0; idx < M2X_COMMAND_QUEUE_LENGTH; idx++) {
    /* Break early if we've reache the terminator. */
    if (command_ids[idx][0] == '\0')
      break;

    /* Fetch the detailed representation of the command by id. */
    response = m2x_device_view_command(ctx, dev_id, command_ids[idx]);
    if (!m2x_is_success(&response))
      break;

    /* Get the representation token from the response json. */
    arr = (struct json_token *) response.data;
    tok = find_json_token(arr, "body");
    if (!tok)
      break;

    /* Create and insert the command into the commands queue. */
    command = m2x_create_command(ctx, tok->ptr, tok->len);
    if (command) {
      m2x_insert_command(ctx, command);
      inserted_any = 1;
    }

    /* Clean up. */
    m2x_release_response(ctx, &response);
    break;
  }

  /* Clean up. */
  m2x_release_response(ctx, &response);

  return inserted_any;
}