void processinput() { const char *inptr( g_iobuf ); Serial.print(CMNT); Serial.println( g_iobuf ); if( iscommand( inptr, F("dl"))) { g_logger.dump( &Serial ); } else if( iscommand( inptr, F("tl"))) { // truncate log g_logger.truncate(); } else if( iscommand( inptr, F("get") )) { // get database::dbrecord rec; int id( getintparam( inptr )); if( id != -1 && g_db.getParams( id, rec )) { rec.serialize( g_iobuf ); serialoutln( RESP, g_iobuf ); } else Serial.println( F(ERRS "ERR")); } else if( iscommand( inptr, F("set") )) { // set database::dbrecord rec; int id( getintparam( inptr )); if( id != -1 && rec.parse( inptr )) { if( g_db.setParams( id, rec )) Serial.println( F( RESPS "OK")); else Serial.println( F(ERRS "ERR")); } else Serial.println( F(ERRS "ERR")); } else if( iscommand( inptr, F("imp"))) { // import thindb tdb( g_sd ); uint16_t from( getintparam( inptr )); uint16_t to( getintparam( inptr )); if( from == 0xffff ) from = 0; if( to == 0xffff ) to = 1023; uint16_t imported(importdb(from, to)); if( imported != (uint16_t)-1 ) { serialoutln(F(RESPS "OK "), imported); } else serialoutln(F(ERRS "ERR "), imported); } else if( iscommand( inptr, F("dmp"))) { // dump database::dbrecord rec; uint16_t from( getintparam( inptr )); uint16_t to( getintparam( inptr )); uint16_t id; g_iobuf[3] = ' '; if( from == 0xffff ) from = 0; if( to == 0xffff ) to = 1023; for( id = from; id <= to; ++id ) { if( g_db.getParams( id, rec )) { uitohex( g_iobuf, id, 3 ); rec.serialize( g_iobuf + 4 ); serialoutln( RESP, g_iobuf ); } else break; } if( id == to + 1 ) Serial.println( RESP ); else Serial.println( F(ERRS "ERR" )); } else { Serial.println( F(ERRS "CMD")); } g_inidx = 0; }
void setup() { bool loginit(false); bool sdinit(false); bool dbinit(false); uint8_t tlpins[] = { INNER_LIGHTS_PINS, OUTER_LIGHTS_PINS }; Serial.begin( BAUDRATE ); #ifdef VERBOSE delay(10); Serial.print(CMNT); for( char c = 0; c < 79; ++c ) Serial.print('>'); Serial.println(); #endif I2c.begin(); I2c.timeOut(1000); g_display.init(); // calls Wire.begin() g_display.print( freeMemory() ); g_loop.init( PIN_INNERLOOP, PIN_OUTERLOOP, LOOP_ACTIVE ); setup433(); g_codeready = false; g_code = 0; #ifdef USE_IOEXTENDER_OUTPUTS g_outputs.set(0xff); #else { uint8_t all_output_pins[8] = { ALL_RAW_OUTPUT_PINS }; g_outputs.init(all_output_pins, RELAY_OFF); } #endif if((sdinit = g_sd.begin( SS, SPI_HALF_SPEED ))) { if( !(loginit = g_logger.init()) ) Serial.println(F("Logger fail")); } else Serial.println(F("SD fail")); dbinit = g_db.init(); g_display.print( ' ' ); g_display.print( sdinit ); g_display.print( ' ' ); g_display.print( loginit ); g_display.print( ' ' ); g_display.print( dbinit ); //runlight for( uint8_t pin = 0; pin < sizeof(tlpins) + 3; ++pin ) { if(pin < sizeof(tlpins)) { g_outputs.set( tlpins[pin], RELAY_ON); } if(pin > 2) g_outputs.set(tlpins[pin-3], RELAY_OFF); delay(150); } if( !loginit ) { delay(100); for( int i = 0; i < 3; ++i ) { g_outputs.set( PIN_IN_RED, RELAY_ON ); g_outputs.set( PIN_OUT_RED, RELAY_ON ); delay( 500 ); g_outputs.set( PIN_IN_RED, RELAY_OFF ); g_outputs.set( PIN_OUT_RED, RELAY_OFF ); delay( 500 ); } } g_clk.init( DS3231_INTCN ); g_timevalid = updatedt(); #ifdef VERBOSE Serial.print(CMNT); Serial.println(F("DS3231 init done.")); #endif g_logger.log( logwriter::INFO, g_time, F("Reset") ); g_display.clear(); if(sdinit) { SdFile f; if(f.open("IMPORT")) { f.close(); g_display.print(F("IMPORTING ")); uint16_t imported(importdb(0, 1023)); if(imported != (uint16_t) -1) { g_display.print(imported); g_sd.remove("IMPORT"); } else g_display.print(F("FAIL")); delay(2000); g_display.clear(); } } #ifdef VERBOSE Serial.print(CMNT); for( char c = 0; c < 79; ++c ) Serial.print('<'); Serial.println(); #endif }
END_TEST START_TEST(importdb_can_parse_exported_database) { int i; char buffer[512]; DATA filleddb; FILE *exportfile; initdb(); strcpy(data.interface, "something"); strcpy(data.nick, "nothing"); data.totalrx = 1; data.totaltx = 2; data.currx = 3; data.curtx = 4; data.totalrxk = 5; data.totaltxk = 6; data.btime = 7; for (i=0; i<30; i++) { data.day[i].date = i+1; data.day[i].rx = data.day[i].tx = i*100; data.day[i].rxk = data.day[i].txk = i; data.day[i].used = 1; } for (i=0; i<10; i++) { data.top10[i].date = i+1; data.top10[i].rx = data.top10[i].tx = i*100; data.top10[i].rxk = data.top10[i].txk = i; data.top10[i].used = 1; } for (i=0; i<12; i++) { data.month[i].month = i+1; data.month[i].rx = data.month[i].tx = i*100; data.month[i].rxk = data.month[i].txk = i; data.month[i].used = 1; } for (i=0; i<24; i++) { data.hour[i].date = i+1; data.hour[i].rx = data.hour[i].tx = i*100; } memcpy(&filleddb, &data, sizeof(DATA)); ck_assert_int_eq(remove_directory(TESTDIR), 1); ck_assert_int_eq(clean_testdbdir(), 1); fflush(stdout); snprintf(buffer, 512, "%s/dbexport", TESTDBDIR); exportfile = fopen(buffer, "w"); dup2(fileno(exportfile), STDOUT_FILENO); fclose(exportfile); exportdb(); fflush(stdout); memset(&data, '\0', sizeof(DATA)); ck_assert_int_gt(importdb(buffer), 0); ck_assert_str_eq(data.interface, filleddb.interface); ck_assert_str_eq(data.nick, filleddb.nick); ck_assert_int_eq(data.version, filleddb.version); ck_assert_int_eq(data.active, filleddb.active); ck_assert_int_eq(data.totalrx, filleddb.totalrx); ck_assert_int_eq(data.totaltx, filleddb.totaltx); ck_assert_int_eq(data.currx, filleddb.currx); ck_assert_int_eq(data.curtx, filleddb.curtx); ck_assert_int_eq(data.totalrxk, filleddb.totalrxk); ck_assert_int_eq(data.totaltxk, filleddb.totaltxk); ck_assert_int_eq(data.btime, filleddb.btime); ck_assert_int_eq(data.created, filleddb.created); ck_assert_int_eq(data.lastupdated, filleddb.lastupdated); for (i=0; i<30; i++) { ck_assert_int_eq(data.day[i].date, filleddb.day[i].date); ck_assert_int_eq(data.day[i].rx, filleddb.day[i].rx); ck_assert_int_eq(data.day[i].tx, filleddb.day[i].tx); ck_assert_int_eq(data.day[i].rxk, filleddb.day[i].rxk); ck_assert_int_eq(data.day[i].txk, filleddb.day[i].txk); ck_assert_int_eq(data.day[i].used, filleddb.day[i].used); } for (i=0; i<10; i++) { ck_assert_int_eq(data.top10[i].date, filleddb.top10[i].date); ck_assert_int_eq(data.top10[i].rx, filleddb.top10[i].rx); ck_assert_int_eq(data.top10[i].tx, filleddb.top10[i].tx); ck_assert_int_eq(data.top10[i].rxk, filleddb.top10[i].rxk); ck_assert_int_eq(data.top10[i].txk, filleddb.top10[i].txk); ck_assert_int_eq(data.top10[i].used, filleddb.top10[i].used); } for (i=0; i<12; i++) { ck_assert_int_eq(data.month[i].month, filleddb.month[i].month); ck_assert_int_eq(data.month[i].rx, filleddb.month[i].rx); ck_assert_int_eq(data.month[i].tx, filleddb.month[i].tx); ck_assert_int_eq(data.month[i].rxk, filleddb.month[i].rxk); ck_assert_int_eq(data.month[i].txk, filleddb.month[i].txk); ck_assert_int_eq(data.month[i].used, filleddb.month[i].used); } for (i=0; i<24; i++) { ck_assert_int_eq(data.hour[i].date, filleddb.hour[i].date); ck_assert_int_eq(data.hour[i].rx, filleddb.hour[i].rx); ck_assert_int_eq(data.hour[i].tx, filleddb.hour[i].tx); } }