source: S-port/trunk/Core/Src/cmd_plsUnit.c

Last change on this file was 1, checked in by AlexLir, 3 years ago
File size: 9.7 KB
Line 
1/****************************************************************************************
2*
3* cmd_plcUnit.c
4*
5* Óïðàâëåíèå PLC áëîêàìè
6****************************************************************************************/
7
8#include "commands.h"
9#include "main.h"
10#include "plc.h"
11#include "lwip.h"
12#include <string.h>
13
14extern plc_data_typeDef PLC_Data[AMOUNT_BLOCK_PLC];
15extern plc_common_typeDef plc_common;
16extern network_settings table_network[MAX_IP];
17
18void cmd_plc_blockparams(json_tokens_data_t *tokens_data, json_response_t *json_response)
19{
20 uint16_t unit = 0;
21 if(json_get_value_object(tokens_data, "unit", &unit, json_type_integer, json_root)== json_success){
22 if(unit > AMOUNT_BLOCK_PLC - 1){
23 json_write_obj_string("status", "error");
24 json_write_obj_string("error", "invalid parametr");
25 return;
26 }
27 json_write_obj_string("status", "ok");
28 json_write_obj_int("unit", unit);
29 json_write_obj_int("vers", PLC_Data[unit].VersionMD);
30 /* ôîðìèðîâàíèå îòâåòà ïî ôèëüòðó */
31 json_write_obj_array("filter");
32 json_write_arr_int(PLC_Data[unit].Type_Filter);
33 json_write_arr_int(PLC_Data[unit].FilterATT);
34 json_write_end(); // çàêðûâàåì ìàññèâ
35 json_write_obj_int("power", (PLC_Data[unit].Power_PRD * 20));
36 json_write_obj_array("HF");
37 for(uint8_t i = 0; i < 3; i++){
38 json_write_arr_array();
39 json_write_arr_bool(PLC_Data[unit].HF_Channel[i].En);
40 json_write_arr_int(PLC_Data[unit].HF_Channel[i].Status_Receiver);
41 json_write_arr_int(PLC_Data[unit].HF_Channel[i].Mode);
42 json_write_arr_int(PLC_Data[unit].HF_Channel[i].Frequency_Transmitter);
43 json_write_arr_int(PLC_Data[unit].HF_Channel[i].Frequency_Receiver);
44 json_write_arr_int(PLC_Data[unit].HF_Channel[i].Level_Receiver);
45 json_write_arr_int(PLC_Data[unit].HF_Channel[i].Level_InputSignal);
46 json_write_arr_int(PLC_Data[unit].HF_Channel[i].Rate_Transmitter);
47 json_write_arr_int(PLC_Data[unit].HF_Channel[i].Rate_Receiver);
48 json_write_arr_int(PLC_Data[unit].HF_Channel[i].QualityNoise_Receiver);
49 json_write_arr_int(PLC_Data[unit].HF_Channel[i].SNR_Receiver);
50 json_write_arr_int(PLC_Data[unit].HF_Channel[i].CounterError);
51 json_write_arr_int(PLC_Data[unit].HF_Channel[i].Kosh_sig);
52 json_write_arr_int(PLC_Data[unit].HF_Channel[i].Kosh_exp);
53 json_write_arr_int(PLC_Data[unit].HF_Channel[i].Kosh_raw);
54 json_write_end(); // çàêðûâàåì ìàññèâ
55 }
56 json_write_end(); // çàêðûâàåì ìàññèâ
57 json_write_obj_array("pwrAmp");
58 for(uint8_t i = 0; i < 2; i++){
59 json_write_arr_array();
60 json_write_arr_bool(PLC_Data[unit].PowerAmplifier[i].Presence);
61 json_write_arr_int(PLC_Data[unit].PowerAmplifier[i].Version);
62 json_write_arr_int(PLC_Data[unit].PowerAmplifier[i].Alarm);
63 json_write_arr_int(PLC_Data[unit].PowerAmplifier[i].Level_Transmitter);
64 json_write_arr_int(PLC_Data[unit].PowerAmplifier[i].Temp);
65 json_write_arr_int(PLC_Data[unit].PowerAmplifier[i].Voltage);
66 json_write_end(); // çàêðûâàåì ìàññèâ
67 }
68 json_write_end(); // çàêðûâàåì ìàññèâ
69 }
70 else {
71 json_write_obj_string("status", "error");
72 json_write_obj_string("error", "invalid parametr");
73 }
74}
75
76void cmd_plc_getblock(json_tokens_data_t *tokens_data, json_response_t *json_response)
77{
78 uint16_t unit = 0;
79 if(json_get_value_object(tokens_data, "unit", &unit, json_type_integer, json_root)== json_success){
80 if(unit > AMOUNT_BLOCK_PLC - 1){
81 json_write_obj_string("status", "error");
82 json_write_obj_string("error", "invalid parametr");
83 return;
84 }
85 json_write_obj_string("status", "ok");
86 json_write_obj_bool("enable", PLC_Data[unit].EnablePoll);
87 json_write_obj_string("name", PLC_Data[unit].NameBlock);
88 json_write_obj_int("timePoll", PLC_Data[unit].PollingTimeout);
89 json_write_obj_string("ip", table_network[unit].ipv4_addr);
90 json_write_obj_string("nm", table_network[unit].ipv4_nm);
91 json_write_obj_string("gw", table_network[unit].ipv4_gw);
92 json_write_obj_array("filter");
93 json_write_arr_int(PLC_Data[unit].Type_Filter);
94 json_write_arr_int(PLC_Data[unit].FilterATT);
95 json_write_end(); // çàêðûâàåì ìàññèâ
96 if(unit != 0x00){
97 json_write_obj_array("route");
98 for(uint8_t i = 0; i < sizeof(PLC_Data[unit].Route); i++) json_write_arr_int(PLC_Data[unit].Route[i]);
99 json_write_end(); // çàêðûâàåì ìàññèâ
100 }
101 }
102 else {
103 json_write_obj_string("status", "error");
104 json_write_obj_string("error", "invalid parametr");
105 }
106}
107
108
109void cmd_plc_setblock(json_tokens_data_t *tokens_data, json_response_t *json_response)
110{
111 uint16_t unit = 0;
112 uint8_t value = 0;
113 if(json_get_value_object(tokens_data, "unit", &unit, json_type_integer, json_root)== json_success){
114 if(unit > AMOUNT_BLOCK_PLC - 1){
115 json_write_obj_string("status", "error");
116 json_write_obj_string("error", "invalid parametr");
117 return;
118 }
119 json_write_obj_string("status", "ok");
120 PLC_Data[unit].EnablePoll = 1;
121 json_get_value_object(tokens_data, "timePoll", &value, json_type_integer, json_root);
122 PLC_Data[unit].CommonAlarm = 1; // ãîâîðèì ÷òî èçíà÷àëüíî áëîê îòñòóòñâóåò
123 PLC_Data[unit].PollingTimeout = value;
124 json_get_value_object(tokens_data, "name", &PLC_Data[unit].NameBlock, json_type_string , json_root);
125
126 if(unit != 0x00){ // êàê åãî ñðàâíèòü ñ ñóùåñòâóþùèì ìàðøðóòîì, äëÿ îáîçíà÷åíèÿ ÷òî ìàðøðóò èçìåíèëñÿ
127 json_get_value_object(tokens_data, "route", NULL, json_type_array , json_root);
128 for(uint8_t i = 0; i < sizeof(PLC_Data[unit].Route); i++){
129 json_get_value_array(tokens_data, &value, i, json_type_integer, json_obj);
130 if(value != PLC_Data[unit].Route[i]){// åñëè áàéò ìàðøðóòà îòëè÷àåòñÿ òî çíà÷èò åñòü èçìåíåíèå
131 PLC_Data[unit].Route[i] = value;
132 PLC_Data[unit].ChangeSettings = 1;
133 }
134 }
135 json_get_value_object(tokens_data, "ip", table_network[unit].ipv4_addr, json_type_string , json_root); /* ó êàæäîãî ñâîé IP(ïðèêèäûâàåìñÿ îòäåëüíûìè óñòðîéñòâàìè)*/
136 json_get_value_object(tokens_data, "nm", table_network[unit].ipv4_nm, json_type_string, json_root);
137 json_get_value_object(tokens_data, "gw", table_network[unit].ipv4_gw, json_type_string, json_root);
138 LWIP_NetApply(&table_network[unit]);
139 netif_set_up(&table_network[unit].netif); // åñëè ðàçðåøåí ìîíèòîðèíã, ïîäíèìàåì lwip èíòåðôåéñ
140 }
141
142 json_get_value_object(tokens_data, "filter", NULL, json_type_array , json_root);
143 json_get_value_array(tokens_data, &value, 0, json_type_integer , json_obj);
144 PLC_Data[unit].Type_Filter = (TypeFilter_typeDef)value;
145 json_get_value_array(tokens_data, &value, 1, json_type_integer, json_obj);
146 PLC_Data[unit].FilterATT = (FilterATT_typeDef)value;
147
148 plc_common.num = 1;
149 for(uint8_t i = 1; i < AMOUNT_BLOCK_PLC; i++){ // ïåðåñ÷åò êîëëè÷åñòâà áëîêîâ äëÿ ìîíèòîðèíãà
150 if(PLC_Data[i].EnablePoll) plc_common.num++;
151 }
152 }
153 else {
154 json_write_obj_string("status", "error");
155 json_write_obj_string("error", "invalid parametr");
156 }
157}
158
159extern plc_common_typeDef plc_common;
160
161void cmd_plc_enable_all_pool(json_tokens_data_t *tokens_data, json_response_t *json_response)
162{
163 _Bool en = 0;
164 json_write_obj_string("status", "ok");
165
166 if(json_get_value_object(tokens_data, "enable", &en, json_type_boolean , json_root)== json_success) plc_common.enable_all_pool = en; // åñòü ïîëå òî çàïèñü
167 else json_write_obj_bool("enable", plc_common.enable_all_pool);// íåò ïîëÿ òî ÷òåíèå
168}
169
170void cmd_plc_getBlockslist(json_tokens_data_t *tokens_data, json_response_t *json_response)
171{
172 json_write_obj_string("status", "ok");
173 json_write_obj_array("list");
174 json_write_arr_int(plc_common.num);
175 for(uint8_t i = 0; i < plc_common.num; i++){
176 json_write_arr_array();
177 json_write_arr_string(PLC_Data[i].NameBlock);
178 json_write_arr_string(table_network[i].ipv4_addr);
179 json_write_end(); // çàêðûâàåì ìàññèâ
180 }
181 json_write_end(); // çàêðûâàåì ìàññèâ
182}
183
184void cmd_plc_rmblock(json_tokens_data_t *tokens_data, json_response_t *json_response) // êîìàíäà äëÿ óäàëåíèÿ áëîêà, íóæíî ïåðåëàïà÷èâàòü âñþ ñòðóêòóðó äàííûõ
185{
186 uint16_t unit = 0;
187 uint8_t up = 0;
188 if(json_get_value_object(tokens_data, "unit", &unit, json_type_integer , json_root)== json_success){
189 if((unit > AMOUNT_BLOCK_PLC - 1) || (unit == 0)){ // òàê êàê íåëüçÿ óäàëèòü ñàìûé ïåðâûé áëîê
190 json_write_obj_string("status", "error");
191 json_write_obj_string("error", "invalid parametr");
192 return;
193 }
194 up = plc_common.num - unit - 1; // ýòî ñêîëüêî óðîâíåé ââåðõ íóæíî ïîäíÿòü
195 plc_common.change_block = unit;
196 if(up != 0) {
197 for(uint8_t i = 0; i < up; i++) {
198 memcpy(&PLC_Data[unit], &PLC_Data[unit + 1], sizeof(PLC_Data[unit + 1]));
199 memcpy(&table_network[unit].ipv4_addr, &table_network[unit + 1].ipv4_addr, sizeof(table_network[unit + 1].ipv4_addr));
200 memcpy(&table_network[unit].ipv4_nm, &table_network[unit + 1].ipv4_nm, sizeof(table_network[unit + 1].ipv4_nm));
201 memcpy(&table_network[unit].ipv4_gw, &table_network[unit + 1].ipv4_gw, sizeof(table_network[unit + 1].ipv4_gw));
202 LWIP_NetApply(&table_network[unit]); // åùå àäðåñ IP
203 PLC_Data[unit + 1].EnablePoll = 0;
204 PLC_Data[unit + 1].CommonAlarm = 1;
205 unit++;
206 }
207 }
208 else {
209 PLC_Data[unit].EnablePoll = 0;
210 PLC_Data[unit].CommonAlarm = 1;
211 }
212 plc_common.num --;
213 netif_set_down(&table_network[plc_common.num].netif);
214 json_write_obj_string("status", "ok");
215 }
216 else {
217 json_write_obj_string("status", "error");
218 json_write_obj_string("error", "invalid parametr");
219 }
220}
221
222void cmd_plc_alarm(json_tokens_data_t *tokens_data, json_response_t *json_response)
223{
224 json_write_obj_string("status", "ok");
225 json_write_obj_array("alarms");
226 for(uint8_t i = 0; i < plc_common.num; i++)json_write_arr_int(PLC_Data[i].CommonAlarm);
227 json_write_end(); // çàêðûâàåì ìàññèâ
228}
Note: See TracBrowser for help on using the repository browser.