Skip to content

Commit

Permalink
Add component identification method
Browse files Browse the repository at this point in the history
To be used for identifying lightbulbs

cc @timoschilling
  • Loading branch information
rojer committed Mar 16, 2022
1 parent 7cb7f29 commit 2909504
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 9 deletions.
2 changes: 1 addition & 1 deletion libreset/src/shelly_reset.cpp
Expand Up @@ -226,7 +226,7 @@ void CheckRebootCounter() {
ResetDevice(-1);
return;
}
SetRebootCounter((void *) (intptr_t) (reboot_counter + 1));
SetRebootCounter((void *) (intptr_t)(reboot_counter + 1));
mgos_set_timer(10000, 0, SetRebootCounter, (void *) 0);
}

Expand Down
10 changes: 9 additions & 1 deletion src/ShellyVintage/shelly_init.cpp
Expand Up @@ -53,5 +53,13 @@ void CreateComponents(std::vector<std::unique_ptr<Component>> *comps,
pri_acc->AddService(hap_light.get());

comps->push_back(std::move(hap_light));

SetIdentifyCB([](const HAPAccessoryIdentifyRequest *request UNUSED_ARG) {
if (!g_comps.empty()) {
g_comps[0]->Identify();
}
return kHAPError_None;
});
}
} // namespace shelly

} // namespace shelly
4 changes: 4 additions & 0 deletions src/shelly_component.cpp
Expand Up @@ -29,6 +29,10 @@ int Component::id() const {
return id_;
}

void Component::Identify() {
LOG(LL_INFO, ("== Identify %d %d %s", id(), (int) type(), name().c_str()));
}

bool Component::IsIdle() {
return true;
}
Expand Down
3 changes: 3 additions & 0 deletions src/shelly_component.hpp
Expand Up @@ -62,6 +62,9 @@ class Component {
bool *restart_required) = 0;
// Set state from UI.
virtual Status SetState(const std::string &state_json) = 0;
// Do something to identify this component to the user.
// Default is to print a log message, which is to say - not much.
virtual void Identify();

// Is there any activity going on?
// If true is returned, it means it's ok to destroy the component.
Expand Down
5 changes: 5 additions & 0 deletions src/shelly_hap_light_bulb.cpp
Expand Up @@ -397,6 +397,11 @@ Status LightBulb::SetState(const std::string &state_json) {
return Status::OK();
}

void LightBulb::Identify() {
LOG(LL_INFO, ("=== IDENTIFY ==="));
// TODO: Set brightness to max and blink 5 times at 100 ms.
}

void LightBulb::ResetAutoOff() {
auto_off_timer_.Reset(cfg_->auto_off_delay * 1000, 0);
}
Expand Down
15 changes: 8 additions & 7 deletions src/shelly_hap_light_bulb.hpp
Expand Up @@ -41,15 +41,16 @@ class LightBulb : public Component, public mgos::hap::Service {
virtual ~LightBulb();

// Component interface impl.
Type type() const override;
std::string name() const override;
Status Init() override;
Type type() const final;
std::string name() const final;
Status Init() final;

StatusOr<std::string> GetInfo() const override;
StatusOr<std::string> GetInfoJSON() const override;
StatusOr<std::string> GetInfo() const final;
StatusOr<std::string> GetInfoJSON() const final;
Status SetConfig(const std::string &config_json,
bool *restart_required) override;
Status SetState(const std::string &state_json) override;
bool *restart_required) final;
Status SetState(const std::string &state_json) final;
void Identify() final;

protected:
void InputEventHandler(Input::Event ev, bool state);
Expand Down
25 changes: 25 additions & 0 deletions src/shelly_rpc_service.cpp
Expand Up @@ -332,6 +332,29 @@ static void SetStateHandler(struct mg_rpc_request_info *ri, void *cb_arg,
(void) fi;
}

static void IdentifyHandler(struct mg_rpc_request_info *ri,
void *cb_arg UNUSED_ARG,
struct mg_rpc_frame_info *fi UNUSED_ARG,
struct mg_str args) {
int id = -1;
int type = -1;

json_scanf(args.p, args.len, ri->args_fmt, &id, &type);

Status st = Status::OK();
bool found = false;
for (auto &c : g_comps) {
if (c->id() != id || (int) c->type() != type) continue;
c->Identify();
found = true;
break;
}
if (!found) {
st = mgos::Errorf(STATUS_INVALID_ARGUMENT, "component not found");
}
SendStatusResp(ri, st);
}

static void InjectInputEventHandler(struct mg_rpc_request_info *ri,
void *cb_arg, struct mg_rpc_frame_info *fi,
struct mg_str args) {
Expand Down Expand Up @@ -556,6 +579,8 @@ bool RPCServiceInit(HAPAccessoryServerRef *server,
SetConfigHandler, nullptr);
mg_rpc_add_handler(c, "Shelly.SetState", "{id: %d, type: %d, state: %T}",
SetStateHandler, nullptr);
mg_rpc_add_handler(c, "Shelly.Identify", "{id: %d, type: %d}",
IdentifyHandler, nullptr);
mg_rpc_add_handler(c, "Shelly.InjectInputEvent", "{id: %d, event: %d}",
InjectInputEventHandler, nullptr);
mg_rpc_add_handler(c, "Shelly.Abort", "", AbortHandler, nullptr);
Expand Down

0 comments on commit 2909504

Please sign in to comment.