Index: sources/device/DeviceController.cpp =================================================================== diff -u -r61f16c988a159401c92730b4cbfca5085c77222f -r97d593e2e7adb36f2f9f97f9bb9958dcef740bc1 --- sources/device/DeviceController.cpp (.../DeviceController.cpp) (revision 61f16c988a159401c92730b4cbfca5085c77222f) +++ sources/device/DeviceController.cpp (.../DeviceController.cpp) (revision 97d593e2e7adb36f2f9f97f9bb9958dcef740bc1) @@ -28,8 +28,12 @@ #include "Logger.h" #include "ApplicationController.h" #include "FileHandler.h" +#include "DeviceModels.h" +#include "DeviceError.h" // namespace +using namespace Model; +using namespace Device; using namespace Storage; /*! @@ -39,7 +43,7 @@ * Qt handles the children destruction by their parent objects life-cycle. */ DeviceController::DeviceController(QObject *parent) : QObject(parent) { - _processBrightness .setParent(this); + DEVICE_DEV_PARENT_LIST } /*! @@ -107,13 +111,8 @@ connect(&_Logger , SIGNAL(didRemoveLogs(bool)), this , SLOT( onRemoveLogs(bool))); + DEVICE_DEV_INIT_CONNECTIONS_LIST - - // App -> Dev - connect(&_ApplicationController , SIGNAL(didAttributeRequest(const DeviceBrightnessRequestData &)), - this , SLOT( onAttributeRequest(const DeviceBrightnessRequestData &))); - connect(&_processBrightness , SIGNAL( finished(int)), - this, SLOT(onProcessBrightnessFinished(int))); } /*! @@ -282,7 +281,7 @@ /// DEBUG: qDebug() << Storage::SDCard_Base_Path_Name << mCIsReady << mOTotal << mCTotal << (mOTotal == mCTotal) << mOAvailable << mCAvailable << (mOAvailable == mCAvailable) << mPercent << mCIsReadOnly; if (mOTotal == mCTotal && - mOAvailable == mCAvailable) { + mOAvailable == mCAvailable) { return; } @@ -410,18 +409,27 @@ // coco end /*! - * \brief DeviceController::checkScript - * \details Prepends the script folder path to the script name and checks if the script exists and is executable. - * \param vScript : The script name with full path which will be set in this parameter passed argument. - * \param vShellScript : The shell script name - * \return true if succeeds and false otherwise */ -bool DeviceController::checkScript(QString &vScript, const QString &vShellScript) + * \brief DeviceController::checkError + * \details check if has error, then sets the base model accept to false and the reason to the error. + * in that case logs the error message and emit the didAttributeResponse to notify the GUI. + * \param vError - the error code, this can be the Gui enum or system number + * \param vExtraLogInfo - any extra information to be logged. Not display to user. + * \return + */ +template +bool DeviceController::checkError(DeviceError::Scripts_Error_Enum vError, TModel &vModel, QString vExtraLogInfo) { - vScript = _scriptsFolder + vShellScript; - QFileInfo info(vScript); - if ( ! info.exists () ) { LOG_DEBUG(QString("script %1 does not exist." ).arg(vScript)); return false; } - if ( ! info.isExecutable() ) { LOG_DEBUG(QString("script %1 is not executable." ).arg(vScript)); return false; } - return true; + if ( vError ) { + QString src = vError > DeviceError::eScripts_Error_Start ? MAbstract::unitText(MAbstract::Unit_Enum::eUI) : MAbstract::unitText(MAbstract::Unit_Enum::eDV) + ","; + + vModel._data.mAccepted = false ; + vModel._data.mReason = vError ; + vModel._data.mMessage = DeviceError::scriptErrorText(vError) + QString(" [%1]").arg(vError); + LOG_EVENT(src + vModel._data.mMessage + " " + vExtraLogInfo); + emit didAttributeResponse(vModel._data); + return true; + } + return false; } /*! @@ -431,45 +439,64 @@ */ void DeviceController::onAttributeRequest(const DeviceBrightnessRequestData &vData) { - qDebug() << "_DeviceController" << __FUNCTION__; - // check that script exists. - QString script; if ( ! checkScript(script, Brightness_Set ) ) return; - // extract the required data - quint8 mBrightnessPercent = vData.mBrightnessPercent; - // check if the process is not running - if ( _processBrightness.state() != QProcess::NotRunning ) return; - // check the data is valid - if ( _brightness_min > mBrightnessPercent || mBrightnessPercent > _brightness_max ) { - LOG_DEBUG(QString("Out of range brightness [%1]").arg(mBrightnessPercent)); + + Model::MDeviceBrightnessRequest mRequest (vData); + Model::MDeviceBrightnessResponse mResponse ; + mResponse._data.mBrightnessPercent = _brightness_old * 10; // set to old value in case of error + + // ----- extract the required data + _brightness_val = vData.mBrightnessPercent / 10; // convert the GUI value(percent) to device value + LOG_EVENT(mRequest.toString()); + + // ----- check that script exists. + QString script; + if ( checkError( DeviceError::checkScript(script, Brightness_Set ), mResponse, script) ) return; + + // ----- check if the process is not running + if ( _processBrightness.state() != QProcess::NotRunning ) { + checkError(DeviceError::eScripts_Error_IsRunning, mResponse); + return; } + + // ----- check the data is valid + if ( _brightness_min > _brightness_val || _brightness_val > _brightness_max ) { + checkError(DeviceError::eScripts_Error_OutOfRange, mResponse); + return; + } + // ----- check the value is in the list of correct resolution + // create the list of correct values QList _allowableValues; - for ( quint8 i = _brightness_min; i <= _brightness_max; i += _brightness_res ) - _allowableValues += i; - if ( ! _allowableValues.contains(mBrightnessPercent) ) { - LOG_DEBUG(QString("Incorrect brightness value [%1]").arg(mBrightnessPercent)); + for ( quint8 i = _brightness_min; i <= _brightness_max; i += _brightness_res ) _allowableValues += i; + // check the value is in the list + if ( ! _allowableValues.contains(_brightness_val) ) { + checkError(DeviceError::eScripts_Error_Incorrect, mResponse); return; } - // keep the requested data - _brightness_val = mBrightnessPercent / 10; // convert the percent to device value - // run the process + + // ----- run the process _processBrightness.start(script, QStringList() << QString::number(_brightness_val)); } /*! * \brief DeviceController::onProcessBrightnessFinished * \details Called when the process to set the brightness has finished * \param vExitCode (int) the exit code + * \note exit code -> 0 : set Accept [MBase] -> Log -> emit + * !0 : set Attrib [MBrgh] -> Log -> emit + * 1 - get an error when in onAttributeRequest : scriptErrorText([Gui Enum ]) + * 2 - get an error when in onProcessBrightnessExitCode : scriptErrorText([vExitCode]) + * 3 - get no error when in onProcessBrightnessExitCode : MDeviceResponse.toString() + * - in case 3 the specific model _data has to be filled prior to the toString to have it in the log. */ -void DeviceController::onProcessBrightnessFinished(int vExitCode) +void DeviceController::onProcessBrightnessExitCode(int vExitCode) { - DeviceBrightnessResponseData data; - if ( vExitCode != 0 ) { - LOG_DEBUG(QString("Failed Brightness [%1]").arg(vExitCode)); - data.mAccepted = false ; - data.mReason = vExitCode ; - return; + Model::MDeviceBrightnessResponse mResponse; + mResponse._data.mBrightnessPercent = _brightness_old * 10; // set to old value in case of error + if ( ! checkError(static_cast(vExitCode), mResponse, Brightness_Set) ) { // has no error + mResponse._data.mBrightnessPercent = _brightness_val * 10; + mResponse._data.mMessage = mResponse.toString(); + LOG_EVENT(mResponse._data.mMessage); + emit didAttributeResponse(mResponse._data); } - data.mBrightnessPercent = _brightness_val * 10; // convert the device value to percent - emit didAttributeResponse(data); }