-
Notifications
You must be signed in to change notification settings - Fork 127
/
shelly_init.cpp
112 lines (95 loc) · 3.93 KB
/
shelly_init.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Copyright (c) Shelly-HomeKit Contributors
* All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mgos_hap.h"
#include "shelly_hap_garage_door_opener.hpp"
#include "shelly_hap_input.hpp"
#include "shelly_hap_temperature_sensor.hpp"
#include "shelly_input_pin.hpp"
#include "shelly_main.hpp"
#include "shelly_temp_sensor_ow.hpp"
#define MAX_TS_NUM 3
namespace shelly {
static std::unique_ptr<Onewire> s_onewire;
void CreatePeripherals(std::vector<std::unique_ptr<Input>> *inputs,
std::vector<std::unique_ptr<Output>> *outputs,
std::vector<std::unique_ptr<PowerMeter>> *pms UNUSED_ARG,
std::unique_ptr<TempSensor> *sys_temp UNUSED_ARG) {
outputs->emplace_back(new OutputPin(1, 4, 1));
auto *in1 = new InputPin(1, 5, 1, MGOS_GPIO_PULL_NONE, true);
in1->AddHandler(std::bind(&HandleInputResetSequence, in1, 4, _1, _2));
in1->Init();
inputs->emplace_back(in1);
bool addon_detected = DetectAddon(3, 0);
if (addon_detected) {
s_onewire.reset(new Onewire(3, 0));
if (s_onewire->DiscoverAll().empty()) {
s_onewire.reset();
// No sensor detected, we assume we have a switch attached
auto *in2 = new InputPin(2, 3, 0, MGOS_GPIO_PULL_NONE, false);
in2->Init();
inputs->emplace_back(in2);
}
}
}
void CreateComponents(std::vector<std::unique_ptr<Component>> *comps,
std::vector<std::unique_ptr<mgos::hap::Accessory>> *accs,
HAPAccessoryServerRef *svr) {
if (mgos_sys_config_get_shelly_mode() == (int) Mode::kGarageDoor) {
auto *gdo_cfg = (struct mgos_config_gdo *) mgos_sys_config_get_gdo1();
std::unique_ptr<hap::GarageDoorOpener> gdo(new hap::GarageDoorOpener(
1, FindInput(1), FindInput(2), FindOutput(1), FindOutput(1), gdo_cfg));
if (gdo == nullptr) return;
auto st = gdo->Init();
if (!st.ok()) {
LOG(LL_ERROR, ("GDO init failed: %s", st.ToString().c_str()));
return;
}
gdo->set_primary(true);
mgos::hap::Accessory *pri_acc = (*accs)[0].get();
pri_acc->SetCategory(kHAPAccessoryCategory_GarageDoorOpeners);
pri_acc->AddService(gdo.get());
comps->emplace_back(std::move(gdo));
return;
}
// Sensor Discovery
std::vector<std::unique_ptr<TempSensor>> sensors;
if (s_onewire != nullptr) {
sensors = s_onewire->DiscoverAll();
}
bool ext_sensor_switch = (FindInput(2) != nullptr);
// Single switch with non-detached input and no sensors = only one accessory.
bool to_pri_acc = (sensors.empty() && (mgos_sys_config_get_sw1_in_mode() !=
(int) InMode::kDetached)) &&
!ext_sensor_switch;
CreateHAPSwitch(1, mgos_sys_config_get_sw1(), mgos_sys_config_get_in1(),
comps, accs, svr, to_pri_acc);
if (!sensors.empty()) {
struct mgos_config_ts *ts_cfgs[MAX_TS_NUM] = {
(struct mgos_config_ts *) mgos_sys_config_get_ts1(),
(struct mgos_config_ts *) mgos_sys_config_get_ts2(),
(struct mgos_config_ts *) mgos_sys_config_get_ts3(),
};
for (size_t i = 0; i < std::min((size_t) MAX_TS_NUM, sensors.size()); i++) {
auto *ts_cfg = ts_cfgs[i];
CreateHAPTemperatureSensor(i + 1, std::move(sensors[i]), ts_cfg, comps,
accs, svr);
}
} else if (ext_sensor_switch) {
hap::CreateHAPInput(2, mgos_sys_config_get_in2(), comps, accs, svr);
}
}
} // namespace shelly