diff --git a/Common/Logger.hxx b/Common/Logger.hxx index 8a422f0..91e9b94 100644 --- a/Common/Logger.hxx +++ b/Common/Logger.hxx @@ -37,12 +37,7 @@ namespace Common { */ class Logger{ public: - Logger() : creationTime(0), logNum(0), devNum(0), prefix(""){ - }; - - Logger(int devNum):creationTime(0), logNum(0), devNum(devNum){ - updatePrefix(); - }; + Logger(){}; ~Logger(); @@ -52,11 +47,6 @@ public: */ static void setLogLvl(int16_t lvl); - /*! - * Setting number of device to which logger is binded - * \param device number - */ - void setDevNum(int num); static void globalInfo(int lvl, const char *note1 = NULL, const char* note2 = NULL, const char* note3 = NULL); @@ -72,26 +62,8 @@ public: static const int getLogLevel(); private: - - std::fstream& getStream(); - - void closeStream(); - - void updatePrefix(); - static int16_t loggingLevel; - - std::fstream f; - long creationTime; - int logNum; - - int devNum; - - std::string prefix; - - static const char * timestrformat; - - mutex localVariableDataAccess; + static const char* timestrformat; }; @@ -104,15 +76,5 @@ inline const int Logger::getLogLevel(){ return loggingLevel; } -inline void Logger::setDevNum(int num){ - lock_guard raiiLock(localVariableDataAccess); - devNum = num; - updatePrefix(); -} - -inline void Logger::updatePrefix(){ - prefix = devNum? (char *)("[MS " + CharString(devNum) + "] "): ""; -} - }//namespace #endif /* DEBUGMETHODS_HXX_ */ diff --git a/RAMS7200LibFacade.cxx b/RAMS7200LibFacade.cxx index df56033..f4e6c63 100644 --- a/RAMS7200LibFacade.cxx +++ b/RAMS7200LibFacade.cxx @@ -35,9 +35,10 @@ RAMS7200LibFacade::RAMS7200LibFacade(RAMS7200MS& ms, queueToDPCallback cb) void RAMS7200LibFacade::EnsureConnection(bool reduSwitch) { if(reduSwitch) { + Common::Logger::globalInfo(Common::Logger::L1,__PRETTY_FUNCTION__, "Setting connection status on Redu Switch for PLC IP:" + CharString(ms._ip.c_str())); RAMS7200MarkDeviceConnectionError(!_client->Connected()); } - if(_client->Connected() && ioFailures < Common::Constants::getMaxIoFailures()){ + if(_client->Connected() && _wasConnected && ioFailures < Common::Constants::getMaxIoFailures()){ return; } else { if (_wasConnected) { @@ -54,7 +55,7 @@ void RAMS7200LibFacade::EnsureConnection(bool reduSwitch) { sleep_for(std::chrono::seconds(5)); } } while(ms._run && !_wasConnected); - RAMS7200MarkDeviceConnectionError(false); + RAMS7200MarkDeviceConnectionError(!_wasConnected); } } @@ -65,10 +66,11 @@ void RAMS7200LibFacade::Connect() _client.reset(new TS7Client()); _client->SetConnectionParams(ms._ip.c_str(), Common::Constants::getLocalTsapPort(), Common::Constants::getRemoteTsapPort()); - if(_client->Connect() == 0) { + if(_client->Connect() == 0 && _client->Connected()) { + Common::Logger::globalInfo(Common::Logger::L1,__PRETTY_FUNCTION__, "Snap7: Connected to '", ms._ip.c_str()); _wasConnected = true; + ioFailures = 0; } - RAMS7200MarkDeviceConnectionError(!_client->Connected()); } @@ -83,6 +85,10 @@ void RAMS7200LibFacade::Disconnect() void RAMS7200LibFacade::Poll() { + if(!_wasConnected){ + Common::Logger::globalWarning(__PRETTY_FUNCTION__, "Not connected to PLC IP:", ms._ip.c_str()); + return; + } if(ms.vars.empty()){ Common::Logger::globalWarning(__PRETTY_FUNCTION__, "No addresses for PLC IP:", ms._ip.c_str()); return; @@ -119,6 +125,10 @@ void RAMS7200LibFacade::Poll() } void RAMS7200LibFacade::WriteToPLC() { + if(!_wasConnected){ + Common::Logger::globalWarning(__PRETTY_FUNCTION__, "Not connected to PLC IP:", ms._ip.c_str()); + return; + } std::vector addresses; std::vector items; { @@ -242,6 +252,7 @@ void RAMS7200LibFacade::queueAll(std::vector&& dpItems, std::vector(s7items[i].pdata); + s7items[i].pdata = nullptr; } } @@ -263,24 +274,30 @@ void RAMS7200LibFacade::doSmoothing(std::vector&& dpItems, std::vectorsecond; const auto dataSize = var._toDP.Amount * Common::S7Utils::DataSizeByte(var._toDP.WordLen); + if(var._toDP.pdata == nullptr) { + var._toDP.pdata = new char[dataSize]; + } if (std::memcmp(var._toDP.pdata, item.pdata, dataSize) != 0) { std::memcpy(var._toDP.pdata, item.pdata, dataSize); toDPItems.emplace_back(DPInfo.dpAddress.c_str(), dataSize, static_cast(item.pdata)); Common::Logger::globalInfo(Common::Logger::L4, DPInfo.dpAddress.c_str(), "--> Smoothing updated"); - continue; + } else { + delete[] static_cast(item.pdata); } - } + } } else { failed << dpItems[i].dpAddress.c_str() << " "; + delete[] static_cast(item.pdata); } - delete[] static_cast(s7items[i].pdata); + item.pdata = nullptr; } } diff --git a/RAMS7200MS.cxx b/RAMS7200MS.cxx index a4a5381..b11e41b 100644 --- a/RAMS7200MS.cxx +++ b/RAMS7200MS.cxx @@ -49,13 +49,12 @@ void RAMS7200MS::removeVar(std::string varName) void RAMS7200MS::queuePLCItem(const std::string& varName, void* item) { - try - { - std::lock_guard lock{_rwmutex}; - vars.at(varName)._toPlc.pdata = item; - } - catch(const std::out_of_range& e) - { - Common::Logger::globalWarning(__PRETTY_FUNCTION__, "Undefined address", e.what()); - } + std::lock_guard lock{_rwmutex}; + + if (vars.count(varName) == 0) { + Common::Logger::globalWarning(__PRETTY_FUNCTION__, "Undefined variable:", varName.c_str()); + return; + } + + vars.at(varName)._toPlc.pdata = item; }