Skip to content

Commit

Permalink
Migrate Safety proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
naheedsa committed Mar 3, 2024
1 parent 27e59c3 commit 4070b67
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 70 deletions.
116 changes: 58 additions & 58 deletions drivers/weather/weather_safety_proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,34 +74,34 @@ bool WeatherSafetyProxy::initProperties()
{
INDI::Weather::initProperties();

IUFillText(&keywordT[0], "WEATHER_CONDITION", "Weather Condition", "condition");
IUFillTextVector(&keywordTP, keywordT, 1, getDeviceName(), "KEYWORD", "Keywords", OPTIONS_TAB, IP_RW, 60, IPS_IDLE);
keywordTP[0].fill("WEATHER_CONDITION", "Weather Condition", "condition");
keywordTP.fill(getDeviceName(), "KEYWORD", "Keywords", OPTIONS_TAB, IP_RW, 60, IPS_IDLE);

IUFillText(&ScriptsT[WSP_SCRIPT], "WEATHER_SAFETY_SCRIPT", "Weather safety script",
ScriptsTP[WSP_SCRIPT].fill("WEATHER_SAFETY_SCRIPT", "Weather safety script",
"/usr/local/share/indi/scripts/weather_status.py");
IUFillTextVector(&ScriptsTP, ScriptsT, WSP_SCRIPT_COUNT, getDefaultName(), "WEATHER_SAFETY_SCRIPTS", "Script", OPTIONS_TAB,
ScriptsTP.fill(getDefaultName(), "WEATHER_SAFETY_SCRIPTS", "Script", OPTIONS_TAB,
IP_RW, 100, IPS_IDLE);

IUFillText(&UrlT[WSP_URL], "WEATHER_SAFETY_URL", "Weather safety URL", "http://0.0.0.0:5000/weather/safety");
IUFillTextVector(&UrlTP, UrlT, WSP_URL_COUNT, getDefaultName(), "WEATHER_SAFETY_URLS", "Url", OPTIONS_TAB, IP_RW, 100,
UrlTP[WSP_URL].fill("WEATHER_SAFETY_URL", "Weather safety URL", "http://0.0.0.0:5000/weather/safety");
UrlTP.fill(getDefaultName(), "WEATHER_SAFETY_URLS", "Url", OPTIONS_TAB, IP_RW, 100,
IPS_IDLE);

IUFillSwitch(&ScriptOrCurlS[WSP_USE_SCRIPT], "Use script", "", ISS_ON);
IUFillSwitch(&ScriptOrCurlS[WSP_USE_CURL], "Use url", "", ISS_OFF);
IUFillSwitchVector(&ScriptOrCurlSP, ScriptOrCurlS, WSP_USE_COUNT, getDeviceName(), "SCRIPT_OR_CURL", "Script or url",
ScriptOrCurlSP[WSP_USE_SCRIPT].fill("Use script", "", ISS_ON);
ScriptOrCurlSP[WSP_USE_CURL].fill("Use url", "", ISS_OFF);
ScriptOrCurlSP.fill(getDeviceName(), "SCRIPT_OR_CURL", "Script or url",
OPTIONS_TAB, IP_RW, ISR_1OFMANY, 0, IPS_IDLE);

IUFillNumber(&softErrorHysteresisN[WSP_SOFT_ERROR_MAX], "SOFT_ERROR_MAX", "Max soft errors", "%g", 0.0, 1000.0, 1.0, 30.0);
IUFillNumber(&softErrorHysteresisN[WSP_SOFT_ERROR_RECOVERY], "SOFT_ERROR_RECOVERY", "Minimum soft error for recovery", "%g",
softErrorHysteresisNP[WSP_SOFT_ERROR_MAX].fill("SOFT_ERROR_MAX", "Max soft errors", "%g", 0.0, 1000.0, 1.0, 30.0);
softErrorHysteresisNP[WSP_SOFT_ERROR_RECOVERY].fill("SOFT_ERROR_RECOVERY", "Minimum soft error for recovery", "%g",
0.0, 1000.0, 1.0, 7.0);
IUFillNumberVector(&softErrorHysteresisNP, softErrorHysteresisN, 2, getDeviceName(), "SOFT_ERROR_HYSTERESIS",
softErrorHysteresisNP.fill(getDeviceName(), "SOFT_ERROR_HYSTERESIS",
"Soft error hysterese", OPTIONS_TAB, IP_RW, 0, IPS_IDLE);

addParameter("WEATHER_SAFETY", "Weather Safety", 0.9, 1.1, 0); // 0 is unsafe, 1 is safe
setCriticalParameter("WEATHER_SAFETY");

IUFillText(&reasonsT[0], "Reasons", "", nullptr);
IUFillTextVector(&reasonsTP, reasonsT, 1, getDeviceName(), "WEATHER_SAFETY_REASONS", "Weather Safety Reasons",
reasonsTP[0].fill("Reasons", "", nullptr);
reasonsTP.fill(getDeviceName(), "WEATHER_SAFETY_REASONS", "Weather Safety Reasons",
MAIN_CONTROL_TAB, IP_RO, 120, IPS_IDLE);

addDebugControl();
Expand All @@ -115,11 +115,11 @@ bool WeatherSafetyProxy::updateProperties()

if (isConnected())
{
defineProperty(&reasonsTP);
defineProperty(reasonsTP);
}
else
{
deleteProperty(reasonsTP.name);
deleteProperty(reasonsTP.getName());
}

return true;
Expand All @@ -128,10 +128,10 @@ bool WeatherSafetyProxy::updateProperties()
bool WeatherSafetyProxy::saveConfigItems(FILE *fp)
{
INDI::Weather::saveConfigItems(fp);
IUSaveConfigText(fp, &ScriptsTP);
IUSaveConfigText(fp, &UrlTP);
IUSaveConfigSwitch(fp, &ScriptOrCurlSP);
IUSaveConfigNumber(fp, &softErrorHysteresisNP);
ScriptsTP.save(fp);
UrlTP.save(fp);
ScriptOrCurlSP.save(fp);
softErrorHysteresisNP.save(fp);
return true;
}

Expand All @@ -142,10 +142,10 @@ void WeatherSafetyProxy::ISGetProperties(const char *dev)
if (once)
{
once = false;
defineProperty(&ScriptsTP);
defineProperty(&UrlTP);
defineProperty(&ScriptOrCurlSP);
defineProperty(&softErrorHysteresisNP);
defineProperty(ScriptsTP);
defineProperty(UrlTP);
defineProperty(ScriptOrCurlSP);
defineProperty(softErrorHysteresisNP);
loadConfig(false, "WEATHER_SAFETY_SCRIPTS");
loadConfig(false, "WEATHER_SAFETY_URLS");
loadConfig(false, "SCRIPT_OR_CURL");
Expand All @@ -157,11 +157,11 @@ bool WeatherSafetyProxy::ISNewNumber(const char *dev, const char *name, double v
{
if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
{
if (strcmp(name, softErrorHysteresisNP.name) == 0)
if (softErrorHysteresisNP.isNameMatch(name))
{
IUUpdateNumber(&softErrorHysteresisNP, values, names, n);
softErrorHysteresisNP.s = IPS_OK;
IDSetNumber(&softErrorHysteresisNP, nullptr);
softErrorHysteresisNP.update(values, names, n);
softErrorHysteresisNP.setState(IPS_OK);
softErrorHysteresisNP.apply();
return true;
}
}
Expand All @@ -172,28 +172,28 @@ bool WeatherSafetyProxy::ISNewText(const char *dev, const char *name, char *text
{
if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
{
if (strcmp(name, keywordTP.name) == 0)
if (keywordTP.isNameMatch(name))
{
keywordTP.s = IPS_OK;
IUUpdateText(&keywordTP, texts, names, n);
keywordTP.setState(IPS_OK);
keywordTP.update(texts, names, n);
// update client display
IDSetText(&keywordTP, nullptr);
keywordTP.apply();
return true;
}
if (strcmp(name, ScriptsTP.name) == 0)
if (ScriptsTP.isNameMatch(name))
{
ScriptsTP.s = IPS_OK;
IUUpdateText(&ScriptsTP, texts, names, n);
ScriptsTP.setState(IPS_OK);
ScriptsTP.update(texts, names, n);
// update client display
IDSetText(&ScriptsTP, nullptr);
ScriptsTP.apply();
return true;
}
if (strcmp(name, UrlTP.name) == 0)
if (UrlTP.isNameMatch(name))
{
UrlTP.s = IPS_OK;
IUUpdateText(&UrlTP, texts, names, n);
// update client display
IDSetText(&UrlTP, nullptr);
UrlTP.setState(IPS_OK);
UrlTP.update(texts, names, n);
// update client display
UrlTP.apply( );
return true;
}
}
Expand All @@ -205,12 +205,12 @@ bool WeatherSafetyProxy::ISNewSwitch(const char *dev, const char *name, ISState
{
if (!strcmp(getDeviceName(), dev))
{
if (!strcmp(name, ScriptOrCurlSP.name))
if (ScriptOrCurlSP.isNameMatch(name))
{
LOG_DEBUG("WeatherSafetyProxy::ISNewSwitch");
IUUpdateSwitch(&ScriptOrCurlSP, states, names, n);
ScriptOrCurlSP.s = IPS_OK;
IDSetSwitch(&ScriptOrCurlSP, nullptr);
ScriptOrCurlSP.update(states, names, n);
ScriptOrCurlSP.setState(IPS_OK);
ScriptOrCurlSP.apply();
return true;
}
}
Expand All @@ -222,7 +222,7 @@ bool WeatherSafetyProxy::ISNewSwitch(const char *dev, const char *name, ISState
IPState WeatherSafetyProxy::updateWeather()
{
IPState ret = IPS_ALERT;
if (ScriptOrCurlS[WSP_USE_SCRIPT].s == ISS_ON)
if (ScriptOrCurlSP[WSP_USE_SCRIPT].getState() == ISS_ON)
{
ret = executeScript();
}
Expand All @@ -236,15 +236,15 @@ IPState WeatherSafetyProxy::updateWeather()
{
SofterrorCount++;
LOGF_WARN("Soft error %d occurred during SAFE conditions, counting", SofterrorCount);
if (SofterrorCount > softErrorHysteresisN[WSP_SOFT_ERROR_MAX].value)
if (SofterrorCount > softErrorHysteresisNP[WSP_SOFT_ERROR_MAX].getValue())
{
char Warning[] = "Max softerrors reached while Weather was SAFE";
LOG_WARN(Warning);
Safety = WSP_UNSAFE;
setParameterValue("WEATHER_SAFETY", WSP_UNSAFE);
IUSaveText(&reasonsT[0], Warning);
reasonsTP.s = IPS_OK;
IDSetText(&reasonsTP, nullptr);
reasonsTP[0].setText(Warning);
reasonsTP.setState(IPS_OK);
reasonsTP.apply();
SofterrorRecoveryMode = true;
ret = IPS_OK; // So that indiweather actually syncs the CriticalParameters we just set
}
Expand All @@ -265,7 +265,7 @@ IPState WeatherSafetyProxy::updateWeather()

IPState WeatherSafetyProxy::executeScript()
{
const char *cmd = ScriptsT[WSP_SCRIPT].text;
const char *cmd = ScriptsTP[WSP_SCRIPT].getText();

if (access(cmd, F_OK | X_OK) == -1)
{
Expand Down Expand Up @@ -306,11 +306,11 @@ IPState WeatherSafetyProxy::executeCurl()
curl_handle = curl_easy_init();
if (curl_handle)
{
curl_easy_setopt(curl_handle, CURLOPT_URL, UrlT[WSP_URL].text);
curl_easy_setopt(curl_handle, CURLOPT_URL, UrlTP[WSP_URL].getText());
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WSP_WriteCallback);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
LOGF_DEBUG("Call curl %s", UrlT[WSP_URL].text);
LOGF_DEBUG("Call curl %s", UrlTP[WSP_URL].getText());
res = curl_easy_perform(curl_handle);
if (res != CURLE_OK)
{
Expand Down Expand Up @@ -347,7 +347,7 @@ IPState WeatherSafetyProxy::parseSafetyJSON(const std::string &buffer)
if (SofterrorRecoveryMode == true)
{
SofterrorRecoveryCount++;
if (SofterrorRecoveryCount > softErrorHysteresisN[WSP_SOFT_ERROR_RECOVERY].value)
if (SofterrorRecoveryCount > softErrorHysteresisNP[WSP_SOFT_ERROR_RECOVERY].getValue())
{
LOG_INFO("Minimum soft recovery errors reached while Weather was SAFE");
SofterrorRecoveryCount = 0;
Expand Down Expand Up @@ -377,14 +377,14 @@ IPState WeatherSafetyProxy::parseSafetyJSON(const std::string &buffer)
{
char newReasons[MAXRBUF];
snprintf(newReasons, MAXRBUF, "SofterrorRecoveryMode, %s", reasons.c_str());
IUSaveText(&reasonsT[0], newReasons);
reasonsTP[0].setText(newReasons);
}
else
{
IUSaveText(&reasonsT[0], reasons.c_str());
reasonsTP[0].setText(reasons.c_str());
}
reasonsTP.s = IPS_OK;
IDSetText(&reasonsTP, nullptr);
reasonsTP.setState(IPS_OK);
reasonsTP.apply();
}
catch (json::exception &e) {}
}
Expand Down
18 changes: 6 additions & 12 deletions drivers/weather/weather_safety_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,17 @@ class WeatherSafetyProxy : public INDI::Weather
IPState executeCurl();
IPState parseSafetyJSON(const std::string &buffer);

IText keywordT[1] {};
ITextVectorProperty keywordTP;
INDI::PropertyText keywordTP {1};

IText ScriptsT[WSP_SCRIPT_COUNT] {};
ITextVectorProperty ScriptsTP;
INDI::PropertyText ScriptsTP {1};

IText UrlT[WSP_URL_COUNT] {};
ITextVectorProperty UrlTP;
INDI::PropertyText UrlTP {1};

ISwitch ScriptOrCurlS[WSP_USE_COUNT];
ISwitchVectorProperty ScriptOrCurlSP;
INDI::PropertySwitch ScriptOrCurlSP {2};

IText reasonsT[1] {};
ITextVectorProperty reasonsTP;
INDI::PropertyText reasonsTP {1};

INumber softErrorHysteresisN[WSP_SOFT_ERROR_COUNT];
INumberVectorProperty softErrorHysteresisNP;
INDI::PropertyNumber softErrorHysteresisNP {2};

int Safety = -1;
int SofterrorCount = 0;
Expand Down

0 comments on commit 4070b67

Please sign in to comment.