#include "plt.h" #include "appl.h" static struct pcurv_t s_pcurv[PCURV_NBR_MAX + 1]; static int _pcurv_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 pcurv_t *pc = &s_pcurv[idx]; int hh, nn; int aps; pcbparam->nrow++; // log_dbg("%s, ++, row:%d; col:%d",__func__,pcbparam->nrow,ncolumn); hh = 0; nn = 0; aps = 0.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("aps", columnname[i]) == 0) { aps = atoi(columnvalue[i]); } } if (hh >= 0 && hh <= 23 && nn >= 0 && nn <= 59) { pc->aps[hh][nn] = aps; } else { pcbparam->ret = -1; } // log_dbg("%s, --,ret:%d",__func__,pcbparam->ret); return 0; } int pcurv_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 pcurv where idx=%d", idx); cbparam.nrow = 0; cbparam.ret = 0; cbparam.idx = idx; rc = sqlite3_exec(db, sql, _pcurv_dbcb_0, (void *)&cbparam, &errmsg); plt_unlock_ctndb(); if (rc != SQLITE_OK) { log_dbg("%s, result != SQLITE_OK, idx:%d", __func__, idx); ret = -1; pcurv_set_status(idx, PCURVST_ERR); } else if (cbparam.ret != 0) { log_dbg("%s, cbparam.ret != 0, idx:%d", __func__, idx); ret = -1; pcurv_set_status(idx, PCURVST_ERR); } else { if (cbparam.nrow != PCURV_PTS_NBR) { log_dbg("%s, idx:%d, cbparam.nrow(%d) != %d", __func__, idx, cbparam.nrow, PCURV_PTS_NBR); ret = -1; pcurv_set_status(idx, PCURVST_ERR); } else { pcurv_set_status(idx, PCURVST_VALID); } } pcurv_set_info(idx); log_dbg("%s, idx:%d, ret:%d", __func__, idx, ret); return ret; } int pcurv_set_status(int pidx, int newstat) { struct pcurv_t *pc = &s_pcurv[pidx]; pc->status = newstat; switch (newstat) { case PCURVST_ERR: strcpy(pc->szstatus, "err"); break; case PCURVST_VALID: strcpy(pc->szstatus, "valid"); break; case PCURVST_RESET: strcpy(pc->szstatus, "reset"); break; default: strcpy(pc->szstatus, "unkown"); break; } return 0; } int pcurv_set_info(int pidx) { struct pcurv_t *pc = &s_pcurv[pidx]; strcpy(pc->szinfo, "workday"); return 0; } char *pcurv_get_info_str(int pidx) { struct pcurv_t *pc = &s_pcurv[pidx]; return pc->szinfo; } struct pcurv_t pcurv_get(int idx) { return s_pcurv[idx]; } int pcurv_set(int idx, struct pcurv_t val) { s_pcurv[idx] = val; } void pcurv_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 pcurv where idx=%d", idx); rc = sqlite3_exec(db, sql, NULL, NULL, &errmsg); plt_unlock_ctndb(); if (rc != SQLITE_OK) { log_dbg("%s, idx : %d, sqlite3_exec fail:%d, when %s", __func__, idx, rc, sql); pcurv_set_status(idx, PCURVST_ERR); return; } /* insert */ hh = 0; nn = 0; aps = 0.0; while (hh < 24) { while (nn < 60) { sprintf(sql, "insert into pcurv 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); pcurv_set_status(idx, PCURVST_ERR); return; } nn += 60 / PCURV_PTS_PERHOUR; } nn = 0; hh++; } /* load */ pcurv_load(idx); return; } int pcurv_save(int idx, struct pcurv_t val) { int ret = 0; int i, rc; char *errmsg = NULL; sqlite3 *db = NULL; struct pcurv_t *pc = NULL; int hh; int nn; char sql[1024]; log_dbg("%s, ++, idx:%d", __func__, idx); plt_lock_ctndb(); db = plt_get_ctndb(); s_pcurv[idx] = val; pc = &s_pcurv[idx]; hh = 0; nn = 0; while (hh < 24) { while (nn < 60) { sprintf(sql, "update pcurv set aps=%d where idx=%d and hour=%d and minute=%d", pc->aps[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); pcurv_set_status(idx, PCURVST_ERR); ret = -1; goto leave; } nn += 60 / PCURV_PTS_PERHOUR; } nn = 0; hh++; } leave: plt_unlock_ctndb(); log_dbg("%s, idx:%d, --, ret:%d", __func__, idx, ret); return ret; }