megatec.c 29 KB

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