megatec.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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 "fr_timers.h"
  10. #include "usart.h"
  11. #include "megatec.h"
  12. #include "ups_monitor.h"
  13. #include "log.h"
  14. #include "snmp_api.h"
  15. #include "settings_api.h"
  16. #ifdef PRINTF_STDLIB
  17. #include <stdio.h>
  18. #endif
  19. #ifdef PRINTF_CUSTOM
  20. #include "tinystdio.h"
  21. #endif
  22. #include <string.h>
  23. #include <math.h>
  24. #define DBG if (0)
  25. TimerHandle_t UPSRestoreTimer;
  26. /**
  27. * @brief Общая структура настроек
  28. */
  29. extern SETTINGS_t sSettings;
  30. #define UPS_PDU_MAX_LEN 50
  31. float TimeParamFloat = 0;
  32. uint16_t TimeParam = 0;
  33. uint16_t TimeParam2 = 0;
  34. bool megatec_send = true;
  35. UPS_value_t UPS;
  36. static bool flag_buzzer_on = false;
  37. enum {
  38. NORMAL = 0x00,
  39. VER_ERROR = 0x01,
  40. CHKSUM_ERROR = 0x02,
  41. LCHKSUM_ERROR = 0x03,
  42. CID2_INVALID = 0x04,
  43. CMD_FMT_ERROR = 0x05,
  44. INVALID_DATA = 0x06,
  45. };
  46. static struct {
  47. uint8_t data[UPS_PDU_MAX_LEN];
  48. uint16_t len;
  49. } ups_pdu;
  50. const char *MegaTecCMD[] = {
  51. "Q1\r",
  52. "T\r",
  53. "TL\r",
  54. "T",
  55. "Q\r",
  56. "S",
  57. "R",
  58. "C\r",
  59. "CT\r",
  60. "I\r",
  61. "F\r",
  62. "Q2\r",
  63. "QGS\r",
  64. "QBV\r",
  65. "QMD\r",
  66. "QVFW\r",
  67. "QVFW2\r",
  68. "QID\r",
  69. "QPI\r",
  70. "QS\r",
  71. "M\r",
  72. "PDA\r",
  73. "QMOD\r",
  74. "SON\r",
  75. };
  76. extern bool flUpdateLog;
  77. void init_UPS_value(void)
  78. {
  79. UPS.Freq_in = 0;
  80. UPS.VAC_in = 0;
  81. UPS.VAC_out = 0;
  82. UPS.Temp = 0;
  83. UPS.Load = 0;
  84. UPS.SOC = 0;
  85. UPS.SOC_test = 0;
  86. UPS.work_time = 0;
  87. UPS.akb_work_time = 0;
  88. UPS.Status = 0;
  89. UPS.Mode = 0;
  90. UPS.Alarm = 0;
  91. UPS.cnt_err_ups = 0;
  92. UPS.Flag_Present = false;
  93. UPS.Present = false;
  94. memset(UPS.model, 0, sizeof(UPS.model));
  95. memset(UPS.vertion, 0, sizeof(UPS.vertion));
  96. memset(UPS.serial, 0, sizeof(UPS.serial));
  97. }
  98. void send_MegaTec_cmd(cmdMegaTecEnums_t command)
  99. {
  100. DBG printf("MegaTecCMD: %s\r\n", MegaTecCMD[command]);
  101. if (command == ups_test_time) {
  102. uint8_t req[10];
  103. memset(req, 0, 10);
  104. if (TimeParam < 10) {
  105. sprintf(req, "%s0%d\r", MegaTecCMD[command], TimeParam);
  106. } else {
  107. sprintf(req, "%s%d\r", MegaTecCMD[command], TimeParam);
  108. }
  109. ups_send_block(req, strlen(req));
  110. } else if (command == ups_shutdown) {
  111. uint8_t req[10];
  112. memset(req, 0, 10);
  113. if (TimeParamFloat >= 1 && TimeParamFloat < 10) {
  114. sprintf(req, "%s0%d\r", MegaTecCMD[command], (uint16_t)TimeParamFloat);
  115. } else if (TimeParamFloat < 1) {
  116. sprintf(req, "%s.%d\r", MegaTecCMD[command], (uint16_t)(10 * TimeParamFloat));
  117. } else {
  118. sprintf(req, "%s%d\r", MegaTecCMD[command], (uint16_t)TimeParamFloat);
  119. }
  120. ups_send_block(req, strlen(req));
  121. } else if (command == ups_shutdown_restore) {
  122. uint8_t req[10];
  123. memset(req, 0, 10);
  124. if (TimeParamFloat >= 1 && TimeParamFloat < 10) {
  125. sprintf(req, "%s0%d%s000%d\r", MegaTecCMD[command - 1], (uint16_t)TimeParamFloat, MegaTecCMD[command], TimeParam2);
  126. } else if (TimeParamFloat < 1) {
  127. sprintf(req, "%s.%d%s000%d\r", MegaTecCMD[command - 1], (uint16_t)(10 * TimeParamFloat), MegaTecCMD[command], TimeParam2);
  128. } else {
  129. sprintf(req, "%s%d%s000%d\r", MegaTecCMD[command - 1], (uint16_t)TimeParamFloat, MegaTecCMD[command], TimeParam2);
  130. }
  131. ups_send_block(req, strlen(req));
  132. } else {
  133. // TODO ����������� ���������
  134. //ups_send_block(MegaTecCMD[command], strlen(MegaTecCMD[command]));
  135. ups_send_block((void *)MegaTecCMD[command], strlen(MegaTecCMD[command]));
  136. }
  137. }
  138. bool ups_megatec_rx_pdu(void)
  139. {
  140. int c = 0;
  141. uint8_t cnt_answer = 0;
  142. ups_pdu.len = 0;
  143. while (c >= 0) {
  144. c = ups_getchar(300);//portMAX_DELAY200
  145. if (c >= 0) {
  146. ups_pdu.len = 0;
  147. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  148. ups_pdu.data[ups_pdu.len++] = c;
  149. while ((ups_pdu.len < UPS_PDU_MAX_LEN) && (c != 0x0d)) {
  150. c = ups_getchar(100);//portMAX_DELAY200
  151. if (c < 0) {
  152. ups_pdu.len = 0;
  153. return false;
  154. }
  155. ups_pdu.data[ups_pdu.len++] = c;
  156. }
  157. DBG printf("UPS raw data: %s\r\n", ups_pdu.data);
  158. cnt_answer ++;
  159. } else {
  160. if (ups_pdu.len == 0) {
  161. return false;
  162. }
  163. }
  164. }
  165. if (cnt_answer > 1) {
  166. DBG printf("MegaTecCMD: false\r\n");
  167. return false;
  168. }
  169. DBG printf("UPS raw data: %s\r\n", ups_pdu.data);
  170. DBG printf("UPS raw data len: %d\r\n", ups_pdu.len);
  171. return true;
  172. }
  173. int8_t get_ups_param(char *buf, char *param, char *val)
  174. {
  175. char *endValue;
  176. int8_t param_len;
  177. memset(val, 0, 20);
  178. endValue = strpbrk(buf, param);
  179. if (endValue != NULL) {
  180. param_len = endValue - buf;
  181. if (param_len < 20) {
  182. strncpy(val, buf, param_len);
  183. } else {
  184. param_len = 0;
  185. }
  186. } else {
  187. param_len = 0;
  188. }
  189. DBG printf("UPS parameter: %s\r\n", val);
  190. return param_len;
  191. }
  192. void ups_status_response(char *data)
  193. {
  194. uint8_t i;
  195. char value[20];
  196. float tmp;
  197. uint8_t len = 0;
  198. DBG printf("ups_status_response: %s\r\n", data);
  199. if (data[0] != '(') {
  200. return;
  201. }
  202. DBG printf("ups_status_parser_start\r\n");
  203. UPS.Present = true;
  204. UPS.Flag_Present = true;
  205. UPS.cnt_err_ups = 0;
  206. #ifdef UPS_BUZZER_OFF
  207. if (flag_buzzer_on) {
  208. data[45] = '0';
  209. }
  210. #endif
  211. if (flUpdateLog) {
  212. flUpdateLog = false;
  213. log_add(data);
  214. }
  215. data++;
  216. DBG printf("UPS ups_status_parser_startr: %s\r\n", data);
  217. len = get_ups_param(data, " ", value);
  218. data += (len + 1);
  219. if (len > 0) {
  220. tmp = atof(value);
  221. if (tmp > 20) {
  222. UPS.VAC_in = tmp;
  223. } else {
  224. UPS.VAC_in = 0;
  225. }
  226. }
  227. //TODO
  228. len = get_ups_param(data, " ", value);
  229. data += (len + 1);
  230. len = get_ups_param(data, " ", value);
  231. data += (len + 1);
  232. if (len > 0) {
  233. UPS.VAC_out = atof(value);
  234. }
  235. len = get_ups_param(data, " ", value);
  236. data += (len + 1);
  237. if (len > 0) {
  238. UPS.Load = atoi(value);
  239. }
  240. len = get_ups_param(data, " ", value);
  241. data += (len + 1);
  242. if (len > 0) {
  243. UPS.Freq_in = atof(value);
  244. }
  245. //TODO
  246. len = get_ups_param(data, " ", value);
  247. data += (len + 1);
  248. if (len > 0) {
  249. UPS.Vakb_curr = atof(value);
  250. }
  251. if (sSettings.UPS_Setting.type_ups == ups_kestar || sSettings.UPS_Setting.type_ups == ups_offline) {
  252. if (UPS.Vakb_curr < 7) {
  253. UPS.SOC = round(100 * (UPS.Vakb_curr - sSettings.UPS_Setting.Ucellmin) / (sSettings.UPS_Setting.Ucellmax -
  254. sSettings.UPS_Setting.Ucellmin));
  255. } else {
  256. UPS.SOC = round(100 * (UPS.Vakb_curr / AKB_NUM_CELL - sSettings.UPS_Setting.Ucellmin) /
  257. (sSettings.UPS_Setting.Ucellmax - sSettings.UPS_Setting.Ucellmin));
  258. }
  259. }
  260. len = get_ups_param(data, " ", value);
  261. data += (len + 1);
  262. if (len > 0) {
  263. UPS.Temp = atof(value);
  264. }
  265. len = get_ups_param(data, "\r", value);
  266. data += (len + 1);
  267. if (len > 0) {
  268. uint8_t stat = 0;
  269. for (i = 0; i < len; i ++) {
  270. stat |= (value[i] - 0x30) << (7 - i);
  271. }
  272. UPS.Status = stat;
  273. }
  274. #ifdef RELAY_ALARM_AKB
  275. if (sSettings.UPS_Setting.type_ups == ups_kestar || sSettings.UPS_Setting.type_ups == ups_offline) {
  276. UPS_TestSOCMonitor();
  277. }
  278. #endif
  279. }
  280. void ups_general_status_response(char *data)
  281. {
  282. uint8_t i;
  283. char value[20];
  284. float tmp;
  285. uint8_t len = 0;
  286. DBG printf("ups_status_response: %s\r\n", data);
  287. if (data[0] != '(') {
  288. return;
  289. }
  290. DBG printf("ups_status_parser_start\r\n");
  291. UPS.Present = true;
  292. UPS.Flag_Present = true;
  293. UPS.cnt_err_ups = 0;
  294. if (flUpdateLog) {
  295. flUpdateLog = false;
  296. log_add(data);
  297. }
  298. data++;
  299. DBG printf("UPS ups_status_parser_startr: %s\r\n", data);
  300. len = get_ups_param(data, " ", value);
  301. data += (len + 1);
  302. if (len > 0) {
  303. tmp = atof(value);
  304. if (tmp > 20) {
  305. UPS.VAC_in = tmp;
  306. } else {
  307. UPS.VAC_in = 0;
  308. }
  309. }
  310. len = get_ups_param(data, " ", value);
  311. data += (len + 1);
  312. if (len > 0) {
  313. UPS.Freq_in = atof(value);
  314. }
  315. len = get_ups_param(data, " ", value);
  316. data += (len + 1);
  317. if (len > 0) {
  318. UPS.VAC_out = atof(value);
  319. }
  320. //TODO
  321. len = get_ups_param(data, " ", value);
  322. data += (len + 1);
  323. //TODO
  324. len = get_ups_param(data, " ", value);
  325. data += (len + 1);
  326. len = get_ups_param(data, " ", value);
  327. data += (len + 1);
  328. if (len > 0) {
  329. UPS.Load = atoi(value);
  330. }
  331. //TODO
  332. len = get_ups_param(data, " ", value);
  333. data += (len + 1);
  334. //TODO
  335. len = get_ups_param(data, " ", value);
  336. data += (len + 1);
  337. //TODO
  338. len = get_ups_param(data, " ", value);
  339. data += (len + 1);
  340. //TODO
  341. len = get_ups_param(data, " ", value);
  342. data += (len + 1);
  343. len = get_ups_param(data, " ", value);
  344. data += (len + 1);
  345. if (len > 0) {
  346. UPS.Temp = atof(value);
  347. }
  348. len = get_ups_param(data, "\r", value);
  349. data += (len + 1);
  350. if (len > 0) {
  351. uint8_t stat = 0;
  352. for (i = 2; i < (len - 2); i ++) {
  353. stat |= (value[i] - 0x30) << (7 - i);
  354. }
  355. UPS.Status = stat;
  356. }
  357. }
  358. void ups_info_response(char *data)
  359. {
  360. uint8_t i = 0, j = 0;
  361. char value[20];
  362. DBG printf("ups_info_response: %s\r\n", data);
  363. if (data[0] != '#') {
  364. return;
  365. }
  366. DBG printf("ups_info_response_startr: %s\r\n", data);
  367. UPS.Present = true;
  368. UPS.Flag_Present = true;
  369. UPS.cnt_err_ups = 0;
  370. data++;
  371. data += 16; //пропускаем поле название компании
  372. memset(value, 0, sizeof(value));
  373. for (uint8_t i = 0; i < KSTAR_MODEL_LENGTH; i ++) {
  374. if (data[i] != ' ') {
  375. value[j] = data[i];
  376. j ++;
  377. }
  378. }
  379. if (j != 0) {
  380. if (UPS.model[0] == 0) {
  381. strncpy(UPS.model, value, strlen(value));
  382. SNMP_SetObjDescr();
  383. } else {
  384. strncpy(UPS.model, data, strlen(value));
  385. }
  386. } else {
  387. if (UPS.model[0] == 0) {
  388. strcpy(UPS.model, "RTMP-II");
  389. SNMP_SetObjDescr();
  390. } else {
  391. strcpy(UPS.model, "RTMP-II");
  392. }
  393. }
  394. data += (KSTAR_MODEL_LENGTH + 1);
  395. strncpy(UPS.serial, data, 8);
  396. data += 8;
  397. strncpy(UPS.vertion, data, 2);
  398. SNMP_SetObjDescr();
  399. }
  400. void ups_remain_time_response(char *data)
  401. {
  402. char value[20];
  403. if (data[0] != '(') {
  404. return;
  405. }
  406. DBG printf("ups_remain_time_response: %s\r\n", data);
  407. UPS.Present = true;
  408. UPS.Flag_Present = true;
  409. UPS.cnt_err_ups = 0;
  410. data++;
  411. if (strlen(data) > 5) {
  412. return;
  413. }
  414. memset(value, 0, 10);
  415. strcpy(value, data);
  416. //if((UPS.Status >> 7) & 0x01)
  417. UPS.work_time = atoi(value);
  418. //else
  419. // UPS.work_time = 0;
  420. }
  421. void ups_akb_info_response(char *data)
  422. {
  423. char value[20];
  424. uint8_t len = 0;
  425. DBG printf("ups_akb_info_response: %s\r\n", data);
  426. if (data[0] != '(') {
  427. return;
  428. }
  429. DBG printf("ups_akb_info_parser_start\r\n");
  430. UPS.Present = true;
  431. UPS.Flag_Present = true;
  432. UPS.cnt_err_ups = 0;
  433. data++;
  434. DBG printf("UPS ups_akb_info_parser_start: %s\r\n", data);
  435. //TODO
  436. len = get_ups_param(data, " ", value);
  437. data += (len + 1);
  438. //TODO
  439. len = get_ups_param(data, " ", value);
  440. data += (len + 1);
  441. //TODO
  442. len = get_ups_param(data, " ", value);
  443. data += (len + 1);
  444. len = get_ups_param(data, " ", value);
  445. data += (len + 1);
  446. if (len > 0) {
  447. UPS.SOC = atoi(value);
  448. }
  449. #ifdef RELAY_ALARM_AKB
  450. UPS_TestSOCMonitor();
  451. #endif
  452. len = get_ups_param(data, "\r", value);
  453. data += (len + 1);
  454. if (len > 0) {
  455. UPS.work_time = atoi(value);
  456. }
  457. }
  458. void ups_model_response(char *data)
  459. {
  460. uint8_t j = 0;
  461. char value[20];
  462. uint8_t len = 0;
  463. DBG printf("ups_akb_info_response: %s\r\n", data);
  464. if (data[0] != '(') {
  465. return;
  466. }
  467. DBG printf("ups_akb_info_parser_start\r\n");
  468. UPS.Present = true;
  469. UPS.Flag_Present = true;
  470. UPS.cnt_err_ups = 0;
  471. data++;
  472. DBG printf("UPS ups_akb_info_parser_start: %s\r\n", data);
  473. memset(value, 0, 20);
  474. if (UPS.model[0] == 0) {
  475. for (uint8_t i = 0; i < VOLTRONIC_MODEL_LENGTH; i ++) {
  476. if (data[i] != '#') {
  477. value[j] = data[i];
  478. j ++;
  479. }
  480. }
  481. if(strncmp(value, "WPHV", 4) == 0 || strncmp(value, "G2", 2) == 0 || value[0] == 0) {
  482. strcpy(UPS.model, "RTMP-II");
  483. } else {
  484. len = strlen(value);
  485. strncpy(UPS.model, value, len);
  486. }
  487. SNMP_SetObjDescr();
  488. } else {
  489. return;
  490. }
  491. }
  492. void ups_version_response(char *data)
  493. {
  494. char value[20];
  495. uint8_t len = 0;
  496. DBG printf("ups_akb_info_response: %s\r\n", data);
  497. if (data[0] != '(') {
  498. return;
  499. }
  500. DBG printf("ups_akb_info_parser_start\r\n");
  501. UPS.Present = true;
  502. UPS.Flag_Present = true;
  503. UPS.cnt_err_ups = 0;
  504. data++;
  505. if (UPS.vertion[0] != 0) {
  506. if(strncmp(data, UPS.vertion, strlen(UPS.vertion)) == 0){
  507. return;
  508. }
  509. }
  510. memset(UPS.vertion, 0, sizeof(UPS.vertion));
  511. len = get_ups_param(data, ":", value);
  512. data += (len + 1);
  513. DBG printf("UPS ups_akb_info_parser_start: %s\r\n", data);
  514. len = get_ups_param(data, "\r", value);
  515. if (len > VOLTRONIC_VERSION_LENGTH) {
  516. len = VOLTRONIC_VERSION_LENGTH;
  517. }
  518. strncpy(UPS.vertion, value, len);
  519. }
  520. void ups_version_part2_response(char *data)
  521. {
  522. char value[20];
  523. uint8_t len = 0;
  524. DBG printf("ups_akb_info_response: %s\r\n", data);
  525. if (data[0] != '(') {
  526. return;
  527. }
  528. DBG printf("ups_akb_info_parser_start\r\n");
  529. UPS.Present = true;
  530. UPS.Flag_Present = true;
  531. UPS.cnt_err_ups = 0;
  532. if (strncmp(ups_pdu.data, "(NAK", 4) == 0) {
  533. return;
  534. }
  535. data++;
  536. if (UPS.vertion[VOLTRONIC_VERSION_LENGTH] != 0) {
  537. return;
  538. }
  539. DBG printf("UPS ups_akb_info_parser_start: %s\r\n", data);
  540. len = get_ups_param(data, ":", value);
  541. data += (len + 1);
  542. len = get_ups_param(data, "\r", value);
  543. if (len > VOLTRONIC_VERSION_LENGTH) {
  544. len = VOLTRONIC_VERSION_LENGTH;
  545. }
  546. strncat(UPS.vertion, "/", 1);
  547. strncat(UPS.vertion, value, len);
  548. }
  549. void ups_serial_response(char *data)
  550. {
  551. char value[20];
  552. uint8_t len = 0;
  553. DBG printf("ups_serialresponse: %s\r\n", data);
  554. if (data[0] != '(') {
  555. return;
  556. }
  557. DBG printf("ups_serial_parser_start\r\n");
  558. UPS.Present = true;
  559. UPS.Flag_Present = true;
  560. UPS.cnt_err_ups = 0;
  561. data++;
  562. DBG printf("UPS ups_serial_parser_start: %s\r\n", data);
  563. len = get_ups_param(data, "\r", value);
  564. strncpy(UPS.serial, value, VOLTRONIC_SN_LENGTH);
  565. }
  566. void ups_protocol_id_response(char *data)
  567. {
  568. DBG printf("ups_protocol_id_response: %s\r\n", data);
  569. UPS.Present = true;
  570. UPS.Flag_Present = true;
  571. UPS.cnt_err_ups = 0;
  572. if (data[0] != '(') {
  573. if (strncmp(data, "NAK", 3) == 0 || strncmp(data, " \r", 2) == 0) {
  574. sSettings.UPS_Setting.type_ups = ups_kestar;
  575. }
  576. else if(strncmp(data, "QPI", 3) == 0) {
  577. sSettings.UPS_Setting.type_ups = ups_offline;
  578. }
  579. return;
  580. }
  581. data++;
  582. sSettings.UPS_Setting.type_ups = ups_voltronic;
  583. DBG printf("UPS ups_protocol_id_parser_start: %s\r\n", data);
  584. }
  585. void ups_buzzer_cntrl_response(char *data)
  586. {
  587. UPS.Present = true;
  588. UPS.Flag_Present = true;
  589. UPS.cnt_err_ups = 0;
  590. if (strlen(data) != 0) {
  591. if (strncmp(data, "(ACK", 4) == 0) {
  592. flag_buzzer_on = true;
  593. }
  594. }
  595. DBG printf("UPS ups_buzzer_cntrl_parser_start: %s\r\n", data);
  596. }
  597. void ups_mode_response(char *data)
  598. {
  599. char value[20];
  600. uint8_t len = 0;
  601. DBG printf("ups_serialresponse: %s\r\n", data);
  602. if (data[0] != '(') {
  603. return;
  604. }
  605. DBG printf("ups_serial_parser_start\r\n");
  606. UPS.Present = true;
  607. UPS.Flag_Present = true;
  608. UPS.cnt_err_ups = 0;
  609. data++;
  610. DBG printf("UPS ups_mode_parser_start: %s\r\n", data);
  611. UPS.Mode = data[0];
  612. }
  613. void ups_megatec_process_pdu(cmdMegaTecEnums_t command)
  614. {
  615. switch (command) {
  616. case ups_status_req:
  617. case ups_offline_status_req:
  618. ups_status_response(ups_pdu.data);
  619. break;
  620. case ups_info:
  621. ups_info_response(ups_pdu.data);
  622. break;
  623. case ups_rating_info:
  624. break;
  625. case ups_remain_time_reg:
  626. ups_remain_time_response(ups_pdu.data);
  627. break;
  628. case ups_general_status_req:
  629. ups_general_status_response(ups_pdu.data);
  630. break;
  631. case ups_akb_info:
  632. ups_akb_info_response(ups_pdu.data);
  633. break;
  634. case ups_model_req:
  635. ups_model_response(ups_pdu.data);
  636. break;
  637. case ups_version_req:
  638. ups_version_response(ups_pdu.data);
  639. break;
  640. case ups_version2_req:
  641. ups_version_part2_response(ups_pdu.data);
  642. break;
  643. case ups_serial_req:
  644. ups_serial_response(ups_pdu.data);
  645. break;
  646. case ups_protocol_id_req:
  647. ups_protocol_id_response(ups_pdu.data);
  648. break;
  649. case ups_buzzer_cntrl:
  650. ups_buzzer_cntrl_response(ups_pdu.data);
  651. break;
  652. case ups_mode_req:
  653. ups_mode_response(ups_pdu.data);
  654. break;
  655. default:
  656. break;
  657. }
  658. }
  659. int ups_metac_service_pdu(cmdMegaTecEnums_t command)
  660. {
  661. uint8_t UPS_Status_prev;
  662. if (UPS.Present) {
  663. while (!megatec_send) {
  664. vTaskDelay(50);
  665. }
  666. UPS_Status_prev = UPS.Status;
  667. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  668. megatec_send = false;
  669. if (command == ups_shutdown) {//sSettings.UPS_Setting.type_ups == ups_offline &&
  670. TimeParam2 = 1;
  671. send_MegaTec_cmd(ups_shutdown_restore);
  672. } else {
  673. UPS.SOC_test = UPS.SOC;
  674. send_MegaTec_cmd(command);
  675. }
  676. if (ups_megatec_rx_pdu()) {
  677. megatec_send = true;
  678. if (strncmp(ups_pdu.data, "ACK", 3) == 0 || strncmp(ups_pdu.data, "(ACK", 4) == 0) {
  679. if (command == ups_shutdown) {
  680. xTimerChangePeriod(UPSRestoreTimer, (TimeParamFloat + 0.3)*60*configTICK_RATE_HZ, 0);
  681. xTimerStart(UPSRestoreTimer, 0);
  682. } else if(command == ups_cancel_shut_down) {
  683. xTimerStop(UPSRestoreTimer, 0);
  684. }
  685. return 1;
  686. } else if (strncmp(ups_pdu.data, "NAK", 3) == 0 || strncmp(ups_pdu.data, "(NAK", 4) == 0) {
  687. return 0;
  688. }
  689. }
  690. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  691. megatec_send = false;
  692. UPS.Flag_Present = false;
  693. send_MegaTec_cmd(ups_status_req);
  694. if (ups_megatec_rx_pdu()) {
  695. ups_megatec_process_pdu(ups_status_req);
  696. }
  697. megatec_send = true;
  698. if (command == ups_shutdown) {
  699. if ((((UPS.Status >> 1) & 0x01) != ((UPS_Status_prev >> 1) & 0x01))
  700. && ((UPS.Status >> 1) & 0x01)) {
  701. return 1;
  702. }
  703. } else if (command == ups_cancel_shut_down) {
  704. if ((((UPS.Status >> 1) & 0x01) != ((UPS_Status_prev >> 1) & 0x01))
  705. && !((UPS.Status >> 1) & 0x01)) {
  706. return 1;
  707. }
  708. } else if (command == ups_cancel_test) {
  709. if ((((UPS.Status >> 2) & 0x01) != ((UPS_Status_prev >> 2) & 0x01))
  710. && !((UPS.Status >> 2) & 0x01)) {
  711. return 1;
  712. }
  713. } else {
  714. if ((((UPS.Status >> 2) & 0x01) != ((UPS_Status_prev >> 2) & 0x01))
  715. && ((UPS.Status >> 2) & 0x01)) {
  716. return 1;
  717. }
  718. }
  719. megatec_send = true;
  720. }
  721. return -1;
  722. }
  723. void kstar_mode(void)
  724. {
  725. if((UPS.Status >> 4) & 0x01) {
  726. UPS.Mode = 'F';
  727. return;
  728. }
  729. if(!((UPS.Status >> 4) & 0x01) && UPS.VAC_out == 0) {
  730. UPS.Mode = 'D';
  731. return;
  732. }
  733. if(((UPS.Status >> 5) & 0x01) && UPS.VAC_out > 120 && UPS.VAC_in != 0) {
  734. UPS.Mode = 'Y';
  735. return;
  736. }
  737. if(((UPS.Status >> 7) & 0x01) && UPS.VAC_out > 190) {
  738. UPS.Mode = 'B';
  739. return;
  740. }
  741. if(!((UPS.Status >> 7) & 0x01) && UPS.VAC_out > 190) {
  742. UPS.Mode = 'L';
  743. return;
  744. }
  745. }
  746. // TODO ����������� ���������
  747. //void request_task(void)
  748. void request_task(void *params)
  749. {
  750. uint8_t kestar_req[3] = { ups_status_req, ups_remain_time_reg, ups_info};
  751. uint8_t voltronic_req[7] = {ups_status_req, ups_akb_info, ups_model_req, ups_version_req, ups_version2_req, ups_serial_req, ups_mode_req};//
  752. uint8_t num_req = 0;
  753. uint8_t *req;
  754. ups_megatec_rx_pdu();
  755. for (;;) {
  756. if (UPS.Present == true) {
  757. if (UPS.Flag_Present == false) {
  758. if (UPS.cnt_err_ups != 2) {
  759. UPS.cnt_err_ups++;
  760. } else {
  761. UPS.Freq_in = 0;
  762. UPS.VAC_in = 0;
  763. UPS.VAC_out = 0;
  764. UPS.Temp = 0;
  765. UPS.Load = 0;
  766. UPS.SOC = 0;
  767. UPS.work_time = 0;
  768. UPS.Status = 0;
  769. UPS.Mode = 0;
  770. UPS.Alarm = 0;
  771. UPS.Present = false;
  772. //memset(UPS.model, 0, 11);
  773. memset(UPS.vertion, 0, sizeof(UPS.vertion));
  774. init_ups_rbuf();
  775. }
  776. }
  777. }
  778. if (megatec_send) {
  779. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  780. megatec_send = false;
  781. UPS.Flag_Present = false;
  782. send_MegaTec_cmd(ups_protocol_id_req);
  783. if (ups_megatec_rx_pdu()) {
  784. ups_megatec_process_pdu(ups_protocol_id_req);
  785. }
  786. megatec_send = true;
  787. }
  788. switch (sSettings.UPS_Setting.type_ups) {
  789. case ups_kestar:
  790. case ups_offline:
  791. num_req = sizeof(kestar_req);
  792. req = kestar_req;
  793. break;
  794. case ups_voltronic:
  795. num_req = sizeof(voltronic_req);
  796. req = voltronic_req;
  797. #ifdef UPS_BUZZER_OFF
  798. if (!flag_buzzer_on) {
  799. if (megatec_send) {
  800. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  801. megatec_send = false;
  802. UPS.Flag_Present = false;
  803. send_MegaTec_cmd(ups_buzzer_cntrl);
  804. if (ups_megatec_rx_pdu()) {
  805. ups_megatec_process_pdu(ups_buzzer_cntrl);
  806. }
  807. megatec_send = true;
  808. }
  809. }
  810. #endif
  811. break;
  812. default:
  813. num_req = sizeof(kestar_req);
  814. req = kestar_req;
  815. break;
  816. }
  817. vTaskDelay(1000);
  818. for (uint8_t i = 0; i < num_req; i++) {
  819. if (megatec_send) {
  820. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  821. megatec_send = false;
  822. UPS.Flag_Present = false;
  823. send_MegaTec_cmd(req[i]);
  824. if (ups_megatec_rx_pdu()) {
  825. ups_megatec_process_pdu(req[i]);
  826. }
  827. megatec_send = true;
  828. }
  829. }
  830. if(sSettings.UPS_Setting.type_ups == ups_kestar
  831. || sSettings.UPS_Setting.type_ups == ups_offline) {
  832. kstar_mode();
  833. }
  834. }
  835. }
  836. void akb_time_work_task(void *params)
  837. {
  838. static uint32_t tick_prev = 0;
  839. for (;;) {
  840. if (UPS.Present && ((UPS.Status >> 7) & 0x01)) {
  841. if (tick_prev == 0) {
  842. tick_prev = xTaskGetTickCount();
  843. }
  844. UPS.akb_work_time += (xTaskGetTickCount() - tick_prev) / 1000;
  845. tick_prev = xTaskGetTickCount();
  846. } else {
  847. tick_prev = 0;
  848. UPS.akb_work_time = 0;
  849. }
  850. vTaskDelay(1000);
  851. }
  852. }
  853. void UPSRestoreCallback(TimerHandle_t pxTimer)
  854. {
  855. if (UPS.Present) {
  856. while (!megatec_send) {
  857. vTaskDelay(50);
  858. }
  859. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  860. megatec_send = false;
  861. //UPS.Flag_Present = false;
  862. send_MegaTec_cmd(ups_remote_turn_on);
  863. if (ups_megatec_rx_pdu()) {
  864. ups_megatec_process_pdu(ups_remote_turn_on);
  865. }
  866. megatec_send = true;
  867. }
  868. }
  869. void ups_megatec_init(void)
  870. {
  871. init_UPS_value();
  872. UPS.Present = true;
  873. xTaskCreate(request_task, ( char * ) "request_task", configMINIMAL_STACK_SIZE * 2, NULL, tskIDLE_PRIORITY, NULL);
  874. xTaskCreate(akb_time_work_task, ( char * ) "akb_time_work_task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY,
  875. NULL);
  876. UPSRestoreTimer = xTimerCreate("UPSRestoreTmr", configTICK_RATE_HZ*30, pdFALSE, ( void * ) 0, UPSRestoreCallback);
  877. }