Example #1
0
/**
 * The main update method of the controller, called from
 * main.cpp. This sends all the current variables to the GUI, so that
 * they can be updated on the screen if necessary.
 */
void Controller::update() {
  check_warning_level();
  m_keytrigger=false;


  // only do_logging if we are enable
if( m_log_interval_seconds > 0)
    do_logging();

  check_sleep_switch();

  if(m_sleeping) return;

  do_dimming();

  send_cpm_values();
  send_graph_data();
  send_total_timer();
  send_svrem();
  send_becq();
  send_logstatus();
}
Example #2
0
void
handle_client(int fd) {
    int     r;
    int     i;
    char   *p;
    static char buffer[LEN_BUFFER];
    DB_ADAPTER *db;

    // open the database
    if ((db = open_db()) == NULL)
        return;

    // read socket
    if ((r = read(fd, buffer, sizeof(buffer))) < 0) {
        printf("read() failed\n");
        return;
    }

    // terminate received string
    if (r > 0 && r < sizeof(buffer))
        buffer[r] = '\0';
    else
        buffer[0] = '\0';


    // filter requests we don't support
    if (strncmp(buffer, "GET ", 4) && strncmp(buffer, "POST ", 5)) {
        send_error(fd, "not supported (only GET and POST)");
        return;
    }

    // look for second space (or newline) and terminate string (skip headers)
    if ((p = strchr(buffer, ' ')) != NULL) {
        for (;;) {
            p++;
            if (*p == '\r' || *p == '\n' || *p == ' ') {
                *p = '\0';
                break;
            }
        }
    }
    else {
        send_error(fd, "invalid request.\n");
        return;
    }

    // convert / to index.html
    if (!strncmp(buffer, "GET /\0", 6))
        strncpy(buffer, "GET /index.html", sizeof(buffer));


    // check for illegal parent directory requests
    for (i = 0;; i++) {
        if (buffer[i] == '\0' || buffer[i + 1] == '\0')
            break;

        if (buffer[i] == '.' && buffer[i + 1] == '.') {
            send_error(fd, ".. detected.\n");
            return;
        }
    }

    // point p to filename
    if ((p = strchr(buffer, '/')) == NULL)
        return;

#ifdef DEBUG_AJAX
    printf("requested: %s\n", p);
#endif

    // send json data
    if (!strncmp(p, "/data.json", 10))
        send_data(fd, db);
    else if (!strncmp(p, "/averages.json", 14))
        send_averages(fd, db);
    else if (!strncmp(p, "/consumption.json", 17))
        send_graph_data(fd, db, "consumption_per_100km", buffer);
    else if (!strncmp(p, "/speed.json", 11))
        send_graph_data(fd, db, "speed", buffer);
    else if (!strncmp(p, "/gps_altitude.json", 18))
        send_graph_data(fd, db, "gps_altitude", buffer);

    // send file
    else if (send_file(fd, p) != 0)
        send_error(fd, "could not send file.\n");


    close_db(db);
    return;
}