diff --git a/Code/Libs/ODC/IOTypesJSON.cpp b/Code/Libs/ODC/IOTypesJSON.cpp index 2cc01f24..cbc3d4fd 100644 --- a/Code/Libs/ODC/IOTypesJSON.cpp +++ b/Code/Libs/ODC/IOTypesJSON.cpp @@ -261,7 +261,9 @@ template<> ControlRelayOutputBlock PayloadFromJson(const Json::Value& JLoad) ControlRelayOutputBlock CROB; if(JLoad["ControlCode"].isUInt()) CROB.functionCode = static_cast(JLoad["ControlCode"].asUInt()&0xFF); - else if(!JLoad["ControlCode"].isString() || !ToControlCode(JLoad["ControlCode"].asString(),CROB.functionCode)) + else if(JLoad["ControlCode"].isString()) + CROB.functionCode = ControlCodeFromString(JLoad["ControlCode"].asString()); + else throw std::invalid_argument("Payload 'ControlCode' is not unsigned integer or valid string."); if(JLoad.isMember("CommandStatus")) @@ -401,10 +403,12 @@ template<> ConnectState PayloadFromJson(const Json::Value& JLoad) return static_cast(JLoad.asUInt()); } - ConnectState CS; - if(JLoad.isString() && GetConnectStateFromStringName(JLoad.asString(),CS)) - return CS; - + if(JLoad.isString()) + { + ConnectState CS = ConnectStateFromString(JLoad.asString()); + if(CS != ConnectState::UNDEFINED) + return CS; + } throw std::invalid_argument("Payload not convertable to odc::ConnectState."); } diff --git a/Code/Ports/PyPort/PyPort.cpp b/Code/Ports/PyPort/PyPort.cpp index fd045237..373170fd 100644 --- a/Code/Ports/PyPort/PyPort.cpp +++ b/Code/Ports/PyPort/PyPort.cpp @@ -354,8 +354,8 @@ std::shared_ptr PyPort::CreateEventFromStrParams(const std::stri { case EventType::ConnectState: { - ConnectState state; // PORT_UP,CONNECTED,DISCONNECTED,PORT_DOWN - if (!GetConnectStateFromStringName(PayloadStr, state)) + ConnectState state = ConnectStateFromString(PayloadStr); + if (state == ConnectState::UNDEFINED) { LOGERROR("Invalid Connection State passed from Python Code to ODC - {}", PayloadStr); return nullptr; @@ -406,8 +406,8 @@ std::shared_ptr PyPort::CreateEventFromStrParams(const std::stri auto Parts = split(PayloadStr, '|'); if (Parts.size() != 5) throw std::runtime_error("Payload for ControlRelayOutputBlock does not have enough sections " + PayloadStr); - ControlCode ControlCodeResult; - ToControlCode(Parts[1], ControlCodeResult); + ControlCode ControlCodeResult = ControlCodeFromString(Parts[1]); + if(ControlCodeResult == ControlCode::UNDEFINED) throw std::runtime_error("ControlCode field of ControlRelayOutputBlock not in " + Parts[1]); val.functionCode = ControlCodeResult; if (Parts[2].find("Count") == std::string::npos) throw std::runtime_error("Count field of ControlRelayOutputBlock not in " + Parts[2]); diff --git a/Code/Ports/SimPort/SimPort.cpp b/Code/Ports/SimPort/SimPort.cpp index 41f37005..8716b773 100644 --- a/Code/Ports/SimPort/SimPort.cpp +++ b/Code/Ports/SimPort/SimPort.cpp @@ -708,7 +708,7 @@ void SimPort::Build() std::string error = ""; if (parameters.count("type") != 0) - type = ToEventType(parameters.at("type")); + type = EventTypeFromString(parameters.at("type")); else error = "No 'type' parameter found"; diff --git a/Code/Ports/SimPort/SimPortCollection.h b/Code/Ports/SimPort/SimPortCollection.h index e4c1689a..59130fc5 100644 --- a/Code/Ports/SimPort/SimPortCollection.h +++ b/Code/Ports/SimPort/SimPortCollection.h @@ -51,7 +51,7 @@ class SimPortCollection: public ResponderMap< std::weak_ptr > { return IUIResponder::GenerateResult("Bad parameter"); } - auto type = ToEventType(params.at("0")); + auto type = EventTypeFromString(params.at("0")); auto index = params.at("1"); auto period = params.at("2"); if(target->UISetUpdateInterval(type,index,period)) @@ -119,7 +119,7 @@ class SimPortCollection: public ResponderMap< std::weak_ptr > { return IUIResponder::GenerateResult("Bad parameter"); } - auto type = ToEventType(params.at("0")); + auto type = EventTypeFromString(params.at("0")); auto index = params.at("1"); if(target->UIRelease(type,index)) return IUIResponder::GenerateResult("Success"); @@ -148,7 +148,7 @@ class SimPortCollection: public ResponderMap< std::weak_ptr > { return IUIResponder::GenerateResult("Bad parameter"); } - auto type = ToEventType(params.at("0")); + auto type = EventTypeFromString(params.at("0")); auto index = params.at("1"); auto value = params.at("2"); std::string quality = ""; diff --git a/Code/tests/DNP3Port_tests/TestDNP3EventHandling.cpp b/Code/tests/DNP3Port_tests/TestDNP3EventHandling.cpp index aacd3692..af557f52 100644 --- a/Code/tests/DNP3Port_tests/TestDNP3EventHandling.cpp +++ b/Code/tests/DNP3Port_tests/TestDNP3EventHandling.cpp @@ -192,10 +192,8 @@ void CheckPointDB(const std::shared_ptr& port, const std::vector StrEventType.length()) - continue; - if (StrEventType.find(FindStr, StrEventType.length()-FindStr.length()) != std::string::npos) + if (StrEventType == ToString(EventTypeResult)) break; } while (EventTypeResult != EventType::AfterRange); return EventTypeResult; } -//FIXME: this is an anti-pattern. Should be -// ControlCodeFromString(), returning a ControlCode, UNDEFINED if not found -inline bool ToControlCode(const std::string StrControlCode, ControlCode& ControlCodeResult) +inline ControlCode ControlCodeFromString(const std::string& StrControlCode) { - #define CHECKCONTROLCODESTRING(X) if (StrControlCode.find(ToString(X)) != std::string::npos) ControlCodeResult = X - - ControlCodeResult = ControlCode::UNDEFINED; - - CHECKCONTROLCODESTRING(ControlCode::CLOSE_PULSE_ON); - CHECKCONTROLCODESTRING(ControlCode::LATCH_OFF); - CHECKCONTROLCODESTRING(ControlCode::LATCH_ON); - CHECKCONTROLCODESTRING(ControlCode::NUL); - CHECKCONTROLCODESTRING(ControlCode::PULSE_OFF); - CHECKCONTROLCODESTRING(ControlCode::PULSE_ON); - CHECKCONTROLCODESTRING(ControlCode::TRIP_PULSE_ON); - - return (ControlCodeResult != ControlCode::UNDEFINED); -} - -//FIXME: this is an anti-pattern. Should be -// ConnectStateFromString(), returning a ConnectState, UNDEFINED if not found -inline bool GetConnectStateFromStringName(const std::string StrConnectState, ConnectState& ConnectStateResult) -{ - #define CHECKCONNECTSTATESTRING(X) if (StrConnectState.find(ToString(X)) != std::string::npos) {ConnectStateResult = X;return true;} - - CHECKCONNECTSTATESTRING(ConnectState::CONNECTED); - CHECKCONNECTSTATESTRING(ConnectState::DISCONNECTED); - CHECKCONNECTSTATESTRING(ConnectState::PORT_DOWN); - CHECKCONNECTSTATESTRING(ConnectState::PORT_UP); - - return false; + CHECKENUMSTRING(StrControlCode,ControlCode,CLOSE_PULSE_ON); + CHECKENUMSTRING(StrControlCode,ControlCode,LATCH_OFF); + CHECKENUMSTRING(StrControlCode,ControlCode,LATCH_ON); + CHECKENUMSTRING(StrControlCode,ControlCode,NUL); + CHECKENUMSTRING(StrControlCode,ControlCode,PULSE_OFF); + CHECKENUMSTRING(StrControlCode,ControlCode,PULSE_ON); + CHECKENUMSTRING(StrControlCode,ControlCode,TRIP_PULSE_ON); + return ControlCode::UNDEFINED; } -inline EventType ToEventType(const std::string& str_type) +inline ConnectState ConnectStateFromString(const std::string StrConnectState) { - EventType type = EventType::BeforeRange; - if (to_lower(str_type) == "binary") - type = EventType::Binary; - else if (to_lower(str_type) == "analog") - type = EventType::Analog; - return type; + CHECKENUMSTRING(StrConnectState,ConnectState,CONNECTED); + CHECKENUMSTRING(StrConnectState,ConnectState,DISCONNECTED); + CHECKENUMSTRING(StrConnectState,ConnectState,PORT_DOWN); + CHECKENUMSTRING(StrConnectState,ConnectState,PORT_UP); + return ConnectState::UNDEFINED; } //Map EventTypes to payload types