@@ -26,8 +26,8 @@ QJsonObject MtbUni::moduleInfo(bool state, bool config) const {
26
26
{" ir" , this ->isIrSupport ()},
27
27
};
28
28
29
- if (config)
30
- uni[" config" ] = this ->config .json (this ->isIrSupport (), false );
29
+ if (( config) && ( this -> config . has_value ()) )
30
+ uni[" config" ] = this ->config .value (). json (this ->isIrSupport (), false );
31
31
32
32
if (state && this ->active && !this ->busModuleInfo .inBootloader ()) {
33
33
uni[" state" ] = QJsonObject{
@@ -238,14 +238,14 @@ void MtbUni::jsonSetConfig(QTcpSocket *socket, const QJsonObject &request) {
238
238
239
239
MtbModule::jsonSetConfig (socket, request);
240
240
241
- MtbUniConfig oldConfig = this ->configToWrite ;
242
- this ->configToWrite .fromJson ( request[" config" ].toObject ());
241
+ std::optional< MtbUniConfig> oldConfig = this ->configToWrite ;
242
+ this ->configToWrite .emplace ( MtbUniConfig ( request[" config" ].toObject () ));
243
243
this ->configWriting = ServerRequest (socket, request);
244
244
245
245
if ((this ->active ) && (oldConfig != this ->configToWrite )) {
246
246
mtbusb.send (
247
247
Mtb::CmdMtbModuleSetConfig (
248
- this ->address , this ->configToWrite .serializeForMtbUsb (this ->isIrSupport ()),
248
+ this ->address , this ->configToWrite .value (). serializeForMtbUsb (this ->isIrSupport ()),
249
249
{[this ](uint8_t , void *) { this ->mtbBusConfigWritten (); }},
250
250
{[this ](Mtb::CmdError error, void *) { this ->mtbBusConfigNotWritten (error); }}
251
251
)
@@ -334,11 +334,13 @@ void MtbUni::resetOutputsOfClient(QTcpSocket *socket) {
334
334
MtbModule::resetOutputsOfClient (socket);
335
335
336
336
bool send = false ;
337
- for (size_t i = 0 ; i < UNI_IO_CNT; i++) {
338
- if (this ->whoSetOutput [i] == socket) {
339
- this ->outputsWant [i] = this ->config .outputsSafe [i];
340
- this ->whoSetOutput [i] = nullptr ;
341
- send = true ;
337
+ if (this ->config .has_value ()) {
338
+ for (size_t i = 0 ; i < UNI_IO_CNT; i++) {
339
+ if (this ->whoSetOutput [i] == socket) {
340
+ this ->outputsWant [i] = this ->config .value ().outputsSafe [i];
341
+ this ->whoSetOutput [i] = nullptr ;
342
+ send = true ;
343
+ }
342
344
}
343
345
}
344
346
@@ -409,7 +411,7 @@ std::array<uint8_t, UNI_IO_CNT> MtbUni::moduleOutputsData(const std::vector<uint
409
411
410
412
void MtbUni::allOutputsReset () {
411
413
for (size_t i = 0 ; i < UNI_IO_CNT; i++) {
412
- this ->outputsWant [i] = this ->config .outputsSafe [i];
414
+ this ->outputsWant [i] = this ->config .has_value () ? this -> config . value (). outputsSafe [i] : 0 ;
413
415
this ->outputsConfirmed [i] = this ->outputsWant [i];
414
416
this ->whoSetOutput [i] = nullptr ;
415
417
}
@@ -450,11 +452,11 @@ void MtbUni::activate() {
450
452
this ->mlog (" Module warning=" +QString::number (this ->busModuleInfo .warning )+" , error=" +
451
453
QString::number (this ->busModuleInfo .error ), Mtb::LogLevel::Warning);
452
454
453
- if (this ->configLoaded ) {
455
+ if (this ->config . has_value () ) {
454
456
this ->mlog (" Config previously loaded from file, setting to module..." , Mtb::LogLevel::Info);
455
457
mtbusb.send (
456
458
Mtb::CmdMtbModuleSetConfig (
457
- this ->address , this ->config .serializeForMtbUsb (this ->isIrSupport ()),
459
+ this ->address , this ->config .value (). serializeForMtbUsb (this ->isIrSupport ()),
458
460
{[this ](uint8_t , void *) { this ->configSet (); }},
459
461
{[this ](Mtb::CmdError error, void *) {
460
462
this ->mlog (" Unable to set module config." , Mtb::LogLevel::Error);
@@ -468,8 +470,7 @@ void MtbUni::activate() {
468
470
Mtb::CmdMtbModuleGetConfig (
469
471
this ->address ,
470
472
{[this ](uint8_t , const std::vector<uint8_t >& data, void *) {
471
- this ->config .fromMtbUsb (data);
472
- this ->configLoaded = true ;
473
+ this ->config .emplace (MtbUniConfig (data));
473
474
this ->configSet ();
474
475
}},
475
476
{[this ](Mtb::CmdError error, void *) {
@@ -518,8 +519,10 @@ void MtbUni::storeInputsState(const std::vector<uint8_t> &data) {
518
519
}
519
520
520
521
void MtbUni::outputsReset () {
521
- this ->outputsWant = this ->config .outputsSafe ;
522
- this ->outputsConfirmed = this ->outputsWant ;
522
+ for (size_t i = 0 ; i < UNI_IO_CNT; i++) {
523
+ this ->outputsWant [i] = this ->config .has_value () ? this ->config .value ().outputsSafe [i] : 0 ;
524
+ this ->outputsConfirmed [i] = this ->outputsWant [i];
525
+ }
523
526
524
527
for (size_t i = 0 ; i < UNI_IO_CNT; i++)
525
528
this ->whoSetOutput [i] = nullptr ;
@@ -669,14 +672,13 @@ void MtbUni::reactivateCheck() {
669
672
670
673
void MtbUni::loadConfig (const QJsonObject &json) {
671
674
MtbModule::loadConfig (json);
672
- this ->config .fromJson (json[" config" ].toObject ());
673
- this ->configLoaded = true ;
675
+ this ->config .emplace (MtbUniConfig (json[" config" ].toObject ()));
674
676
}
675
677
676
678
void MtbUni::saveConfig (QJsonObject &json) const {
677
679
MtbModule::saveConfig (json);
678
- if (this ->configLoaded )
679
- json[" config" ] = this ->config .json (this ->isIrSupport (), true );
680
+ if (this ->config . has_value () )
681
+ json[" config" ] = this ->config .value (). json (this ->isIrSupport (), true );
680
682
}
681
683
682
684
/* Diagnostic Values -------------------------------------------------------- */
0 commit comments