From 91f981d036090463eb8f56f8355d82d9fb8dcddc Mon Sep 17 00:00:00 2001 From: Marcus O'Flaherty Date: Sun, 9 Aug 2026 18:13:28 +0100 Subject: [PATCH] move options.Has("value") check in SlowControlElement::Get call after read function call --- .../SlowControlCollection.cpp | 305 +++++++++--------- src/ServiceDiscovery/SlowControlElement.cpp | 17 +- 2 files changed, 162 insertions(+), 160 deletions(-) diff --git a/src/ServiceDiscovery/SlowControlCollection.cpp b/src/ServiceDiscovery/SlowControlCollection.cpp index f339179..750615a 100644 --- a/src/ServiceDiscovery/SlowControlCollection.cpp +++ b/src/ServiceDiscovery/SlowControlCollection.cpp @@ -122,16 +122,16 @@ bool SlowControlCollection::Init(zmq::context_t* context, int sc_port, bool new_ m_pub->bind(tmp.str().c_str()); if(!m_util->AddPort("alerts",alert_send_port)){ - - delete m_pub; - m_pub=0; - m_alerts_send = false; - - delete args; - args=0; - std::clog<<"Error adding alert send port to SD"<sub = new zmq::socket_t(*(m_context), ZMQ_SUB); args->sub->setsockopt(ZMQ_SUBSCRIBE, "", 0); args->sub->setsockopt(ZMQ_LINGER, 0); @@ -162,20 +162,20 @@ bool SlowControlCollection::Init(zmq::context_t* context, int sc_port, bool new_ args->alert_functions=&m_alert_functions; args->alert_functions_mutex=&m_alert_functions_mutex; - + if(!m_util->AddPort("alertr",alert_receive_port)){ - - delete args->sub; - args->sub = 0; - m_alerts_receive = false; - - delete args; - args=0; - - - std::clog<<"Error adding port alert receive to SD"<sub; + args->sub = 0; + m_alerts_receive = false; + + delete args; + args=0; + + + std::clog<<"Error adding port alert receive to SD"<items[1].events=ZMQ_POLLIN; args->items[1].revents=0; } - + args->SCC=this; Add("Status",SlowControlElementType(INFO),0,0,false,false); Add("?",SlowControlElementType(BUTTON),0,0,false,true); @@ -246,7 +246,7 @@ bool SlowControlCollection::InitThreadedReceiver(zmq::context_t* context, int po if(args) return false; //printf("InitThreadedReceiver\n"); m_thread=true; - + //std::cout<<"new_service="<poll_length=poll_length; @@ -267,7 +267,7 @@ void SlowControlCollection::Thread(Thread_args* arg){ zmq::message_t identity; int ok = args->sock->recv(&identity); - + if(ok==0 || !identity.more()){ std::cerr<<"error: Poorly formatted slowcontrol input [identity problem]"<SCC, key, value, reply, strip, *(args->testing)); /* if(key == "?"){ //printf("args->SCC->Print()=%s\n", args->SCC->Print().c_str()); if(value=="JSON"){ - reply=args->SCC->PrintJSON(); - strip=true; + reply=args->SCC->PrintJSON(); + strip=true; } else reply=args->SCC->Print(); //printf("reply=%s\n", reply.c_str()); @@ -336,29 +335,29 @@ void SlowControlCollection::Thread(Thread_args* arg){ } else{ - reply=key; - if((*args->SCC)[key]->GetType() == SlowControlElementType(BUTTON)){ - value="1"; - } + reply=key; + if((*args->SCC)[key]->GetType() == SlowControlElementType(BUTTON)){ + value="1"; + } //std::stringstream input; //input<("msg_value"); - // std::string key=""; - // std::string value=""; + // std::string key=""; + // std::string value=""; //input>>key>>value; - //printf("d0 %s = %s : %s\n", reply.c_str(), key.c_str(), value.c_str()); - if(value!=""){ - (*args->SCC)[key]->SetValue(value); - //(*args->SCC)[key]->Print(); - SCFunction tmp_func= (*args->SCC)[key]->GetChangeFunction(); - if (tmp_func!=nullptr) reply=tmp_func(key.c_str()); - - } - else{ - SCFunction tmp_func= (*args->SCC)[key]->GetReadFunction(); - if (tmp_func!=nullptr) reply=tmp_func(key.c_str()); - else (*args->SCC)[key]->GetValue(reply); - - } + //printf("d0 %s = %s : %s\n", reply.c_str(), key.c_str(), value.c_str()); + if(value!=""){ + (*args->SCC)[key]->SetValue(value); + //(*args->SCC)[key]->Print(); + SCFunction tmp_func= (*args->SCC)[key]->GetChangeFunction(); + if (tmp_func!=nullptr) reply=tmp_func(key.c_str()); + + } + else{ + SCFunction tmp_func= (*args->SCC)[key]->GetReadFunction(); + if (tmp_func!=nullptr) reply=tmp_func(key.c_str()); + else (*args->SCC)[key]->GetValue(reply); + + } } } */ @@ -398,7 +397,6 @@ void SlowControlCollection::Thread(Thread_args* arg){ std::cerr<<"failed to receive alert!"<(message.data())); - // receive alert payload std::string payload; @@ -413,7 +411,7 @@ void SlowControlCollection::Thread(Thread_args* arg){ memcpy((void*)payload.data(),message.data(),message.size()); has_data=true; } - + //int a=0; while(message.more()){ @@ -427,46 +425,46 @@ void SlowControlCollection::Thread(Thread_args* arg){ if(iss.str() == "LoadConfig") (*args->SC_vars)["Config"]->SetValue((int)ConfigState::LoadStart); else if(iss.str() == "ChangeConfig"){ if((*args->SC_vars)["NewConfig"]->GetValue() == 0){ - args->alert_functions_mutex->unlock(); - return; + args->alert_functions_mutex->unlock(); + return; } (*args->SC_vars)["Config"]->SetValue((int)ConfigState::ChangeStart); } - + bool error = false; if(args->alert_functions->count(iss.str())){ if(has_data){ - try{ - error = !((*(args->alert_functions))[iss.str()](iss.str().c_str(), payload.c_str())); - } - catch(...){ - error = true; - } - if(iss.str() == "LoadConfig"){ - if(error)(*args->SC_vars)["Config"]->SetValue((int)ConfigState::LoadFail); - else (*args->SC_vars)["Config"]->SetValue((int)ConfigState::LoadEnd); - } - + try{ + error = !((*(args->alert_functions))[iss.str()](iss.str().c_str(), payload.c_str())); + } + catch(...){ + error = true; + } + if(iss.str() == "LoadConfig"){ + if(error)(*args->SC_vars)["Config"]->SetValue((int)ConfigState::LoadFail); + else (*args->SC_vars)["Config"]->SetValue((int)ConfigState::LoadEnd); + } + } else { - try{ - error=!((*(args->alert_functions))[iss.str()](iss.str().c_str(), 0)); - } - catch(...){ - error = true; - } - if(iss.str() == "ChangeConfig"){ - if(error)(*args->SC_vars)["Config"]->SetValue((int)ConfigState::ChangeFail); - else (*args->SC_vars)["Config"]->SetValue((int)ConfigState::ChangeEnd); - (*args->SC_vars)["NewConfig"]->SetValue(0); - } + try{ + error=!((*(args->alert_functions))[iss.str()](iss.str().c_str(), 0)); + } + catch(...){ + error = true; + } + if(iss.str() == "ChangeConfig"){ + if(error)(*args->SC_vars)["Config"]->SetValue((int)ConfigState::ChangeFail); + else (*args->SC_vars)["Config"]->SetValue((int)ConfigState::ChangeEnd); + (*args->SC_vars)["NewConfig"]->SetValue(0); + } } - - if(error) std::cerr<<"alert fucntion failed: "<alert_functions_mutex->unlock(); //if(payload!=nullptr) free(payload); @@ -488,7 +486,7 @@ void SlowControlCollection::Clear(){ bool SlowControlCollection::Add(std::string name, SlowControlElementType type, SCFunction change_function, SCFunction read_function, bool testing_lock, bool hidden){ - + if(SC_vars.count(name)) return false; SC_vars[name] = new SlowControlElement(name, type, change_function, read_function,testing_lock, hidden); @@ -543,7 +541,7 @@ std::string SlowControlCollection::PrintJSON(){ first = false; } reply+="]"; - + return reply; } @@ -560,7 +558,7 @@ bool SlowControlCollection::AlertSubscribe(std::string alert, AlertFunction func bool SlowControlCollection::AlertSend(std::string alert, std::string payload){ - + // TODO add some means of returning error info, e.g. accept alert by reference and set to err description on error if(!m_alerts_send) return false; // err: "unknown alert" zmq::message_t message(alert.length()+1); @@ -578,15 +576,15 @@ bool SlowControlCollection::AlertSend(std::string alert, std::string payload){ } void SlowControlCollection::JsonParser(std::string json){ - + std::map out; - + Unpack(json, out); - + //std::cout<<"out map"<::iterator it=out.begin(); it!=out.end(); it++){ - + //std::cout<first<<" -> "<second<first, SlowControlElementType::VARIABLE); @@ -594,7 +592,6 @@ void SlowControlCollection::JsonParser(std::string json){ } - } void SlowControlCollection::Unpack(std::string in, std::map &out, std::string header){ @@ -620,16 +617,16 @@ void SlowControlCollection::Unpack(std::string in, std::mapsecond.length(); i++){ - if(it->second[i]=='{') first=i; - if(it->second[i]=='}'){ - // std::stringstream tmp; - //tmp<second.substr(first,i-first+1)); - out[header+tmp.Get("name")]=it->second.substr(first,i-first+1); - // counter++; - } - + if(it->second[i]=='{') first=i; + if(it->second[i]=='}'){ + // std::stringstream tmp; + //tmp<second.substr(first,i-first+1)); + out[header+tmp.Get("name")]=it->second.substr(first,i-first+1); + // counter++; + } + } } @@ -641,17 +638,17 @@ void SlowControlCollection::Unpack(std::string in, std::mapsecond.length(); i++){ - //std::cout<<"i="<second[i]="<second[i]<<" : counter="<second[i]=='}') bracket_counter--; + //std::cout<<"i="<second[i]="<second[i]<<" : counter="<second[i]=='}') bracket_counter--; } } } @@ -685,8 +682,8 @@ bool SlowControlCollection::Update(SlowControlCollection* SCC, std::string key, //std::cout<<"variable exists"<GetType() == SlowControlElementType(INFO)){ if(!(*SCC)[key]->GetValue(value)){ - reply="Error getting value form key: "+key; - return false; + reply="Error getting value from key: "+key; + return false; } reply=value; return true; @@ -695,7 +692,7 @@ bool SlowControlCollection::Update(SlowControlCollection* SCC, std::string key, else{ reply=key; if((*SCC)[key]->GetType() == SlowControlElementType(BUTTON)){ - value="1"; + value="1"; } //std::stringstream input; //input<("msg_value"); @@ -704,49 +701,49 @@ bool SlowControlCollection::Update(SlowControlCollection* SCC, std::string key, //input>>key>>value; //printf("d0 %s = %s : %s\n", reply.c_str(), key.c_str(), value.c_str()); if(value!=""){ - if(!testing || (testing && !(*SCC)[key]->Lockable())){ - - if(!(*SCC)[key]->SetValue(value)){ - reply =" Error setting "+key+" to value: " + value; - return false; - } - else{ - reply = value; - return true; - } - //(*SCC)[key]->Print(); - /* - SCFunction tmp_func= (*SCC)[key]->GetChangeFunction(); - if (tmp_func!=nullptr){ - try{ - reply=tmp_func(key.c_str()); - - } - catch(...){ - reply= "change function failed"; - } - } - */ - } - else reply = key + " locked"; + if(!testing || (testing && !(*SCC)[key]->Lockable())){ + + if(!(*SCC)[key]->SetValue(value)){ + reply =" Error setting "+key+" to value: " + value; + return false; + } + else{ + reply = value; + return true; + } + //(*SCC)[key]->Print(); + /* + SCFunction tmp_func= (*SCC)[key]->GetChangeFunction(); + if (tmp_func!=nullptr){ + try{ + reply=tmp_func(key.c_str()); + + } + catch(...){ + reply= "change function failed"; + } + } + */ + } + else reply = key + " locked"; } else{ - /* - SCFunction tmp_func= (*SCC)[key]->GetReadFunction(); - if (tmp_func!=nullptr){ - try{ - reply=tmp_func(key.c_str()); - } - catch(...){ - reply="read function failed"; - } - } - else (*SCC)[key]->GetValue(reply); - */ + /* + SCFunction tmp_func= (*SCC)[key]->GetReadFunction(); + if (tmp_func!=nullptr){ + try{ + reply=tmp_func(key.c_str()); + } + catch(...){ + reply="read function failed"; + } + } + else (*SCC)[key]->GetValue(reply); + */ if(!(*SCC)[key]->GetValue(reply)){ - reply="Error getting value from key: "+key; - return false; - } + reply="Error getting value from key: "+key; + return false; + } } } return true; diff --git a/src/ServiceDiscovery/SlowControlElement.cpp b/src/ServiceDiscovery/SlowControlElement.cpp index 7696fc5..fad1e46 100644 --- a/src/ServiceDiscovery/SlowControlElement.cpp +++ b/src/ServiceDiscovery/SlowControlElement.cpp @@ -255,20 +255,25 @@ bool SlowControlElement::SetValue(std::string value){ bool SlowControlElement::GetValue(std::string &value){ mtx.lock(); - if(!options.Has("value")){ - mtx.unlock(); - return false; - } if(m_read_function!=0){ try{ - options.Set("value",m_read_function("")); + options.Set("value",m_read_function("")); + } + catch(std::exception& e){ + std::cerr<<"caught "<