megatec.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  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. log_event_data(LOG_UPS_MODE, value);
  888. }
  889. memcmp(fault_status, value, sizeof(value));
  890. return;
  891. }
  892. }
  893. SNMP_SendUserTrap(UPS_MODE);
  894. syslog(SYSLOG_INFORMATIONAL, "Режим ИБП: %s", value);
  895. log_event_data(LOG_UPS_MODE, value);
  896. #endif
  897. }
  898. // -----------------------------------------------------------------------
  899. #endif
  900. #if 0 // Пока закроем
  901. if (UPS.Mode != data[0]) {
  902. UPS.Mode = data[0];
  903. #ifdef UPS_MODE_MONITOR
  904. memset(value, 0, sizeof(value));
  905. GetModeStr(value, &len);
  906. if(UPS.Mode == 'F') {
  907. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  908. send_MegaTec_cmd(ups_fault_status);
  909. if (ups_megatec_rx_pdu()) {
  910. if (ups_pdu.data[0] != '(' || strncmp(ups_pdu.data, "(OK", 3) == 0) {
  911. return;
  912. }
  913. UPS.fault_type = (ups_pdu.data[1] - 0x30) << 4 | (ups_pdu.data[2] - 0x30);
  914. strncat(value, &ups_pdu.data[1], 6);
  915. strncat(value, &ups_pdu.data[14], 4);
  916. strncat(value, &ups_pdu.data[25], 8);
  917. strncat(value, &ups_pdu.data[35], 4);
  918. strncat(value, &ups_pdu.data[41], 4);
  919. strncat(value, &ups_pdu.data[47], 4);
  920. strncat(value, &ups_pdu.data[53], 4);
  921. strncat(value, &ups_pdu.data[59], 8);
  922. }
  923. }
  924. SNMP_SendUserTrap(UPS_MODE);
  925. syslog(SYSLOG_INFORMATIONAL, "Режим ИБП: %s", value);
  926. log_event_data(LOG_UPS_MODE, value);
  927. #endif
  928. }
  929. #endif
  930. }
  931. #define BIT_OFFSET(c, offset) ((c - 0x30) << offset)
  932. void ups_warn_status_response(char *data)
  933. {
  934. if (data[0] != '(') {
  935. return;
  936. }
  937. UPS.Present = UPS_CONNECTED;
  938. UPS.Flag_Present = true;
  939. UPS.cnt_err_ups = 0;
  940. data++;
  941. sync_data = false;
  942. UPS.warn_status = BIT_OFFSET(data[0], 0) + BIT_OFFSET(data[2], 1) + BIT_OFFSET(data[5], 2) + BIT_OFFSET(data[6], 3)
  943. + 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);
  944. }
  945. void ups_get_ack(char *data)
  946. {
  947. UPS.Present = UPS_CONNECTED;
  948. UPS.Flag_Present = true;
  949. UPS.cnt_err_ups = 0;
  950. if (strlen(data) != 0) {
  951. if (strncmp(data, "(ACK", 4) == 0) {
  952. return;
  953. }
  954. }
  955. DBG printf("UPS ups_buzzer_cntrl_parser_start: %s\r\n", data);
  956. }
  957. void ups_qflag_status_get(char *data)
  958. {
  959. char *ptr1, *ptr2;
  960. UPS.Present = UPS_CONNECTED;
  961. UPS.Flag_Present = true;
  962. UPS.cnt_err_ups = 0;
  963. if (strlen(data) != 0) {
  964. ptr1 = strchr(data, 'l');
  965. ptr2 = strchr(data, 'D');
  966. if (ptr1 != NULL && ptr2 != NULL) {
  967. if (ptr2 < ptr1) {
  968. QFLAG_L = true;
  969. }
  970. }
  971. }
  972. }
  973. void ups_kstar_status_response(char *data)
  974. {
  975. char value[20];
  976. uint8_t len;
  977. if (data[0] != '#') {
  978. return;
  979. }
  980. DBG printf("ups_kstar_status_response: %s\r\n", data);
  981. UPS.Present = UPS_CONNECTED;
  982. UPS.Flag_Present = true;
  983. UPS.cnt_err_ups = 0;
  984. data++;
  985. len = get_ups_param(data, " ", value);
  986. data += (len + 1);
  987. if (len > 0) {
  988. UPS.work_time = atoi(value);
  989. }
  990. /* len = get_ups_param(data, "\r", value);
  991. data += (len + 1);
  992. if (len > 0) {
  993. UPS.warn_status = value[3] - 0x30;
  994. }
  995. */
  996. }
  997. void ups_temperature_response(char *data)
  998. {
  999. char value[20];
  1000. uint8_t len = 0;
  1001. float temp = 0;
  1002. if (data[0] != '(') {
  1003. return;
  1004. }
  1005. UPS.Present = UPS_CONNECTED;
  1006. UPS.Flag_Present = true;
  1007. UPS.cnt_err_ups = 0;
  1008. data += 13;
  1009. // Вернул получение значения UPS.Temp в команду "ups_status_response"
  1010. #if 0
  1011. len = get_ups_param(data, " ", value);
  1012. data += (len + 1);
  1013. if (len > 0) {
  1014. temp = atof(value);
  1015. if (temp == 0 && UPS.temp_stm32 < 0) {
  1016. UPS.Temp = UPS.temp_stm32;
  1017. } else {
  1018. UPS.Temp = temp;
  1019. }
  1020. }
  1021. #endif
  1022. }
  1023. void ups_megatec_process_pdu(cmdMegaTecEnums_t command)
  1024. {
  1025. switch (command) {
  1026. case ups_status_req:
  1027. case ups_offline_status_req:
  1028. ups_status_response(ups_pdu.data);
  1029. break;
  1030. case ups_info:
  1031. ups_info_response(ups_pdu.data);
  1032. break;
  1033. case ups_rating_info:
  1034. break;
  1035. case ups_remain_time_reg:
  1036. ups_remain_time_response(ups_pdu.data);
  1037. break;
  1038. case ups_general_status_req:
  1039. ups_general_status_response(ups_pdu.data);
  1040. break;
  1041. case ups_akb_info:
  1042. ups_akb_info_response(ups_pdu.data);
  1043. break;
  1044. case ups_model_req:
  1045. ups_model_response(ups_pdu.data);
  1046. break;
  1047. case ups_version_req:
  1048. ups_version_response(ups_pdu.data);
  1049. break;
  1050. case ups_version2_req:
  1051. ups_version_part2_response(ups_pdu.data);
  1052. break;
  1053. case ups_serial_req:
  1054. ups_serial_response(ups_pdu.data);
  1055. break;
  1056. case ups_protocol_id_req:
  1057. ups_protocol_id_response(ups_pdu.data);
  1058. break;
  1059. case ups_buzzer_cntrl:
  1060. ups_buzzer_cntrl_response(ups_pdu.data);
  1061. break;
  1062. case ups_mode_req:
  1063. ups_mode_response(ups_pdu.data);
  1064. break;
  1065. case ups_warning_status:
  1066. ups_warn_status_response(ups_pdu.data);
  1067. break;
  1068. case ups_bypass_hvolt_264_set:
  1069. case ups_cmd_enable_flag_l:
  1070. ups_get_ack(ups_pdu.data);
  1071. break;
  1072. case ups_qflag_status:
  1073. ups_qflag_status_get(ups_pdu.data);
  1074. break;
  1075. case ups_kstar_status:
  1076. ups_kstar_status_response(ups_pdu.data);
  1077. break;
  1078. case ups_temperature:
  1079. ups_temperature_response(ups_pdu.data);
  1080. break;
  1081. default:
  1082. break;
  1083. }
  1084. }
  1085. int ups_metac_service_pdu(cmdMegaTecEnums_t command)
  1086. {
  1087. uint8_t UPS_Status_prev;
  1088. if (UPS.Present == UPS_CONNECTED) {
  1089. while (!megatec_send) {
  1090. vTaskDelay(50);
  1091. }
  1092. UPS_Status_prev = UPS.Status;
  1093. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1094. megatec_send = false;
  1095. if (command == ups_shutdown) {//sSettings.UPS_Setting.type_ups == ups_offline &&
  1096. TimeParam2 = 1;
  1097. xTimerChangePeriod(UPSRestoreTimer, (TimeParamFloat + 0.1)*60*configTICK_RATE_HZ, 0);
  1098. xTimerStart(UPSRestoreTimer, 0);
  1099. send_MegaTec_cmd(ups_shutdown_restore);
  1100. } else {
  1101. if((UPS.Status >> 2) & 0x01) {
  1102. if (command == ups_cancel_test) {
  1103. test_time = 100;
  1104. }
  1105. } else {
  1106. if (command == ups_test_low_bat) {
  1107. test_time = 100;
  1108. } else {
  1109. test_time = (int)TimeParam;
  1110. }
  1111. }
  1112. send_MegaTec_cmd(command);
  1113. }
  1114. if (ups_megatec_rx_pdu()) {
  1115. megatec_send = true;
  1116. if (strncmp(ups_pdu.data, "ACK", 3) == 0 || strncmp(ups_pdu.data, "(ACK", 4) == 0) {
  1117. if (command == ups_shutdown) {
  1118. xTimerChangePeriod(UPSRestoreTimer, (TimeParamFloat + 0.3)*60*configTICK_RATE_HZ, 0);
  1119. xTimerStart(UPSRestoreTimer, 0);
  1120. } else if(command == ups_cancel_shut_down) {
  1121. xTimerStop(UPSRestoreTimer, 0);
  1122. } else if (command == ups_test_time) {
  1123. init_var_for_testing();
  1124. }
  1125. return 1;
  1126. } else if (strncmp(ups_pdu.data, "NAK", 3) == 0 || strncmp(ups_pdu.data, "(NAK", 4) == 0) {
  1127. return 0;
  1128. }
  1129. }
  1130. vTaskDelay(1500);
  1131. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1132. megatec_send = false;
  1133. UPS.Flag_Present = false;
  1134. send_MegaTec_cmd(ups_status_req);
  1135. if (ups_megatec_rx_pdu()) {
  1136. ups_megatec_process_pdu(ups_status_req);
  1137. }
  1138. megatec_send = true;
  1139. if (command == ups_shutdown) {
  1140. if ((((UPS.Status >> 1) & 0x01) != ((UPS_Status_prev >> 1) & 0x01))
  1141. && ((UPS.Status >> 1) & 0x01)) {
  1142. xTimerChangePeriod(UPSRestoreTimer, (TimeParamFloat + 0.3)*60*configTICK_RATE_HZ, 0);
  1143. xTimerStart(UPSRestoreTimer, 0);
  1144. return 1;
  1145. }
  1146. } else if (command == ups_cancel_shut_down) {
  1147. if ((((UPS.Status >> 1) & 0x01) != ((UPS_Status_prev >> 1) & 0x01))
  1148. && !((UPS.Status >> 1) & 0x01)) {
  1149. xTimerStop(UPSRestoreTimer, 0);
  1150. return 1;
  1151. }
  1152. } else if (command == ups_cancel_test) {
  1153. if ((((UPS.Status >> 2) & 0x01) != ((UPS_Status_prev >> 2) & 0x01))
  1154. && !((UPS.Status >> 2) & 0x01)) {
  1155. return 1;
  1156. }
  1157. } else {
  1158. if ((((UPS.Status >> 2) & 0x01) != ((UPS_Status_prev >> 2) & 0x01))
  1159. && ((UPS.Status >> 2) & 0x01)) {
  1160. init_var_for_testing();
  1161. return 1;
  1162. }
  1163. if ((((UPS.Status >> 1) & 0x01) != ((UPS_Status_prev >> 1) & 0x01))
  1164. && ((UPS.Status >> 1) & 0x01)) {
  1165. return 1;
  1166. }
  1167. }
  1168. megatec_send = true;
  1169. }
  1170. if (command == ups_shutdown) {
  1171. return 1;
  1172. }
  1173. return -1;
  1174. }
  1175. void kstar_mode(void)
  1176. {
  1177. uint8_t mode = UPS.Mode;
  1178. #if 1
  1179. if((UPS.Status >> 4) & 0x01) {
  1180. UPS.Mode = 'F';
  1181. } else if(!((UPS.Status >> 4) & 0x01) && UPS.VAC_out == 0) {
  1182. UPS.Mode = 'D';
  1183. } else if(((UPS.Status >> 5) & 0x01) && UPS.VAC_out > 120 && UPS.VAC_in != 0) {
  1184. UPS.Mode = 'Y';
  1185. } else if(((UPS.Status >> 7) & 0x01) && UPS.VAC_out > 190) {
  1186. UPS.Mode = 'B';
  1187. } else if(!((UPS.Status >> 7) & 0x01) && UPS.VAC_out > 190) {
  1188. UPS.Mode = 'L';
  1189. } else if((UPS.Status >> 2) & 0x01) {
  1190. UPS.Mode = 'T';
  1191. }
  1192. #endif
  1193. #if 0
  1194. if((UPS.Status >> 4) & 0x01) {
  1195. UPS.Mode = 'F';
  1196. } else if(!((UPS.Status >> 4) & 0x01) && UPS.VAC_out == 0) {
  1197. UPS.Mode = 'D';
  1198. } else if(((UPS.Status >> 5) & 0x01) && UPS.VAC_out > 120 && UPS.VAC_in != 0) {
  1199. UPS.Mode = 'Y';
  1200. } else if(((UPS.Status >> 7) & 0x01) && UPS.VAC_out > 190) {
  1201. UPS.Mode = 'B';
  1202. } else if((UPS.Status >> 2) & 0x01) {
  1203. UPS.Mode = 'T';
  1204. }
  1205. #endif
  1206. if (UPS.Mode != mode) {
  1207. #ifdef UPS_MODE_MONITOR
  1208. char value[20];
  1209. uint8_t len = 0;
  1210. memset(value, 0, sizeof(value));
  1211. GetModeStr(value, &len);
  1212. SNMP_SendUserTrap(UPS_MODE);
  1213. syslog(SYSLOG_INFORMATIONAL, "Режим ИБП: %s", value);
  1214. log_event_data(LOG_UPS_MODE, value);
  1215. #endif
  1216. }
  1217. }
  1218. // TODO ����������� ���������
  1219. //void request_task(void)
  1220. void request_task(void *params)
  1221. {
  1222. uint8_t kestar_req[4] = { ups_status_req, ups_remain_time_reg, ups_info, ups_kstar_status};
  1223. 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};//
  1224. uint8_t num_req = 0;
  1225. uint8_t *req;
  1226. bool flag_init_ups = false;
  1227. ups_megatec_rx_pdu();
  1228. for (;;) {
  1229. if (UPS.Present != UPS_FAIL_CONNECT) {
  1230. if (UPS.Flag_Present == false) {
  1231. if (UPS.cnt_err_ups != 2) {
  1232. UPS.cnt_err_ups++;
  1233. } else {
  1234. UPS.Freq_in = 0;
  1235. UPS.VAC_in = 0;
  1236. UPS.VAC_out = 0;
  1237. UPS.Temp = 0;
  1238. UPS.Load = 0;
  1239. UPS.Load_test_akb = 0;
  1240. UPS.SOC = 0;
  1241. UPS.SOC_prev = 0;
  1242. UPS.work_time = 0;
  1243. UPS.Status = 0;
  1244. UPS.Mode = 0;
  1245. UPS.Test_Status = 0;
  1246. UPS.Alarm = 0;
  1247. UPS.warn_status = 0;
  1248. UPS.fault_type = 0;
  1249. UPS.Present = UPS_FAIL_CONNECT;
  1250. //memset(UPS.model, 0, 11);
  1251. memset(UPS.vertion, 0, sizeof(UPS.vertion));
  1252. init_ups_rbuf();
  1253. flag_init_ups = false;
  1254. }
  1255. }
  1256. }
  1257. //printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n");
  1258. //printf("megatec_send: %u\r\n", megatec_send);
  1259. if (megatec_send) {
  1260. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1261. megatec_send = false;
  1262. UPS.Flag_Present = false;
  1263. send_MegaTec_cmd(ups_protocol_id_req);
  1264. if (ups_megatec_rx_pdu()) {
  1265. ups_megatec_process_pdu(ups_protocol_id_req);
  1266. }
  1267. megatec_send = true;
  1268. }
  1269. switch (sSettings.UPS_Setting.type_ups) {
  1270. case ups_kestar:
  1271. case ups_offline:
  1272. num_req = sizeof(kestar_req);
  1273. req = kestar_req;
  1274. break;
  1275. case ups_voltronic:
  1276. num_req = sizeof(voltronic_req);
  1277. req = voltronic_req;
  1278. #ifdef UPS_BUZZER_OFF
  1279. if (!flag_buzzer_on) {
  1280. if (megatec_send) {
  1281. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1282. megatec_send = false;
  1283. UPS.Flag_Present = false;
  1284. send_MegaTec_cmd(ups_buzzer_cntrl);
  1285. if (ups_megatec_rx_pdu()) {
  1286. ups_megatec_process_pdu(ups_buzzer_cntrl);
  1287. }
  1288. megatec_send = true;
  1289. }
  1290. }
  1291. #endif
  1292. break;
  1293. default:
  1294. num_req = sizeof(kestar_req);
  1295. req = kestar_req;
  1296. break;
  1297. }
  1298. if(!flag_init_ups) {
  1299. flag_init_ups = true;
  1300. if(sSettings.UPS_Setting.type_ups == ups_voltronic) {
  1301. if (megatec_send) {
  1302. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1303. megatec_send = false;
  1304. UPS.Flag_Present = false;
  1305. send_MegaTec_cmd(ups_bypass_hvolt_264_set);
  1306. if (ups_megatec_rx_pdu()) {
  1307. ups_megatec_process_pdu(ups_bypass_hvolt_264_set);
  1308. }
  1309. megatec_send = true;
  1310. }
  1311. if (megatec_send) {
  1312. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1313. megatec_send = false;
  1314. UPS.Flag_Present = false;
  1315. send_MegaTec_cmd(ups_qflag_status);
  1316. if (ups_megatec_rx_pdu()) {
  1317. ups_megatec_process_pdu(ups_qflag_status);
  1318. }
  1319. megatec_send = true;
  1320. }
  1321. if (QFLAG_L) {
  1322. if (megatec_send) {
  1323. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1324. megatec_send = false;
  1325. UPS.Flag_Present = false;
  1326. send_MegaTec_cmd(ups_cmd_enable_flag_l);
  1327. if (ups_megatec_rx_pdu()) {
  1328. ups_megatec_process_pdu(ups_cmd_enable_flag_l);
  1329. }
  1330. megatec_send = true;
  1331. }
  1332. }
  1333. } else if( sSettings.UPS_Setting.type_ups == ups_kestar || sSettings.UPS_Setting.type_ups == ups_offline){
  1334. uint8_t specific_req_sn[7] = {0x01, 0x06, 0x95, 0x00, 0x0f, 0x49, 0x60};
  1335. vTaskDelay(1000);
  1336. ups_send_block(specific_req_sn, 7);
  1337. if (ups_additional_prtcl_rx_pdu()) {
  1338. ups_specific_response_sn(ups_pdu.data);
  1339. }
  1340. }
  1341. #ifdef CHECK_LOAD_ENABLE
  1342. xTimerStart(AutoTestTimer, 0);
  1343. xTimerChangePeriod(AutoTestTimer, 60*configTICK_RATE_HZ, 0);
  1344. #endif
  1345. }
  1346. vTaskDelay(1000);
  1347. for (uint8_t i = 0; i < num_req; i++) {
  1348. if (megatec_send) {
  1349. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1350. megatec_send = false;
  1351. UPS.Flag_Present = false;
  1352. // printf("~~~~~~~~~~ set UPS.Flag_Present false\r\n");
  1353. send_MegaTec_cmd(req[i]);
  1354. if (ups_megatec_rx_pdu()) {
  1355. ups_megatec_process_pdu(req[i]);
  1356. }
  1357. megatec_send = true;
  1358. // printf("~~~~~~~~~~~~~~~ Current UPS.Flag_Present: %u\r\n", (uint8_t)UPS.Flag_Present);
  1359. }
  1360. }
  1361. // printf("UPS.Flag_Present: %u\r\n", (uint8_t)UPS.Flag_Present);
  1362. // printf("UPS_Setting.type_ups: %u\r\n", sSettings.UPS_Setting.type_ups);
  1363. if(UPS.Present == UPS_CONNECTED && (sSettings.UPS_Setting.type_ups == ups_kestar
  1364. || sSettings.UPS_Setting.type_ups == ups_offline)) {
  1365. kstar_mode();
  1366. }
  1367. }
  1368. }
  1369. bool get_sync_data(void)
  1370. {
  1371. return sync_data;
  1372. }
  1373. void akb_time_work_task(void *params)
  1374. {
  1375. static uint32_t tick_prev = 0;
  1376. static uint32_t cnt_sec = 0;
  1377. for (;;) {
  1378. if (UPS.Present == UPS_CONNECTED && ((UPS.Status >> 7) & 0x01)) {
  1379. if (tick_prev == 0) {
  1380. tick_prev = xTaskGetTickCount();
  1381. }
  1382. UPS.akb_work_time += (xTaskGetTickCount() - tick_prev) / 1000;
  1383. tick_prev = xTaskGetTickCount();
  1384. } else {
  1385. tick_prev = 0;
  1386. UPS.akb_work_time = 0;
  1387. }
  1388. if(flag_WPHVR2K0L) {
  1389. cnt_sec ++;
  1390. if (cnt_sec == 3600) {
  1391. cnt_sec = 0;
  1392. int res = ups_metac_service_pdu(ups_test_10sec);
  1393. }
  1394. }
  1395. vTaskDelay(1000);
  1396. }
  1397. }
  1398. void UPSRestoreCallback(TimerHandle_t pxTimer)
  1399. {
  1400. if (UPS.Present == UPS_CONNECTED) {
  1401. while (!megatec_send) {
  1402. vTaskDelay(50);
  1403. }
  1404. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1405. megatec_send = false;
  1406. //UPS.Flag_Present = false;
  1407. //send_MegaTec_cmd(ups_remote_turn_on);
  1408. send_MegaTec_cmd(ups_cancel_shut_down);
  1409. if (ups_megatec_rx_pdu()) {
  1410. //ups_megatec_process_pdu(ups_remote_turn_on);
  1411. }
  1412. megatec_send = true;
  1413. }
  1414. }
  1415. void UPScmd(cmdMegaTecEnums_t cmd)
  1416. {
  1417. if (UPS.Present == UPS_CONNECTED) {
  1418. while (!megatec_send) {
  1419. vTaskDelay(50);
  1420. }
  1421. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1422. megatec_send = false;
  1423. //UPS.Flag_Present = false;
  1424. send_MegaTec_cmd(cmd);
  1425. if (ups_megatec_rx_pdu()) {
  1426. ups_megatec_process_pdu(cmd);
  1427. }
  1428. megatec_send = true;
  1429. }
  1430. }
  1431. void UPSReadTestStatus(void)
  1432. {
  1433. if(sSettings.UPS_Setting.type_ups == ups_kestar
  1434. || sSettings.UPS_Setting.type_ups == ups_offline) {
  1435. return;
  1436. }
  1437. if (UPS.Present == UPS_CONNECTED) {
  1438. while (!megatec_send) {
  1439. vTaskDelay(50);
  1440. }
  1441. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  1442. megatec_send = false;
  1443. //UPS.Flag_Present = false;
  1444. send_MegaTec_cmd(ups_general_status_req);
  1445. if (ups_megatec_rx_pdu()) {
  1446. ups_megatec_process_pdu(ups_general_status_req);
  1447. }
  1448. megatec_send = true;
  1449. }
  1450. }
  1451. void AutoTestCallback(void)
  1452. {
  1453. static bool first_start = false;
  1454. if (!first_start) {
  1455. xTimerChangePeriod(AutoTestTimer, configTICK_RATE_HZ*PERIOD_AUTO_TEST, 0);
  1456. }
  1457. if(!((UPS.Status >> 2) & 0x01)) {
  1458. // printf("Test command!!!!!!!!!!!!!!!!");
  1459. int res = ups_metac_service_pdu(ups_test_10sec);
  1460. set_act_source(AUTO_ACT);
  1461. }
  1462. first_start = true;
  1463. }
  1464. void ups_megatec_init(void)
  1465. {
  1466. init_UPS_value();
  1467. xTaskCreate(request_task, ( char * ) "request_task", configMINIMAL_STACK_SIZE * 3, NULL, tskIDLE_PRIORITY, NULL);
  1468. xTaskCreate(akb_time_work_task, ( char * ) "akb_time_work_task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY,
  1469. NULL);
  1470. UPSRestoreTimer = xTimerCreate("UPSRestoreTmr", configTICK_RATE_HZ*30, pdFALSE, ( void * ) 0, UPSRestoreCallback);
  1471. #ifdef CHECK_LOAD_ENABLE
  1472. AutoTestTimer = xTimerCreate("AutoTestTmr", configTICK_RATE_HZ*PERIOD_AUTO_TEST, pdTRUE, ( void * ) 0, AutoTestCallback);
  1473. #endif
  1474. }