megatec.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  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. #include "hal.h"
  19. #include "rtc.h"
  20. #include "crc_calc.h"
  21. #ifdef PRINTF_STDLIB
  22. #include <stdio.h>
  23. #endif
  24. #ifdef PRINTF_CUSTOM
  25. #include "tinystdio.h"
  26. #endif
  27. #include <string.h>
  28. #include <math.h>
  29. #define DBG if (0)
  30. TimerHandle_t UPSRestoreTimer;
  31. TimerHandle_t AutoTestTimer;
  32. /**
  33. * @brief Общая структура настроек
  34. */
  35. extern SETTINGS_t sSettings;
  36. #define UPS_PDU_MAX_LEN 80
  37. #define UPS_PDU_MIN_LEN 5
  38. float TimeParamFloat = 0;
  39. uint16_t TimeParam = 0;
  40. uint16_t TimeParam2 = 0;
  41. bool megatec_send = true;
  42. UPS_value_t UPS;
  43. int test_time = 0;
  44. static bool flag_buzzer_on = false;
  45. static float Ubat_sum = 0;
  46. static float Pload_sum = 0;
  47. uint32_t Period_testing = 0;
  48. static bool sync_data = false;
  49. static bool QFLAG_L = false;
  50. enum {
  51. NORMAL = 0x00,
  52. VER_ERROR = 0x01,
  53. CHKSUM_ERROR = 0x02,
  54. LCHKSUM_ERROR = 0x03,
  55. CID2_INVALID = 0x04,
  56. CMD_FMT_ERROR = 0x05,
  57. INVALID_DATA = 0x06,
  58. };
  59. static struct {
  60. uint8_t data[UPS_PDU_MAX_LEN];
  61. uint16_t len;
  62. } ups_pdu;
  63. const char *MegaTecCMD[] = {
  64. "Q1\r", // ups_status_req [1] - OK
  65. "T\r", // ups_test_10sec
  66. "TL\r", // ups_test_low_bat
  67. "T", // ups_test_time
  68. "Q\r", // ups_beep
  69. "S", // ups_shutdown
  70. "R", // ups_shutdown_restore
  71. "C\r", // ups_cancel_shut_down
  72. "CT\r", // ups_cancel_test
  73. "I\r", // ups_info [3] - OK
  74. "F\r", // ups_rating_info
  75. "Q2\r", // ups_remain_time_reg [2] - ERR
  76. "QGS\r", // ups_general_status_req
  77. "QBV\r", // ups_akb_info
  78. "QMD\r", // ups_model_req
  79. "QVFW\r", // ups_version_req
  80. "QVFW2\r", // ups_version2_req
  81. "QID\r", // ups_serial_req
  82. "QPI\r", // ups_protocol_id_req
  83. "QS\r", // ups_offline_status_req
  84. "M\r", // ups_passw_req
  85. "PDA\r", // ups_buzzer_cntrl
  86. "QMOD\r", // ups_mode_req
  87. "SON\r", // ups_remote_turn_on
  88. "QWS\r", // ups_warning_status
  89. "QBYV\r", // ups_bypass_volt_range
  90. "PHV264\r", // ups_bypass_hvolt_264_set
  91. "QFLAG\r", // ups_qflag_status
  92. "PEL\r", // ups_cmd_enable_flag_l
  93. "BT\r", // ups_kstar_status [4] - ERR
  94. "QTPR\r", // ups_temperature
  95. "QFS\r", // ups_fault_status
  96. "SOFF\r", // ups_remote_turn_off
  97. };
  98. extern bool flUpdateLog;
  99. static bool flag_WPHVR2K0L = false;
  100. void AutoTestCallback(void);
  101. void init_UPS_value(void)
  102. {
  103. UPS.Freq_in = 0;
  104. UPS.VAC_in = 0;
  105. UPS.VAC_out = 0;
  106. UPS.Temp = 0;
  107. UPS.data_check_load = 0;
  108. UPS.data_check_load_flag = false;
  109. UPS.Load = 0;
  110. UPS.Load_test_akb = 0;
  111. UPS.SOC = 0;
  112. UPS.SOC_prev = 0;
  113. UPS.work_time = 0;
  114. UPS.akb_work_time = 0;
  115. UPS.Status = 0;
  116. UPS.Mode = 0;
  117. UPS.Alarm = 0;
  118. UPS.temp_stm32 = 0;
  119. UPS.warn_status = 0;
  120. UPS.Test_Status = 0;
  121. UPS.fault_type = 0;
  122. UPS.cnt_err_ups = 0;
  123. UPS.Flag_Present = false;
  124. UPS.Present = UPS_WAIT_CONNECT;
  125. memset(UPS.model, 0, sizeof(UPS.model));
  126. memset(UPS.vertion, 0, sizeof(UPS.vertion));
  127. memset(UPS.serial, 0, sizeof(UPS.serial));
  128. }
  129. void init_var_for_testing(void)
  130. {
  131. Ubat_sum = 0;
  132. Pload_sum = 0;
  133. Period_testing = 0;
  134. }
  135. float voltage_bat_average(void)
  136. {
  137. return (Ubat_sum / Period_testing);
  138. }
  139. float power_load_average(void)
  140. {
  141. return (Pload_sum / Period_testing);
  142. }
  143. bool ups_additional_prtcl_rx_pdu(void)
  144. {
  145. int c = 0;
  146. uint8_t cnt_answer = 0;
  147. ups_pdu.len = 0;
  148. /* Check empty responce (UART has no data) */
  149. if ((c = ups_getchar(1000)) < 0) {
  150. DBG printf("no data\r\n");
  151. return false;
  152. } else {
  153. ups_pdu.len = 0;
  154. ups_pdu.data[ups_pdu.len++] = c;
  155. }
  156. while ((ups_pdu.len < UPS_PDU_MAX_LEN) && ((c = ups_getchar(10)) >= 0)) {
  157. ups_pdu.data[ups_pdu.len++] = c;
  158. }
  159. DBG printf("rx %d bytes from 0x%X\r\n", ups_pdu.len, ups_pdu.data[0]);
  160. if (ups_pdu.len < UPS_PDU_MIN_LEN ||
  161. ups_pdu.data[0] != 0x01) {
  162. return false;
  163. }
  164. #if 1
  165. uint16_t crc = (uint16_t)(ups_pdu.data[ups_pdu.len - 2] | ((uint16_t)(ups_pdu.data[ups_pdu.len - 1]) << 8));
  166. if (crc != calc_crc16(ups_pdu.data, ups_pdu.len - 2)) {
  167. DBG {
  168. printf("bad crc:");
  169. for (uint16_t i = 0; i < ups_pdu.len; i++) {
  170. printf(" 0x%X", ups_pdu.data[i]);
  171. }
  172. printf("\r\n");
  173. }
  174. return false;
  175. }
  176. #endif
  177. return true;
  178. }
  179. void send_MegaTec_cmd(cmdMegaTecEnums_t command)
  180. {
  181. DBG printf("MegaTecCMD: %s\r\n", MegaTecCMD[command]);
  182. if (command == ups_test_time) {
  183. uint8_t req[10];
  184. memset(req, 0, 10);
  185. if (TimeParam < 10) {
  186. sprintf(req, "%s0%d\r", MegaTecCMD[command], TimeParam);
  187. } else {
  188. sprintf(req, "%s%d\r", MegaTecCMD[command], TimeParam);
  189. }
  190. ups_send_block(req, strlen(req));
  191. } else if (command == ups_shutdown) {
  192. uint8_t req[10];
  193. memset(req, 0, 10);
  194. if (TimeParamFloat >= 1 && TimeParamFloat < 10) {
  195. sprintf(req, "%s0%d\r", MegaTecCMD[command], (uint16_t)TimeParamFloat);
  196. } else if (TimeParamFloat < 1) {
  197. sprintf(req, "%s.%d\r", MegaTecCMD[command], (uint16_t)(10 * TimeParamFloat));
  198. } else {
  199. sprintf(req, "%s%d\r", MegaTecCMD[command], (uint16_t)TimeParamFloat);
  200. }
  201. ups_send_block(req, strlen(req));
  202. } else if (command == ups_shutdown_restore) {
  203. uint8_t req[10];
  204. memset(req, 0, 10);
  205. if (TimeParamFloat >= 1 && TimeParamFloat < 10) {
  206. sprintf(req, "%s0%d%s000%d\r", MegaTecCMD[command - 1], (uint16_t)TimeParamFloat, MegaTecCMD[command], TimeParam2);
  207. } else if (TimeParamFloat < 1) {
  208. sprintf(req, "%s.%d%s000%d\r", MegaTecCMD[command - 1], (uint16_t)(10 * TimeParamFloat), MegaTecCMD[command], TimeParam2);
  209. } else {
  210. sprintf(req, "%s%d%s000%d\r", MegaTecCMD[command - 1], (uint16_t)TimeParamFloat, MegaTecCMD[command], TimeParam2);
  211. }
  212. ups_send_block(req, strlen(req));
  213. } else {
  214. // TODO ����������� ���������
  215. //ups_send_block(MegaTecCMD[command], strlen(MegaTecCMD[command]));
  216. ups_send_block((void *)MegaTecCMD[command], strlen(MegaTecCMD[command]));
  217. }
  218. }
  219. bool ups_megatec_rx_pdu(void)
  220. {
  221. int c = 0;
  222. uint8_t cnt_answer = 0;
  223. ups_pdu.len = 0;
  224. while (c >= 0) {
  225. c = ups_getchar(300);//portMAX_DELAY200
  226. if (c >= 0) {
  227. ups_pdu.len = 0;
  228. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  229. ups_pdu.data[ups_pdu.len++] = c;
  230. while ((ups_pdu.len < UPS_PDU_MAX_LEN) && (c != 0x0d)) {
  231. c = ups_getchar(100);//portMAX_DELAY200
  232. if (c < 0) {
  233. ups_pdu.len = 0;
  234. return false;
  235. }
  236. ups_pdu.data[ups_pdu.len++] = c;
  237. }
  238. DBG printf("UPS raw data: %s\r\n", ups_pdu.data);
  239. cnt_answer ++;
  240. } else {
  241. if (ups_pdu.len == 0) {
  242. return false;
  243. }
  244. }
  245. }
  246. if (cnt_answer > 1) {
  247. DBG printf("MegaTecCMD: false\r\n");
  248. return false;
  249. }
  250. // printf("UPS raw data: %s\r\n", ups_pdu.data);
  251. // printf("UPS raw data len: %d\r\n", ups_pdu.len);
  252. return true;
  253. }
  254. int8_t get_ups_param(char *buf, char *param, char *val)
  255. {
  256. char *endValue;
  257. int8_t param_len;
  258. memset(val, 0, 20);
  259. endValue = strpbrk(buf, param);
  260. if (endValue != NULL) {
  261. param_len = endValue - buf;
  262. if (param_len < 20) {
  263. strncpy(val, buf, param_len);
  264. } else {
  265. param_len = 0;
  266. }
  267. } else {
  268. param_len = 0;
  269. }
  270. DBG printf("UPS parameter: %s\r\n", val);
  271. return param_len;
  272. }
  273. void ups_status_response(char *data)
  274. {
  275. uint8_t i;
  276. char value[20];
  277. float tmp;
  278. uint8_t len = 0;
  279. // printf("ups_status_response: %s\r\n", data);
  280. if (data[0] != '(') {
  281. return;
  282. }
  283. DBG printf("ups_status_parser_start\r\n");
  284. UPS.Present = UPS_CONNECTED;
  285. UPS.Flag_Present = true;
  286. UPS.cnt_err_ups = 0;
  287. #ifdef UPS_BUZZER_OFF
  288. if (flag_buzzer_on) {
  289. data[45] = '0';
  290. }
  291. #endif
  292. if (flUpdateLog) {
  293. if(sSettings.UPS_Setting.type_ups == ups_voltronic) {
  294. char temperature_str[5];
  295. sprintf(temperature_str, "%3.1f", UPS.Temp);
  296. for(uint8_t k = 0; k < 4; k++){
  297. data[33 + k] = temperature_str[k];
  298. }
  299. }
  300. flUpdateLog = false;
  301. log_add(data);
  302. }
  303. data++;
  304. DBG printf("UPS ups_status_parser_startr: %s\r\n", data);
  305. len = get_ups_param(data, " ", value);
  306. data += (len + 1);
  307. if (len > 0) {
  308. tmp = atof(value);
  309. if (tmp > 20) {
  310. UPS.VAC_in = tmp;
  311. } else {
  312. UPS.VAC_in = 0;
  313. }
  314. }
  315. //TODO
  316. len = get_ups_param(data, " ", value);
  317. data += (len + 1);
  318. len = get_ups_param(data, " ", value);
  319. data += (len + 1);
  320. if (len > 0) {
  321. UPS.VAC_out = atof(value);
  322. }
  323. len = get_ups_param(data, " ", value);
  324. data += (len + 1);
  325. if (len > 0) {
  326. UPS.Load = atoi(value);
  327. }
  328. sync_data = true;
  329. len = get_ups_param(data, " ", value);
  330. data += (len + 1);
  331. if (len > 0) {
  332. UPS.Freq_in = atof(value);
  333. }
  334. //TODO
  335. len = get_ups_param(data, " ", value);
  336. data += (len + 1);
  337. if (len > 0) {
  338. UPS.Vcell_curr = atof(value);
  339. }
  340. len = get_ups_param(data, " ", value);
  341. data += (len + 1);
  342. if (len > 0) {
  343. tmp = atof(value);
  344. if (tmp == 0 && UPS.temp_stm32 < 0) {
  345. UPS.Temp = UPS.temp_stm32;
  346. } else {
  347. UPS.Temp = tmp;
  348. }
  349. }
  350. len = get_ups_param(data, "\r", value);
  351. data += (len + 1);
  352. if (len > 0) {
  353. uint8_t stat = 0;
  354. for (i = 0; i < len; i ++) {
  355. stat |= (value[i] - 0x30) << (7 - i);
  356. }
  357. UPS.Status = stat; // теперь параметр как обычное число
  358. }
  359. // if (sSettings.UPS_Setting.type_ups == ups_kestar || sSettings.UPS_Setting.type_ups == ups_offline) {
  360. // Bat Capacity
  361. if(UPS.Status & 0x04) {
  362. UPS.SOC_prev = UPS.SOC;
  363. }
  364. #ifdef HARDWARE_BT6721
  365. if(UPS.Mode == 'B') {
  366. UPS.SOC = round(100 * (UPS.Vcell_curr / AKB_NUM_CELL - 1.96) /
  367. (2.13 - 1.96));
  368. } else
  369. #endif
  370. {
  371. if (UPS.Vcell_curr < 7) {
  372. UPS.SOC = round(100 * (UPS.Vcell_curr - sSettings.UPS_Setting.Ucellmin) / (sSettings.UPS_Setting.Ucellmax -
  373. sSettings.UPS_Setting.Ucellmin));
  374. } else {
  375. UPS.SOC = round(100 * (UPS.Vcell_curr / AKB_NUM_CELL - sSettings.UPS_Setting.Ucellmin) /
  376. (sSettings.UPS_Setting.Ucellmax - sSettings.UPS_Setting.Ucellmin));
  377. }
  378. }
  379. // }
  380. if ((UPS.Status >> 2) & 0x01 || ((UPS.Status >> 7) & 0x01)) {
  381. UPS.Load_test_akb = UPS.Load;
  382. TM_RTC_t data;
  383. TM_RTC_GetDateTime(&data, TM_RTC_Format_BIN);
  384. UPS.data_check_load_flag = true;
  385. UPS.data_check_load = data.unix;
  386. }
  387. }
  388. void ups_general_status_response(char *data)
  389. {
  390. uint8_t i;
  391. char value[20];
  392. float tmp;
  393. uint8_t len = 0;
  394. DBG printf("ups_status_response: %s\r\n", data);
  395. if (data[0] != '(') {
  396. return;
  397. }
  398. DBG printf("ups_status_parser_start\r\n");
  399. UPS.Present = UPS_CONNECTED;
  400. UPS.Flag_Present = true;
  401. UPS.cnt_err_ups = 0;
  402. data++;
  403. DBG printf("UPS ups_status_parser_startr: %s\r\n", data);
  404. data = data + 72;
  405. uint8_t stat = 0;
  406. UPS.Test_Status = 0;
  407. for (i = 0; i < 2; i ++) {
  408. UPS.Test_Status |= (data[i] - 0x30) << (1 - i);
  409. }
  410. /*
  411. len = get_ups_param(data, " ", value);
  412. data += (len + 1);
  413. if (len > 0) {
  414. tmp = atof(value);
  415. if (tmp > 20) {
  416. UPS.VAC_in = tmp;
  417. } else {
  418. UPS.VAC_in = 0;
  419. }
  420. }
  421. len = get_ups_param(data, " ", value);
  422. data += (len + 1);
  423. if (len > 0) {
  424. UPS.Freq_in = atof(value);
  425. }
  426. len = get_ups_param(data, " ", value);
  427. data += (len + 1);
  428. if (len > 0) {
  429. UPS.VAC_out = atof(value);
  430. }
  431. //TODO
  432. len = get_ups_param(data, " ", value);
  433. data += (len + 1);
  434. //TODO
  435. len = get_ups_param(data, " ", value);
  436. data += (len + 1);
  437. len = get_ups_param(data, " ", value);
  438. data += (len + 1);
  439. if (len > 0) {
  440. UPS.Load = atoi(value);
  441. }
  442. //TODO
  443. len = get_ups_param(data, " ", value);
  444. data += (len + 1);
  445. //TODO
  446. len = get_ups_param(data, " ", value);
  447. data += (len + 1);
  448. //TODO
  449. len = get_ups_param(data, " ", value);
  450. data += (len + 1);
  451. //TODO
  452. len = get_ups_param(data, " ", value);
  453. data += (len + 1);
  454. len = get_ups_param(data, " ", value);
  455. data += (len + 1);
  456. if (len > 0) {
  457. UPS.Temp = atof(value);
  458. }
  459. len = get_ups_param(data, "\r", value);
  460. data += (len + 1);
  461. if (len > 0) {
  462. uint8_t stat = 0;
  463. for (i = 2; i < (len - 2); i ++) {
  464. stat |= (value[i] - 0x30) << (7 - i);
  465. }
  466. UPS.Status = stat;
  467. }
  468. value[len - 2] = 0;
  469. if (flUpdateLog) {
  470. flUpdateLog = false;
  471. memset(data, 0, UPS_PDU_MAX_LEN);
  472. 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]);
  473. log_add(data);
  474. }
  475. */
  476. }
  477. void ups_specific_response_sn(uint8_t *data)
  478. {
  479. uint8_t len = data[4] - 3;
  480. if(len >= sizeof(UPS.serial)) {
  481. len = sizeof(UPS.serial) - 1;
  482. }
  483. strncpy(UPS.serial, &data[8], len);
  484. }
  485. void ups_info_response(char *data)
  486. {
  487. uint8_t i = 0, j = 0;
  488. char value[20];
  489. DBG printf("ups_info_response: %s\r\n", data);
  490. if (data[0] != '#') {
  491. return;
  492. }
  493. DBG printf("ups_info_response_startr: %s\r\n", data);
  494. UPS.Present = UPS_CONNECTED;
  495. UPS.Flag_Present = true;
  496. UPS.cnt_err_ups = 0;
  497. data++;
  498. data += 16; //пропускаем поле название компании
  499. memset(value, 0, sizeof(value));
  500. for (uint8_t i = 0; i < KSTAR_MODEL_LENGTH; i ++) {
  501. if (data[i] != ' ') {
  502. value[j] = data[i];
  503. j ++;
  504. }
  505. }
  506. if (j != 0) {
  507. if (UPS.model[0] == 0) {
  508. strncpy(UPS.model, value, strlen(value));
  509. SNMP_SetObjDescr();
  510. } else {
  511. strncpy(UPS.model, data, strlen(value));
  512. }
  513. } else {
  514. if (UPS.model[0] == 0) {
  515. strcpy(UPS.model, "RTMP-II");
  516. SNMP_SetObjDescr();
  517. } else {
  518. strcpy(UPS.model, "RTMP-II");
  519. }
  520. }
  521. data += (KSTAR_MODEL_LENGTH + 1);
  522. if (UPS.serial[0] == 0 && data[0] != ' '){
  523. strncpy(UPS.serial, data, 8);
  524. data += 8;
  525. } else {
  526. while (*data == ' '){
  527. data += 1;
  528. }
  529. }
  530. memset(value, 0, sizeof(value));
  531. j = 0;
  532. while (*data != '\r'){
  533. value[j] = *data;
  534. j++;
  535. data += 1;
  536. }
  537. if( (sSettings.UPS_Setting.type_ups == ups_kestar || sSettings.UPS_Setting.type_ups == ups_offline) && strlen(UPS.serial) == 0){
  538. uint8_t specific_req_sn[7] = {0x01, 0x06, 0x95, 0x00, 0x0f, 0x49, 0x60};
  539. ups_send_block(specific_req_sn, 7);
  540. if (ups_additional_prtcl_rx_pdu()) {
  541. ups_specific_response_sn(ups_pdu.data);
  542. }
  543. }
  544. strncpy(UPS.vertion, value, (j+1));
  545. SNMP_SetObjDescr();
  546. }
  547. void ups_remain_time_response(char *data)
  548. {
  549. char value[20];
  550. if (data[0] != '(') {
  551. return;
  552. }
  553. DBG printf("ups_remain_time_response: %s\r\n", data);
  554. UPS.Present = UPS_CONNECTED;
  555. UPS.Flag_Present = true;
  556. UPS.cnt_err_ups = 0;
  557. data++;
  558. if (strlen(data) > 5) {
  559. return;
  560. }
  561. memset(value, 0, 10);
  562. strcpy(value, data);
  563. //if((UPS.Status >> 7) & 0x01)
  564. UPS.work_time = atoi(value);
  565. //else
  566. // UPS.work_time = 0;
  567. }
  568. void ups_akb_info_response(char *data)
  569. {
  570. char value[20];
  571. uint8_t len = 0;
  572. DBG printf("ups_akb_info_response: %s\r\n", data);
  573. if (data[0] != '(') {
  574. return;
  575. }
  576. DBG printf("ups_akb_info_parser_start\r\n");
  577. UPS.Present = UPS_CONNECTED;
  578. UPS.Flag_Present = true;
  579. UPS.cnt_err_ups = 0;
  580. data++;
  581. DBG printf("UPS ups_akb_info_parser_start: %s\r\n", data);
  582. len = get_ups_param(data, " ", value);
  583. data += (len + 1);
  584. if(len > 0) {
  585. UPS.Vakb_curr = atof(value);
  586. }
  587. #ifdef TEST_ALARM_AKB_MONITOR
  588. if((UPS.Status >> 2) & 0x01) {
  589. Ubat_sum += UPS.Vakb_curr;
  590. Pload_sum += UPS.Load;
  591. Period_testing ++;
  592. }
  593. #endif
  594. //TODO
  595. len = get_ups_param(data, " ", value);
  596. data += (len + 1);
  597. //TODO
  598. len = get_ups_param(data, " ", value);
  599. data += (len + 1);
  600. len = get_ups_param(data, " ", value);
  601. data += (len + 1);
  602. /* if (len > 0) {
  603. UPS.SOC_prev = UPS.SOC;
  604. UPS.SOC = atoi(value);
  605. }*/
  606. len = get_ups_param(data, "\r", value);
  607. data += (len + 1);
  608. if (len > 0) {
  609. UPS.work_time = atoi(value);
  610. }
  611. }
  612. void ups_model_response(char *data)
  613. {
  614. uint8_t j = 0;
  615. char value[20];
  616. uint8_t len = 0;
  617. DBG printf("ups_akb_info_response: %s\r\n", data);
  618. if (data[0] != '(') {
  619. return;
  620. }
  621. DBG printf("ups_akb_info_parser_start\r\n");
  622. UPS.Present = UPS_CONNECTED;
  623. UPS.Flag_Present = true;
  624. UPS.cnt_err_ups = 0;
  625. data++;
  626. DBG printf("UPS ups_akb_info_parser_start: %s\r\n", data);
  627. memset(value, 0, 20);
  628. if (UPS.model[0] == 0) {
  629. for (uint8_t i = 0; i < VOLTRONIC_MODEL_LENGTH; i ++) {
  630. if (data[i] != '#') {
  631. value[j] = data[i];
  632. j ++;
  633. }
  634. }
  635. if(strncmp(value, "WPHV", 4) == 0 || strncmp(value, "G2", 2) == 0 || value[0] == 0) {
  636. #if HARDWARE_BT6711_V1 || HARDWARE_BT6721
  637. if(UPS.serial[0] == 0) {
  638. return;
  639. }
  640. if(strncmp(value, "WPHVR2K0L", 9) == 0) {
  641. strcpy(UPS.model, "RTMP-II v1rih");
  642. flag_WPHVR2K0L = true;
  643. } else if(strncmp(UPS.serial, "83122103100051", 14) == 0) {
  644. strcpy(UPS.model, "RTMP-II v0hi v1");
  645. //flag_WPHVR2K0L = true;
  646. }
  647. else
  648. #endif
  649. #if HARDWARE_BT6709_MTS || HARDWARE_BT6710
  650. if(UPS.serial[0] == 0) {
  651. return;
  652. }
  653. if(strncmp(value, "WPHVR3K0", 8) == 0) {
  654. strcpy(UPS.model, "RTMP-II v3r");
  655. } else if(strncmp(value, "WPHVR2K0", 8) == 0) {
  656. strcpy(UPS.model, "RTMP-II v2r");
  657. } else if(strncmp(value, "WPHVR1K0", 8) == 0) {
  658. strcpy(UPS.model, "RTMP-II r");
  659. } else
  660. #endif
  661. {
  662. strcpy(UPS.model, "RTMP-II");
  663. }
  664. } else {
  665. #ifdef HARDWARE_BT6709
  666. if(UPS.serial[0] == 0) {
  667. return;
  668. }
  669. if(strncmp(UPS.serial, "83122104100", 11) == 0) {
  670. char str[15];
  671. strncpy(str, &UPS.serial[11], 3);
  672. int32_t tmp = atoi(str);
  673. if (tmp >= 281 && tmp <= 770) {
  674. strcpy(UPS.model, "RTMP-II hi");
  675. } else {
  676. len = strlen(value);
  677. strncpy(UPS.model, value, len);
  678. }
  679. } else
  680. #endif
  681. #if HARDWARE_BT6709_MTS || HARDWARE_BT6710
  682. if(strncmp(value, "RTMP-II v", 9) == 0 || strncmp(value, "RTMP-IIv", 8) == 0) {
  683. strcpy(UPS.model, "RTMP-II r");
  684. } else
  685. #endif
  686. {
  687. len = strlen(value);
  688. strncpy(UPS.model, value, len);
  689. }
  690. }
  691. #if HARDWARE_BT6711_V1 || HARDWARE_BT6721
  692. if(UPS.serial[0] == 0) {
  693. memset(UPS.model, 0, sizeof(UPS.model));
  694. return;
  695. }
  696. if(strncmp(UPS.serial, "83122103100051", 14) == 0) {
  697. strcpy(UPS.model, "RTMP-II v0hi v1");
  698. //flag_WPHVR2K0L = true;
  699. } else if (strncmp(UPS.serial, "83122204100", 11) == 0) {
  700. char str[15];
  701. strncpy(str, &UPS.serial[11], 3);
  702. int32_t tmp = atoi(str);
  703. if (tmp >= 139 && tmp <= 938) {
  704. strcpy(UPS.model, "RTMP-II v0hi v1");
  705. }
  706. }
  707. #endif
  708. SNMP_SetObjDescr();
  709. data += (VOLTRONIC_MODEL_LENGTH + 1);
  710. memset(value, 0, sizeof(value));
  711. j = 0;
  712. for (uint8_t i = 0; i < VOLTRONIC_OUTPUT_RATED_LENGTH; i ++) {
  713. if (data[i] != '#') {
  714. value[j] = data[i];
  715. j ++;
  716. }
  717. }
  718. float temp_value = atof(value);
  719. if (sSettings.UPS_Setting.ups_power != temp_value) {
  720. sSettings.UPS_Setting.ups_power = temp_value;
  721. SETTINGS_Save();
  722. }
  723. } else {
  724. return;
  725. }
  726. }
  727. void ups_version_response(char *data)
  728. {
  729. char value[20];
  730. uint8_t len = 0;
  731. DBG printf("ups_akb_info_response: %s\r\n", data);
  732. if (data[0] != '(') {
  733. return;
  734. }
  735. DBG printf("ups_akb_info_parser_start\r\n");
  736. UPS.Present = UPS_CONNECTED;
  737. UPS.Flag_Present = true;
  738. UPS.cnt_err_ups = 0;
  739. data++;
  740. if (UPS.vertion[0] != 0) {
  741. if(strncmp(data, UPS.vertion, strlen(UPS.vertion)) == 0){
  742. return;
  743. }
  744. }
  745. memset(UPS.vertion, 0, sizeof(UPS.vertion));
  746. len = get_ups_param(data, ":", value);
  747. data += (len + 1);
  748. DBG printf("UPS ups_akb_info_parser_start: %s\r\n", data);
  749. len = get_ups_param(data, "\r", value);
  750. if (len > VOLTRONIC_VERSION_LENGTH) {
  751. len = VOLTRONIC_VERSION_LENGTH;
  752. }
  753. strncpy(UPS.vertion, value, len);
  754. }
  755. void ups_version_part2_response(char *data)
  756. {
  757. char value[20];
  758. uint8_t len = 0;
  759. DBG printf("ups_akb_info_response: %s\r\n", data);
  760. if (data[0] != '(') {
  761. return;
  762. }
  763. DBG printf("ups_akb_info_parser_start\r\n");
  764. UPS.Present = UPS_CONNECTED;
  765. UPS.Flag_Present = true;
  766. UPS.cnt_err_ups = 0;
  767. if (strncmp(ups_pdu.data, "(NAK", 4) == 0) {
  768. return;
  769. }
  770. data++;
  771. if (UPS.vertion[VOLTRONIC_VERSION_LENGTH] != 0) {
  772. return;
  773. }
  774. DBG printf("UPS ups_akb_info_parser_start: %s\r\n", data);
  775. len = get_ups_param(data, ":", value);
  776. data += (len + 1);
  777. len = get_ups_param(data, "\r", value);
  778. if (len > VOLTRONIC_VERSION_LENGTH) {
  779. len = VOLTRONIC_VERSION_LENGTH;
  780. }
  781. strncat(UPS.vertion, "/", 1);
  782. strncat(UPS.vertion, value, len);
  783. }
  784. void ups_serial_response(char *data)
  785. {
  786. char value[20];
  787. uint8_t len = 0;
  788. DBG printf("ups_serialresponse: %s\r\n", data);
  789. if (data[0] != '(') {
  790. return;
  791. }
  792. DBG printf("ups_serial_parser_start\r\n");
  793. UPS.Present = UPS_CONNECTED;
  794. UPS.Flag_Present = true;
  795. UPS.cnt_err_ups = 0;
  796. data++;
  797. DBG printf("UPS ups_serial_parser_start: %s\r\n", data);
  798. len = get_ups_param(data, "\r", value);
  799. strncpy(UPS.serial, value, VOLTRONIC_SN_LENGTH);
  800. }
  801. void ups_protocol_id_response(char *data)
  802. {
  803. //printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n");
  804. DBG printf("ups_protocol_id_response: %s\r\n", data);
  805. UPS.Present = UPS_CONNECTED;
  806. UPS.Flag_Present = true;
  807. UPS.cnt_err_ups = 0;
  808. if (data[0] != '(') {
  809. if (strncmp(data, "NAK", 3) == 0 || strncmp(data, " \r", 2) == 0) {
  810. printf("-------------------------------------------");
  811. printf("UPS protocol kestar!!!!!!!");
  812. printf("-------------------------------------------");
  813. sSettings.UPS_Setting.type_ups = ups_kestar;
  814. }
  815. else if(strncmp(data, "QPI", 3) == 0) {
  816. printf("-------------------------------------------");
  817. printf("UPS protocol ups_offline!!!!!!!");
  818. printf("-------------------------------------------");
  819. sSettings.UPS_Setting.type_ups = ups_offline;
  820. }
  821. return;
  822. }
  823. data++;
  824. printf("-------------------------------------------");
  825. printf("UPS protocol coltronic!!!!!!!");
  826. printf("-------------------------------------------");
  827. sSettings.UPS_Setting.type_ups = ups_voltronic;
  828. DBG printf("UPS ups_protocol_id_parser_start: %s\r\n", data);
  829. }
  830. void ups_buzzer_cntrl_response(char *data)
  831. {
  832. UPS.Present = UPS_CONNECTED;
  833. UPS.Flag_Present = true;
  834. UPS.cnt_err_ups = 0;
  835. if (strlen(data) != 0) {
  836. if (strncmp(data, "(ACK", 4) == 0) {
  837. flag_buzzer_on = true;
  838. }
  839. }
  840. DBG printf("UPS ups_buzzer_cntrl_parser_start: %s\r\n", data);
  841. }
  842. void ups_mode_response(char *data)
  843. {
  844. static char fault_status[50] = {0};
  845. char value[50];
  846. uint8_t len = 0;
  847. DBG printf("ups_serialresponse: %s\r\n", data);
  848. if (data[0] != '(') {
  849. return;
  850. }
  851. DBG printf("ups_serial_parser_start\r\n");
  852. UPS.Present = UPS_CONNECTED;
  853. UPS.Flag_Present = true;
  854. UPS.cnt_err_ups = 0;
  855. data++;
  856. DBG printf("UPS ups_mode_parser_start: %s\r\n", data);
  857. #if 1
  858. // -----------------------------------------------------------------------
  859. // Тестовый
  860. // Если значение режима изменилось или режим "Fault"
  861. if ((UPS.Mode != data[0]) || (data[0] == 'F')) {
  862. UPS.Mode = data[0];
  863. #ifdef UPS_MODE_MONITOR
  864. memset(value, 0, sizeof(value));
  865. GetModeStr(value, &len);
  866. // В режиме "Fault" запрашиваем статус
  867. if (data[0] == 'F') {
  868. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  869. send_MegaTec_cmd(ups_fault_status);
  870. if (ups_megatec_rx_pdu()) {
  871. if (ups_pdu.data[0] != '(' || strncmp(ups_pdu.data, "(OK", 3) == 0) {
  872. return;
  873. }
  874. UPS.fault_type = (ups_pdu.data[1] - 0x30) << 4 | (ups_pdu.data[2] - 0x30);
  875. strncat(value, &ups_pdu.data[1], 6);
  876. strncat(value, &ups_pdu.data[14], 4);
  877. strncat(value, &ups_pdu.data[25], 8);
  878. strncat(value, &ups_pdu.data[35], 4);
  879. strncat(value, &ups_pdu.data[41], 4);
  880. strncat(value, &ups_pdu.data[47], 4);
  881. strncat(value, &ups_pdu.data[53], 4);
  882. strncat(value, &ups_pdu.data[59], 8);
  883. if ((strncmp(value, fault_status, sizeof(value)) != 0))
  884. {
  885. SNMP_SendUserTrap(UPS_MODE);
  886. syslog(SYSLOG_INFORMATIONAL, "Режим ИБП: %s", value);
  887. printf("UPS mode to syslog file: %s\r\n", value);
  888. log_event_data(LOG_UPS_MODE, value);
  889. }
  890. memcmp(fault_status, value, sizeof(value));
  891. return;
  892. }
  893. }
  894. SNMP_SendUserTrap(UPS_MODE);
  895. syslog(SYSLOG_INFORMATIONAL, "Режим ИБП: %s", value);
  896. printf("UPS mode to syslog file: %s\r\n", value);
  897. log_event_data(LOG_UPS_MODE, value);
  898. #endif
  899. }
  900. // -----------------------------------------------------------------------
  901. #endif
  902. #if 0 // Пока закроем
  903. if (UPS.Mode != data[0]) {
  904. UPS.Mode = data[0];
  905. #ifdef UPS_MODE_MONITOR
  906. memset(value, 0, sizeof(value));
  907. GetModeStr(value, &len);
  908. if(UPS.Mode == 'F') {
  909. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  910. send_MegaTec_cmd(ups_fault_status);
  911. if (ups_megatec_rx_pdu()) {
  912. if (ups_pdu.data[0] != '(' || strncmp(ups_pdu.data, "(OK", 3) == 0) {
  913. return;
  914. }
  915. UPS.fault_type = (ups_pdu.data[1] - 0x30) << 4 | (ups_pdu.data[2] - 0x30);
  916. strncat(value, &ups_pdu.data[1], 6);
  917. strncat(value, &ups_pdu.data[14], 4);
  918. strncat(value, &ups_pdu.data[25], 8);
  919. strncat(value, &ups_pdu.data[35], 4);
  920. strncat(value, &ups_pdu.data[41], 4);
  921. strncat(value, &ups_pdu.data[47], 4);
  922. strncat(value, &ups_pdu.data[53], 4);
  923. strncat(value, &ups_pdu.data[59], 8);
  924. }
  925. }
  926. SNMP_SendUserTrap(UPS_MODE);
  927. syslog(SYSLOG_INFORMATIONAL, "Режим ИБП: %s", value);
  928. log_event_data(LOG_UPS_MODE, value);
  929. #endif
  930. }
  931. #endif
  932. }
  933. #define BIT_OFFSET(c, offset) ((c - 0x30) << offset)
  934. void ups_warn_status_response(char *data)
  935. {
  936. if (data[0] != '(') {
  937. return;
  938. }
  939. UPS.Present = UPS_CONNECTED;
  940. UPS.Flag_Present = true;
  941. UPS.cnt_err_ups = 0;
  942. data++;
  943. sync_data = false;
  944. UPS.warn_status = BIT_OFFSET(data[0], 0) + BIT_OFFSET(data[2], 1) + BIT_OFFSET(data[5], 2) + BIT_OFFSET(data[6], 3)
  945. + BIT_OFFSET(data[7], 4) + BIT_OFFSET(data[8], 5) + BIT_OFFSET(data[12], 6) + BIT_OFFSET(data[13], 7) + BIT_OFFSET(data[10], 8);
  946. }
  947. void ups_get_ack(char *data)
  948. {
  949. UPS.Present = UPS_CONNECTED;
  950. UPS.Flag_Present = true;
  951. UPS.cnt_err_ups = 0;
  952. if (strlen(data) != 0) {
  953. if (strncmp(data, "(ACK", 4) == 0) {
  954. return;
  955. }
  956. }
  957. DBG printf("UPS ups_buzzer_cntrl_parser_start: %s\r\n", data);
  958. }
  959. void ups_qflag_status_get(char *data)
  960. {
  961. char *ptr1, *ptr2;
  962. UPS.Present = UPS_CONNECTED;
  963. UPS.Flag_Present = true;
  964. UPS.cnt_err_ups = 0;
  965. if (strlen(data) != 0) {
  966. ptr1 = strchr(data, 'l');
  967. ptr2 = strchr(data, 'D');
  968. if (ptr1 != NULL && ptr2 != NULL) {
  969. if (ptr2 < ptr1) {
  970. QFLAG_L = true;
  971. }
  972. }
  973. }
  974. }
  975. void ups_kstar_status_response(char *data)
  976. {
  977. char value[20];
  978. uint8_t len;
  979. if (data[0] != '#') {
  980. return;
  981. }
  982. DBG printf("ups_kstar_status_response: %s\r\n", data);
  983. UPS.Present = UPS_CONNECTED;
  984. UPS.Flag_Present = true;
  985. UPS.cnt_err_ups = 0;
  986. data++;
  987. len = get_ups_param(data, " ", value);
  988. data += (len + 1);
  989. if (len > 0) {
  990. UPS.work_time = atoi(value);
  991. }
  992. /* len = get_ups_param(data, "\r", value);
  993. data += (len + 1);
  994. if (len > 0) {
  995. UPS.warn_status = value[3] - 0x30;
  996. }
  997. */
  998. }
  999. void ups_temperature_response(char *data)
  1000. {
  1001. char value[20];
  1002. uint8_t len = 0;
  1003. float temp = 0;
  1004. if (data[0] != '(') {
  1005. return;
  1006. }
  1007. UPS.Present = UPS_CONNECTED;
  1008. UPS.Flag_Present = true;
  1009. UPS.cnt_err_ups = 0;
  1010. data += 13;
  1011. // Вернул получение значения UPS.Temp в команду "ups_status_response"
  1012. #if 0
  1013. len = get_ups_param(data, " ", value);
  1014. data += (len + 1);
  1015. if (len > 0) {
  1016. temp = atof(value);
  1017. if (temp == 0 && UPS.temp_stm32 < 0) {
  1018. UPS.Temp = UPS.temp_stm32;
  1019. } else {
  1020. UPS.Temp = temp;
  1021. }
  1022. }
  1023. #endif
  1024. }
  1025. void ups_megatec_process_pdu(cmdMegaTecEnums_t command)
  1026. {
  1027. switch (command) {
  1028. case ups_status_req:
  1029. case ups_offline_status_req:
  1030. ups_status_response(ups_pdu.data);
  1031. break;
  1032. case ups_info:
  1033. ups_info_response(ups_pdu.data);
  1034. break;
  1035. case ups_rating_info:
  1036. break;
  1037. case ups_remain_time_reg:
  1038. ups_remain_time_response(ups_pdu.data);
  1039. break;
  1040. case ups_general_status_req:
  1041. ups_general_status_response(ups_pdu.data);
  1042. break;
  1043. case ups_akb_info:
  1044. ups_akb_info_response(ups_pdu.data);
  1045. break;
  1046. case ups_model_req:
  1047. ups_model_response(ups_pdu.data);
  1048. break;
  1049. case ups_version_req:
  1050. ups_version_response(ups_pdu.data);
  1051. break;
  1052. case ups_version2_req:
  1053. ups_version_part2_response(ups_pdu.data);
  1054. break;
  1055. case ups_serial_req:
  1056. ups_serial_response(ups_pdu.data);
  1057. break;
  1058. case ups_protocol_id_req:
  1059. ups_protocol_id_response(ups_pdu.data);
  1060. break;
  1061. case ups_buzzer_cntrl:
  1062. ups_buzzer_cntrl_response(ups_pdu.data);
  1063. break;
  1064. case ups_mode_req:
  1065. ups_mode_response(ups_pdu.data);
  1066. break;
  1067. case ups_warning_status:
  1068. ups_warn_status_response(ups_pdu.data);
  1069. break;
  1070. case ups_bypass_hvolt_264_set:
  1071. case ups_cmd_enable_flag_l:
  1072. ups_get_ack(ups_pdu.data);
  1073. break;
  1074. case ups_qflag_status:
  1075. ups_qflag_status_get(ups_pdu.data);
  1076. break;
  1077. case ups_kstar_status:
  1078. ups_kstar_status_response(ups_pdu.data);
  1079. break;
  1080. case ups_temperature:
  1081. ups_temperature_response(ups_pdu.data);
  1082. break;
  1083. default:
  1084. break;
  1085. }
  1086. }
  1087. int ups_metac_service_pdu(cmdMegaTecEnums_t command)
  1088. {
  1089. uint8_t UPS_Status_prev;
  1090. if (UPS.Present == UPS_CONNECTED) {
  1091. while (!megatec_send) {
  1092. vTaskDelay(50);
  1093. }
  1094. UPS_Status_prev = UPS.Status;
  1095. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1096. megatec_send = false;
  1097. if (command == ups_shutdown) {//sSettings.UPS_Setting.type_ups == ups_offline &&
  1098. TimeParam2 = 1;
  1099. xTimerChangePeriod(UPSRestoreTimer, (TimeParamFloat + 0.1)*60*configTICK_RATE_HZ, 0);
  1100. xTimerStart(UPSRestoreTimer, 0);
  1101. send_MegaTec_cmd(ups_shutdown_restore);
  1102. } else {
  1103. if((UPS.Status >> 2) & 0x01) {
  1104. if (command == ups_cancel_test) {
  1105. test_time = 100;
  1106. }
  1107. } else {
  1108. if (command == ups_test_low_bat) {
  1109. test_time = 100;
  1110. } else {
  1111. test_time = (int)TimeParam;
  1112. }
  1113. }
  1114. send_MegaTec_cmd(command);
  1115. }
  1116. if (ups_megatec_rx_pdu()) {
  1117. megatec_send = true;
  1118. if (strncmp(ups_pdu.data, "ACK", 3) == 0 || strncmp(ups_pdu.data, "(ACK", 4) == 0) {
  1119. if (command == ups_shutdown) {
  1120. xTimerChangePeriod(UPSRestoreTimer, (TimeParamFloat + 0.3)*60*configTICK_RATE_HZ, 0);
  1121. xTimerStart(UPSRestoreTimer, 0);
  1122. } else if(command == ups_cancel_shut_down) {
  1123. xTimerStop(UPSRestoreTimer, 0);
  1124. } else if (command == ups_test_time) {
  1125. init_var_for_testing();
  1126. }
  1127. return 1;
  1128. } else if (strncmp(ups_pdu.data, "NAK", 3) == 0 || strncmp(ups_pdu.data, "(NAK", 4) == 0) {
  1129. return 0;
  1130. }
  1131. }
  1132. vTaskDelay(1500);
  1133. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1134. megatec_send = false;
  1135. UPS.Flag_Present = false;
  1136. send_MegaTec_cmd(ups_status_req);
  1137. if (ups_megatec_rx_pdu()) {
  1138. ups_megatec_process_pdu(ups_status_req);
  1139. }
  1140. megatec_send = true;
  1141. if (command == ups_shutdown) {
  1142. if ((((UPS.Status >> 1) & 0x01) != ((UPS_Status_prev >> 1) & 0x01))
  1143. && ((UPS.Status >> 1) & 0x01)) {
  1144. xTimerChangePeriod(UPSRestoreTimer, (TimeParamFloat + 0.3)*60*configTICK_RATE_HZ, 0);
  1145. xTimerStart(UPSRestoreTimer, 0);
  1146. return 1;
  1147. }
  1148. } else if (command == ups_cancel_shut_down) {
  1149. if ((((UPS.Status >> 1) & 0x01) != ((UPS_Status_prev >> 1) & 0x01))
  1150. && !((UPS.Status >> 1) & 0x01)) {
  1151. xTimerStop(UPSRestoreTimer, 0);
  1152. return 1;
  1153. }
  1154. } else if (command == ups_cancel_test) {
  1155. if ((((UPS.Status >> 2) & 0x01) != ((UPS_Status_prev >> 2) & 0x01))
  1156. && !((UPS.Status >> 2) & 0x01)) {
  1157. return 1;
  1158. }
  1159. } else {
  1160. if ((((UPS.Status >> 2) & 0x01) != ((UPS_Status_prev >> 2) & 0x01))
  1161. && ((UPS.Status >> 2) & 0x01)) {
  1162. init_var_for_testing();
  1163. return 1;
  1164. }
  1165. if ((((UPS.Status >> 1) & 0x01) != ((UPS_Status_prev >> 1) & 0x01))
  1166. && ((UPS.Status >> 1) & 0x01)) {
  1167. return 1;
  1168. }
  1169. }
  1170. megatec_send = true;
  1171. }
  1172. #if HARDWARE_BT6721
  1173. if (command == ups_shutdown) {
  1174. return 1;
  1175. }
  1176. #endif
  1177. return -1;
  1178. }
  1179. void kstar_mode(void)
  1180. {
  1181. uint8_t mode = UPS.Mode;
  1182. static uint32_t fault_mode_cnt = 0;
  1183. #if 1
  1184. if((UPS.Status >> 4) & 0x01) {
  1185. UPS.Mode = 'F';
  1186. } else if(!((UPS.Status >> 4) & 0x01) && UPS.VAC_out == 0) {
  1187. UPS.Mode = 'D';
  1188. } else if(((UPS.Status >> 5) & 0x01) && UPS.VAC_out > 120 && UPS.VAC_in != 0) {
  1189. UPS.Mode = 'Y';
  1190. } else if(((UPS.Status >> 7) & 0x01) && UPS.VAC_out > 190) {
  1191. UPS.Mode = 'B';
  1192. } else if(!((UPS.Status >> 7) & 0x01) && UPS.VAC_out > 190) {
  1193. UPS.Mode = 'L';
  1194. } else if((UPS.Status >> 2) & 0x01) {
  1195. UPS.Mode = 'T';
  1196. }
  1197. #endif
  1198. // Добавилл отправку команды "C", т.к. ИБП
  1199. // самостоятельно не выходит из состояния Fault.
  1200. if (UPS.Mode == 'F') {
  1201. fault_mode_cnt++;
  1202. }
  1203. else {
  1204. fault_mode_cnt = 0;
  1205. }
  1206. if (fault_mode_cnt == UPA_CANSEL_DELAY) {
  1207. send_MegaTec_cmd(ups_cancel_shut_down);
  1208. fault_mode_cnt = 0;
  1209. }
  1210. if (UPS.Mode != mode) {
  1211. #ifdef UPS_MODE_MONITOR
  1212. char value[20];
  1213. uint8_t len = 0;
  1214. memset(value, 0, sizeof(value));
  1215. GetModeStr(value, &len);
  1216. SNMP_SendUserTrap(UPS_MODE);
  1217. syslog(SYSLOG_INFORMATIONAL, "Режим ИБП: %s", value);
  1218. log_event_data(LOG_UPS_MODE, value);
  1219. printf("UPS mode to log file: %s\r\n", value);
  1220. #endif
  1221. }
  1222. }
  1223. // TODO ����������� ���������
  1224. //void request_task(void)
  1225. void request_task(void *params)
  1226. {
  1227. uint8_t kestar_req[4] = { ups_status_req, ups_remain_time_reg, ups_info, ups_kstar_status};
  1228. uint8_t voltronic_req[9] = {ups_temperature, ups_status_req, ups_akb_info, ups_model_req, ups_mode_req, ups_version_req, ups_version2_req, ups_serial_req, ups_warning_status};//
  1229. uint8_t num_req = 0;
  1230. uint8_t *req;
  1231. bool flag_init_ups = false;
  1232. ups_megatec_rx_pdu();
  1233. for (;;) {
  1234. if (UPS.Present != UPS_FAIL_CONNECT) {
  1235. if (UPS.Flag_Present == false) {
  1236. if (UPS.cnt_err_ups != 2) {
  1237. UPS.cnt_err_ups++;
  1238. } else {
  1239. UPS.Freq_in = 0;
  1240. UPS.VAC_in = 0;
  1241. UPS.VAC_out = 0;
  1242. UPS.Temp = 0;
  1243. UPS.Load = 0;
  1244. UPS.Load_test_akb = 0;
  1245. UPS.SOC = 0;
  1246. UPS.SOC_prev = 0;
  1247. UPS.work_time = 0;
  1248. UPS.Status = 0;
  1249. UPS.Mode = 0;
  1250. UPS.Test_Status = 0;
  1251. UPS.Alarm = 0;
  1252. UPS.warn_status = 0;
  1253. UPS.fault_type = 0;
  1254. UPS.Present = UPS_FAIL_CONNECT;
  1255. //memset(UPS.model, 0, 11);
  1256. memset(UPS.vertion, 0, sizeof(UPS.vertion));
  1257. init_ups_rbuf();
  1258. flag_init_ups = false;
  1259. }
  1260. }
  1261. }
  1262. //printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n");
  1263. //printf("megatec_send: %u\r\n", megatec_send);
  1264. if (megatec_send) {
  1265. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1266. megatec_send = false;
  1267. UPS.Flag_Present = false;
  1268. send_MegaTec_cmd(ups_protocol_id_req);
  1269. if (ups_megatec_rx_pdu()) {
  1270. ups_megatec_process_pdu(ups_protocol_id_req);
  1271. }
  1272. megatec_send = true;
  1273. }
  1274. switch (sSettings.UPS_Setting.type_ups) {
  1275. case ups_kestar:
  1276. case ups_offline:
  1277. num_req = sizeof(kestar_req);
  1278. req = kestar_req;
  1279. break;
  1280. case ups_voltronic:
  1281. num_req = sizeof(voltronic_req);
  1282. req = voltronic_req;
  1283. #ifdef UPS_BUZZER_OFF
  1284. if (!flag_buzzer_on) {
  1285. if (megatec_send) {
  1286. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1287. megatec_send = false;
  1288. UPS.Flag_Present = false;
  1289. send_MegaTec_cmd(ups_buzzer_cntrl);
  1290. if (ups_megatec_rx_pdu()) {
  1291. ups_megatec_process_pdu(ups_buzzer_cntrl);
  1292. }
  1293. megatec_send = true;
  1294. }
  1295. }
  1296. #endif
  1297. break;
  1298. default:
  1299. num_req = sizeof(kestar_req);
  1300. req = kestar_req;
  1301. break;
  1302. }
  1303. if(!flag_init_ups) {
  1304. flag_init_ups = true;
  1305. if(sSettings.UPS_Setting.type_ups == ups_voltronic) {
  1306. if (megatec_send) {
  1307. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1308. megatec_send = false;
  1309. UPS.Flag_Present = false;
  1310. send_MegaTec_cmd(ups_bypass_hvolt_264_set);
  1311. if (ups_megatec_rx_pdu()) {
  1312. ups_megatec_process_pdu(ups_bypass_hvolt_264_set);
  1313. }
  1314. megatec_send = true;
  1315. }
  1316. if (megatec_send) {
  1317. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1318. megatec_send = false;
  1319. UPS.Flag_Present = false;
  1320. send_MegaTec_cmd(ups_qflag_status);
  1321. if (ups_megatec_rx_pdu()) {
  1322. ups_megatec_process_pdu(ups_qflag_status);
  1323. }
  1324. megatec_send = true;
  1325. }
  1326. if (QFLAG_L) {
  1327. if (megatec_send) {
  1328. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1329. megatec_send = false;
  1330. UPS.Flag_Present = false;
  1331. send_MegaTec_cmd(ups_cmd_enable_flag_l);
  1332. if (ups_megatec_rx_pdu()) {
  1333. ups_megatec_process_pdu(ups_cmd_enable_flag_l);
  1334. }
  1335. megatec_send = true;
  1336. }
  1337. }
  1338. } else if( sSettings.UPS_Setting.type_ups == ups_kestar || sSettings.UPS_Setting.type_ups == ups_offline){
  1339. uint8_t specific_req_sn[7] = {0x01, 0x06, 0x95, 0x00, 0x0f, 0x49, 0x60};
  1340. vTaskDelay(1000);
  1341. ups_send_block(specific_req_sn, 7);
  1342. if (ups_additional_prtcl_rx_pdu()) {
  1343. ups_specific_response_sn(ups_pdu.data);
  1344. }
  1345. }
  1346. #ifdef CHECK_LOAD_ENABLE
  1347. xTimerStart(AutoTestTimer, 0);
  1348. xTimerChangePeriod(AutoTestTimer, 60*configTICK_RATE_HZ, 0);
  1349. #endif
  1350. }
  1351. vTaskDelay(1000);
  1352. for (uint8_t i = 0; i < num_req; i++) {
  1353. if (megatec_send) {
  1354. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1355. megatec_send = false;
  1356. UPS.Flag_Present = false;
  1357. // printf("~~~~~~~~~~ set UPS.Flag_Present false\r\n");
  1358. send_MegaTec_cmd(req[i]);
  1359. if (ups_megatec_rx_pdu()) {
  1360. ups_megatec_process_pdu(req[i]);
  1361. }
  1362. megatec_send = true;
  1363. // printf("~~~~~~~~~~~~~~~ Current UPS.Flag_Present: %u\r\n", (uint8_t)UPS.Flag_Present);
  1364. }
  1365. }
  1366. // printf("UPS.Flag_Present: %u\r\n", (uint8_t)UPS.Flag_Present);
  1367. // printf("UPS_Setting.type_ups: %u\r\n", sSettings.UPS_Setting.type_ups);
  1368. if(UPS.Present == UPS_CONNECTED && (sSettings.UPS_Setting.type_ups == ups_kestar
  1369. || sSettings.UPS_Setting.type_ups == ups_offline)) {
  1370. kstar_mode();
  1371. }
  1372. }
  1373. }
  1374. bool get_sync_data(void)
  1375. {
  1376. return sync_data;
  1377. }
  1378. void akb_time_work_task(void *params)
  1379. {
  1380. static uint32_t tick_prev = 0;
  1381. static uint32_t cnt_sec = 0;
  1382. for (;;) {
  1383. if (UPS.Present == UPS_CONNECTED && ((UPS.Status >> 7) & 0x01)) {
  1384. if (tick_prev == 0) {
  1385. tick_prev = xTaskGetTickCount();
  1386. }
  1387. UPS.akb_work_time += (xTaskGetTickCount() - tick_prev) / 1000;
  1388. tick_prev = xTaskGetTickCount();
  1389. } else {
  1390. tick_prev = 0;
  1391. UPS.akb_work_time = 0;
  1392. }
  1393. if(flag_WPHVR2K0L) {
  1394. cnt_sec ++;
  1395. if (cnt_sec == 3600) {
  1396. cnt_sec = 0;
  1397. int res = ups_metac_service_pdu(ups_test_10sec);
  1398. }
  1399. }
  1400. vTaskDelay(1000);
  1401. }
  1402. }
  1403. void UPSRestoreCallback(TimerHandle_t pxTimer)
  1404. {
  1405. if (UPS.Present == UPS_CONNECTED) {
  1406. while (!megatec_send) {
  1407. vTaskDelay(50);
  1408. }
  1409. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1410. megatec_send = false;
  1411. //UPS.Flag_Present = false;
  1412. #ifdef HARDWARE_BT6721
  1413. send_MegaTec_cmd(ups_cancel_shut_down);
  1414. #else
  1415. send_MegaTec_cmd(ups_remote_turn_on);
  1416. #endif
  1417. if (ups_megatec_rx_pdu()) {
  1418. #ifndef HARDWARE_BT6721
  1419. ups_megatec_process_pdu(ups_remote_turn_on);
  1420. #endif
  1421. }
  1422. megatec_send = true;
  1423. }
  1424. }
  1425. void UPScmd(cmdMegaTecEnums_t cmd)
  1426. {
  1427. if (UPS.Present == UPS_CONNECTED) {
  1428. while (!megatec_send) {
  1429. vTaskDelay(50);
  1430. }
  1431. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1432. megatec_send = false;
  1433. //UPS.Flag_Present = false;
  1434. send_MegaTec_cmd(cmd);
  1435. if (ups_megatec_rx_pdu()) {
  1436. ups_megatec_process_pdu(cmd);
  1437. }
  1438. megatec_send = true;
  1439. }
  1440. }
  1441. void UPSReadTestStatus(void)
  1442. {
  1443. if(sSettings.UPS_Setting.type_ups == ups_kestar
  1444. || sSettings.UPS_Setting.type_ups == ups_offline) {
  1445. return;
  1446. }
  1447. if (UPS.Present == UPS_CONNECTED) {
  1448. while (!megatec_send) {
  1449. vTaskDelay(50);
  1450. }
  1451. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1452. megatec_send = false;
  1453. //UPS.Flag_Present = false;
  1454. send_MegaTec_cmd(ups_general_status_req);
  1455. if (ups_megatec_rx_pdu()) {
  1456. ups_megatec_process_pdu(ups_general_status_req);
  1457. }
  1458. megatec_send = true;
  1459. }
  1460. }
  1461. void AutoTestCallback(void)
  1462. {
  1463. static bool first_start = false;
  1464. if (!first_start) {
  1465. xTimerChangePeriod(AutoTestTimer, configTICK_RATE_HZ*PERIOD_AUTO_TEST, 0);
  1466. }
  1467. if(!((UPS.Status >> 2) & 0x01)) {
  1468. // printf("Test command!!!!!!!!!!!!!!!!");
  1469. int res = ups_metac_service_pdu(ups_test_10sec);
  1470. set_act_source(AUTO_ACT);
  1471. }
  1472. first_start = true;
  1473. }
  1474. void ups_megatec_init(void)
  1475. {
  1476. init_UPS_value();
  1477. xTaskCreate(request_task, ( char * ) "request_task", configMINIMAL_STACK_SIZE * 3, NULL, tskIDLE_PRIORITY, NULL);
  1478. xTaskCreate(akb_time_work_task, ( char * ) "akb_time_work_task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY,
  1479. NULL);
  1480. UPSRestoreTimer = xTimerCreate("UPSRestoreTmr", configTICK_RATE_HZ*30, pdFALSE, ( void * ) 0, UPSRestoreCallback);
  1481. #ifdef CHECK_LOAD_ENABLE
  1482. AutoTestTimer = xTimerCreate("AutoTestTmr", configTICK_RATE_HZ*PERIOD_AUTO_TEST, pdTRUE, ( void * ) 0, AutoTestCallback);
  1483. #endif
  1484. }