示例#1
0
static bool 
serviceResponse(LSHandle *sh, LSMessage *reply, void *ctx)
{
    LSError lserror;
    LSErrorInit(&lserror);
    LSMessageToken token;

    const char *payload;
    bool free_payload = false;

    token = LSMessageGetResponseToken(reply);
    payload = LSMessageGetPayload(reply);

    //g_message("%s Handling: %ld, %s", __FUNCTION__, token, payload);
    
    if (line_number) {
      printf("%2d: ", current_line_number++);
    }
    
    if (query_list != NULL) {
      // Use set of queries to transform original object into reduced form that
      // only contains queried selections -- then pass that through normal formatting.
      struct json_object *original = json_tokener_parse(payload);
      struct json_object *new_object = json_object_new_object();
      GList * query = query_list;
      if ( original && !is_error(original) ) {
        while (query) {
          char * query_text = (char*)query->data;
          struct json_object * result = apply_query(original, query_text);
          json_object_object_add(new_object, query_text, result);
          query = query->next;
        }
        payload = strdup(json_object_get_string(new_object));
        free_payload = true;
        json_object_put(new_object);
      }
    }
    
    if (format_response) {
      struct json_object *object = json_tokener_parse(payload);
      if ( !object || is_error(object) ) {
        // fall back to plain print
        printf("%s\n", payload);
      } else {
        pretty_print(object, 0, line_number ? 4 /* expected characters in line numbers */ : 0);
        printf("\n");
        json_object_put(object);
      }
    } else {
      printf("%s\n", payload);
    }
    
    if (free_payload)
      free((void*)payload);
    
    fflush(stdout);

    if (--count == 0)
    {
        bool retVal = LSCallCancel (sh, token, &lserror);
        if (!retVal)
        {
            LSErrorPrint (&lserror, stderr);
            LSErrorFree (&lserror);
        }
        g_timeout_add (300, goodbye, ctx);
        return true;
    }

    return true;
}
示例#2
0
int main() {
    llist entries;
    int status;
    char error[128];

    mime_header("application/json");
    fflush(stdout);

    obj = json_object_new_object();

    status = read_cgi_input(&entries);

    char *act = cgi_val(entries, "act");
    if (!act) {
        sprintf(error, "%s", "act required!");
        json_object_object_add(obj, "error", json_object_new_string(error));
        goto end;
    }

    if (STRCMP(act, "apply_ping"))    apply_ping();



    char *dev = cgi_val(entries, "dev");
    if (!dev) {
        sprintf(error, "%s", "dev required!");
        json_object_object_add(obj, "error", json_object_new_string(error));
        goto end;
    }

    int dev_num = atoi(dev);
    if (dev_num < 0 || dev_num >= MAX_DEV_NUM) {
        sprintf(error, "dev_num(%d) out of reange!", dev_num);
        json_object_object_add(obj, "error", json_object_new_string(error));
        goto end;
    }

    if (STRCMP(act, "apply_on"))    apply_on(dev_num);
    if (STRCMP(act, "apply_off"))   apply_off(dev_num);
    if (STRCMP(act, "apply_query")) apply_query(dev_num);



    if (STRCMP(act, "apply_delay")) {
    char *delay = cgi_val(entries, "delay");
        if (!delay) {
            sprintf(error, "%s", "delay required!");
            json_object_object_add(obj, "error", json_object_new_string(error));
            goto end;
        }
        apply_delay(dev_num, atoi(delay));
    }



    if (STRCMP(act, "apply_time")) {

        char *year = cgi_val(entries, "year");
        if (!year) {
            sprintf(error, "%s", "year required!");
            json_object_object_add(obj, "error", json_object_new_string(error));
            goto end;
        }
        int _year = atoi(year);

        char *month = cgi_val(entries, "month");
        if (!month) {
            sprintf(error, "%s", "month required!");
            json_object_object_add(obj, "error", json_object_new_string(error));
            goto end;
        }
        int _month = atoi(month);

        char *day = cgi_val(entries, "day");
        if (!day) {
            sprintf(error, "%s", "day required!");
            json_object_object_add(obj, "error", json_object_new_string(error));
            goto end;
        }
        int _day = atoi(day);

        char *hour = cgi_val(entries, "hour");
        if (!hour) {
            sprintf(error, "%s", "hour required!");
            json_object_object_add(obj, "error", json_object_new_string(error));
            goto end;
        }
        int _hour = atoi(hour);

        char *minute = cgi_val(entries, "minute");
        if (!minute) {
            sprintf(error, "%s", "minute required!");
            json_object_object_add(obj, "error", json_object_new_string(error));
            goto end;
        }
        int _minute = atoi(minute);

        char *second = cgi_val(entries, "second");
        if (!second) {
            sprintf(error, "%s", "second required!");
            json_object_object_add(obj, "error", json_object_new_string(error));
            goto end;
        }
        int _second = atoi(second);

        apply_time(dev_num, _year, _month, _day, _hour, _minute, _second);
    }



    if (STRCMP(act, "apply_schedule")) {

        char *index = cgi_val(entries, "index");
        if (!index) {
            sprintf(error, "%s", "index required!");
            json_object_object_add(obj, "error", json_object_new_string(error));
            goto end;
        }
        int _index = atoi(index);

        char *hour_start = cgi_val(entries, "hour_start");
        if (!hour_start) {
            sprintf(error, "%s", "hour_start required!");
            json_object_object_add(obj, "error", json_object_new_string(error));
            goto end;
        }
        int _hour_start = atoi(hour_start);

        char *minute_start = cgi_val(entries, "minute_start");
        if (!minute_start) {
            sprintf(error, "%s", "minute_start required!");
            json_object_object_add(obj, "error", json_object_new_string(error));
            goto end;
        }
        int _minute_start = atoi(minute_start);

        char *hour_end = cgi_val(entries, "hour_end");
        if (!hour_end) {
            sprintf(error, "%s", "hour_end required!");
            json_object_object_add(obj, "error", json_object_new_string(error));
            goto end;
        }
        int _hour_end = atoi(hour_end);

        char *minute_end = cgi_val(entries, "minute_end");
        if (!minute_end) {
            sprintf(error, "%s", "minute_end required!");
            json_object_object_add(obj, "error", json_object_new_string(error));
            goto end;
        }
        int _minute_end = atoi(minute_end);

        apply_schedule(dev_num, _index, _hour_start, _minute_start, _hour_end, _minute_end);
    }






    if (STRCMP(act, "get_status"))  get_status(dev_num);

    sprintf(error, "act(%s) not support!", act);
    json_object_object_add(obj, "error", json_object_new_string(error));

end:
    printf("%s", json_object_to_json_string(obj));
    json_object_put(obj);
    return 0;
}