123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- #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;
- }
|