megatec.c 29 KB

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