megatec.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 "usart.h"
  10. #include "megatec.h"
  11. #include "log.h"
  12. #include "snmp_api.h"
  13. #ifdef PRINTF_STDLIB
  14. #include <stdio.h>
  15. #endif
  16. #ifdef PRINTF_CUSTOM
  17. #include "tinystdio.h"
  18. #endif
  19. #include <string.h>
  20. #include <math.h>
  21. #define UPS_PDU_MAX_LEN 50
  22. float TimeParamFloat = 0;
  23. uint16_t TimeParam = 0;
  24. uint16_t TimeParam2 = 0;
  25. bool megatec_send = true;
  26. UPS_value_t UPS;
  27. enum {
  28. NORMAL = 0x00,
  29. VER_ERROR = 0x01,
  30. CHKSUM_ERROR = 0x02,
  31. LCHKSUM_ERROR = 0x03,
  32. CID2_INVALID = 0x04,
  33. CMD_FMT_ERROR = 0x05,
  34. INVALID_DATA = 0x06,
  35. };
  36. static struct {
  37. uint8_t data[UPS_PDU_MAX_LEN];
  38. uint16_t len;
  39. } ups_pdu;
  40. const char* MegaTecCMD[] =
  41. {
  42. "Q1\r",
  43. "T\r",
  44. "TL\r",
  45. "T",
  46. "Q\r",
  47. "S",
  48. "R",
  49. "C\r",
  50. "CT\r",
  51. "I\r",
  52. "F\r",
  53. "Q2\r"
  54. };
  55. extern bool flUpdateLog;
  56. void init_UPS_value(void)
  57. {
  58. UPS.Freq_in = 0;
  59. UPS.VAC_in = 0;
  60. UPS.VAC_out = 0;
  61. UPS.Temp = 0;
  62. UPS.Load = 0;
  63. UPS.SOC = 0;
  64. UPS.work_time = 0;
  65. UPS.Status = 0;
  66. UPS.Alarm = 0;
  67. UPS.cnt_err_ups = 0;
  68. UPS.Flag_Present = false;
  69. UPS.Present = false;
  70. memset(UPS.model, 0, 11);
  71. memset(UPS.vertion, 0, 11);
  72. }
  73. void send_MegaTec_cmd(cmdMegaTecEnums_t command)
  74. {
  75. if(command == ups_test_time){
  76. uint8_t req[10];
  77. memset(req, 0, 10);
  78. sprintf(req, "%s%d\r", MegaTecCMD[command], TimeParam);
  79. ups_send_block(req, strlen(req));
  80. }
  81. else if(command == ups_shutdown){
  82. uint8_t req[10];
  83. memset(req, 0, 10);
  84. if(TimeParamFloat >= 1 && TimeParamFloat < 10)
  85. {
  86. sprintf(req, "%s0%f\r", MegaTecCMD[command], TimeParamFloat);
  87. }
  88. else if(TimeParamFloat < 1){
  89. sprintf(req, "%s.%f\r", MegaTecCMD[command], 10*TimeParamFloat);
  90. }
  91. else{
  92. sprintf(req, "%s%f\r", MegaTecCMD[command], TimeParamFloat);
  93. }
  94. ups_send_block(req, strlen(req));
  95. }
  96. else if(command == ups_shutdown_restore){
  97. uint8_t req[10];
  98. memset(req, 0, 10);
  99. sprintf(req, "%s.%d%s%d\r", MegaTecCMD[command-1], TimeParam, MegaTecCMD[command], TimeParam2);
  100. ups_send_block(req, strlen(req));
  101. }
  102. else{
  103. // TODO ����������� ���������
  104. //ups_send_block(MegaTecCMD[command], strlen(MegaTecCMD[command]));
  105. ups_send_block((void*)MegaTecCMD[command], strlen(MegaTecCMD[command]));
  106. }
  107. }
  108. bool ups_megatec_rx_pdu(void)
  109. {
  110. int c = 0;
  111. ups_pdu.len = 0;
  112. while ((ups_pdu.len < UPS_PDU_MAX_LEN) && (c != 0x0d)) {
  113. c = ups_getchar(500);//portMAX_DELAY
  114. if(c < 0)
  115. {
  116. ups_pdu.len = 0;
  117. break;
  118. }
  119. ups_pdu.data[ups_pdu.len++] = c;
  120. }
  121. if (ups_pdu.len == 0)
  122. return false;
  123. return true;
  124. }
  125. void ups_status_response(char *data)
  126. {
  127. uint8_t i;
  128. char *endValue;
  129. char value[10];
  130. uint8_t len = 0;
  131. if(data[0] != '(')
  132. return;
  133. UPS.Present = true;
  134. UPS.Flag_Present = true;
  135. UPS.cnt_err_ups = 0;
  136. if(flUpdateLog){
  137. flUpdateLog = false;
  138. log_add(data);
  139. }
  140. data++;
  141. memset(value, 0, 10);
  142. endValue = strpbrk(data," ");
  143. len = endValue - data;
  144. strncpy(value, data, len);
  145. data += (len + 1);
  146. UPS.VAC_in = atof(value);
  147. //TODO
  148. memset(value, 0, 10);
  149. endValue = strpbrk(data," ");
  150. len = endValue - data;
  151. strncpy(value, data, len);
  152. data += (len + 1);
  153. memset(value, 0, 10);
  154. endValue = strpbrk(data," ");
  155. len = endValue - data;
  156. strncpy(value, data, len);
  157. data += (len + 1);
  158. UPS.VAC_out = atof(value);
  159. memset(value, 0, 10);
  160. endValue = strpbrk(data," ");
  161. len = endValue - data;
  162. strncpy(value, data, len);
  163. data += (len + 1);
  164. UPS.Load = atoi(value);
  165. memset(value, 0, 10);
  166. endValue = strpbrk(data," ");
  167. len = endValue - data;
  168. strncpy(value, data, len);
  169. data += (len + 1);
  170. UPS.Freq_in = atof(value);
  171. //TODO
  172. memset(value, 0, 10);
  173. endValue = strpbrk(data," ");
  174. len = endValue - data;
  175. strncpy(value, data, len);
  176. data += (len + 1);
  177. UPS.SOC = 100*((atof(value)) - 1.6)/0.7;
  178. memset(value, 0, 10);
  179. endValue = strpbrk(data," ");
  180. len = endValue - data;
  181. strncpy(value, data, len);
  182. data += (len + 1);
  183. UPS.Temp = atof(value);
  184. memset(value, 0, 10);
  185. endValue = strpbrk(data,"\r");
  186. len = endValue - data;
  187. strncpy(value, data, len);
  188. uint8_t stat = 0;
  189. for(i = 0; i < len; i ++)
  190. {
  191. stat |= (value[i] - 0x30) << (7-i);
  192. }
  193. UPS.Status = stat;
  194. }
  195. void ups_info_response(char *data)
  196. {
  197. uint8_t i = 0;
  198. if(data[0] != '#')
  199. return;
  200. UPS.Present = true;
  201. UPS.Flag_Present = true;
  202. UPS.cnt_err_ups = 0;
  203. data++;
  204. data += 16;
  205. while(data[0] == ' '){
  206. data ++;
  207. i ++;
  208. }
  209. if(i < 15){
  210. /*endValue = strpbrk(data," ");
  211. len = endValue - data;*/
  212. if(UPS.model[0] == 0){
  213. strncpy(UPS.model, data, 10);
  214. SNMP_SetObjDescr();
  215. }
  216. else{
  217. strncpy(UPS.model, data, 10);
  218. }
  219. data += 11;
  220. }
  221. else
  222. {
  223. if(UPS.model[0] == 0){
  224. strcpy(UPS.model, "RTMP II");
  225. SNMP_SetObjDescr();
  226. }
  227. else{
  228. strcpy(UPS.model, "RTMP II");
  229. }
  230. data += 11;
  231. }
  232. strncpy(UPS.serial, data, 8);
  233. data += 8;
  234. strncpy(UPS.vertion, data, 2);
  235. }
  236. void ups_remain_time_response(char *data)
  237. {
  238. char value[10];
  239. if(data[0] != '(')
  240. return;
  241. UPS.Present = true;
  242. UPS.Flag_Present = true;
  243. UPS.cnt_err_ups = 0;
  244. data++;
  245. memset(value, 0, 10);
  246. strcpy(value, data);
  247. if((UPS.Status >> 7) & 0x01)
  248. UPS.work_time = atof(value);
  249. else
  250. UPS.work_time = 0;
  251. }
  252. void ups_megatec_process_pdu(cmdMegaTecEnums_t command)
  253. {
  254. switch(command)
  255. {
  256. case ups_status_req:
  257. ups_status_response(ups_pdu.data);
  258. break;
  259. case ups_info:
  260. ups_info_response(ups_pdu.data);
  261. break;
  262. case ups_rating_info:
  263. break;
  264. case ups_remain_time_reg:
  265. ups_remain_time_response(ups_pdu.data);
  266. break;
  267. default:
  268. break;
  269. }
  270. }
  271. int ups_metac_service_pdu(cmdMegaTecEnums_t command)
  272. {
  273. while(!megatec_send)
  274. {
  275. vTaskDelay(50);
  276. }
  277. megatec_send = false;
  278. send_MegaTec_cmd(command);
  279. if (ups_megatec_rx_pdu())
  280. {
  281. megatec_send = true;
  282. if(strncmp(ups_pdu.data, "ACK", 3) == 0)
  283. return 1;
  284. else if(strncmp(ups_pdu.data, "NCK", 3) == 0)
  285. return 0;
  286. }
  287. return -1;
  288. }
  289. // TODO ����������� ���������
  290. //void request_task(void)
  291. void request_task(void* params)
  292. {
  293. for(;;)
  294. {
  295. if(UPS.Present == true){
  296. if(UPS.Flag_Present == false)
  297. {
  298. if(UPS.cnt_err_ups != 2)
  299. UPS.cnt_err_ups++;
  300. else{
  301. UPS.Freq_in = 0;
  302. UPS.VAC_in = 0;
  303. UPS.VAC_out = 0;
  304. UPS.Temp = 0;
  305. UPS.Load = 0;
  306. UPS.SOC = 0;
  307. UPS.work_time = 0;
  308. UPS.Status = 0;
  309. UPS.Alarm = 0;
  310. UPS.Present = false;
  311. //memset(UPS.model, 0, 11);
  312. memset(UPS.vertion, 0, 11);
  313. }
  314. }
  315. }
  316. if(megatec_send){
  317. megatec_send= false;
  318. UPS.Flag_Present = false;
  319. send_MegaTec_cmd(ups_status_req);
  320. if (ups_megatec_rx_pdu())
  321. ups_megatec_process_pdu(ups_status_req);
  322. megatec_send=true;
  323. }
  324. if(megatec_send){
  325. memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
  326. megatec_send= false;
  327. UPS.Flag_Present = false;
  328. send_MegaTec_cmd(ups_remain_time_reg);
  329. if (ups_megatec_rx_pdu())
  330. ups_megatec_process_pdu(ups_remain_time_reg);
  331. megatec_send=true;
  332. }
  333. //vTaskDelay(100);
  334. if(megatec_send){
  335. megatec_send = false;
  336. UPS.Flag_Present = false;
  337. send_MegaTec_cmd(ups_info);
  338. if (ups_megatec_rx_pdu())
  339. ups_megatec_process_pdu(ups_info);
  340. megatec_send = true;
  341. }
  342. vTaskDelay(1000);
  343. }
  344. }
  345. void ups_megatec_init(void) {
  346. init_UPS_value();
  347. UPS.Present = true;
  348. xTaskCreate(request_task, ( char * ) "request_task", configMINIMAL_STACK_SIZE * 2, NULL, tskIDLE_PRIORITY, NULL);
  349. }