@@ -74,8 +74,7 @@ void WebApiSunSpecClass::onSunSpecGet(AsyncWebServerRequest* request)
74
74
}
75
75
}
76
76
77
- response->setLength ();
78
- request->send (response);
77
+ WebApi.sendJsonResponse (request, response, __FUNCTION__, __LINE__);
79
78
}
80
79
81
80
void WebApiSunSpecClass::onSunSpecPost (AsyncWebServerRequest* request)
@@ -84,44 +83,17 @@ void WebApiSunSpecClass::onSunSpecPost(AsyncWebServerRequest* request)
84
83
return ;
85
84
}
86
85
87
- bool reboot {false };
88
86
AsyncJsonResponse* response = new AsyncJsonResponse ();
89
- auto & retMsg = response->getRoot ();
90
- retMsg[" type" ] = " warning" ;
91
-
92
- if (!request->hasParam (" data" , true )) {
93
- retMsg[" message" ] = " No values found!" ;
94
- retMsg[" code" ] = WebApiError::GenericNoValueFound;
95
- response->setLength ();
96
- request->send (response);
97
- return ;
98
- }
99
-
100
- const String json = request->getParam (" data" , true )->value ();
101
-
102
- if (json.length () > 1024 ) {
103
- retMsg[" message" ] = " Data too large!" ;
104
- retMsg[" code" ] = WebApiError::GenericDataTooLarge;
105
- response->setLength ();
106
- request->send (response);
107
- return ;
108
- }
109
-
110
87
JsonDocument root;
111
- const DeserializationError error = deserializeJson (root, json);
112
-
113
- if (error) {
114
- retMsg[" message" ] = " Failed to parse data!" ;
115
- retMsg[" code" ] = WebApiError::GenericParseError;
116
- response->setLength ();
117
- request->send (response);
88
+ if (!WebApi.parseRequestData (request, response, root)) {
118
89
return ;
119
90
}
120
91
121
- auto & config = Configuration. get ();
92
+ auto & retMsg = response-> getRoot ();
122
93
94
+ auto & config = Configuration.get ();
123
95
auto sunspec_enabled = root[" enabled" ].as <bool >();
124
- reboot | = (config.SunSpec .Enabled != sunspec_enabled);
96
+ bool reboot = (config.SunSpec .Enabled != sunspec_enabled);
125
97
126
98
config.SunSpec .Enabled = sunspec_enabled;
127
99
config.SunSpec .RemoteControl = root[" remote_control" ].as <bool >();
@@ -135,34 +107,30 @@ void WebApiSunSpecClass::onSunSpecPost(AsyncWebServerRequest* request)
135
107
if (inverterArray.size () > INV_MAX_COUNT) {
136
108
retMsg[" message" ] = " Invalid amount of max channel setting given!" ;
137
109
retMsg[" code" ] = WebApiError::InverterInvalidMaxChannel;
138
- response->setLength ();
139
- request->send (response);
110
+ WebApi.sendJsonResponse (request, response, __FUNCTION__, __LINE__);
140
111
return ;
141
112
}
142
113
143
114
for (JsonVariant item : inverterArray) {
144
115
if (!(item.containsKey (" id" ) && item.containsKey (" enabled" ) && item.containsKey (" max_power" ) && item.containsKey (" channel_ac" ))) {
145
116
retMsg[" message" ] = " Values are missing!" ;
146
117
retMsg[" code" ] = WebApiError::GenericValueMissing;
147
- response->setLength ();
148
- request->send (response);
118
+ WebApi.sendJsonResponse (request, response, __FUNCTION__, __LINE__);
149
119
return ;
150
120
}
151
121
152
122
if (item[" id" ].as <uint8_t >() > INV_MAX_COUNT - 1 ) {
153
123
retMsg[" message" ] = " Invalid ID specified!" ;
154
124
retMsg[" code" ] = WebApiError::InverterInvalidId;
155
- response->setLength ();
156
- request->send (response);
125
+ WebApi.sendJsonResponse (request, response, __FUNCTION__, __LINE__);
157
126
return ;
158
127
}
159
128
160
129
JsonArray channelArrayAC = item[" channel_ac" ].as <JsonArray>();
161
130
if (channelArrayAC.size () == 0 || channelArrayAC.size () > INV_MAX_CHAN_COUNT) {
162
131
retMsg[" message" ] = " Invalid amount of max channel setting given!" ;
163
132
retMsg[" code" ] = WebApiError::InverterInvalidMaxChannel;
164
- response->setLength ();
165
- request->send (response);
133
+ WebApi.sendJsonResponse (request, response, __FUNCTION__, __LINE__);
166
134
return ;
167
135
}
168
136
@@ -178,7 +146,6 @@ void WebApiSunSpecClass::onSunSpecPost(AsyncWebServerRequest* request)
178
146
}
179
147
}
180
148
181
-
182
149
if (reboot ) {
183
150
WebApi.writeConfig (retMsg, WebApiError::MaintenanceRebootTriggered, " Inverter SunSpec configuration changed!" );
184
151
Utils::restartDtu ();
@@ -187,6 +154,5 @@ void WebApiSunSpecClass::onSunSpecPost(AsyncWebServerRequest* request)
187
154
WebApi.writeConfig (retMsg, WebApiError::SunSpecSettingsChanged, " Inverter SunSpec configuration changed!" );
188
155
}
189
156
190
- response->setLength ();
191
- request->send (response);
157
+ WebApi.sendJsonResponse (request, response, __FUNCTION__, __LINE__);
192
158
}
0 commit comments