megatec.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. SNMP_SendUserTrap(UPS_MODE);
  661. log_event_data(LOG_UPS_MODE, value);
  662. }
  663. #endif
  664. UPS.Mode = data[0];
  665. }
  666. void ups_megatec_process_pdu(cmdMegaTecEnums_t command)
  667. {
  668. switch (command) {
  669. case ups_status_req:
  670. case ups_offline_status_req:
  671. ups_status_response(ups_pdu.data);
  672. break;
  673. case ups_info:
  674. ups_info_response(ups_pdu.data);
  675. break;
  676. case ups_rating_info:
  677. break;
  678. case ups_remain_time_reg:
  679. ups_remain_time_response(ups_pdu.data);
  680. break;
  681. case ups_general_status_req:
  682. ups_general_status_response(ups_pdu.data);
  683. break;
  684. case ups_akb_info:
  685. ups_akb_info_response(ups_pdu.data);
  686. break;
  687. case ups_model_req:
  688. ups_model_response(ups_pdu.data);
  689. break;
  690. case ups_version_req:
  691. ups_version_response(ups_pdu.data);
  692. break;
  693. case ups_version2_req:
  694. ups_version_part2_response(ups_pdu.data);
  695. break;
  696. case ups_serial_req:
  697. ups_serial_response(ups_pdu.data);
  698. break;
  699. case ups_protocol_id_req:
  700. ups_protocol_id_response(ups_pdu.data);
  701. break;
  702. case ups_buzzer_cntrl:
  703. ups_buzzer_cntrl_response(ups_pdu.data);
  704. break;
  705. case ups_mode_req:
  706. ups_mode_response(ups_pdu.data);
  707. break;
  708. default:
  709. break;
  710. }
  711. }
  712. int ups_metac_service_pdu(cmdMegaTecEnums_t command)
  713. {
  714. uint8_t UPS_Status_prev;
  715. if (UPS.Present) {
  716. while (!megatec_send) {
  717. vTaskDelay(50);
  718. }
  719. UPS_Status_prev = UPS.Status;
  720. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  721. megatec_send = false;
  722. if (command == ups_shutdown) {//sSettings.UPS_Setting.type_ups == ups_offline &&
  723. TimeParam2 = 1;
  724. send_MegaTec_cmd(ups_shutdown_restore);
  725. } else {
  726. if((UPS.Status >> 2) & 0x01) {
  727. if (command == ups_cancel_test) {
  728. test_time = 100;
  729. }
  730. } else {
  731. if (command == ups_test_low_bat) {
  732. test_time = 100;
  733. } else {
  734. test_time = (int)TimeParam;
  735. }
  736. }
  737. send_MegaTec_cmd(command);
  738. }
  739. if (ups_megatec_rx_pdu()) {
  740. megatec_send = true;
  741. if (strncmp(ups_pdu.data, "ACK", 3) == 0 || strncmp(ups_pdu.data, "(ACK", 4) == 0) {
  742. if (command == ups_shutdown) {
  743. xTimerChangePeriod(UPSRestoreTimer, (TimeParamFloat + 0.3)*60*configTICK_RATE_HZ, 0);
  744. xTimerStart(UPSRestoreTimer, 0);
  745. } else if(command == ups_cancel_shut_down) {
  746. xTimerStop(UPSRestoreTimer, 0);
  747. } else if (command == ups_test_time) {
  748. init_var_for_testing();
  749. }
  750. return 1;
  751. } else if (strncmp(ups_pdu.data, "NAK", 3) == 0 || strncmp(ups_pdu.data, "(NAK", 4) == 0) {
  752. return 0;
  753. }
  754. }
  755. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  756. megatec_send = false;
  757. UPS.Flag_Present = false;
  758. send_MegaTec_cmd(ups_status_req);
  759. if (ups_megatec_rx_pdu()) {
  760. ups_megatec_process_pdu(ups_status_req);
  761. }
  762. megatec_send = true;
  763. if (command == ups_shutdown) {
  764. if ((((UPS.Status >> 1) & 0x01) != ((UPS_Status_prev >> 1) & 0x01))
  765. && ((UPS.Status >> 1) & 0x01)) {
  766. return 1;
  767. }
  768. } else if (command == ups_cancel_shut_down) {
  769. if ((((UPS.Status >> 1) & 0x01) != ((UPS_Status_prev >> 1) & 0x01))
  770. && !((UPS.Status >> 1) & 0x01)) {
  771. return 1;
  772. }
  773. } else if (command == ups_cancel_test) {
  774. if ((((UPS.Status >> 2) & 0x01) != ((UPS_Status_prev >> 2) & 0x01))
  775. && !((UPS.Status >> 2) & 0x01)) {
  776. return 1;
  777. }
  778. } else {
  779. if ((((UPS.Status >> 2) & 0x01) != ((UPS_Status_prev >> 2) & 0x01))
  780. && ((UPS.Status >> 2) & 0x01)) {
  781. init_var_for_testing();
  782. return 1;
  783. }
  784. }
  785. megatec_send = true;
  786. }
  787. return -1;
  788. }
  789. void kstar_mode(void)
  790. {
  791. uint8_t mode = UPS.Mode;
  792. if((UPS.Status >> 4) & 0x01) {
  793. UPS.Mode = 'F';
  794. } else if(!((UPS.Status >> 4) & 0x01) && UPS.VAC_out == 0) {
  795. UPS.Mode = 'D';
  796. } else if(((UPS.Status >> 5) & 0x01) && UPS.VAC_out > 120 && UPS.VAC_in != 0) {
  797. UPS.Mode = 'Y';
  798. } else if(((UPS.Status >> 7) & 0x01) && UPS.VAC_out > 190) {
  799. UPS.Mode = 'B';
  800. } else if(!((UPS.Status >> 7) & 0x01) && UPS.VAC_out > 190) {
  801. UPS.Mode = 'L';
  802. }
  803. #ifdef UPS_MODE_MONITOR
  804. if (UPS.Mode != mode) {
  805. char value[20];
  806. uint8_t len = 0;
  807. memset(value, 0, sizeof(value));
  808. GetModeStr(value, &len);
  809. SNMP_SendUserTrap(UPS_MODE);
  810. log_event_data(LOG_UPS_MODE, value);
  811. }
  812. #endif
  813. }
  814. // TODO ����������� ���������
  815. //void request_task(void)
  816. void request_task(void *params)
  817. {
  818. uint8_t kestar_req[3] = { ups_status_req, ups_remain_time_reg, ups_info};
  819. 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};//
  820. uint8_t num_req = 0;
  821. uint8_t *req;
  822. ups_megatec_rx_pdu();
  823. for (;;) {
  824. if (UPS.Present == true) {
  825. if (UPS.Flag_Present == false) {
  826. if (UPS.cnt_err_ups != 2) {
  827. UPS.cnt_err_ups++;
  828. } else {
  829. UPS.Freq_in = 0;
  830. UPS.VAC_in = 0;
  831. UPS.VAC_out = 0;
  832. UPS.Temp = 0;
  833. UPS.Load = 0;
  834. UPS.SOC = 0;
  835. UPS.work_time = 0;
  836. UPS.Status = 0;
  837. UPS.Mode = 0;
  838. UPS.Test_Status = 0;
  839. UPS.Alarm = 0;
  840. UPS.Present = false;
  841. //memset(UPS.model, 0, 11);
  842. memset(UPS.vertion, 0, sizeof(UPS.vertion));
  843. init_ups_rbuf();
  844. }
  845. }
  846. }
  847. if (megatec_send) {
  848. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  849. megatec_send = false;
  850. UPS.Flag_Present = false;
  851. send_MegaTec_cmd(ups_protocol_id_req);
  852. if (ups_megatec_rx_pdu()) {
  853. ups_megatec_process_pdu(ups_protocol_id_req);
  854. }
  855. megatec_send = true;
  856. }
  857. switch (sSettings.UPS_Setting.type_ups) {
  858. case ups_kestar:
  859. case ups_offline:
  860. num_req = sizeof(kestar_req);
  861. req = kestar_req;
  862. break;
  863. case ups_voltronic:
  864. num_req = sizeof(voltronic_req);
  865. req = voltronic_req;
  866. #ifdef UPS_BUZZER_OFF
  867. if (!flag_buzzer_on) {
  868. if (megatec_send) {
  869. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  870. megatec_send = false;
  871. UPS.Flag_Present = false;
  872. send_MegaTec_cmd(ups_buzzer_cntrl);
  873. if (ups_megatec_rx_pdu()) {
  874. ups_megatec_process_pdu(ups_buzzer_cntrl);
  875. }
  876. megatec_send = true;
  877. }
  878. }
  879. #endif
  880. break;
  881. default:
  882. num_req = sizeof(kestar_req);
  883. req = kestar_req;
  884. break;
  885. }
  886. vTaskDelay(1000);
  887. for (uint8_t i = 0; i < num_req; i++) {
  888. if (megatec_send) {
  889. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  890. megatec_send = false;
  891. UPS.Flag_Present = false;
  892. send_MegaTec_cmd(req[i]);
  893. if (ups_megatec_rx_pdu()) {
  894. ups_megatec_process_pdu(req[i]);
  895. }
  896. megatec_send = true;
  897. }
  898. }
  899. if(sSettings.UPS_Setting.type_ups == ups_kestar
  900. || sSettings.UPS_Setting.type_ups == ups_offline) {
  901. kstar_mode();
  902. }
  903. }
  904. }
  905. void akb_time_work_task(void *params)
  906. {
  907. static uint32_t tick_prev = 0;
  908. for (;;) {
  909. if (UPS.Present && ((UPS.Status >> 7) & 0x01)) {
  910. if (tick_prev == 0) {
  911. tick_prev = xTaskGetTickCount();
  912. }
  913. UPS.akb_work_time += (xTaskGetTickCount() - tick_prev) / 1000;
  914. tick_prev = xTaskGetTickCount();
  915. } else {
  916. tick_prev = 0;
  917. UPS.akb_work_time = 0;
  918. }
  919. vTaskDelay(1000);
  920. }
  921. }
  922. void UPSRestoreCallback(TimerHandle_t pxTimer)
  923. {
  924. if (UPS.Present) {
  925. while (!megatec_send) {
  926. vTaskDelay(50);
  927. }
  928. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  929. megatec_send = false;
  930. //UPS.Flag_Present = false;
  931. send_MegaTec_cmd(ups_remote_turn_on);
  932. if (ups_megatec_rx_pdu()) {
  933. ups_megatec_process_pdu(ups_remote_turn_on);
  934. }
  935. megatec_send = true;
  936. }
  937. }
  938. void UPSReadTestStatus(void)
  939. {
  940. if(sSettings.UPS_Setting.type_ups == ups_kestar
  941. || sSettings.UPS_Setting.type_ups == ups_offline) {
  942. return;
  943. }
  944. if (UPS.Present) {
  945. while (!megatec_send) {
  946. vTaskDelay(50);
  947. }
  948. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  949. megatec_send = false;
  950. //UPS.Flag_Present = false;
  951. send_MegaTec_cmd(ups_general_status_req);
  952. if (ups_megatec_rx_pdu()) {
  953. ups_megatec_process_pdu(ups_general_status_req);
  954. }
  955. megatec_send = true;
  956. }
  957. }
  958. void ups_megatec_init(void)
  959. {
  960. init_UPS_value();
  961. UPS.Present = true;
  962. xTaskCreate(request_task, ( char * ) "request_task", configMINIMAL_STACK_SIZE * 2, NULL, tskIDLE_PRIORITY, NULL);
  963. xTaskCreate(akb_time_work_task, ( char * ) "akb_time_work_task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY,
  964. NULL);
  965. UPSRestoreTimer = xTimerCreate("UPSRestoreTmr", configTICK_RATE_HZ*30, pdFALSE, ( void * ) 0, UPSRestoreCallback);
  966. }