// This function pushes changes to the cloud for two specific column names, and pushes new information every time without updating previous records
void updateOnParse(const char *column, int value)//, const char *healthColumn, int healthValue)
{	
	
	ParseClient client = parseInitialize("TSexah1prtbhpF6w4dellQ2XYWyk2bqcljOiElrN", "xLnvnzcTMO1w9MwuFNBTO6hLjOtnKmZn4iz4SBnu");
	char strValue[4];
	char strColumn[4];
	//char IDvalue[10];
	char pathLabel[4];
	char grabbed_string[] = "1111111111";
	sprintf(strValue, "%d", value);

	char data[100] = "{ \"";
	
	strcat(data, column);
	strcat(data, "\": ");
	strcat(data, strValue);
	strcat(data, " }");
	
	parseSendRequest(client, "POST", "/1/functions/put_objectID", "{\"value\":\"echo\"}", myCloudFunctionCallback);
	strncpy(grabbed_string, copied_string+11,10);
	//need a way to retrieve the object ID so that these things can be associated
	strcat(pathLabel, "/1/classes/Bulb/");
	strcat(pathLabel, grabbed_string);

	printf("%s\n", pathLabel);
	sleep(1); //putting this here lets other stuff run... loks like parse isn't accepting stuff
	printf("lightbulb::updateOnParse(): Trying data  %s\n", data);
	parseSendRequest(client, "POST", pathLabel, data, NULL);
	printf("lightbulb::updateOnParse(): Pushed data  %s\n", data);
	
}
Exemplo n.º 2
0
/*
* main function to handle Parse GET/CREATE/DELETE/UPDATE/QUERY request
* @params
* -v - http verb
* -e - http endpoint
* -d - request body
* -p - parameters (used only for Parse query)
* -i - command to get installation id
*/
int main(int argc , char **argv) {
  char c;
  extern char* optarg;

  yunReadProvisioningInfo();

  ParseClient client = parseInitialize(g_cAppID,g_cClientKey);

  if(g_cInstallationID[0] != '\0') {
	parseSetInstallationId(client, g_cInstallationID);
  }
  if(g_cSessionToken[0] != '\0') {
	parseSetSessionToken(client, g_cSessionToken);
  }

  while((c=getopt(argc,argv,"v:e:d:p:is"))!=-1){
    switch(c){
      case 'v': // http verb
      if(!(verb=strdup(optarg))){
        return -1;
      }
      break;
      case 'e': // http endpoint
      if(!(endpoint=strdup(optarg))){
        return -1;
      }
      break;
      case 'd': // request body
      if(!(data=strdup(optarg))){
      }
      fprintf(stderr, "data = '%s'\n", data);
      break;
      case 'p': // parameters
      if(!(params=strdup(optarg))){
      }
      break;
      case 'i':
      arduinoGetInstallationId(client);
      exit(0);
      case 's':
      arduinoGetSessionToken(client);
      exit(0);
    }
  }

  if (strncmp("GET", verb, 3) == 0) {
    data = params;
  }

  if (client) {
    if (!params) {
      parseSendRequest(client, verb, endpoint, data, arduinoCallHandler);
    } else {
      parseSendRequest(client, verb, endpoint, params, arduinoQueryHandler);
    }
  }

  exit(0);
}
Exemplo n.º 3
0
static void getSessionObjectCallback(ParseClient client, int error, int httpStatus, const char* httpResponseBody) {
    ParseClientInternal *clientInternal = getClient(client);

    if (error == 0 && httpResponseBody != NULL) {
        // extract objectId for the session if no object id, this is invalid token
        char objectId[16];
        if (simpleJsonProcessor(httpResponseBody, "objectId", objectId, sizeof(objectId))) {
            char installationId[40];
            if (simpleJsonProcessor(httpResponseBody, "installationId", installationId, sizeof(installationId))) {
                if(strncmp(clientInternal->installationId, installationId, sizeof(installationId))) {
                    // else if installation id does not match we cannot use this ession
                    // let it fall but disregard it. 
                    parseLog(PARSE_LOG_WARN, "InstallationId does not match.\n");
                    return;
                }
                // if it has installation id and it matches we are done
                parseLog(PARSE_LOG_INFO, "Session and installationId matched.\n");
            } else {
                // if no installation id this is new session and need to 
                // associate it with simple PUT /1/sessions/me with empty body {}
                // and header with installation id.
                parseSendRequest(client, "PUT", "/1/sessions/me", "{}", NULL);
            }
        } else {
            // no session found, do nothing
            parseLog(PARSE_LOG_WARN, "My session is not found\n");
        }
    } else {
        parseLog(PARSE_LOG_ERROR, "Did not manage to talk to the server.\n");
    }
}
Exemplo n.º 4
0
int main(int argc, char *argv[])
{
    ParseClient client = parseInitialize("VOB4wXj2mGOjJaqzdhkM701n2ahTSRMqZW6QQ8XU", "XCPG2OTVrapoymNGS5XGQIhsRM3F2tnVUgaceyec");
    
    int childpid = fork();
    
    if (childpid == 0)
    {
        while (1)
        {
            float currentTemp = getTemperature();
            char data[256];
            sprintf(data, "{ \"value\": %f }", currentTemp);
            parseSendRequest(client, "POST", "/1/classes/Temperature", data, NULL);
        }
    }
    
    printf("CHILDPID: %d\n", childpid);
    
    return 0;
}
Exemplo n.º 5
0
void parseSetSessionToken(ParseClient client, const char *sessionToken)
{
    ParseClientInternal *clientInternal = getClient(client);

    if (sessionToken != NULL) {
        parseCreateInstallationIdIfNeeded(client);
        parseOsStoreKey(clientInternal->applicationId, PARSE_SESSION_TOKEN, sessionToken);
        if (clientInternal->sessionToken != NULL) {
            free(clientInternal->sessionToken);
        }
        clientInternal->sessionToken= strdup(sessionToken);

        // query the session object by doing GET on /1/sessions/me
        parseSendRequest(client, "GET", "/1/sessions/me", NULL, getSessionObjectCallback);
    } else {
        parseOsClearKey(clientInternal->applicationId, PARSE_SESSION_TOKEN);
        if (clientInternal->sessionToken != NULL) {
            free(clientInternal->sessionToken);
            clientInternal->sessionToken = NULL;
        }
    }
}
int main(int argc , char **argv) {
  char c;

  int isYun = yunReadProvisioningInfo();

  // process pre init options
  while((c=getopt(argc,argv,":a:k:x:y:v:e:d:p:ish"))!=-1){
    switch(c) {
        case 'h':
            displayHelp();
            exit(0);
        case 'a':
            strncpy(g_cAppID, optarg, sizeof(g_cAppID));
            break;
        case 'k':
            strncpy(g_cClientKey, optarg, sizeof(g_cClientKey));
            break;
        case 'x':
            strncpy(g_cInstallationID, optarg, sizeof(g_cInstallationID));
            break;
        case 'y':
            strncpy(g_cSessionToken, optarg, sizeof(g_cSessionToken));
            break;
        case 'v':
        case 'e':
        case 'd':
        case 'p':
        case 'i':
        case 's':
            // these we want to process later
            break;
        case ':':
          fprintf(stderr, "%s: option '-%c' requires an argument\n", argv[0], optopt);
          exit(0);
        case '?':
          fprintf(stderr, "%s: option '-%c' is not recognized \n", argv[0], optopt);
          exit(0);
        default:
          fprintf(stderr, "%s: option '-%c' is not implemented\n", argv[0], optopt);
          exit(0);
    }
  }

  if (g_cAppID[0] == '\0') {
    fprintf(stdout, "{\"error\":\"missing application id\"}\n");
    exit(0);
  }
  if (g_cClientKey[0] == '\0') {
    fprintf(stdout, "{\"error\":\"missing client key\"}\n");
    exit(0);
  }

  ParseClient client = parseInitialize(g_cAppID,g_cClientKey);

  if(g_cInstallationID[0] != '\0') {
	parseSetInstallationId(client, g_cInstallationID);
  }
  if(g_cSessionToken[0] != '\0') {
	parseSetSessionToken(client, g_cSessionToken);
  }

  // process post init options
  optreset = 1;
  optind = 1;

  while((c=getopt(argc,argv,":a:k:x:y:v:e:d:p:ish"))!=-1){
    switch(c){
      case 'v': // http verb
        if(!(verb=strdup(optarg))){
            return -1;
        }
        break;
      case 'e': // http endpoint
        if(!(endpoint=strdup(optarg))){
            return -1;
        }
        break;
      case 'd': // request body
        if(!(data=strdup(optarg))){
        }
        break;
      case 'p': // parameters
        if(!(params=strdup(optarg))){
        }
        break;
      case 'i':
        arduinoGetInstallationId(client);
        exit(0);
      case 's':
        arduinoGetSessionToken(client);
        exit(0);
    }
  }

  if (strncmp("GET", verb, 3) == 0) {
    data = params;
  }

  if (client) {
    if (!params) {
      parseSendRequest(client, verb, endpoint, data, arduinoCallHandler);
    } else {
      parseSendRequest(client, verb, endpoint, params, arduinoQueryHandler);
    }
  }

  exit(0);
}
Exemplo n.º 7
0
int main(int argc, char *argv[]) {

    // CREATE UNIQUE IDENTIFIER FOR THE RUN

    run = time(NULL);
    char classPathOne[128] = {0};
    char classPathTwo[128] = {0};
    char classPathThree[128] = {0};
    char objectId[11] = {0};
    char path[256]  = {0};

    snprintf(classPathOne, sizeof(classPathOne), "/classes/TestObjectOne%ld", run);


    // TEST INITIALIZATION
    ParseClient client = parseInitializeWithServerURL(YOUR_APP_IP, YOUR_CLIENT_KEY, YOUR_SERVER_URL);
    logResults(client != NULL, 1, "parseInitialize call", "failed to start parse");


    // TEST GENERATION OF INSTALLATION ID

    const char* id = parseGetInstallationId(client);
    logResults(id == NULL, 0, "parseGetInstallationId call", "remove .parse-embedded from home directory");

    parseSendRequest(client, "GET", "/classes/testObjectFake/1111111", NULL, NULL);

    id = parseGetInstallationId(client);
    logResults(id != NULL, 1, "parseGetInstallationId call", "did not create the installation id properly");

    logResults(verifyInstallationId(id), 0, "installation id generated correctly", "installation id is not correct");

    // TEST CREATING AND FETCHING OBJECTS ON/FROM THE SERVER

    clearCachedResults(); 

    parseSendRequest(client, "POST", classPathOne, "{\"test\":\"object1\", \"value\":1}", callback);   
    logResults(cachedRunResult != NULL, 0, "create test object 1", "creating object failed");
    memset(objectId, 0, sizeof(objectId));
    logResults(simpleJsonProcessor(cachedRunResult, "objectId", objectId, sizeof(objectId)), 0, "object 1 created", "could not create an object");

    clearCachedResults();

    memset(path, 0, sizeof(path));
    snprintf(path, sizeof(path), "%s/%s", classPathOne, objectId);
    parseSendRequest(client, "GET", classPathOne, NULL, callback);   
    logResults(cachedRunResult != NULL, 0, "fetch test object 1", "fetching object failed");

    clearCachedResults(); 

    parseSendRequest(client, "POST", classPathOne, "{\"test\":\"object1\", \"value\":2}", callback);   
    logResults(cachedRunResult != NULL, 0, "create test object 2", "creating object failed");
    memset(objectId, 0, sizeof(objectId));
    logResults(simpleJsonProcessor(cachedRunResult, "objectId", objectId, sizeof(objectId)), 0, "object 2 created", "could not create an object");
    
    clearCachedResults();

    memset(path, 0, sizeof(path));
    snprintf(path, sizeof(path), "%s/%s", classPathOne, objectId);
    parseSendRequest(client, "GET", classPathOne, NULL, callback);   
    logResults(cachedRunResult != NULL, 0, "fetch test object 2", "fetching object failed");

    clearCachedResults(); 

    parseSendRequest(client, "POST", classPathOne, "{\"test\":\"object1\", \"value\":3}", callback);   
    logResults(cachedRunResult != NULL, 0, "create test object 3", "creating object failed");
    char objectIdKeepAround[11] = {0};
    logResults(simpleJsonProcessor(cachedRunResult, "objectId", objectIdKeepAround, sizeof(objectIdKeepAround)), 0, "object 3 created", "could not create an object");

    clearCachedResults();

    memset(path, 0, sizeof(path));
    snprintf(path, sizeof(path), "%s/%s", classPathOne, objectIdKeepAround);
    parseSendRequest(client, "GET", classPathOne, NULL, callback);   
    logResults(cachedRunResult != NULL, 0, "fetch test object 3", "fetching object failed");

    clearCachedResults(); 

    parseSendRequest(client, "POST", classPathOne, "{\"test\":\"object1\", \"value\":2}", callback);   
    logResults(cachedRunResult != NULL, 0, "create test object 4", "creating object failed");
    memset(objectId, 0, sizeof(objectId));
    logResults(simpleJsonProcessor(cachedRunResult, "objectId", objectId, sizeof(objectId)), 0, "object 4 created", "could not create an object");
    
    clearCachedResults();

    memset(path, 0, sizeof(path));
    snprintf(path, sizeof(path), "%s/%s", classPathOne, objectId);
    parseSendRequest(client, "GET", classPathOne, NULL, callback);   
    logResults(cachedRunResult != NULL, 0, "fetch test object 4", "fetching object failed");

    // TEST QUERIES

    clearCachedResults();

    parseSendRequest(client, "GET", classPathOne, "where={\"value\":2}", callback);   
    logResults(cachedRunResult != NULL, 0, "query objects", "querying objects failed");

    const char* results = "{\"results\":[{\"";
    int cmp = !strncmp(cachedRunResult, results, strlen(results));
    char* location1 = strstr(cachedRunResult, "objectId");
    char* location2 = strstr(location1 + 1, "objectId");
    char* location3 = strstr(location2 + 1, "objectId");
    logResults(cmp && location1 && location2 && !location3, 0, "query value", "query results not valid");


    // TEST OBJECT MODIFICATION

    clearCachedResults(); 

    memset(path, 0, sizeof(path));
    snprintf(path, sizeof(path), "%s/%s", classPathOne, objectIdKeepAround);

    parseSendRequest(client, "PUT", path, "{\"test\":\"object1\", \"value\":2}", callback);   
    logResults(cachedRunResult != NULL, 0, "modify test object 3", "modifying object failed");

    clearCachedResults();

    parseSendRequest(client, "GET", classPathOne, "where={\"value\":2}", callback);   
    logResults(cachedRunResult != NULL, 0, "query objects after modification", "querying objects failed");

    results = "{\"results\":[{\"";
    cmp = !strncmp(cachedRunResult, results, strlen(results));
    location1 = strstr(cachedRunResult, "objectId");
    location2 = strstr(location1 + 1, "objectId");
    location3 = strstr(location2 + 1, "objectId");
    logResults(cmp && location1 && location2 && location3, 0, "query value after modifying an object", "query results not valid");

    // TEST DELETING AN OBJECT

    clearCachedResults(); 

    memset(path, 0, sizeof(path));
    snprintf(path, sizeof(path), "%s/%s", classPathOne, objectIdKeepAround);

    parseSendRequest(client, "DELETE", path, NULL, callback);   
    logResults(cachedRunResult != NULL, 0, "delete test object 3", "deleting object failed");

    clearCachedResults();

    parseSendRequest(client, "GET", classPathOne, "where={\"value\":2}", callback);   
    logResults(cachedRunResult != NULL, 0, "query objects after delete", "querying objects failed");

    results = "{\"results\":[{\"";
    cmp = !strncmp(cachedRunResult, results, strlen(results));
    location1 = strstr(cachedRunResult, "objectId");
    location2 = strstr(location1 + 1, "objectId");
    location3 = strstr(location2 + 1, "objectId");
    logResults(cmp && location1 && location2 && !location3, 0, "query value after deleting an object", "query results not valid");

    // TEST PUSH

    parseSetPushCallback(client, pushCallback);
    parseStartPushService(client);
    int socket = parseGetPushSocket(client);

    int loopCount = 20;


    printf("[!!!!!!!] Run ./push.sh %s %ld\n", id, run);

    while(loopCount--) {
        struct timeval tv;
        fd_set receive, send, error;

        tv.tv_sec = 10;
        tv.tv_usec= 0;
        FD_ZERO(&receive);
        FD_ZERO(&send);
        FD_ZERO(&error);
        FD_SET(socket, &error);
        FD_SET(socket, &receive);
        select(socket + 1, &receive, &send, &error, &tv);

        parseProcessNextPushNotification(client);

        if (pushCounter == 10) loopCount = loopCount > 2 ? 2 : loopCount;
    }

    logResults(pushCounter == 10, 0, "receive push notifications", "did not receive the push notifications correctly");

    logSummary();
    return 0;
}