Skip to content

Commit

Permalink
Add first version of HomeKit Support
Browse files Browse the repository at this point in the history
  • Loading branch information
sieren committed May 31, 2019
1 parent da09e96 commit 3a13006
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 26 deletions.
6 changes: 6 additions & 0 deletions data/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
"mqttbroker": "mqtt://192.168.1.1",
"mqttusername": "myusername",
"mqttpasswd": "mypassword",
"homekitpin": "111-11-111",
"timezone": "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00",
"scenes": [
{
"name": "Entrance",
"type": "HomeKitSwitch",
"icon": "door"
},
{
"name": "Entrance",
"type": "Light",
Expand Down
17 changes: 13 additions & 4 deletions main/AppContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ namespace ctx
void AppContext::setup()
{
fs::FileSystem::getInstance().loadPartitions();
mMQTTGroups = fs::ConfigReader::getMQTTGroups();
mpMQTTConnection = std::make_shared<mqtt::MQTTConnection>(fs::ConfigReader::getMQTTConfig(), mMQTTGroups);
mDeviceGroups = fs::ConfigReader::getDeviceGroups();
const auto hkConfig = fs::ConfigReader::getHKConfig();
if (hkConfig.isEnabled)
{
mpHKConnection = std::make_shared<homekit::HKConnection>(hkConfig, mDeviceGroups);
}
mpMQTTConnection = std::make_shared<mqtt::MQTTConnection>(fs::ConfigReader::getMQTTConfig(), mDeviceGroups);
const auto timeZone = fs::ConfigReader::getTimeZone();
if (timeZone != "")
{
Expand All @@ -34,12 +39,16 @@ namespace ctx
if (cb == mqtt::MQTTConnectionStatus::CONNECTED)
{
mpMQTTConnection->bindScenes();
if (mpHKConnection)
{
mpHKConnection->start();
}
}
});
}

std::vector<MQTTVariants> &AppContext::getMQTTGroups()
std::vector<DeviceVariants> &AppContext::getDeviceGroups()
{
return mMQTTGroups;
return mDeviceGroups;
}
} // namespace ctx
6 changes: 4 additions & 2 deletions main/AppContext.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <homekit/HKConnection.h>
#include <mqtt/MQTTConnection.h>
#include <mqtt/MQTTGroup.hpp>
#include <ntp/NTPSync.h>
Expand All @@ -21,13 +22,14 @@ namespace ctx

WifiContext& getWifiContext() { return mWifiContext; };
std::shared_ptr<mqtt::MQTTConnection> getMQTTConnection() { return mpMQTTConnection; };
std::vector<MQTTVariants> &getMQTTGroups();
std::vector<DeviceVariants> &getDeviceGroups();

private:
std::shared_ptr<mqtt::MQTTConnection> mpMQTTConnection;
std::shared_ptr<homekit::HKConnection> mpHKConnection = nullptr;
std::shared_ptr<ntp::NTPSync> mNTPSync;
WifiContext mWifiContext;
std::vector<MQTTVariants> mMQTTGroups;
std::vector<DeviceVariants> mDeviceGroups;
rapidjson::Document mConfigDocument;
};
} // namespace ctx
4 changes: 2 additions & 2 deletions main/AppScreen.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace gfx
template<class ScreenDriver, class NavigationDriver>
void AppScreen<ScreenDriver, NavigationDriver>::presentScreen(const uint16_t sceneId)
{
auto& scenes = mpAppContext->getMQTTGroups();
auto& scenes = mpAppContext->getDeviceGroups();
auto widgets = std::vector<WidgetPtr>();

auto mqttScene = std::find_if(scenes.begin(), scenes.end(), [&](auto& scene)
Expand Down Expand Up @@ -111,7 +111,7 @@ namespace gfx
{
std::lock_guard<std::mutex> guard(viewMutex);
auto screenNavigator = std::make_shared<ScreenNavigator<NavigationDriver>>(&mTft, menuFrame, 1000);
auto& scenes = mpAppContext->getMQTTGroups();
auto& scenes = mpAppContext->getDeviceGroups();
auto widgets = std::vector<WidgetPtr>();
for (auto& scene : scenes)
{
Expand Down
2 changes: 2 additions & 0 deletions main/fs/ConfigReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <util/warnings.h>
#include "rapidjson/document.h"
#include "Filesystem.h"
#include <homekit/HKConfig.hpp>
#include <homekit/HKDevice.hpp>
#include <mqtt/MQTTConnection.h>
#include <mqtt/MQTTGroup.hpp>
#include <mqtt/MQTTSensorGroup.hpp>
Expand Down
18 changes: 1 addition & 17 deletions main/homekit/HKDevice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,9 @@ namespace homekit
{
struct HKDevice
{

static void switch_on_callback(homekit_characteristic_t *_ch, homekit_value_t on, void *context)
{
printf("Switch On Callback\r\n");
printf("With ID: %i\r\n", _ch->id);
if (_ch->value.bool_value != on.bool_value)
{
_ch->value.bool_value = on.bool_value;
homekit_characteristic_notify(_ch, _ch->value);
}
(*static_cast<HKDevice*>(context)).setDeviceState(on.bool_value);
}

static void deviceSetCallback(homekit_characteristic_t *ch, const homekit_value_t value)
{
(*static_cast<HKDevice*>(ch->context)).setDeviceState(value.bool_value);
printf("Device has been set!");
}

void setDeviceState(bool on)
Expand All @@ -54,9 +40,6 @@ namespace homekit
mSetNeedsUpdateCB(on);
}
}
printf("Updated with new value");
printf(sceneName.c_str());
printf("\r\n");
}

HKDevice(const HKDevice&) =delete;
Expand Down Expand Up @@ -96,6 +79,7 @@ namespace homekit
{
return mIsActive;
}

std::string sceneName = "";
std::string iconName = "";
uint16_t groupId;
Expand Down
35 changes: 34 additions & 1 deletion main/ui/UIWidgetBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,39 @@ namespace util
return button;
};

auto operator()(HKDevicePtr ptr, ScreenType screen) -> std::shared_ptr<UIButton>
{
auto button = std::make_shared<UIButton>(&(screen->mTft), Frame(), ptr->groupId);
button->setBackgroundColor(Color::InactiveBgColor());
button->setLabel(ptr->sceneName);

const auto icons = GetIconFileNames(ptr->iconName);
const auto textColor = ptr->isActive() ? Color::ActiveBgColor() : Color::InactiveTextColor();
const auto imagePath = ptr->isActive() ? icons.first : icons.second;
button->setImage(imagePath);
button->setTextColor(textColor);
auto& context = screen->mpAppContext;
button->addTargetAction([ptr, context](const uint16_t id) {
const bool isActive = ptr->isActive();
ptr->setDeviceState(!isActive);
});

auto& screenSaver = screen->mScreenSaver;
ptr->mSetNeedsUpdateCB = [ptr, weakBtn = std::weak_ptr<UIButton>(button), &screenSaver, icons](const bool state) {
const auto textColor = state ? Color::ActiveBgColor() : Color::InactiveTextColor();
const auto imagePath = state ? icons.first : icons.second;
auto button = weakBtn.lock();
if (!button)
{
return;
}
button->setTextColor(textColor);
button->setImage(imagePath);
screenSaver.activate();
};
return button;
};

auto operator()(MQTTSensorGroupPtr ptr, ScreenType screen) -> std::shared_ptr<UIWidget>
{
auto button = std::make_shared<UISensorComboWidget>(&(screen->mTft), Frame(), ptr->groupId);
Expand Down Expand Up @@ -127,4 +160,4 @@ namespace util
}
};
} // namespace util
} // namespace gfx
} // namespace gfx

0 comments on commit 3a13006

Please sign in to comment.