void jsmn_find_extract_token(const char * string, const jsmntok_t * tokens, const word object_index, const char * search, char * result, const size_t max_length) { // Search the object at token number object_index for a name of value search, and return the value of the next token. size_t index = jsmn_find_name_token(string, tokens, object_index, search); if(index > 0 && tokens[index + 1].start >= 0) { jsmn_extract_token(string, tokens, index + 1, result, max_length); } else { result[0] = '\0'; } }
static void process_create_schedule(const char * string, const jsmntok_t * tokens, const word update) { // update true indicates this is as the result of a VSTP update. char zs[128], zs1[128]; char query[2048]; word i; char uid[16], stp_indicator[2]; char signalling_id[8]; if(debug) jsmn_dump_tokens(string, tokens, 0); time_t now = time(NULL); sprintf(query, "INSERT INTO cif_schedules VALUES(0, %ld, %lu", now, NOT_DELETED); // update_id == 0 => VSTP EXTRACT_APPEND_SQL("CIF_bank_holiday_running"); //EXTRACT_APPEND_SQL("CIF_stp_indicator"); EXTRACT("CIF_stp_indicator", stp_indicator); sprintf(zs1, ", '%s'", stp_indicator); strcat(query, zs1); //EXTRACT_APPEND_SQL("CIF_train_uid"); EXTRACT("CIF_train_uid", uid); sprintf(zs1, ", '%s'", uid); strcat(query, zs1); EXTRACT_APPEND_SQL("applicable_timetable"); EXTRACT_APPEND_SQL("atoc_code"); // EXTRACT_APPEND_SQL("traction_class"); EXTRACT_APPEND_SQL("uic_code"); EXTRACT("schedule_days_runs", zs); for(i=0; i<7; i++) { strcat(query, ", "); strcat(query, (zs[i]=='1')?"1":"0"); } EXTRACT("schedule_end_date", zs); time_t z = parse_datestamp(zs); sprintf(zs1, ", %ld", z); strcat(query, zs1); EXTRACT("signalling_id", signalling_id); sprintf(zs1, ", '%s'", signalling_id); strcat(query, zs1); EXTRACT_APPEND_SQL("CIF_train_category"); EXTRACT_APPEND_SQL("CIF_headcode"); //EXTRACT_APPEND_SQL("CIF_course_indicator"); EXTRACT_APPEND_SQL("CIF_train_service_code"); EXTRACT_APPEND_SQL("CIF_business_sector"); EXTRACT_APPEND_SQL("CIF_power_type"); EXTRACT_APPEND_SQL("CIF_timing_load"); EXTRACT_APPEND_SQL("CIF_speed"); EXTRACT_APPEND_SQL("CIF_operating_characteristics"); EXTRACT_APPEND_SQL("CIF_train_class"); EXTRACT_APPEND_SQL("CIF_sleepers"); EXTRACT_APPEND_SQL("CIF_reservations"); EXTRACT_APPEND_SQL("CIF_connection_indicator"); EXTRACT_APPEND_SQL("CIF_catering_code"); EXTRACT_APPEND_SQL("CIF_service_branding"); EXTRACT("schedule_start_date", zs); z = parse_datestamp(zs); sprintf(zs1, ", %ld", z); strcat(query, zs1); EXTRACT_APPEND_SQL("train_status"); strcat(query, ", 0, '', '')"); // id filled by MySQL if(!db_query(query)) { stats[update?UpdateCreate:Create]++; } dword id = db_insert_id(); word index = jsmn_find_name_token(string, tokens, 0, "schedule_location"); word locations = tokens[index+1].size; huyton_flag = false; index += 2; for(i = 0; i < locations; i++) { index = process_create_schedule_location(string, tokens, index, id); } if(stp_indicator[0] == 'O' && (signalling_id[0] == '\0' || signalling_id[0] == ' ')) { // Search db for schedules with a deduced headcode, and add it to this one, status = D // Bug: Really this should also look for schedules with a signalling_id MYSQL_RES * result; MYSQL_ROW row; sprintf(query, "SELECT deduced_headcode FROM cif_schedules WHERE CIF_train_uid = '%s' AND deduced_headcode != '' AND schedule_end_date > %ld ORDER BY created DESC", uid, now - (64L * 24L * 60L * 60L)); if(!db_query(query)) { result = db_store_result(); if((row = mysql_fetch_row(result))) { sprintf(query, "UPDATE cif_schedules SET deduced_headcode = '%s', deduced_headcode_status = 'D' WHERE id = %ld", row[0], id); db_query(query); _log(DEBUG, "Deduced headcode \"%s\" applied to overlay schedule %ld, uid \"%s\".", row[0], id, uid); stats[HeadcodeDeduced]++; } else { _log(DEBUG, "Deduced headcode not found for overlay schedule %ld, uid \"%s\".", id, uid); } mysql_free_result(result); } } sprintf(query, "UPDATE status SET last_vstp_processed = %ld", now); db_query(query); if(huyton_flag) { _log(DEBUG, "Created schedule %ld%s. +++ Passes Huyton +++", id, update?" as a result of an Update transaction":""); } if(huyton_flag) { char title[64], message[512]; MYSQL_RES * result0; MYSQL_ROW row0; char stp[4]; sprintf(title, "Huyton Schedule Created."); sprintf(message, "Created schedule which passes Huyton."); if(update) strcat(message, " Due to a VSTP Update transaction."); strcat(message, "\n\n"); EXTRACT("CIF_train_uid", zs1); EXTRACT("CIF_stp_indicator", stp); sprintf(zs, "%ld (%s %s) ", id, zs1, stp); EXTRACT("signalling_id", zs1); strcat(zs, zs1); sprintf(query, "SELECT tiploc_code, departure FROM cif_schedule_locations WHERE record_identity = 'LO' AND cif_schedule_id = %ld", id); if(!db_query(query)) { result0 = db_store_result(); if((row0 = mysql_fetch_row(result0))) { sprintf(zs1, " %s %s to ", show_time_text(row0[1]), tiploc_name(row0[0])); strcat(zs, zs1); } mysql_free_result(result0); } sprintf(query, "SELECT tiploc_code FROM cif_schedule_locations WHERE record_identity = 'LT' AND cif_schedule_id = %ld", id); if(!db_query(query)) { result0 = db_store_result(); if((row0 = mysql_fetch_row(result0))) { strcat (zs, tiploc_name(row0[0])); } mysql_free_result(result0); } strcat(message, zs); sprintf(query, "SELECT schedule_start_date, schedule_end_date, CIF_stp_indicator FROM cif_schedules WHERE id = %ld", id); if(!db_query(query)) { result0 = db_store_result(); if((row0 = mysql_fetch_row(result0))) { dword from = atol(row0[0]); dword to = atol(row0[1]); if(from == to) { strcat(message, " Runs on "); strcat(message, date_text(from, true)); } else { strcat(message, " Runs from "); strcat(message, date_text(from, true)); strcat(message, " to "); strcat(message, date_text(to, true)); } if(row0[2][0] == 'C') strcat(message, " CANCELLED"); strcat(message, "\n"); } mysql_free_result(result0); } sprintf(query, "SELECT departure, arrival, pass, tiploc_code FROM cif_schedule_locations WHERE (tiploc_code = 'HUYTON' OR tiploc_code = 'HUYTJUN') AND cif_schedule_id = %ld", id); if(!db_query(query)) { result0 = db_store_result(); while((row0 = mysql_fetch_row(result0))) { char where[32], z[128]; if(row0[3][4] == 'J') strcpy(where, "Huyton Junction"); else strcpy(where, "Huyton"); if(row0[0][0]) { sprintf(z, "Depart %s at %s.\n", where, row0[0]); strcat(message, z); } else if(row0[1][0]) { sprintf(z, "Arrive %s at %s.\n", where, row0[1]); strcat(message, z); } else if(row0[2][0]) { sprintf(z, "Pass %s at %s.\n", where, row0[2]); strcat(message, z); } } mysql_free_result(result0); } email_alert(NAME, BUILD, title, message); } }