sta.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. #include "sta.h"
  2. #include "plt.h"
  3. static int sta_dbcb_0(void *para, int ncolumn, char **columnvalue, char *columnname[])
  4. {
  5. int i;
  6. struct dbcbparam_t *pcbparam = (struct dbcbparam_t *)para;
  7. struct station_t *sta = &STA;
  8. pcbparam->nrow++;
  9. log_dbg("%s, ++, row:%d; col:%d", __func__, pcbparam->nrow, ncolumn);
  10. for (i = 0; i < ncolumn; i++)
  11. {
  12. if (strcmp("info", columnname[i]) == 0)
  13. {
  14. strcpy(sta->szinfo, columnvalue[i]);
  15. }
  16. else if (strcmp("projid", columnname[i]) == 0)
  17. {
  18. strcpy(sta->szprojId, columnvalue[i]);
  19. }
  20. else if (strcmp("time_zone", columnname[i]) == 0)
  21. {
  22. sta->time_zone = atoi(columnvalue[i]);
  23. }
  24. else if (strcmp("idx", columnname[i]) == 0)
  25. {
  26. sta->idx = atoi(columnvalue[i]);
  27. }
  28. }
  29. pcbparam->ret = 0;
  30. log_dbg("%s, --,ret:%d", __func__, pcbparam->ret);
  31. return 0;
  32. }
  33. int sta_get_timezone()
  34. {
  35. return STA.time_zone;
  36. }
  37. char *sta_get_prjid(void)
  38. {
  39. return STA.szprojId;
  40. }
  41. int sta_get_norm_cap()
  42. {
  43. return STA.pow.norm_cap;
  44. }
  45. int sta_get_norm_pow()
  46. {
  47. return STA.pow.norm_pow;
  48. }
  49. char *sta_get_err_str()
  50. {
  51. return STA.sm.szerrcode;
  52. }
  53. int sta_get_state(void)
  54. {
  55. return STA.sm.state;
  56. }
  57. void sta_set_dhgable(void)
  58. {
  59. STA.pow.bdhgable = 1;
  60. }
  61. void sta_reset_dhgable(void)
  62. {
  63. STA.pow.bdhgable = 0;
  64. }
  65. int sta_get_dhgable()
  66. {
  67. return STA.pow.bdhgable;
  68. }
  69. int sta_get_chgable()
  70. {
  71. return STA.pow.bchgable;
  72. }
  73. void sta_set_chgable(void)
  74. {
  75. STA.pow.bchgable = 1;
  76. }
  77. void sta_reset_chgable(void)
  78. {
  79. STA.pow.bchgable = 0;
  80. }
  81. int sta_set_aps(int val)
  82. {
  83. int ret = 0;
  84. STA.pow.active_p_set = val;
  85. // log_dbg( "%s, val:%d, ret:%d", __func__, val, ret);
  86. return ret;
  87. }
  88. int sta_get_ap()
  89. {
  90. return STA.pow.active_p;
  91. }
  92. double sta_get_soc()
  93. {
  94. return STA.pow.soc;
  95. }
  96. int sta_get_aps()
  97. {
  98. return STA.pow.active_p_set;
  99. }
  100. char *sta_get_state_str()
  101. {
  102. return STA.sm.szstate;
  103. }
  104. void sta_set_state(int state, int errcode)
  105. {
  106. struct station_t *dev = &STA;
  107. STA.sm.step = 0;
  108. STA.sm.state = state;
  109. STA.sm.errcode = errcode;
  110. switch (state)
  111. {
  112. case SMST_LAUNCH:
  113. strcpy(dev->sm.szstate, "launch");
  114. break;
  115. case SMST_STDBY:
  116. strcpy(dev->sm.szstate, "stdby");
  117. break;
  118. case SMST_STOP:
  119. strcpy(dev->sm.szstate, "stop");
  120. break;
  121. case SMST_READY:
  122. strcpy(dev->sm.szstate, "ready");
  123. break;
  124. case SMST_DHG:
  125. strcpy(dev->sm.szstate, "dhg");
  126. break;
  127. case SMST_CHG:
  128. strcpy(dev->sm.szstate, "chg");
  129. break;
  130. case SMST_ERR:
  131. strcpy(dev->sm.szstate, "err");
  132. break;
  133. default:
  134. strcpy(dev->sm.szstate, "unknown");
  135. break;
  136. }
  137. switch (errcode)
  138. {
  139. case STAERR_NONE:
  140. strcpy(dev->sm.szerrcode, "none");
  141. break;
  142. /* launch */
  143. case STAERR_LAUNCH_WAIT_ALL_CTN_STDBY_TIMEOUT:
  144. strcpy(dev->sm.szerrcode, "launch wait all ctn stdby timeout");
  145. break;
  146. case STAERR_LAUNCH_WAIT_ALL_METER_READY_TIMEOUT:
  147. strcpy(dev->sm.szerrcode, "launch wait all meter ready timeout");
  148. break;
  149. /* stdby */
  150. case STAERR_STDBY_OFFLINE_CTN_DETECTED:
  151. strcpy(dev->sm.szerrcode, "stdby ctn offline detected");
  152. break;
  153. case STAERR_STDBY_NOT_READY_METER_DETECTED:
  154. strcpy(dev->sm.szerrcode, "stdby not ready meter detected");
  155. break;
  156. case STAERR_STDBY_WAIT_ALL_CTN_STOP_TIMEOUT:
  157. strcpy(dev->sm.szerrcode, "stdby wait all ctn stop timeout");
  158. break;
  159. /* stop */
  160. case STAERR_STOP_OFFLINE_CTN_DETECTED:
  161. strcpy(dev->sm.szerrcode, "stop offline ctn detected");
  162. break;
  163. case STAERR_STOP_NOT_READY_METER_DETECTED:
  164. strcpy(dev->sm.szerrcode, "stop not ready meter detected");
  165. break;
  166. case STAERR_STOP_WAIT_ALL_CTN_STDBY_TIMEOUT:
  167. strcpy(dev->sm.szerrcode, "stop wait all ctn stdby timeout");
  168. break;
  169. case STAERR_STOP_WAIT_ALL_CTN_READY_TIMEOUT:
  170. strcpy(dev->sm.szerrcode, "stop wait all ctn ready timeout");
  171. break;
  172. /* ready */
  173. case STAERR_READY_NOT_READY_CTN_DETECTED:
  174. strcpy(dev->sm.szerrcode, "ready not ready ctn detected");
  175. break;
  176. case STAERR_READY_NOT_READY_METER_DETECTED:
  177. strcpy(dev->sm.szerrcode, "ready not ready meter detected");
  178. break;
  179. case STAERR_READY_DISPOW_FAIL:
  180. strcpy(dev->sm.szerrcode, "ready dis pow fail");
  181. break;
  182. case STAERR_READY_WAIT_DHG_TIMEOUT:
  183. strcpy(dev->sm.szerrcode, "ready wait dhg timeout");
  184. break;
  185. case STAERR_READY_WAIT_CHG_TIMEOUT:
  186. strcpy(dev->sm.szerrcode, "ready wait chg timeout");
  187. break;
  188. case STAERR_READY_WAIT_ALL_CTN_STOP_TIMEOUT:
  189. strcpy(dev->sm.szerrcode, "ready wait all ctn stop timeout");
  190. break;
  191. /* dhg */
  192. case STAERR_DHG_DISPOW_FAIL:
  193. strcpy(dev->sm.szerrcode, "dhg dis pow fail");
  194. break;
  195. case STAERR_DHG_WAIT_ALL_CTN_READY_TIMEOUT:
  196. strcpy(dev->sm.szerrcode, "dhg wait all ctn ready timeout");
  197. break;
  198. case STAERR_DHG_NOT_READY_METER_DETECTED:
  199. strcpy(dev->sm.szerrcode, "dhg not ready meter dectected");
  200. break;
  201. case STAERR_DHG_ERR_CTN_DETECTED:
  202. strcpy(dev->sm.szerrcode, "dhg err ctn dectected");
  203. break;
  204. case STAERR_DHG_OFFLINE_CTN_DETECTED:
  205. strcpy(dev->sm.szerrcode, "dhg offline ctn dectected");
  206. break;
  207. /* chg */
  208. case STAERR_CHG_DISPOW_FAIL:
  209. strcpy(dev->sm.szerrcode, "chg dis pow fail");
  210. break;
  211. case STAERR_CHG_WAIT_ALL_CTN_READY_TIMEOUT:
  212. strcpy(dev->sm.szerrcode, "chg wait all ctn ready timeout");
  213. break;
  214. case STAERR_CHG_NOT_READY_METER_DETECTED:
  215. strcpy(dev->sm.szerrcode, "chg not ready meter dectected");
  216. break;
  217. case STAERR_CHG_ERR_CTN_DETECTED:
  218. strcpy(dev->sm.szerrcode, "chg err ctn dectected");
  219. break;
  220. case STAERR_CHG_OFFLINE_CTN_DETECTED:
  221. strcpy(dev->sm.szerrcode, "chg offline ctn dectected");
  222. break;
  223. default:
  224. strcpy(dev->sm.szerrcode, "unknown");
  225. break;
  226. }
  227. log_dbg("%s, state:%d, errcode:%d", __func__, state, errcode);
  228. }
  229. int sta_send_cmd(int cmd)
  230. {
  231. int ret = 0;
  232. STA.cmd = cmd;
  233. log_dbg("%s, cmd:%d, ret:%d", __func__, cmd, ret);
  234. return ret;
  235. }
  236. static void sta_sm_launch(void)
  237. {
  238. int *step = &STA.sm.step;
  239. int *count = &STA.sm.count;
  240. int *cmd = &STA.cmd;
  241. char *szstate = STA.sm.szstate;
  242. if (*step == 0)
  243. { // reserved
  244. // log_dbg("%s, state:%s, step:%d, all ctn online cmd sent", __func__, szstate,*step);
  245. // ctn_send_cmd_all(CMD_SM_ONLINE);
  246. log_dbg("%s, state:%s, step:%d, wait stdby cmd to continue", __func__, szstate, *step);
  247. *step = 10;
  248. *count = 0;
  249. }
  250. else if (*step == 10)
  251. { // wait stdby cmd
  252. if ((*count)++ >= 600)
  253. { // 60s
  254. log_dbg("%s, state:%s, step:%d, wait stdby cmd to continue", __func__, szstate, *step);
  255. *count = 0;
  256. }
  257. if (*cmd == CMD_SM_STDBY)
  258. { // stdby cmd
  259. *cmd = CMD_SM_DONE;
  260. log_dbg("%s, state:%s, step:%d, got stdby cmd, all ctn stdby cmd sent, wait and chk", __func__, szstate, *step);
  261. ctn_send_cmd_all(CMD_SM_STDBY);
  262. *step = 100;
  263. *count = 0;
  264. }
  265. }
  266. else if (*step == 100)
  267. { /* wait all ctn stdby */
  268. if (ctn_chk_stat_all(SMST_STDBY) == 0)
  269. { /* ok */
  270. log_dbg("%s, state:%s, step:%d, chk all ctn stdby ok, all meter ready cmd sent, wait and chk", __func__, szstate, *step);
  271. meter_send_cmd_all(CMD_SM_READY);
  272. *step = 200;
  273. *count = 0;
  274. }
  275. else
  276. {
  277. if ((*count)++ >= 100)
  278. { // 10 seconds
  279. log_dbg("%s, state:%s, step:%d, wait all ctn stdby timeout, goto err", __func__, szstate, *step);
  280. sta_set_state(SMST_ERR, STAERR_LAUNCH_WAIT_ALL_CTN_STDBY_TIMEOUT);
  281. }
  282. else
  283. {
  284. log_dbg("%s, state:%s, step:%d, waiting all ctn stdby, count:%d", __func__, szstate, *step, *count);
  285. }
  286. }
  287. }
  288. else if (*step == 200)
  289. { /* wait meter ready */
  290. if (meter_chk_stat_all(SMST_READY) == 0)
  291. { /* ok */
  292. log_dbg("%s, state:%s, step:%d, chk all meter ready ok, goto stdby", __func__, szstate, *step);
  293. sta_set_state(SMST_STDBY, STAERR_NONE);
  294. }
  295. else
  296. {
  297. if ((*count)++ >= 100)
  298. {
  299. log_dbg("%s, state:%s, step:%d, wait all meter ready timeout, goto err", __func__, szstate, *step);
  300. sta_set_state(SMST_ERR, STAERR_LAUNCH_WAIT_ALL_METER_READY_TIMEOUT);
  301. }
  302. else
  303. {
  304. log_dbg("%s, state:%s, step:%d, waiting all meter ready, count:%d", __func__, szstate, *step, *count);
  305. }
  306. }
  307. }
  308. }
  309. static void sta_sm_stdby(void)
  310. {
  311. int *step = &STA.sm.step;
  312. int *count = &STA.sm.count;
  313. int *cmd = &STA.cmd;
  314. char *szstate = STA.sm.szstate;
  315. int *last_aps = &STA.pow.last_active_p_set;
  316. if (*step == 0)
  317. { // reserved
  318. *step = 10;
  319. }
  320. else if (*step == 10)
  321. { // wait cmd
  322. if (*cmd == CMD_SM_STOP)
  323. {
  324. *cmd = CMD_SM_DONE;
  325. log_dbg("%s, state:%s, step:%d, got stop cmd, send all ctn stop cmd, wait and chk", __func__, szstate, *step);
  326. ctn_send_cmd_all(CMD_SM_STOP);
  327. *step = 100;
  328. *count = 0;
  329. }
  330. else if (ctn_has_stat(SMST_OFFLINE) == 0)
  331. {
  332. log_dbg("%s, state:%s, step:%d, ctn offline detected, goto err", __func__, szstate, *step);
  333. sta_set_state(SMST_ERR, STAERR_STDBY_OFFLINE_CTN_DETECTED);
  334. }
  335. else if (meter_chk_stat_all(SMST_READY) != 0)
  336. {
  337. log_dbg("%s, state:%s, step:%d, meter not ready detected, goto err", __func__, szstate, *step);
  338. sta_set_state(SMST_ERR, STAERR_STDBY_NOT_READY_METER_DETECTED);
  339. }
  340. }
  341. else if (*step == 100)
  342. { /* wait all ctn stop */
  343. if (ctn_chk_stat_all(SMST_STOP) == 0)
  344. { /* ok */
  345. log_dbg("%s, state:%s, step:%d, chk all ctn stop ok, goto stop", __func__, szstate, *step);
  346. sta_set_state(SMST_STOP, STAERR_NONE);
  347. }
  348. else
  349. {
  350. if ((*count)++ >= 100)
  351. { // 10 seconds
  352. log_dbg("%s, state:%s, step:%d, wait all ctn stop timeout, goto err", __func__, szstate, *step);
  353. sta_set_state(SMST_ERR, STAERR_STDBY_WAIT_ALL_CTN_STOP_TIMEOUT);
  354. }
  355. else
  356. {
  357. log_dbg("%s, state:%s, step:%d, waiting all ctn stop, count:%d", __func__, szstate, *step, *count);
  358. }
  359. }
  360. }
  361. }
  362. static void sta_sm_stop(void)
  363. {
  364. int *step = &STA.sm.step;
  365. int *count = &STA.sm.count;
  366. int *cmd = &STA.cmd;
  367. char *szstate = STA.sm.szstate;
  368. int *last_aps = &STA.pow.last_active_p_set;
  369. if (*step == 0)
  370. { // reserved
  371. *step = 10;
  372. }
  373. else if (*step == 10)
  374. { // wait cmd
  375. if (*cmd == CMD_SM_STDBY)
  376. {
  377. *cmd = CMD_SM_DONE;
  378. log_dbg("%s, state:%s, step:%d, got stdby cmd, send all ctn stdby cmd, wait and chk", __func__, szstate, *step);
  379. ctn_send_cmd_all(CMD_SM_STDBY);
  380. *step = 100;
  381. *count = 0;
  382. }
  383. else if (*cmd == CMD_SM_READY)
  384. {
  385. *cmd = CMD_SM_DONE;
  386. log_dbg("%s, state:%s, step:%d, got ready cmd, send all ctn ready cmd, wait and chk", __func__, szstate, *step);
  387. ctn_send_cmd_all(CMD_SM_READY);
  388. *step = 200;
  389. *count = 0;
  390. }
  391. else if (ctn_has_stat(SMST_OFFLINE) == 0)
  392. {
  393. log_dbg("%s, state:%s, step:%d, ctn offline detected, goto err", __func__, szstate, *step);
  394. sta_set_state(SMST_ERR, STAERR_STOP_OFFLINE_CTN_DETECTED);
  395. }
  396. else if (meter_chk_stat_all(SMST_READY) != 0)
  397. {
  398. log_dbg("%s, state:%s, step:%d, meter not ready detected, goto err", __func__, szstate, *step);
  399. sta_set_state(SMST_ERR, STAERR_STOP_NOT_READY_METER_DETECTED);
  400. }
  401. }
  402. else if (*step == 100)
  403. { /* wait all ctn stdby */
  404. if (ctn_chk_stat_all(SMST_STDBY) == 0)
  405. { /* ok */
  406. log_dbg("%s, state:%s, step:%d, chk all ctn stdby ok, goto stdby", __func__, szstate, *step);
  407. sta_set_state(SMST_STDBY, STAERR_NONE);
  408. }
  409. else
  410. {
  411. if ((*count)++ >= 100)
  412. { // 10 seconds
  413. log_dbg("%s, state:%s, step:%d, wait all ctn stdby timeout, goto err", __func__, szstate, *step);
  414. sta_set_state(SMST_ERR, STAERR_STOP_WAIT_ALL_CTN_STDBY_TIMEOUT);
  415. }
  416. else
  417. {
  418. log_dbg("%s, state:%s, step:%d, waiting all ctn stdby, count:%d", __func__, szstate, *step, *count);
  419. }
  420. }
  421. }
  422. else if (*step == 200)
  423. { /* wait all ctn ready */
  424. if (ctn_chk_stat_all(SMST_READY) == 0)
  425. { /* ok */
  426. log_dbg("%s, state:%s, step:%d, chk all ctn ready ok, goto ready", __func__, szstate, *step);
  427. sta_set_state(SMST_READY, STAERR_NONE);
  428. //*aps = 0;
  429. //*last_aps = *aps;
  430. }
  431. else
  432. {
  433. if ((*count)++ >= 8000)
  434. { // 10 seconds
  435. log_dbg("%s, state:%s, step:%d, wait all ctn ready timeout, goto err", __func__, szstate, *step);
  436. sta_set_state(SMST_ERR, STAERR_STOP_WAIT_ALL_CTN_READY_TIMEOUT);
  437. }
  438. else
  439. {
  440. log_dbg("%s, state:%s, step:%d, waiting all ctn ready, count:%d", __func__, szstate, *step, *count);
  441. }
  442. }
  443. }
  444. }
  445. static void sta_sm_ready(void)
  446. {
  447. int *step = &STA.sm.step;
  448. int *count = &STA.sm.count;
  449. int *cmd = &STA.cmd;
  450. char *szstate = STA.sm.szstate;
  451. int *aps = &STA.pow.active_p_set;
  452. int *last_aps = &STA.pow.last_active_p_set;
  453. struct ems_t *ems = &STA.ems;
  454. if (*step == 0)
  455. { // entry
  456. *aps = 0;
  457. *last_aps = *aps;
  458. *step = 10;
  459. ctn_disable_bsytik_all();
  460. }
  461. else if (*step == 10)
  462. { /* wait and chk*/
  463. if (*cmd == CMD_SM_STOP)
  464. {
  465. *cmd = CMD_SM_DONE;
  466. log_dbg("%s, state:%s, step:%d, got stop cmd, send all ctn stop cmd, wait and chk", __func__, szstate, *step);
  467. ctn_send_cmd_all(CMD_SM_STOP);
  468. *step = 100;
  469. *count = 0;
  470. }
  471. else if (*aps != 0)
  472. { /* !! new aps*/
  473. // log_dbg("%s, state:%s, step:%d, new aps:%d detected, regulating", __func__, szstate,*step, *aps);
  474. // ems_sta_aps_regulate();
  475. if (*aps > 0)
  476. { /* prepare to dhg */
  477. if (sta_get_dhgable())
  478. {
  479. if (ems_sta_aps_dis() == 0)
  480. {
  481. *last_aps = *aps;
  482. log_dbg("%s, state:%s, step:%d, power dispatch ok, chk to enter dhg", __func__, szstate, *step);
  483. *step = 20;
  484. *count = 0;
  485. // ctn_enable_bsytik_all();
  486. }
  487. else
  488. {
  489. log_dbg("%s, state:%s, step:%d, aps dis fail, stay idle", __func__, szstate, *step);
  490. *aps = 0;
  491. }
  492. }
  493. }
  494. else if (*aps < 0)
  495. { /* prepare to chg */
  496. if (sta_get_chgable())
  497. {
  498. if (ems_sta_aps_dis() == 0)
  499. {
  500. log_dbg("%s, state:%s, step:%d, power dispatch ok, chk to enter chg", __func__, szstate, *step);
  501. *last_aps = *aps;
  502. *step = 30;
  503. *count = 0;
  504. // ctn_enable_bsytik_all();
  505. }
  506. else
  507. {
  508. log_dbg("%s, state:%s, step:%d, aps dis fail, stay idle", __func__, szstate, *step);
  509. *aps = 0;
  510. }
  511. }
  512. }
  513. else
  514. { /* stay idle, continue to wait cmd or new aps */
  515. log_dbg("%s, state:%s, step:%d, aps = 0 after regulate, stay idle", __func__, szstate, *step);
  516. }
  517. }
  518. else if (ctn_chk_stat_all(SMST_READY) != 0)
  519. {
  520. log_dbg("%s, state:%s, step:%d, not ready ctn detected, goto err", __func__, szstate, *step);
  521. sta_set_state(SMST_ERR, STAERR_READY_NOT_READY_CTN_DETECTED);
  522. }
  523. else if (meter_chk_stat_all(SMST_READY) != 0)
  524. {
  525. log_dbg("%s, state:%s, step:%d, not ready meter detected, goto err", __func__, szstate, *step);
  526. sta_set_state(SMST_ERR, STAERR_READY_NOT_READY_METER_DETECTED);
  527. }
  528. }
  529. else if (*step == 20)
  530. { /* chk to enter dhg */
  531. if (ctn_chk_dhg_stat() == 0)
  532. { /* ok */
  533. log_dbg("%s, state:%s, step:%d, chk dhg ok, goto dhg", __func__, szstate, *step);
  534. sta_set_state(SMST_DHG, STAERR_NONE);
  535. sta_set_chgable();
  536. }
  537. else
  538. {
  539. if ((*count)++ >= 2000) // cyx : 修改为 20s
  540. { // 10 seconds
  541. log_dbg("%s, state:%s, step:%d, wait to dhg timeout, goto err", __func__, szstate, *step);
  542. sta_set_state(SMST_ERR, STAERR_READY_WAIT_DHG_TIMEOUT);
  543. }
  544. else
  545. {
  546. log_dbg("%s, state:%s, step:%d, waiting to dhg, count:%d", __func__, szstate, *step, *count);
  547. }
  548. }
  549. }
  550. else if (*step == 30)
  551. { /* chk to enter chg */
  552. if (ctn_chk_chg_stat() == 0)
  553. { // idle to chg
  554. log_dbg("%s, state:%s, step:%d, chk chg ok, goto chg", __func__, szstate, *step);
  555. sta_set_state(SMST_CHG, STAERR_NONE);
  556. sta_set_dhgable();
  557. }
  558. else
  559. {
  560. if ((*count)++ >= 2000) // cyx :修改为20s
  561. { // 10 seconds
  562. log_dbg("%s, state:%s, step:%d, wait to chg timeout, goto err", __func__, szstate, *step);
  563. sta_set_state(SMST_ERR, STAERR_READY_WAIT_CHG_TIMEOUT);
  564. }
  565. else
  566. {
  567. log_dbg("%s, state:%s, step:%d, waiting to chg, count:%d", __func__, szstate, *step, *count);
  568. }
  569. }
  570. }
  571. else if (*step == 100)
  572. { /* chk to stop */
  573. if (ctn_chk_stat_all(SMST_STOP) == 0)
  574. { // wait ctn stop
  575. log_dbg("%s, state:%s, step:%d, chk all ctn stop ok, goto stop", __func__, szstate, *step);
  576. sta_set_state(SMST_STOP, STAERR_NONE);
  577. }
  578. else
  579. {
  580. if ((*count)++ >= 100)
  581. { // 10 seconds
  582. log_dbg("%s, state:%s, step:%d, wait all ctn stop timeout, goto err", __func__, szstate, *step);
  583. sta_set_state(SMST_ERR, STAERR_READY_WAIT_ALL_CTN_STOP_TIMEOUT);
  584. }
  585. else
  586. {
  587. log_dbg("%s, state:%s, step:%d, waiting all ctn stop, count:%d", __func__, szstate, *step, *count);
  588. }
  589. }
  590. }
  591. }
  592. static void sta_sm_dhg(void)
  593. {
  594. int *step = &STA.sm.step;
  595. int *count = &STA.sm.count;
  596. int *cmd = &STA.cmd;
  597. char *szstate = STA.sm.szstate;
  598. int *aps = &STA.pow.active_p_set;
  599. int *last_aps = &STA.pow.last_active_p_set;
  600. int *powreg_timer = &STA.ems.pow_reg.timer;
  601. int *powreg_intv = &STA.ems.pow_reg.intv;
  602. int *powreg_lo = &STA.ems.pow_reg.low;
  603. if (*step == 0)
  604. { // entry
  605. *powreg_timer = 0;
  606. *step = 10;
  607. }
  608. else if (*step == 10)
  609. { /* wait and chk */
  610. if (*cmd == CMD_SM_READY)
  611. { /* ready cmd */
  612. *cmd = CMD_SM_DONE;
  613. log_dbg("%s, state:%s, step:%d, got ready cmd, send all ctn ready cmd, wait and chk", __func__, szstate, *step);
  614. ctn_send_cmd_all(CMD_SM_READY);
  615. *step = 100;
  616. *count = 0;
  617. }
  618. else if (*aps < 5)
  619. { /* aps <= 0 0 means idle; <0 means illegal value */
  620. log_dbg("%s, state:%s, step:%d, aps < 5, send all ctn idle cmd, wait and chk", __func__, szstate, *step);
  621. ctn_send_cmd_all(CMD_SM_READY);
  622. *step = 100;
  623. *count = 0;
  624. }
  625. else if (*aps != *last_aps)
  626. { // new aps(by manual) detected
  627. // log_dbg("%s, state:%s, step:%d, new aps:%d detected, regulating", __func__, szstate,*step, *aps);
  628. // ems_sta_aps_regulate();
  629. if (*aps >= 5)
  630. {
  631. log_dbg("%s, state:%s, step:%d, new aps:%d valid, try to dispatch", __func__, szstate, *step, *aps);
  632. if (ems_sta_aps_dis() == 0)
  633. {
  634. log_dbg("%s, state:%s, step:%d, aps dis ok, stay dhg", __func__, szstate, *step);
  635. *last_aps = *aps;
  636. }
  637. else
  638. {
  639. log_dbg("%s, state:%s, step:%d, aps dis fail, set aps=0", __func__, szstate, *step);
  640. *aps = 0;
  641. }
  642. }
  643. else
  644. {
  645. log_dbg("%s, state:%s, step:%d, new aps:%d invalid, send all ctn idle cmd, wait and chk", __func__, szstate, *step, *aps);
  646. ctn_send_cmd_all(CMD_SM_READY);
  647. *step = 100;
  648. *count = 0;
  649. }
  650. }
  651. else if (meter_chk_stat_all(SMST_READY) != 0)
  652. {
  653. log_dbg("%s, state:%s, step:%d, not ready meter detected, goto err", __func__, szstate, *step);
  654. sta_set_state(SMST_ERR, STAERR_DHG_NOT_READY_METER_DETECTED);
  655. }
  656. else if (ctn_has_stat(SMST_ERR) == 0)
  657. { /* err ctn */
  658. log_dbg("%s, state:%s, step:%d, err ctn detected, goto err", __func__, szstate, *step);
  659. sta_set_state(SMST_ERR, STAERR_DHG_ERR_CTN_DETECTED);
  660. }
  661. else if (ctn_has_stat(SMST_OFFLINE) == 0)
  662. { /* offline ctn */
  663. log_dbg("%s, state:%s, step:%d, offline ctn detected, goto err", __func__, szstate, *step);
  664. sta_set_state(SMST_ERR, STAERR_DHG_OFFLINE_CTN_DETECTED);
  665. }
  666. else if (ctn_chk_stat_all(SMST_READY) == 0)
  667. {
  668. log_dbg("%s, state:%s, step:%d, all ctn ready detected, goto ready", __func__, szstate, *step);
  669. /* ctn_send_cmd_all(CMD_SM_IDLE); */ /* already all idle, no need to send idle */
  670. sta_reset_dhgable();
  671. sta_set_state(SMST_READY, STAERR_NONE);
  672. }
  673. else
  674. {
  675. if ((*powreg_timer)++ >= *powreg_intv)
  676. { // 5s
  677. *powreg_timer = 0;
  678. // ems_sta_aps_regulate();
  679. /*
  680. if( *aps > 0 ){
  681. log_dbg("%s, state:%s, step:%d, new aps:%d valid, try to dispatch", __func__, szstate,*step, *aps);
  682. *last_aps = *aps;
  683. if( ems_sta_aps_dis() != 0 ){
  684. log_dbg("%s, state:%s, step:%d, power dispatch fail, goto err", __func__, szstate,*step);
  685. sta_set_state( SMST_ERR, STAERR_DHG_DISPOW_FAIL);
  686. }
  687. }else{
  688. log_dbg("%s, state:%s, step:%d, new aps:%d invalid, send all ctn idle cmd, wait and chk", __func__, szstate,*step, *aps);
  689. ctn_send_cmd_all(CMD_SM_IDLE);
  690. *step = 100;
  691. *count = 0;
  692. }
  693. */
  694. }
  695. }
  696. }
  697. else if (*step == 100)
  698. {
  699. if (ctn_chk_stat_all(SMST_READY) == 0)
  700. { // wait ctn ready
  701. log_dbg("%s, state:%s, step:%d, chk all ctn ready ok, goto ready", __func__, szstate, *step);
  702. sta_set_state(SMST_READY, STAERR_NONE);
  703. }
  704. else
  705. {
  706. if ((*count)++ >= 1000)
  707. { // 10 seconds
  708. log_dbg("%s, state:%s, step:%d, wait all ctn ready timeout, goto err",
  709. __func__, szstate, *step);
  710. sta_set_state(SMST_ERR, STAERR_DHG_WAIT_ALL_CTN_READY_TIMEOUT);
  711. }
  712. else
  713. {
  714. log_dbg("%s, state:%s, step:%d, waiting all ctn ready, count:%d",
  715. __func__, szstate, *step, *count);
  716. }
  717. }
  718. }
  719. }
  720. static void sta_sm_chg(void)
  721. {
  722. int *step = &STA.sm.step;
  723. int *count = &STA.sm.count;
  724. int *cmd = &STA.cmd;
  725. char *szstate = STA.sm.szstate;
  726. int *aps = &STA.pow.active_p_set;
  727. int *last_aps = &STA.pow.last_active_p_set;
  728. int *powreg_timer = &STA.ems.pow_reg.timer;
  729. int *powreg_intv = &STA.ems.pow_reg.intv;
  730. int *powreg_lo = &STA.ems.pow_reg.low;
  731. if (*step == 0)
  732. { /* entry */
  733. *powreg_timer = 0;
  734. *step = 10;
  735. }
  736. else if (*step == 10)
  737. { /* wait and chk */
  738. if (*cmd == CMD_SM_READY)
  739. { /* ready cmd */
  740. *cmd = CMD_SM_DONE;
  741. log_dbg("%s, state:%s, step:%d, got ready cmd, send all ctn ready cmd, wait and chk", __func__, szstate, *step);
  742. ctn_send_cmd_all(CMD_SM_READY);
  743. *step = 100;
  744. *count = 0;
  745. }
  746. else if (*aps > -5)
  747. { /* illegal value */
  748. log_dbg("%s, state:%s, step:%d, aps = 0, send all ctn ready cmd, wait and chk", __func__, szstate, *step);
  749. ctn_send_cmd_all(CMD_SM_READY);
  750. *step = 100;
  751. *count = 0;
  752. }
  753. else if (*aps != *last_aps)
  754. { // new aps detected
  755. // log_dbg("%s, state:%s, step:%d, new aps:%d detected, regulating", __func__, szstate,*step, *aps);
  756. // ems_sta_aps_regulate();
  757. if (*aps < -5)
  758. { /* continue to chg */
  759. log_dbg("%s, state:%s, step:%d, new aps:%d valid, try to dispatch", __func__, szstate, *step, *aps);
  760. if (ems_sta_aps_dis() == 0)
  761. {
  762. log_dbg("%s, state:%s, step:%d, power dis ok, stay chg", __func__, szstate, *step);
  763. *last_aps = *aps;
  764. }
  765. else
  766. {
  767. log_dbg("%s, state:%s, step:%d, aps dis fail, set aps=0", __func__, szstate, *step);
  768. *aps = 0;
  769. }
  770. }
  771. else
  772. {
  773. log_dbg("%s, state:%s, step:%d, new aps:%d invalid, send all ctn ready cmd, wait and chk", __func__, szstate, *step, *aps);
  774. ctn_send_cmd_all(CMD_SM_READY);
  775. *step = 100;
  776. *count = 0;
  777. }
  778. }
  779. else if (meter_chk_stat_all(SMST_READY) != 0)
  780. {
  781. log_dbg("%s, state:%s, step:%d, not ready meter detected, goto err", __func__, szstate, *step);
  782. sta_set_state(SMST_ERR, STAERR_CHG_NOT_READY_METER_DETECTED);
  783. }
  784. else if (ctn_has_stat(SMST_ERR) == 0)
  785. { /* some ctn enter err */
  786. log_dbg("%s, state:%s, step:%d, err ctn detected, goto err", __func__, szstate, *step);
  787. sta_set_state(SMST_ERR, STAERR_CHG_ERR_CTN_DETECTED);
  788. }
  789. else if (ctn_has_stat(SMST_OFFLINE) == 0)
  790. { /* some ctn enter offline */
  791. log_dbg("%s, state:%s, step:%d, offline ctn detected, goto err", __func__, szstate, *step);
  792. sta_set_state(SMST_ERR, STAERR_CHG_OFFLINE_CTN_DETECTED);
  793. }
  794. else if (ctn_chk_stat_all(SMST_READY) == 0)
  795. {
  796. log_dbg("%s, state:%s, step:%d, all ctn ready detected, goto ready", __func__, szstate, *step);
  797. /*ctn_send_cmd_all(CMD_SM_IDLE);*/ /* already all idel, no need to send idle cmd */
  798. sta_reset_chgable();
  799. sta_set_state(SMST_READY, STAERR_NONE);
  800. }
  801. else
  802. {
  803. if ((*powreg_timer)++ >= *powreg_intv)
  804. { // 1s
  805. *powreg_timer = 0;
  806. // ems_sta_aps_regulate();
  807. /*
  808. if( *aps < 0 ){
  809. log_dbg("%s, state:%s, step:%d, new aps:%d valid, try to dispatch", __func__, szstate,*step, *aps);
  810. *last_aps = *aps;
  811. if( ems_sta_aps_dis() != 0 ){
  812. log_dbg("%s, state:%s, step:%d, power dispatch fail, goto err", __func__, szstate,*step);
  813. sta_set_state( SMST_ERR, STAERR_CHG_DISPOW_FAIL);
  814. }
  815. }else{
  816. log_dbg("%s, state:%s, step:%d, new aps:%d invalid, send all ctn idle cmd, wait and chk", __func__, szstate,*step, *aps);
  817. ctn_send_cmd_all(CMD_SM_IDLE);
  818. *step = 100;
  819. *count = 0;
  820. }
  821. */
  822. }
  823. }
  824. }
  825. else if (*step == 100)
  826. {
  827. if (ctn_chk_stat_all(SMST_READY) == 0)
  828. { // wait ctn ready
  829. log_dbg("%s, state:%s, step:%d, chk all ctn ready ok, goto ready", __func__, szstate, *step);
  830. sta_set_state(SMST_READY, STAERR_NONE);
  831. }
  832. else
  833. {
  834. if ((*count)++ >= 1000)
  835. { // 10 seconds
  836. log_dbg("%s, state:%s, step:%d, wait all ctn ready timeout, goto err", __func__, szstate, *step);
  837. sta_set_state(SMST_ERR, STAERR_CHG_WAIT_ALL_CTN_READY_TIMEOUT);
  838. }
  839. else
  840. {
  841. log_dbg("%s, state:%s, step:%d, waiting all ctn ready, count:%d", __func__, szstate, *step, *count);
  842. }
  843. }
  844. }
  845. }
  846. static void sta_sm_err(void)
  847. {
  848. int *step = &STA.sm.step;
  849. int *count = &STA.sm.count;
  850. int *cmd = &STA.cmd;
  851. char *szstate = STA.sm.szstate;
  852. if (*step == 0)
  853. { /* entry */
  854. *step = 10;
  855. // ctn_disable_bsytik_all();
  856. ctn_send_cmd_all(CMD_SM_READY);
  857. }
  858. else if (*step == 10)
  859. { // wait cmd
  860. if (*cmd == CMD_SM_STDBY)
  861. {
  862. *cmd = CMD_SM_DONE;
  863. log_dbg("%s, state:%s, step:%d, got stdby cmd, try to enter stdby from pwrup", __func__, szstate, *step);
  864. sta_set_state(SMST_LAUNCH, STAERR_NONE);
  865. sta_send_cmd(CMD_SM_STDBY);
  866. }
  867. }
  868. }
  869. void sta_sm(void)
  870. {
  871. struct station_t *dev = &STA;
  872. int idx;
  873. double apsum, capsum, powsum, leftcapsum;
  874. /* state machine timing statistics */
  875. double tseclipsed = 0.0;
  876. double ts = 0.0;
  877. if (dev->sm.timing_timer < 0)
  878. { /* rest */
  879. dev->sm.timing_timer = 0;
  880. ts = misc_gettimeofday();
  881. dev->sm.tslastrun = ts;
  882. dev->sm.totalcnt = 0;
  883. dev->sm.totaltime = 0;
  884. dev->sm.ave = -1.0;
  885. dev->sm.max = -1.0;
  886. dev->sm.cur = -1.0;
  887. }
  888. else
  889. {
  890. if (dev->sm.timing_timer++ >= 100)
  891. { /* cal every 100 times */
  892. dev->sm.timing_timer = 0;
  893. ts = misc_gettimeofday();
  894. tseclipsed = ts - dev->sm.tslastrun;
  895. dev->sm.totalcnt += 1;
  896. dev->sm.totaltime += tseclipsed;
  897. dev->sm.tslastrun = ts;
  898. dev->sm.ave = dev->sm.totaltime / dev->sm.totalcnt / 100;
  899. dev->sm.cur = tseclipsed / 100;
  900. if (dev->sm.cur > dev->sm.max)
  901. {
  902. dev->sm.max = dev->sm.cur;
  903. }
  904. if (dev->sm.totalcnt > 100000)
  905. { /* auto reset */
  906. dev->sm.timing_timer = -1;
  907. }
  908. }
  909. }
  910. if (dev->sm.tick_timer++ >= 10)
  911. { /* 1s */
  912. dev->sm.tick_timer = 0;
  913. dev->sm.tick++;
  914. }
  915. apsum = 0.0;
  916. capsum = 0.0;
  917. powsum = 0.0;
  918. leftcapsum = 0.0;
  919. for (idx = 1; idx <= ctn_get_nbr(); idx++)
  920. {
  921. apsum += ctn_get_ap(idx);
  922. capsum += ctn_get_norm_cap(idx);
  923. powsum += ctn_get_norm_pow(idx);
  924. leftcapsum += ctn_get_soc(idx) / (double)100 * ctn_get_norm_cap(idx);
  925. }
  926. STA.pow.active_p = apsum;
  927. STA.pow.norm_cap = capsum;
  928. STA.pow.norm_pow = powsum;
  929. STA.pow.soc = leftcapsum / capsum * 100;
  930. switch (sta_get_state())
  931. {
  932. case SMST_LAUNCH:
  933. sta_sm_launch();
  934. break;
  935. case SMST_STDBY:
  936. sta_sm_stdby();
  937. break;
  938. case SMST_STOP:
  939. sta_sm_stop();
  940. break;
  941. case SMST_READY:
  942. sta_sm_ready();
  943. break;
  944. case SMST_CHG:
  945. sta_sm_chg();
  946. break;
  947. case SMST_DHG:
  948. sta_sm_dhg();
  949. break;
  950. case SMST_ERR:
  951. sta_sm_err();
  952. break;
  953. default:
  954. break;
  955. }
  956. }
  957. static void sta_thrd(void *param)
  958. {
  959. log_dbg("%s, ++", __func__);
  960. char buf[4096];
  961. sta_get_init_data(1, buf);
  962. cloud_upload_init_data(buf);
  963. while (1)
  964. {
  965. sta_sm();
  966. usleep(100000); /* 100ms */
  967. }
  968. log_dbg("%s, --", __func__);
  969. }
  970. int sta_init(void)
  971. {
  972. pthread_t thrd;
  973. char buf[32];
  974. int result;
  975. char *errmsg = NULL;
  976. sqlite3 *db = plt_get_projdb();
  977. char sql[1024];
  978. struct dbcbparam_t cbparam;
  979. int ret = 0;
  980. int i;
  981. struct station_t *dev = &STA;
  982. log_dbg("%s, ++", __func__);
  983. sprintf(sql, "select * from sta");
  984. cbparam.nrow = 0;
  985. plt_lock_projdb();
  986. result = sqlite3_exec(db, sql, sta_dbcb_0, (void *)&cbparam, &errmsg);
  987. plt_unlock_projdb();
  988. if (result != SQLITE_OK)
  989. {
  990. log_dbg("%s, result != SQLITE_OK, cause : %s", __func__, errmsg);
  991. ret = -1;
  992. }
  993. else if (cbparam.ret != 0)
  994. {
  995. log_dbg("%s, cbparam.ret != 0", __func__);
  996. ret = -1;
  997. }
  998. else
  999. {
  1000. if (cbparam.nrow != 1)
  1001. {
  1002. log_dbg("%s, cbparam.nrow(%d) != 1", __func__, cbparam.nrow);
  1003. ret = -1;
  1004. }
  1005. else
  1006. {
  1007. sta_set_state(SMST_LAUNCH, SMERR_NONE);
  1008. dev->sm.tick_timer = 0;
  1009. dev->sm.timing_timer = -1;
  1010. dev->pow.norm_cap = 0.0;
  1011. dev->pow.norm_pow = 0.0;
  1012. sta_set_dhgable();
  1013. sta_set_chgable();
  1014. if (pthread_create(&thrd, NULL, sta_thrd, NULL) != 0)
  1015. {
  1016. log_dbg("%s, create sta_thrd_sm fail", __func__);
  1017. ret = -1;
  1018. }
  1019. }
  1020. }
  1021. log_dbg("%s, --, ret:%d", __func__, ret);
  1022. return ret;
  1023. }
  1024. int sta_get_init_data(int idx, char *buf)
  1025. {
  1026. sprintf(buf, "{\"device_id\":\"%s\", \"type\":0, \"idx\":%d, \"config\":{\"key\":\"test\"}}",
  1027. STA.szprojId, idx);
  1028. return 0;
  1029. }