timeseg.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "plt.h"
  2. #include "appl.h"
  3. static struct timeseg_t s_timeseg[TIMESEG_NBR_MAX + 1];
  4. static int timeseg_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 timeseg_t* ts = &s_timeseg[idx];
  10. int hh, nn;
  11. int val;
  12. pcbparam->nrow++;
  13. //log_dbg("%s, ++, row:%d; col:%d",__func__,pcbparam->nrow,ncolumn);
  14. hh = 0;
  15. nn = 0;
  16. val = 0;
  17. for( i = 0; i < ncolumn; i++){
  18. if( strcmp("hour",columnname[i]) == 0){
  19. hh = atoi(columnvalue[i]);
  20. }else if( strcmp("minute",columnname[i]) == 0){
  21. nn = atoi(columnvalue[i]);
  22. }else if( strcmp("segtype",columnname[i]) == 0){
  23. val = atoi(columnvalue[i]);
  24. }
  25. }
  26. if( hh>=0 && hh<=23 && nn>=0 && nn<=59 ){
  27. ts->seg_type[hh][nn] = val;
  28. }else{
  29. pcbparam->ret = -1;
  30. }
  31. //log_dbg("%s, --,ret:%d",__func__,pcbparam->ret);
  32. return 0;
  33. }
  34. int timeseg_load(int idx)
  35. {
  36. int ret = 0;
  37. int rc;
  38. char *errmsg = NULL;
  39. sqlite3* db = NULL;
  40. char sql[1024];
  41. struct dbcbparam_t cbparam;
  42. plt_lock_ctndb();
  43. db = plt_get_ctndb();
  44. sprintf(sql,"select * from timeseg where idx=%d", idx);
  45. cbparam.nrow = 0;
  46. cbparam.ret = 0;
  47. cbparam.idx = idx;
  48. rc = sqlite3_exec(db,sql, timeseg_dbcb_0,(void*)&cbparam,&errmsg);
  49. plt_unlock_ctndb();
  50. if( rc != SQLITE_OK ){
  51. log_dbg("%s, result != SQLITE_OK, idx:%d", __func__, idx );
  52. ret = -1;
  53. timeseg_set_status(idx, TIMESEGST_ERR);
  54. }else if( cbparam.ret != 0){
  55. log_dbg("%s, cbparam.ret != 0, idx:%d", __func__, idx );
  56. ret = -1;
  57. timeseg_set_status(idx, TIMESEGST_ERR);
  58. }else{
  59. if( cbparam.nrow != TIMESEG_PTS_NBR ){
  60. log_dbg("%s, idx:%d, cbparam.nrow(%d) != %d", __func__, idx, cbparam.nrow, TIMESEG_PTS_NBR );
  61. ret = -1;
  62. timeseg_set_status(idx, TIMESEGST_ERR);
  63. }else{
  64. timeseg_set_status(idx, TIMESEGST_VALID);
  65. }
  66. }
  67. log_dbg("%s, idx:%d, ret:%d", __func__, idx, ret);
  68. return ret;
  69. }
  70. int timeseg_set_status( int pidx, int newstat )
  71. {
  72. struct timeseg_t* ts = &s_timeseg[pidx];
  73. ts->status = newstat;
  74. switch( newstat ){
  75. case TIMESEGST_ERR:
  76. strcpy(ts->szstatus, "err");
  77. break;
  78. case TIMESEGST_VALID:
  79. strcpy(ts->szstatus, "valid");
  80. break;
  81. case TIMESEGST_RESET:
  82. strcpy(ts->szstatus, "reset");
  83. break;
  84. default:
  85. strcpy(ts->szstatus, "unkown");
  86. break;
  87. }
  88. return 0;
  89. }
  90. struct timeseg_t timeseg_get(int idx)
  91. {
  92. return s_timeseg[idx];
  93. }
  94. int timeseg_set(int idx, struct timeseg_t val)
  95. {
  96. s_timeseg[idx] = val;
  97. }
  98. void timeseg_reset( int idx )
  99. {
  100. int rc;
  101. char *errmsg = NULL;
  102. sqlite3* db = NULL;
  103. char sql[1024];
  104. int hh, nn;
  105. int aps;
  106. plt_lock_ctndb();
  107. db = plt_get_ctndb();
  108. /* delete */
  109. sprintf(sql,"delete from timeseg where idx=%d", idx);
  110. rc = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
  111. plt_unlock_ctndb();
  112. if( rc != SQLITE_OK ){
  113. log_dbg("%s, sqlite3_exec fail:%d, when %s", __func__, rc, sql );
  114. timeseg_set_status(idx, TIMESEGST_ERR);
  115. return;
  116. }
  117. /* insert */
  118. hh = 0;
  119. nn = 0;
  120. aps = 0.0;
  121. while( hh < 24 ){
  122. while( nn < 60 ){
  123. sprintf(sql,"insert into timeseg VALUES(%d,%d,%d,%d)",idx,hh,nn,aps);
  124. rc = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
  125. if( rc != SQLITE_OK ){
  126. log_dbg("%s, idx : %d, sqlite3_exec fail:%d, when %s", __func__, idx, rc, sql );
  127. timeseg_set_status(idx, TIMESEGST_ERR);
  128. return;
  129. }
  130. nn += 60/TIMESEG_PTS_PERHOUR;
  131. }
  132. nn = 0;
  133. hh++;
  134. }
  135. /* load */
  136. timeseg_load(idx);
  137. return;
  138. }
  139. int timeseg_save( int idx, struct timeseg_t val )
  140. {
  141. int ret = 0;
  142. int i, rc;
  143. char *errmsg = NULL;
  144. sqlite3* db = NULL;
  145. struct timeseg_t* ts = NULL;
  146. int hh;
  147. int nn;
  148. char sql[1024];
  149. log_dbg("%s, ++, idx:%d", __func__, idx);
  150. plt_lock_ctndb();
  151. db = plt_get_ctndb();
  152. s_timeseg[idx] = val;
  153. ts = &s_timeseg[idx];
  154. hh = 0;
  155. nn = 0;
  156. while( hh < 24 ){
  157. while( nn < 60 ){
  158. sprintf(sql,"update timeseg set segtype=%d where idx=%d and hour=%d and minute=%d",
  159. ts->seg_type[hh][nn], idx, hh, nn);
  160. rc = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
  161. if( rc != SQLITE_OK ){
  162. log_dbg("%s, idx : %d, sqlite3_exec fail:%d, when %s", __func__, idx, rc, sql );
  163. timeseg_set_status(idx, TIMESEGST_ERR);
  164. ret = -1;
  165. goto leave;
  166. }
  167. nn += 60/TIMESEG_PTS_PERHOUR;
  168. }
  169. nn = 0;
  170. hh++;
  171. }
  172. leave:
  173. plt_unlock_ctndb();
  174. log_dbg("%s, idx:%d, --, ret:%d", __func__, idx, ret);
  175. return ret;
  176. }
  177. char* timeseg_segtype2string(int segtype)
  178. {
  179. if( segtype == TIMESEGTYPE_SHARP ){
  180. return "sharp";
  181. }else if( segtype == TIMESEGTYPE_PEAK ){
  182. return "peak";
  183. }else if( segtype == TIMESEGTYPE_FLAT ){
  184. return "flat";
  185. }else if( segtype == TIMESEGTYPE_VALLEY ){
  186. return "valley";
  187. }else{
  188. return "unkown";
  189. }
  190. }
  191. int timeseg_init()
  192. {
  193. }