pcurv.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include "plt.h"
  2. #include "appl.h"
  3. static struct pcurv_t s_pcurv[PCURV_NBR_MAX + 1];
  4. static int _pcurv_dbcb_0(void *para, int ncolumn, char **columnvalue, char *columnname[])
  5. {
  6. int i;
  7. struct dbcbparam_t *pcbparam = (struct dbcbparam_t *)para;
  8. int idx = pcbparam->idx;
  9. struct pcurv_t *pc = &s_pcurv[idx];
  10. int hh, nn;
  11. int aps;
  12. pcbparam->nrow++;
  13. // log_dbg("%s, ++, row:%d; col:%d",__func__,pcbparam->nrow,ncolumn);
  14. hh = 0;
  15. nn = 0;
  16. aps = 0.0;
  17. for (i = 0; i < ncolumn; i++)
  18. {
  19. if (strcmp("hour", columnname[i]) == 0)
  20. {
  21. hh = atoi(columnvalue[i]);
  22. }
  23. else if (strcmp("minute", columnname[i]) == 0)
  24. {
  25. nn = atoi(columnvalue[i]);
  26. }
  27. else if (strcmp("aps", columnname[i]) == 0)
  28. {
  29. aps = atoi(columnvalue[i]);
  30. }
  31. }
  32. if (hh >= 0 && hh <= 23 && nn >= 0 && nn <= 59)
  33. {
  34. pc->aps[hh][nn] = aps;
  35. }
  36. else
  37. {
  38. pcbparam->ret = -1;
  39. }
  40. // log_dbg("%s, --,ret:%d",__func__,pcbparam->ret);
  41. return 0;
  42. }
  43. int pcurv_load(int idx)
  44. {
  45. int ret = 0;
  46. int rc;
  47. char *errmsg = NULL;
  48. sqlite3 *db = NULL;
  49. char sql[1024];
  50. struct dbcbparam_t cbparam;
  51. plt_lock_ctndb();
  52. db = plt_get_ctndb();
  53. sprintf(sql, "select * from pcurv where idx=%d", idx);
  54. cbparam.nrow = 0;
  55. cbparam.ret = 0;
  56. cbparam.idx = idx;
  57. rc = sqlite3_exec(db, sql, _pcurv_dbcb_0, (void *)&cbparam, &errmsg);
  58. plt_unlock_ctndb();
  59. if (rc != SQLITE_OK)
  60. {
  61. log_dbg("%s, result != SQLITE_OK, idx:%d", __func__, idx);
  62. ret = -1;
  63. pcurv_set_status(idx, PCURVST_ERR);
  64. }
  65. else if (cbparam.ret != 0)
  66. {
  67. log_dbg("%s, cbparam.ret != 0, idx:%d", __func__, idx);
  68. ret = -1;
  69. pcurv_set_status(idx, PCURVST_ERR);
  70. }
  71. else
  72. {
  73. if (cbparam.nrow != PCURV_PTS_NBR)
  74. {
  75. log_dbg("%s, idx:%d, cbparam.nrow(%d) != %d", __func__, idx, cbparam.nrow, PCURV_PTS_NBR);
  76. ret = -1;
  77. pcurv_set_status(idx, PCURVST_ERR);
  78. }
  79. else
  80. {
  81. pcurv_set_status(idx, PCURVST_VALID);
  82. }
  83. }
  84. pcurv_set_info(idx);
  85. log_dbg("%s, idx:%d, ret:%d", __func__, idx, ret);
  86. return ret;
  87. }
  88. int pcurv_set_status(int pidx, int newstat)
  89. {
  90. struct pcurv_t *pc = &s_pcurv[pidx];
  91. pc->status = newstat;
  92. switch (newstat)
  93. {
  94. case PCURVST_ERR:
  95. strcpy(pc->szstatus, "err");
  96. break;
  97. case PCURVST_VALID:
  98. strcpy(pc->szstatus, "valid");
  99. break;
  100. case PCURVST_RESET:
  101. strcpy(pc->szstatus, "reset");
  102. break;
  103. default:
  104. strcpy(pc->szstatus, "unkown");
  105. break;
  106. }
  107. return 0;
  108. }
  109. int pcurv_set_info(int pidx)
  110. {
  111. struct pcurv_t *pc = &s_pcurv[pidx];
  112. strcpy(pc->szinfo, "workday");
  113. return 0;
  114. }
  115. char *pcurv_get_info_str(int pidx)
  116. {
  117. struct pcurv_t *pc = &s_pcurv[pidx];
  118. return pc->szinfo;
  119. }
  120. struct pcurv_t pcurv_get(int idx)
  121. {
  122. return s_pcurv[idx];
  123. }
  124. int pcurv_set(int idx, struct pcurv_t val)
  125. {
  126. s_pcurv[idx] = val;
  127. }
  128. void pcurv_reset(int idx)
  129. {
  130. int rc;
  131. char *errmsg = NULL;
  132. sqlite3 *db = NULL;
  133. char sql[1024];
  134. int hh, nn;
  135. int aps;
  136. plt_lock_ctndb();
  137. db = plt_get_ctndb();
  138. /* delete */
  139. sprintf(sql, "delete from pcurv where idx=%d", idx);
  140. rc = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
  141. plt_unlock_ctndb();
  142. if (rc != SQLITE_OK)
  143. {
  144. log_dbg("%s, idx : %d, sqlite3_exec fail:%d, when %s", __func__, idx, rc, sql);
  145. pcurv_set_status(idx, PCURVST_ERR);
  146. return;
  147. }
  148. /* insert */
  149. hh = 0;
  150. nn = 0;
  151. aps = 0.0;
  152. while (hh < 24)
  153. {
  154. while (nn < 60)
  155. {
  156. sprintf(sql, "insert into pcurv VALUES(%d,%d,%d,%d)", idx, hh, nn, aps);
  157. rc = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
  158. if (rc != SQLITE_OK)
  159. {
  160. log_dbg("%s, idx : %d, sqlite3_exec fail:%d, when %s", __func__, idx, rc, sql);
  161. pcurv_set_status(idx, PCURVST_ERR);
  162. return;
  163. }
  164. nn += 60 / PCURV_PTS_PERHOUR;
  165. }
  166. nn = 0;
  167. hh++;
  168. }
  169. /* load */
  170. pcurv_load(idx);
  171. return;
  172. }
  173. int pcurv_save(int idx, struct pcurv_t val)
  174. {
  175. int ret = 0;
  176. int i, rc;
  177. char *errmsg = NULL;
  178. sqlite3 *db = NULL;
  179. struct pcurv_t *pc = NULL;
  180. int hh;
  181. int nn;
  182. char sql[1024];
  183. log_dbg("%s, ++, idx:%d", __func__, idx);
  184. plt_lock_ctndb();
  185. db = plt_get_ctndb();
  186. s_pcurv[idx] = val;
  187. pc = &s_pcurv[idx];
  188. hh = 0;
  189. nn = 0;
  190. while (hh < 24)
  191. {
  192. while (nn < 60)
  193. {
  194. sprintf(sql, "update pcurv set aps=%d where idx=%d and hour=%d and minute=%d",
  195. pc->aps[hh][nn], idx, hh, nn);
  196. rc = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
  197. if (rc != SQLITE_OK)
  198. {
  199. log_dbg("%s, idx : %d, sqlite3_exec fail:%d, when %s", __func__, idx, rc, sql);
  200. pcurv_set_status(idx, PCURVST_ERR);
  201. ret = -1;
  202. goto leave;
  203. }
  204. nn += 60 / PCURV_PTS_PERHOUR;
  205. }
  206. nn = 0;
  207. hh++;
  208. }
  209. leave:
  210. plt_unlock_ctndb();
  211. log_dbg("%s, idx:%d, --, ret:%d", __func__, idx, ret);
  212. return ret;
  213. }