#include "plt.h" #include "appl.h" static struct timeseg_t s_timeseg[TIMESEG_NBR_MAX + 1]; static int timeseg_dbcb_0(void *para,int ncolumn,char ** columnvalue,char *columnname[]) { int i; struct dbcbparam_t* pcbparam = (struct dbcbparam_t*)para; int idx = pcbparam->idx; struct timeseg_t* ts = &s_timeseg[idx]; int hh, nn; int val; pcbparam->nrow++; //log_dbg("%s, ++, row:%d; col:%d",__func__,pcbparam->nrow,ncolumn); hh = 0; nn = 0; val = 0; for( i = 0; i < ncolumn; i++){ if( strcmp("hour",columnname[i]) == 0){ hh = atoi(columnvalue[i]); }else if( strcmp("minute",columnname[i]) == 0){ nn = atoi(columnvalue[i]); }else if( strcmp("segtype",columnname[i]) == 0){ val = atoi(columnvalue[i]); } } if( hh>=0 && hh<=23 && nn>=0 && nn<=59 ){ ts->seg_type[hh][nn] = val; }else{ pcbparam->ret = -1; } //log_dbg("%s, --,ret:%d",__func__,pcbparam->ret); return 0; } int timeseg_load(int idx) { int ret = 0; int rc; char *errmsg = NULL; sqlite3* db = NULL; char sql[1024]; struct dbcbparam_t cbparam; plt_lock_ctndb(); db = plt_get_ctndb(); sprintf(sql,"select * from timeseg where idx=%d", idx); cbparam.nrow = 0; cbparam.ret = 0; cbparam.idx = idx; rc = sqlite3_exec(db,sql, timeseg_dbcb_0,(void*)&cbparam,&errmsg); plt_unlock_ctndb(); if( rc != SQLITE_OK ){ log_dbg("%s, result != SQLITE_OK, idx:%d", __func__, idx ); ret = -1; timeseg_set_status(idx, TIMESEGST_ERR); }else if( cbparam.ret != 0){ log_dbg("%s, cbparam.ret != 0, idx:%d", __func__, idx ); ret = -1; timeseg_set_status(idx, TIMESEGST_ERR); }else{ if( cbparam.nrow != TIMESEG_PTS_NBR ){ log_dbg("%s, idx:%d, cbparam.nrow(%d) != %d", __func__, idx, cbparam.nrow, TIMESEG_PTS_NBR ); ret = -1; timeseg_set_status(idx, TIMESEGST_ERR); }else{ timeseg_set_status(idx, TIMESEGST_VALID); } } log_dbg("%s, idx:%d, ret:%d", __func__, idx, ret); return ret; } int timeseg_set_status( int pidx, int newstat ) { struct timeseg_t* ts = &s_timeseg[pidx]; ts->status = newstat; switch( newstat ){ case TIMESEGST_ERR: strcpy(ts->szstatus, "err"); break; case TIMESEGST_VALID: strcpy(ts->szstatus, "valid"); break; case TIMESEGST_RESET: strcpy(ts->szstatus, "reset"); break; default: strcpy(ts->szstatus, "unkown"); break; } return 0; } struct timeseg_t timeseg_get(int idx) { return s_timeseg[idx]; } int timeseg_set(int idx, struct timeseg_t val) { s_timeseg[idx] = val; } void timeseg_reset( int idx ) { int rc; char *errmsg = NULL; sqlite3* db = NULL; char sql[1024]; int hh, nn; int aps; plt_lock_ctndb(); db = plt_get_ctndb(); /* delete */ sprintf(sql,"delete from timeseg where idx=%d", idx); rc = sqlite3_exec(db,sql,NULL,NULL,&errmsg); plt_unlock_ctndb(); if( rc != SQLITE_OK ){ log_dbg("%s, sqlite3_exec fail:%d, when %s", __func__, rc, sql ); timeseg_set_status(idx, TIMESEGST_ERR); return; } /* insert */ hh = 0; nn = 0; aps = 0.0; while( hh < 24 ){ while( nn < 60 ){ sprintf(sql,"insert into timeseg VALUES(%d,%d,%d,%d)",idx,hh,nn,aps); rc = sqlite3_exec(db,sql,NULL,NULL,&errmsg); if( rc != SQLITE_OK ){ log_dbg("%s, idx : %d, sqlite3_exec fail:%d, when %s", __func__, idx, rc, sql ); timeseg_set_status(idx, TIMESEGST_ERR); return; } nn += 60/TIMESEG_PTS_PERHOUR; } nn = 0; hh++; } /* load */ timeseg_load(idx); return; } int timeseg_save( int idx, struct timeseg_t val ) { int ret = 0; int i, rc; char *errmsg = NULL; sqlite3* db = NULL; struct timeseg_t* ts = NULL; int hh; int nn; char sql[1024]; log_dbg("%s, ++, idx:%d", __func__, idx); plt_lock_ctndb(); db = plt_get_ctndb(); s_timeseg[idx] = val; ts = &s_timeseg[idx]; hh = 0; nn = 0; while( hh < 24 ){ while( nn < 60 ){ sprintf(sql,"update timeseg set segtype=%d where idx=%d and hour=%d and minute=%d", ts->seg_type[hh][nn], idx, hh, nn); rc = sqlite3_exec(db,sql,NULL,NULL,&errmsg); if( rc != SQLITE_OK ){ log_dbg("%s, idx : %d, sqlite3_exec fail:%d, when %s", __func__, idx, rc, sql ); timeseg_set_status(idx, TIMESEGST_ERR); ret = -1; goto leave; } nn += 60/TIMESEG_PTS_PERHOUR; } nn = 0; hh++; } leave: plt_unlock_ctndb(); log_dbg("%s, idx:%d, --, ret:%d", __func__, idx, ret); return ret; } char* timeseg_segtype2string(int segtype) { if( segtype == TIMESEGTYPE_SHARP ){ return "sharp"; }else if( segtype == TIMESEGTYPE_PEAK ){ return "peak"; }else if( segtype == TIMESEGTYPE_FLAT ){ return "flat"; }else if( segtype == TIMESEGTYPE_VALLEY ){ return "valley"; }else{ return "unkown"; } } int timeseg_init() { }