2022-09-21 19:37:20 +00:00
|
|
|
#include "ESPUI.h"
|
|
|
|
|
|
|
|
static uint16_t idCounter = 0;
|
|
|
|
static const String ControlError = "*** ESPUI ERROR: Could not transfer control ***";
|
|
|
|
|
2023-09-14 22:59:10 +00:00
|
|
|
Control::Control(ControlType type, const char* label, std::function<void(Control*, int)> callback,
|
2022-09-21 19:37:20 +00:00
|
|
|
const String& value, ControlColor color, bool visible, uint16_t parentControl)
|
|
|
|
: type(type),
|
|
|
|
label(label),
|
2023-09-14 22:59:10 +00:00
|
|
|
callback(callback),
|
2022-09-21 19:37:20 +00:00
|
|
|
value(value),
|
|
|
|
color(color),
|
|
|
|
visible(visible),
|
|
|
|
wide(false),
|
|
|
|
vertical(false),
|
|
|
|
enabled(true),
|
|
|
|
parentControl(parentControl),
|
|
|
|
next(nullptr)
|
|
|
|
{
|
|
|
|
id = ++idCounter;
|
2023-11-09 16:06:44 +00:00
|
|
|
ControlChangeID = 1;
|
2022-09-21 19:37:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Control::Control(const Control& Control)
|
|
|
|
: type(Control.type),
|
|
|
|
id(Control.id),
|
|
|
|
label(Control.label),
|
|
|
|
callback(Control.callback),
|
|
|
|
value(Control.value),
|
|
|
|
color(Control.color),
|
|
|
|
visible(Control.visible),
|
|
|
|
parentControl(Control.parentControl),
|
2023-11-09 16:06:44 +00:00
|
|
|
next(Control.next),
|
|
|
|
ControlChangeID(Control.ControlChangeID)
|
2022-09-21 19:37:20 +00:00
|
|
|
{ }
|
|
|
|
|
|
|
|
void Control::SendCallback(int type)
|
|
|
|
{
|
|
|
|
if(callback)
|
|
|
|
{
|
|
|
|
callback(this, type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Control::DeleteControl()
|
|
|
|
{
|
2023-11-09 16:06:44 +00:00
|
|
|
_ToBeDeleted = true;
|
2022-09-21 19:37:20 +00:00
|
|
|
callback = nullptr;
|
|
|
|
}
|
|
|
|
|
2023-09-09 19:58:12 +00:00
|
|
|
void Control::MarshalControl(JsonObject & _item, bool refresh, uint32_t StartingOffset)
|
2022-09-21 19:37:20 +00:00
|
|
|
{
|
2023-09-09 19:58:12 +00:00
|
|
|
JsonObject & item = _item;
|
|
|
|
uint32_t length = value.length();
|
|
|
|
uint32_t maxLength = uint32_t(double(ESPUI.jsonInitialDocumentSize) * 0.75);
|
|
|
|
if((length > maxLength) || StartingOffset)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Serial.println(String("MarshalControl:Start Fragment Processing"));
|
|
|
|
Serial.println(String("MarshalControl:id: ") + String(id));
|
|
|
|
Serial.println(String("MarshalControl:length: ") + String(length));
|
|
|
|
Serial.println(String("MarshalControl:StartingOffset: ") + String(StartingOffset));
|
|
|
|
Serial.println(String("MarshalControl:maxLength: ") + String(maxLength));
|
|
|
|
|
|
|
|
if(0 == StartingOffset)
|
|
|
|
{
|
|
|
|
Serial.println(String("MarshalControl: New control to fragement. ID: ") + String(id));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Serial.println(String("MarshalControl: Next fragement. ID: ") + String(id));
|
|
|
|
}
|
2023-09-09 21:01:55 +00:00
|
|
|
*/
|
2023-09-09 19:58:12 +00:00
|
|
|
|
2023-09-09 21:01:55 +00:00
|
|
|
// indicate that no additional controls should be sent
|
2023-09-09 19:58:12 +00:00
|
|
|
_item[F("type")] = uint32_t(ControlType::Fragment);
|
|
|
|
_item[F("id")] = id;
|
|
|
|
|
|
|
|
length = min((length - StartingOffset), maxLength);
|
2023-09-09 21:01:55 +00:00
|
|
|
// Serial.println(String("MarshalControl:Final length: ") + String(length));
|
2023-09-09 19:58:12 +00:00
|
|
|
|
|
|
|
_item[F("offset")] = StartingOffset;
|
|
|
|
_item[F("length")] = length;
|
|
|
|
_item[F("total")] = value.length();
|
|
|
|
item = _item.createNestedObject(F("control"));
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:37:20 +00:00
|
|
|
item[F("id")] = id;
|
2022-11-27 21:59:28 +00:00
|
|
|
ControlType TempType = (ControlType::Password == type) ? ControlType::Text : type;
|
2022-09-21 19:37:20 +00:00
|
|
|
if(refresh)
|
|
|
|
{
|
2022-11-27 21:59:28 +00:00
|
|
|
item[F("type")] = uint32_t(TempType) + uint32_t(ControlType::UpdateOffset);
|
2022-09-21 19:37:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-27 21:59:28 +00:00
|
|
|
item[F("type")] = uint32_t(TempType);
|
2022-09-21 19:37:20 +00:00
|
|
|
}
|
2023-09-09 19:58:12 +00:00
|
|
|
|
2022-09-21 19:37:20 +00:00
|
|
|
item[F("label")] = label;
|
2023-09-09 19:58:12 +00:00
|
|
|
item[F ("value")] = (ControlType::Password == type) ? F ("--------") : value.substring(StartingOffset, length + StartingOffset);
|
2022-09-21 19:37:20 +00:00
|
|
|
item[F("visible")] = visible;
|
|
|
|
item[F("color")] = (int)color;
|
|
|
|
item[F("enabled")] = enabled;
|
|
|
|
|
|
|
|
if (!panelStyle.isEmpty()) {item[F("panelStyle")] = panelStyle;}
|
|
|
|
if (!elementStyle.isEmpty()) {item[F("elementStyle")] = elementStyle;}
|
|
|
|
if (!inputType.isEmpty()) {item[F("inputType")] = inputType;}
|
|
|
|
if (wide == true) {item[F("wide")] = true;}
|
|
|
|
if (vertical == true) {item[F("vertical")] = true;}
|
|
|
|
if (parentControl != Control::noParent)
|
|
|
|
{
|
|
|
|
item[F("parentControl")] = String(parentControl);
|
|
|
|
}
|
|
|
|
|
|
|
|
// special case for selects: to preselect an option, you have to add
|
|
|
|
// "selected" to <option>
|
|
|
|
if (ControlType::Option == type)
|
|
|
|
{
|
|
|
|
Control* ParentControl = ESPUI.getControlNoLock(parentControl);
|
|
|
|
if (nullptr == ParentControl)
|
|
|
|
{
|
|
|
|
item[F("selected")] = emptyString;
|
|
|
|
}
|
|
|
|
else if (ParentControl->value == value)
|
|
|
|
{
|
|
|
|
item[F("selected")] = F("selected");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item[F("selected")] = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Control::MarshalErrorMessage(JsonObject & item)
|
|
|
|
{
|
|
|
|
item[F("id")] = id;
|
|
|
|
item[F("type")] = uint32_t(ControlType::Label);
|
|
|
|
item[F("label")] = ControlError.c_str();
|
|
|
|
item[F("value")] = ControlError;
|
|
|
|
item[F("visible")] = true;
|
|
|
|
item[F("color")] = (int)ControlColor::Carrot;
|
|
|
|
item[F("enabled")] = true;
|
|
|
|
|
|
|
|
if (parentControl != Control::noParent)
|
|
|
|
{
|
|
|
|
item[F("parentControl")] = String(parentControl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Control::onWsEvent(String & cmd, String& data)
|
|
|
|
{
|
|
|
|
do // once
|
|
|
|
{
|
2023-11-09 16:06:44 +00:00
|
|
|
// Serial.println(String(F("Control::onWsEvent")));
|
|
|
|
SetControlChangedId(ESPUI.GetNextControlChangeId());
|
2022-09-21 19:37:20 +00:00
|
|
|
if (!HasCallback())
|
|
|
|
{
|
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.println(String(F("Control::onWsEvent:No callback found for ID ")) + String(id));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serial.println("Control::onWsEvent:Generating callback");
|
|
|
|
if (cmd.equals(F("bdown")))
|
|
|
|
{
|
|
|
|
SendCallback(B_DOWN);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.equals(F("bup")))
|
|
|
|
{
|
|
|
|
SendCallback(B_UP);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.equals(F("pfdown")))
|
|
|
|
{
|
|
|
|
SendCallback(P_FOR_DOWN);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.equals(F("pfup")))
|
|
|
|
{
|
|
|
|
SendCallback(P_FOR_UP);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.equals(F("pldown")))
|
|
|
|
{
|
|
|
|
SendCallback(P_LEFT_DOWN);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (cmd.equals(F("plup")))
|
|
|
|
{
|
|
|
|
SendCallback(P_LEFT_UP);
|
|
|
|
}
|
|
|
|
else if (cmd.equals(F("prdown")))
|
|
|
|
{
|
|
|
|
SendCallback(P_RIGHT_DOWN);
|
|
|
|
}
|
|
|
|
else if (cmd.equals(F("prup")))
|
|
|
|
{
|
|
|
|
SendCallback(P_RIGHT_UP);
|
|
|
|
}
|
|
|
|
else if (cmd.equals(F("pbdown")))
|
|
|
|
{
|
|
|
|
SendCallback(P_BACK_DOWN);
|
|
|
|
}
|
|
|
|
else if (cmd.equals(F("pbup")))
|
|
|
|
{
|
|
|
|
SendCallback(P_BACK_UP);
|
|
|
|
}
|
|
|
|
else if (cmd.equals(F("pcdown")))
|
|
|
|
{
|
|
|
|
SendCallback(P_CENTER_DOWN);
|
|
|
|
}
|
|
|
|
else if (cmd.equals(F("pcup")))
|
|
|
|
{
|
|
|
|
SendCallback(P_CENTER_UP);
|
|
|
|
}
|
|
|
|
else if (cmd.equals(F("sactive")))
|
|
|
|
{
|
|
|
|
value = "1";
|
|
|
|
SendCallback(S_ACTIVE);
|
|
|
|
}
|
|
|
|
else if (cmd.equals(F("sinactive")))
|
|
|
|
{
|
|
|
|
value = "0";
|
|
|
|
// updateControl(c, client->id());
|
|
|
|
SendCallback(S_INACTIVE);
|
|
|
|
}
|
|
|
|
else if (cmd.equals(F("slvalue")))
|
|
|
|
{
|
|
|
|
value = data;
|
|
|
|
// updateControl(c, client->id());
|
|
|
|
SendCallback(SL_VALUE);
|
|
|
|
}
|
|
|
|
else if (cmd.equals(F("nvalue")))
|
|
|
|
{
|
|
|
|
value = data;
|
|
|
|
// updateControl(c, client->id());
|
|
|
|
SendCallback(N_VALUE);
|
|
|
|
}
|
|
|
|
else if (cmd.equals(F("tvalue")))
|
|
|
|
{
|
|
|
|
value = data;
|
|
|
|
// updateControl(c, client->id());
|
|
|
|
SendCallback(T_VALUE);
|
|
|
|
}
|
|
|
|
else if (cmd.equals(F("tabvalue")))
|
|
|
|
{
|
|
|
|
SendCallback(0);
|
|
|
|
}
|
|
|
|
else if (cmd.equals(F("svalue")))
|
|
|
|
{
|
|
|
|
value = data;
|
|
|
|
// updateControl(c, client->id());
|
|
|
|
SendCallback(S_VALUE);
|
|
|
|
}
|
|
|
|
else if (cmd.equals(F("time")))
|
|
|
|
{
|
|
|
|
value = data;
|
|
|
|
// updateControl(c, client->id());
|
|
|
|
SendCallback(TM_VALUE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined(DEBUG_ESPUI)
|
|
|
|
if (ESPUI.verbosity)
|
|
|
|
{
|
|
|
|
Serial.println(F("Control::onWsEvent:Malformed message from the websocket"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} while (false);
|
|
|
|
}
|
2024-02-07 03:38:17 +00:00
|
|
|
|