megatec.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * megatec.c
  3. *
  4. * Created on: 22.05.2017
  5. * Author: balbekova
  6. */
  7. #include "FreeRTOS.h"
  8. #include "task.h"
  9. #include "usart.h"
  10. #include "megatec.h"
  11. #include "log.h"
  12. #include "snmp_api.h"
  13. #include "settings_api.h"
  14. #ifdef PRINTF_STDLIB
  15. #include <stdio.h>
  16. #endif
  17. #ifdef PRINTF_CUSTOM
  18. #include "tinystdio.h"
  19. #endif
  20. #include <string.h>
  21. #include <math.h>
  22. /**
  23. * @brief Общая структура настроек
  24. */
  25. extern SETTINGS_t sSettings;
  26. #define UPS_PDU_MAX_LEN 50
  27. float TimeParamFloat = 0;
  28. uint16_t TimeParam = 0;
  29. uint16_t TimeParam2 = 0;
  30. bool megatec_send = true;
  31. UPS_value_t UPS;
  32. enum {
  33. NORMAL = 0x00,
  34. VER_ERROR = 0x01,
  35. CHKSUM_ERROR = 0x02,
  36. LCHKSUM_ERROR = 0x03,
  37. CID2_INVALID = 0x04,
  38. CMD_FMT_ERROR = 0x05,
  39. INVALID_DATA = 0x06,
  40. };
  41. static struct {
  42. uint8_t data[UPS_PDU_MAX_LEN];
  43. uint16_t len;
  44. } ups_pdu;
  45. const char* MegaTecCMD[] =
  46. {
  47. "Q1\r",
  48. "T\r",
  49. "TL\r",
  50. "T",
  51. "Q\r",
  52. "S",
  53. "R",
  54. "C\r",
  55. "CT\r",
  56. "I\r",
  57. "F\r",
  58. "Q2\r",
  59. "QGS\r",
  60. "QBV\r",
  61. "QMD\r",
  62. "QVFW\r",
  63. "QID\r",
  64. "QPI\r",
  65. "QS\r",
  66. "M\r",
  67. };
  68. extern bool flUpdateLog;
  69. void init_UPS_value(void)
  70. {
  71. UPS.Freq_in = 0;
  72. UPS.VAC_in = 0;
  73. UPS.VAC_out = 0;
  74. UPS.Temp = 0;
  75. UPS.Load = 0;
  76. UPS.SOC = 0;
  77. UPS.work_time = 0;
  78. UPS.Status = 0;
  79. UPS.Alarm = 0;
  80. UPS.cnt_err_ups = 0;
  81. UPS.Flag_Present = false;
  82. UPS.Present = false;
  83. memset(UPS.model, 0, 11);
  84. memset(UPS.vertion, 0, 11);
  85. }
  86. void send_MegaTec_cmd(cmdMegaTecEnums_t command)
  87. {
  88. if(command == ups_test_time){
  89. uint8_t req[10];
  90. memset(req, 0, 10);
  91. if(TimeParam < 10)
  92. sprintf(req, "%s0%d\r", MegaTecCMD[command], TimeParam);
  93. else
  94. sprintf(req, "%s%d\r", MegaTecCMD[command], TimeParam);
  95. ups_send_block(req, strlen(req));
  96. }
  97. else if(command == ups_shutdown){
  98. uint8_t req[10];
  99. memset(req, 0, 10);
  100. if(TimeParamFloat >= 1 && TimeParamFloat < 10)
  101. {
  102. sprintf(req, "%s0%d\r", MegaTecCMD[command], (uint16_t)TimeParamFloat);
  103. }
  104. else if(TimeParamFloat < 1){
  105. sprintf(req, "%s.%d\r", MegaTecCMD[command], (uint16_t)(10*TimeParamFloat));
  106. }
  107. else{
  108. sprintf(req, "%s%d\r", MegaTecCMD[command], (uint16_t)TimeParamFloat);
  109. }
  110. ups_send_block(req, strlen(req));
  111. }
  112. else if(command == ups_shutdown_restore){
  113. uint8_t req[10];
  114. memset(req, 0, 10);
  115. sprintf(req, "%s.%d%s%d\r", MegaTecCMD[command-1], TimeParam, MegaTecCMD[command], TimeParam2);
  116. ups_send_block(req, strlen(req));
  117. }
  118. else{
  119. // TODO ����������� ���������
  120. //ups_send_block(MegaTecCMD[command], strlen(MegaTecCMD[command]));
  121. ups_send_block((void*)MegaTecCMD[command], strlen(MegaTecCMD[command]));
  122. }
  123. }
  124. bool ups_megatec_rx_pdu(void)
  125. {
  126. int c = 0;
  127. ups_pdu.len = 0;
  128. while ((ups_pdu.len < UPS_PDU_MAX_LEN) && (c != 0x0d)) {
  129. c = ups_getchar(100);//portMAX_DELAY
  130. if(c < 0)
  131. {
  132. ups_pdu.len = 0;
  133. break;
  134. }
  135. ups_pdu.data[ups_pdu.len++] = c;
  136. }
  137. DBG printf("UPS raw data: %s\r\n", ups_pdu.data);
  138. DBG printf("UPS raw data len: %d\r\n", ups_pdu.len);
  139. if (ups_pdu.len == 0)
  140. return false;
  141. return true;
  142. }
  143. int8_t get_ups_param(char *buf, char *param, char *val)
  144. {
  145. char *endValue;
  146. int8_t param_len;
  147. memset(val, 0, 20);
  148. endValue = strpbrk(buf, param);
  149. if(endValue != NULL){
  150. param_len = endValue - buf;
  151. if(param_len < 16){
  152. strncpy(val, buf, param_len);
  153. }
  154. else{
  155. param_len = 0;
  156. }
  157. }
  158. else{
  159. param_len = 0;
  160. }
  161. DBG printf("UPS parameter: %s\r\n", val);
  162. return param_len;
  163. }
  164. void ups_status_response(char *data)
  165. {
  166. uint8_t i;
  167. char value[20];
  168. float tmp;
  169. uint8_t len = 0;
  170. DBG printf("ups_status_response: %s\r\n", data);
  171. if(data[0] != '(')
  172. return;
  173. DBG printf("ups_status_parser_start\r\n");
  174. UPS.Present = true;
  175. UPS.Flag_Present = true;
  176. UPS.cnt_err_ups = 0;
  177. if(flUpdateLog){
  178. flUpdateLog = false;
  179. log_add(data);
  180. }
  181. data++;
  182. DBG printf("UPS ups_status_parser_startr: %s\r\n", data);
  183. len = get_ups_param(data, " ", value);
  184. data += (len + 1);
  185. if(len > 0){
  186. tmp =atof(value);
  187. if(tmp > 20)
  188. UPS.VAC_in = tmp;
  189. else
  190. UPS.VAC_in = 0;
  191. }
  192. //TODO
  193. len = get_ups_param(data, " ", value);
  194. data += (len + 1);
  195. len = get_ups_param(data, " ", value);
  196. data += (len + 1);
  197. if(len > 0)
  198. UPS.VAC_out = atof(value);
  199. len = get_ups_param(data, " ", value);
  200. data += (len + 1);
  201. if(len > 0)
  202. UPS.Load = atoi(value);
  203. len = get_ups_param(data, " ", value);
  204. data += (len + 1);
  205. if(len > 0)
  206. UPS.Freq_in = atof(value);
  207. //TODO
  208. len = get_ups_param(data, " ", value);
  209. data += (len + 1);
  210. if(sSettings.UPS_Setting.type_ups == ups_kestar){
  211. if(len > 0){
  212. //UPS.SOC = round(100*(((atof(value))/6) - 1.85)/0.4166666667); // 1.85 - минимальное напряжение на ячейки АКБ; 2.266666667 - максимальное напряжение на ячейки АКБ; (2.266666667 - 1.85) = 0.4166666667
  213. //UPS.SOC = round(100*((atof(value)) - 1.67)/0.58); // 1.667 - минимальное напряжение на ячейки АКБ; 2.267 - максимальное напряжение на ячейки АКБ; (2.267 - 1.667) = 0.6
  214. tmp = atof(value);
  215. if(tmp < 10){
  216. UPS.SOC = round(100*(tmp - sSettings.UPS_Setting.Ucellmin)/(sSettings.UPS_Setting.Ucellmax - sSettings.UPS_Setting.Ucellmin));
  217. }
  218. else{
  219. UPS.SOC = round(100*(tmp/6 - sSettings.UPS_Setting.Ucellmin)/(sSettings.UPS_Setting.Ucellmax - sSettings.UPS_Setting.Ucellmin));
  220. }
  221. }
  222. }
  223. len = get_ups_param(data, " ", value);
  224. data += (len + 1);
  225. if(len > 0)
  226. UPS.Temp = atof(value);
  227. len = get_ups_param(data, "\r", value);
  228. data += (len + 1);
  229. if(len > 0){
  230. uint8_t stat = 0;
  231. for(i = 0; i < len; i ++)
  232. {
  233. stat |= (value[i] - 0x30) << (7-i);
  234. }
  235. UPS.Status = stat;
  236. }
  237. }
  238. void ups_general_status_response(char *data)
  239. {
  240. uint8_t i;
  241. char value[20];
  242. float tmp;
  243. uint8_t len = 0;
  244. DBG printf("ups_status_response: %s\r\n", data);
  245. if(data[0] != '(')
  246. return;
  247. DBG printf("ups_status_parser_start\r\n");
  248. UPS.Present = true;
  249. UPS.Flag_Present = true;
  250. UPS.cnt_err_ups = 0;
  251. if(flUpdateLog){
  252. flUpdateLog = false;
  253. log_add(data);
  254. }
  255. data++;
  256. DBG printf("UPS ups_status_parser_startr: %s\r\n", data);
  257. len = get_ups_param(data, " ", value);
  258. data += (len + 1);
  259. if(len > 0){
  260. tmp =atof(value);
  261. if(tmp > 20)
  262. UPS.VAC_in = tmp;
  263. else
  264. UPS.VAC_in = 0;
  265. }
  266. len = get_ups_param(data, " ", value);
  267. data += (len + 1);
  268. if(len > 0)
  269. UPS.Freq_in = atof(value);
  270. len = get_ups_param(data, " ", value);
  271. data += (len + 1);
  272. if(len > 0)
  273. UPS.VAC_out = atof(value);
  274. //TODO
  275. len = get_ups_param(data, " ", value);
  276. data += (len + 1);
  277. //TODO
  278. len = get_ups_param(data, " ", value);
  279. data += (len + 1);
  280. len = get_ups_param(data, " ", value);
  281. data += (len + 1);
  282. if(len > 0)
  283. UPS.Load = atoi(value);
  284. //TODO
  285. len = get_ups_param(data, " ", value);
  286. data += (len + 1);
  287. //TODO
  288. len = get_ups_param(data, " ", value);
  289. data += (len + 1);
  290. len = get_ups_param(data, " ", value);
  291. data += (len + 1);
  292. if(len > 0)
  293. UPS.Temp = atof(value);
  294. len = get_ups_param(data, "\r", value);
  295. data += (len + 1);
  296. if(len > 0){
  297. uint8_t stat = 0;
  298. for(i = 2; i < (len - 2); i ++)
  299. {
  300. stat |= (value[i] - 0x30) << (7-i);
  301. }
  302. UPS.Status = stat;
  303. }
  304. }
  305. void ups_info_response(char *data)
  306. {
  307. uint8_t i = 0;
  308. if(data[0] != '#')
  309. return;
  310. UPS.Present = true;
  311. UPS.Flag_Present = true;
  312. UPS.cnt_err_ups = 0;
  313. data++;
  314. data += 16;
  315. while(data[0] == ' '){
  316. data ++;
  317. i ++;
  318. }
  319. if(i < 15){
  320. /*endValue = strpbrk(data," ");
  321. len = endValue - data;*/
  322. if(UPS.model[0] == 0){
  323. strncpy(UPS.model, data, 10);
  324. SNMP_SetObjDescr();
  325. }
  326. else{
  327. strncpy(UPS.model, data, 10);
  328. }
  329. data += 11;
  330. }
  331. else
  332. {
  333. if(UPS.model[0] == 0){
  334. strcpy(UPS.model, "RTMP II");
  335. SNMP_SetObjDescr();
  336. }
  337. else{
  338. strcpy(UPS.model, "RTMP II");
  339. }
  340. data += 11;
  341. }
  342. strncpy(UPS.serial, data, 8);
  343. data += 8;
  344. strncpy(UPS.vertion, data, 2);
  345. SNMP_SetObjDescr();
  346. }
  347. void ups_remain_time_response(char *data)
  348. {
  349. char value[20];
  350. if(data[0] != '(')
  351. return;
  352. DBG printf("ups_remain_time_response: %s\r\n", data);
  353. UPS.Present = true;
  354. UPS.Flag_Present = true;
  355. UPS.cnt_err_ups = 0;
  356. data++;
  357. if(strlen(data) > 5)
  358. return;
  359. memset(value, 0, 10);
  360. strcpy(value, data);
  361. if((UPS.Status >> 7) & 0x01)
  362. UPS.work_time = atof(value);
  363. else
  364. UPS.work_time = 0;
  365. }
  366. void ups_akb_info_response(char *data)
  367. {
  368. char value[20];
  369. uint8_t len = 0;
  370. DBG printf("ups_akb_info_response: %s\r\n", data);
  371. if(data[0] != '(')
  372. return;
  373. DBG printf("ups_akb_info_parser_start\r\n");
  374. UPS.Present = true;
  375. UPS.Flag_Present = true;
  376. UPS.cnt_err_ups = 0;
  377. data++;
  378. DBG printf("UPS ups_akb_info_parser_start: %s\r\n", data);
  379. //TODO
  380. len = get_ups_param(data, " ", value);
  381. data += (len + 1);
  382. //TODO
  383. len = get_ups_param(data, " ", value);
  384. data += (len + 1);
  385. //TODO
  386. len = get_ups_param(data, " ", value);
  387. data += (len + 1);
  388. len = get_ups_param(data, " ", value);
  389. data += (len + 1);
  390. if(len > 0)
  391. UPS.SOC = atoi(value);
  392. len = get_ups_param(data, "\r", value);
  393. data += (len + 1);
  394. if(len > 0)
  395. UPS.work_time = atoi(value);
  396. }
  397. void ups_model_response(char *data)
  398. {
  399. char value[20];
  400. uint8_t len = 0;
  401. uint8_t j = 0;
  402. DBG printf("ups_akb_info_response: %s\r\n", data);
  403. if(data[0] != '(')
  404. return;
  405. DBG printf("ups_akb_info_parser_start\r\n");
  406. UPS.Present = true;
  407. UPS.Flag_Present = true;
  408. UPS.cnt_err_ups = 0;
  409. data++;
  410. DBG printf("UPS ups_akb_info_parser_start: %s\r\n", data);
  411. len = get_ups_param(data, " ", value);
  412. if(UPS.model[0] == 0){
  413. for(uint8_t i = 0; i < len; i ++){
  414. if(value[i] != '#'){
  415. UPS.model[j] = value[i];
  416. j ++;
  417. }
  418. }
  419. SNMP_SetObjDescr();
  420. }
  421. else{
  422. j = 0;
  423. for(uint8_t i = 0; i < len; i ++){
  424. if(value[i] != '#'){
  425. UPS.model[j] = value[i];
  426. j ++;
  427. }
  428. }
  429. }
  430. }
  431. void ups_version_response(char *data)
  432. {
  433. char value[20];
  434. uint8_t len = 0;
  435. DBG printf("ups_akb_info_response: %s\r\n", data);
  436. if(data[0] != '(')
  437. return;
  438. DBG printf("ups_akb_info_parser_start\r\n");
  439. UPS.Present = true;
  440. UPS.Flag_Present = true;
  441. UPS.cnt_err_ups = 0;
  442. data++;
  443. DBG printf("UPS ups_akb_info_parser_start: %s\r\n", data);
  444. //TODO
  445. len = get_ups_param(data, ".", value);
  446. data += (len + 1);
  447. len = get_ups_param(data, "\r", value);
  448. if(UPS.vertion[0] == 0){
  449. strncpy(UPS.vertion, value, 2);
  450. SNMP_SetObjDescr();
  451. }
  452. else{
  453. strncpy(UPS.vertion, value, len);
  454. }
  455. }
  456. void ups_serial_response(char *data)
  457. {
  458. char value[20];
  459. uint8_t len = 0;
  460. DBG printf("ups_serialresponse: %s\r\n", data);
  461. if(data[0] != '(')
  462. return;
  463. DBG printf("ups_serial_parser_start\r\n");
  464. UPS.Present = true;
  465. UPS.Flag_Present = true;
  466. UPS.cnt_err_ups = 0;
  467. data++;
  468. DBG printf("UPS ups_serial_parser_start: %s\r\n", data);
  469. len = get_ups_param(data, "\r", value);
  470. strncpy(UPS.serial, &value[9], 5);
  471. }
  472. void ups_protocol_id_response(char *data)
  473. {
  474. DBG printf("ups_protocol_id_response: %s\r\n", data);
  475. DBG printf("ups_protocol_id_parser_start\r\n");
  476. UPS.Present = true;
  477. UPS.Flag_Present = true;
  478. UPS.cnt_err_ups = 0;
  479. if(data[0] != '('){
  480. if(strncmp(data, "NAK", 3) == 0){
  481. sSettings.UPS_Setting.type_ups = ups_kestar;
  482. }
  483. return;
  484. }
  485. data++;
  486. sSettings.UPS_Setting.type_ups = ups_voltronic;
  487. DBG printf("UPS ups_protocol_id_parser_start: %s\r\n", data);
  488. }
  489. void ups_megatec_process_pdu(cmdMegaTecEnums_t command)
  490. {
  491. switch(command)
  492. {
  493. case ups_status_req:
  494. case ups_offline_status_req:
  495. ups_status_response(ups_pdu.data);
  496. break;
  497. case ups_info:
  498. ups_info_response(ups_pdu.data);
  499. break;
  500. case ups_rating_info:
  501. break;
  502. case ups_remain_time_reg:
  503. ups_remain_time_response(ups_pdu.data);
  504. break;
  505. case ups_general_status_req:
  506. ups_general_status_response(ups_pdu.data);
  507. break;
  508. case ups_akb_info:
  509. ups_akb_info_response(ups_pdu.data);
  510. break;
  511. case ups_model_req:
  512. ups_model_response(ups_pdu.data);
  513. break;
  514. case ups_version_req:
  515. ups_version_response(ups_pdu.data);
  516. break;
  517. case ups_serial_req:
  518. ups_serial_response(ups_pdu.data);
  519. break;
  520. case ups_protocol_id_req:
  521. ups_protocol_id_response(ups_pdu.data);
  522. break;
  523. default:
  524. break;
  525. }
  526. }
  527. int ups_metac_service_pdu(cmdMegaTecEnums_t command)
  528. {
  529. if(UPS.Present){
  530. while(!megatec_send)
  531. {
  532. vTaskDelay(50);
  533. }
  534. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  535. megatec_send = false;
  536. send_MegaTec_cmd(command);
  537. if (ups_megatec_rx_pdu())
  538. {
  539. megatec_send = true;
  540. if(sSettings.UPS_Setting.type_ups == ups_kestar){
  541. if(strncmp(ups_pdu.data, "ACK", 3) == 0)
  542. return 1;
  543. else if(strncmp(ups_pdu.data, "NAK", 3) == 0)
  544. return 0;
  545. }
  546. else if(sSettings.UPS_Setting.type_ups == ups_voltronic){
  547. if(strncmp(ups_pdu.data, "(ACK", 4) == 0)
  548. return 1;
  549. else if(strncmp(ups_pdu.data, "(NAK", 4) == 0)
  550. return 0;
  551. }
  552. }
  553. megatec_send = true;
  554. }
  555. return -1;
  556. }
  557. // TODO ����������� ���������
  558. //void request_task(void)
  559. void request_task(void* params)
  560. {
  561. uint8_t kestar_req[3] = {ups_status_req, ups_remain_time_reg, ups_info};
  562. uint8_t voltronic_req[5] = {ups_status_req, ups_akb_info, ups_model_req, ups_version_req, ups_serial_req};
  563. uint8_t num_req = 0;
  564. uint8_t *req;
  565. for(;;)
  566. {
  567. if(UPS.Present == true){
  568. if(UPS.Flag_Present == false)
  569. {
  570. if(UPS.cnt_err_ups != 2)
  571. UPS.cnt_err_ups++;
  572. else{
  573. UPS.Freq_in = 0;
  574. UPS.VAC_in = 0;
  575. UPS.VAC_out = 0;
  576. UPS.Temp = 0;
  577. UPS.Load = 0;
  578. UPS.SOC = 0;
  579. UPS.work_time = 0;
  580. UPS.Status = 0;
  581. UPS.Alarm = 0;
  582. UPS.Present = false;
  583. //memset(UPS.model, 0, 11);
  584. memset(UPS.vertion, 0, 11);
  585. }
  586. }
  587. }
  588. /* if(megatec_send){
  589. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  590. megatec_send= false;
  591. UPS.Flag_Present = false;
  592. send_MegaTec_cmd(ups_protocol_id_req);
  593. if (ups_megatec_rx_pdu())
  594. ups_megatec_process_pdu(ups_protocol_id_req);
  595. megatec_send=true;
  596. }*/
  597. switch(sSettings.UPS_Setting.type_ups){
  598. case ups_kestar:
  599. num_req = sizeof(kestar_req);
  600. req = kestar_req;
  601. break;
  602. case ups_voltronic:
  603. num_req = sizeof(voltronic_req);
  604. req = voltronic_req;
  605. break;
  606. default:
  607. break;
  608. }
  609. for(uint8_t i = 0; i < num_req; i++){
  610. if(megatec_send){
  611. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  612. megatec_send= false;
  613. UPS.Flag_Present = false;
  614. send_MegaTec_cmd(req[i]);
  615. if (ups_megatec_rx_pdu())
  616. ups_megatec_process_pdu(req[i]);
  617. megatec_send=true;
  618. }
  619. }
  620. vTaskDelay(1000);
  621. }
  622. }
  623. void ups_megatec_init(void) {
  624. init_UPS_value();
  625. UPS.Present = true;
  626. xTaskCreate(request_task, ( char * ) "request_task", configMINIMAL_STACK_SIZE * 2, NULL, tskIDLE_PRIORITY, NULL);
  627. }