Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/ServiceDiscovery/Services.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,8 @@ std::string Services::LoadConfigSlowControlFunc(const char* control){
ret <<"Loaded config "<<base_config_id<<":"<<run_mode_config_id;
}

// FIXME currently webpage chokes if a slow control value contains JSON
(*sc_vars)[control]->SetValue("");

return ret.str();

Expand All @@ -1250,12 +1252,14 @@ void Services::BufferThread(Thread_args* args){
// merge into a batch
bool first=true;
for(LogMsg& msg : *m_args->logging_buf){

m_args->local_merge_buf += std::string(first ? "" : ",")
+ "{\"topic\":\"LOGGING\""
+ ",\"time\":\""+TimeStringFromUnixMs(msg.timestamp)+"\""
+ ",\"device\":\""+ msg.device +"\""
+ ",\"severity\":"+std::to_string(int(msg.severity))
+ ",\"message\":\"" + msg. message + "\""
// we need to escape any " or \ symbols in the user message
+ ",\"message\":\"" + JsonEscape(msg.message) + "\""
+ ",\"repeats\":"+std::to_string(msg.repeats)+"}";
first=false;
}
Expand All @@ -1271,6 +1275,7 @@ void Services::BufferThread(Thread_args* args){

first=true;
for(std::pair<const std::string, MonitoringMsg>& msg : *m_args->monitoring_buf){

m_args->local_merge_buf += std::string(first ? "" : ",")
+ "{\"topic\":\"MONITORING\""
+ ",\"time\":\""+TimeStringFromUnixMs(msg.second.timestamp)+"\""
Expand Down Expand Up @@ -1303,6 +1308,16 @@ void Services::BufferThread(Thread_args* args){
return;
}

std::string Services::JsonEscape(std::string s){
// TODO is there a more efficient way to do this...
std::string out;
for(char& a : s){
if(a=='"' || a=='\\') out.push_back('\\');
out.push_back(a);
}
return out;
}

std::string Services::GetLocalConfig(){

return m_local_config;
Expand Down
1 change: 1 addition & 0 deletions src/ServiceDiscovery/Services.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ namespace ToolFramework {
std::string PrintSlowControlVariables();
std::string GetDeviceName();
void SetVerbose(bool in);
static std::string JsonEscape(std::string s);

template<typename T> T GetSlowControlValue(std::string name){
return (*sc_vars)[name]->GetValue<T>();
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceDiscovery/ServicesBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ bool ServicesBackend::Ready(int timeout){
std::chrono::milliseconds time_left = std::chrono::duration_cast<std::chrono::milliseconds>(end-std::chrono::steady_clock::now());
while(time_left>std::chrono::milliseconds{100}){
//std::cout<<"sending test query with time_left: "<<time_left.count()<<" ms"<<std::endl;
if(!SendCommand("W_QUERY"," select now()", &resp, std::min(decltype(time_left.count())(500), time_left.count()))){
if(!SendCommand("W_QUERY"," select now()", &resp, std::min(decltype(time_left.count())(500), time_left.count()))){
if(m_verbosity) std::cerr<<"timeout waiting on test pub"<<std::endl;
} else {
if(m_verbosity) std::cout<<"test pub repsonse: "<<resp<<std::endl;
Expand Down
59 changes: 49 additions & 10 deletions src/ServiceDiscovery/SlowControlCollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,18 @@ bool SlowControlCollection::Init(zmq::context_t* context, int sc_port, bool new_
Add("Status",SlowControlElementType(INFO),0,0,false,false);
Add("?",SlowControlElementType(BUTTON),0,0,false,true);
SC_vars["Status"]->SetValue("N/A");
Add("State",SlowControlElementType(INFO),0,0,false,false);
SC_vars["State"]->SetValue((int)State::Inactive);
Add("Config",SlowControlElementType(INFO),0,0,false,false);
SC_vars["Config"]->SetValue((int)ConfigState::Unconfigured);

Add("State",SlowControlElementType(INFO),0,0,false,false);
SC_vars["State"]->SetValue(0);
Add("ClearState",SlowControlElementType(BUTTON),
[this](const char*) -> std::string { ClearState(); return "OK";},
0,false);

// add state to the service discovery broadcast; default is no flags (not active, no error, no warning)
m_util->AddPort("State",0);

return true;
}

Expand Down Expand Up @@ -423,26 +430,27 @@ void SlowControlCollection::Thread(Thread_args* arg){
error = !((*(args->alert_functions))[iss.str()](iss.str().c_str(), payload.c_str()));
}
catch(...){
error = true;
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 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);
}

}
else
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);
}
}

if(error) std::cerr<<"alert fucntion failed: "<<iss.str().c_str()<<std::endl;

Expand Down Expand Up @@ -657,7 +665,7 @@ bool SlowControlCollection::Update(SlowControlCollection* SCC, std::string key,
return true;
}
else{
reply=SCC->Print();
reply=SCC->Print();
//printf("reply=%s\n", reply.c_str());
return true;
}
Expand Down Expand Up @@ -782,3 +790,34 @@ bool SlowControlCollection::Ready(int timeout_ms){
return timed_locker.try_lock_for(std::chrono::milliseconds(timeout_ms));
}

void SlowControlCollection::SetActive(bool active){
int mask = 1 << (int)State::Active;
m_state = (active ? m_state | mask : m_state & ~mask);
m_util->AddPort("State",m_state); // update service discovery broadcast value
SC_vars["State"]->SetValue(m_state); // update slow control value
return;
}

void SlowControlCollection::SetError(bool error){
int mask = 1 << (int)State::Error;
m_state = (error ? m_state | mask : m_state & ~mask);
m_util->AddPort("State",m_state);
SC_vars["State"]->SetValue(m_state);
return;
}

void SlowControlCollection::SetWarning(bool warn){
int mask = 1 << (int)State::Warning;
m_state = (warn ? m_state | mask : m_state & ~mask);
m_util->AddPort("State",m_state);
SC_vars["State"]->SetValue(m_state);
return;
}

void SlowControlCollection::ClearState(){
int mask = (1 << (int)State::Warning) | (1 << (int)State::Error);
m_state = m_state & ~mask;
m_util->AddPort("State",m_state);
SC_vars["State"]->SetValue(m_state);
return;
}
8 changes: 7 additions & 1 deletion src/ServiceDiscovery/SlowControlCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ namespace ToolFramework{
typedef std::function<bool(const char*, const char*)> AlertFunction;

enum class ConfigState { Unconfigured=0, LoadStart=1, LoadEnd=2, LoadFail=3, ChangeStart=4, ChangeEnd=5, ChangeFail=6};
enum class State { Active=0, Inactive=1, Warning=2, Error=3};
// N.B. m_state is a bitmask, these values represent bit numbers; bit 0 is active, bit 1 is warning...
enum class State { Active=0, Warning=1, Error=2 };

class SlowControlCollection;

Expand Down Expand Up @@ -63,6 +64,10 @@ namespace ToolFramework{
void TestingEnable();
void TestingDisable();
bool Ready(int timeout_ms);
void SetActive(bool active);
void SetError(bool error);
void SetWarning(bool warn);
void ClearState();

template<typename T> T GetValue(std::string name){
if(!SC_vars.count(name)) return T{};
Expand All @@ -85,6 +90,7 @@ namespace ToolFramework{
bool m_alerts_receive;
bool m_alerts_send;
bool m_testing = false;
int m_state = 0;

static void Thread(Thread_args* arg);
void Unpack(std::string in, std::map<std::string,std::string> &out, std::string header="");
Expand Down
Loading