Skip to content

Commit

Permalink
Merged testing
Browse files Browse the repository at this point in the history
  • Loading branch information
hfedcba committed Mar 10, 2019
2 parents 4cfc781 + 169ea47 commit 3cba504
Show file tree
Hide file tree
Showing 23 changed files with 384 additions and 88 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ project(homegear_somfy_tahoma)
set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES
src/Api.cpp
src/Api.h
src/DescriptionCreator.cpp
src/DescriptionCreator.h
src/Factory.cpp
Expand Down
6 changes: 6 additions & 0 deletions misc/Config Directory/somfytahoma.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ ___________________________________________________________________________
[General]

moduleEnabled = false

# Your TaHoma user name
user =

# Your TaHoma password
password =
221 changes: 221 additions & 0 deletions src/Api.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
/* Copyright 2013-2019 Homegear GmbH
*
* Homegear is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Homegear is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Homegear. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/

#include "Api.h"
#include "GD.h"

namespace MyFamily
{

Api::Api()
{
_out.init(GD::bl);
_out.setPrefix(GD::out.getPrefix() + " TaHoma client: ");

signal(SIGPIPE, SIG_IGN);

_httpClient = std::unique_ptr<BaseLib::HttpClient>(new BaseLib::HttpClient(GD::bl, "www.tahomalink.com", 443, false, true));
_jsonEncoder = std::unique_ptr<BaseLib::Rpc::JsonEncoder>(new BaseLib::Rpc::JsonEncoder(GD::bl));
_jsonDecoder = std::unique_ptr<BaseLib::Rpc::JsonDecoder>(new BaseLib::Rpc::JsonDecoder(GD::bl));

_loggedIn = false;
_stopWorkerThread = false;
}

Api::~Api()
{
stop();
}

void Api::start()
{
try
{
_stopWorkerThread = false;
GD::bl->threadManager.start(_workerThread, true, &Api::worker, this);
}
catch(const std::exception& ex)
{
_out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
}
catch(BaseLib::Exception& ex)
{
_out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
}
catch(...)
{
_out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__);
}
}

void Api::stop()
{
try
{
_stopWorkerThread = true;
GD::bl->threadManager.join(_workerThread);
}
catch(const std::exception& ex)
{
_out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
}
catch(BaseLib::Exception& ex)
{
_out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
}
catch(...)
{
_out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__);
}
}

void Api::worker()
{
while(!_stopWorkerThread)
{
try
{
for(int32_t i = 0; i < 60; i++)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
if(_stopWorkerThread) break;
}

login();

std::string getRequest = "GET " + _path + "getSetup HTTP/1.1\r\nUser-Agent: Homegear\r\nHost: www.tahomalink.com:" + std::to_string(443) + "\r\nConnection: Close\r\nCookie: " + _cookie + "\r\n\r\n";
BaseLib::Http result;
_httpClient->sendRequest(getRequest, result);

auto resultJson = _jsonDecoder->decode(result.getContent());

if(result.getHeader().responseCode >= 400 && result.getHeader().responseCode <= 499)
{
auto jsonIterator = resultJson->structValue->find("error");
if(jsonIterator != resultJson->structValue->end()) GD::out.printError("Error getting setup: " + jsonIterator->second->stringValue);
else GD::out.printError("Error getting setup: Unknown error");

_loggedIn = false;
}
else
{
resultJson->print(true, true);
}
}
catch(const std::exception& ex)
{
_out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
}
catch(BaseLib::Exception& ex)
{
_out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
}
catch(...)
{
_out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__);
}
}
}

bool Api::login()
{
try
{
std::lock_guard<std::mutex> loginGuard(_loginMutex);
if(_loggedIn) return true;

std::string settingName = "user";
auto setting = GD::family->getFamilySetting(settingName);
if(!setting || setting->stringValue.empty())
{
_out.printError("Error: Setting \"user\" not specified in \"somfytahoma.conf\".");
return false;
}
std::string user = BaseLib::HelperFunctions::stringReplace(setting->stringValue, "\"", "\\\"");
settingName = "password";
setting = GD::family->getFamilySetting(settingName);
if(!setting || setting->stringValue.empty())
{
_out.printError("Error: Setting \"password\" not specified in \"somfytahoma.conf\".");
return false;
}
std::string password = BaseLib::HelperFunctions::stringReplace(setting->stringValue, "\"", "\\\"");;

std::string postData = "userId=" + BaseLib::Http::encodeURL(user) + "&userPassword=" + BaseLib::Http::encodeURL(password);
std::string postRequest = "POST " + _path + "login HTTP/1.1\r\nUser-Agent: Homegear\r\nHost: www.tahomalink.com:" + std::to_string(443) + "\r\nConnection: Close\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: " + std::to_string(postData.size()) + "\r\n\r\n" + postData;
BaseLib::Http result;
_httpClient->sendRequest(postRequest, result);

if(GD::bl->debugLevel >= 5) _out.printDebug("Debug: Response: " + std::string(result.getContent().data(), result.getContentSize()));

if(result.getHeader().responseCode == 200)
{
auto resultJson = _jsonDecoder->decode(result.getContent());
auto jsonIterator = resultJson->structValue->find("success");
if(jsonIterator != resultJson->structValue->end() && jsonIterator->second->booleanValue)
{
if(GD::bl->debugLevel >= 5) _out.printDebug("Debug: Successfully logged in.");

auto fieldIterator = result.getHeader().fields.find("set-cookie");
if(fieldIterator == result.getHeader().fields.end()) return false;

_cookie = fieldIterator->second;
_loggedIn = true;

return true;
}

return false;
}
else if(result.getHeader().responseCode >= 400 && result.getHeader().responseCode <= 499)
{
auto resultJson = _jsonDecoder->decode(result.getContent());
auto jsonIterator = resultJson->structValue->find("error");
if(jsonIterator != resultJson->structValue->end()) GD::out.printError("Error logging in: " + jsonIterator->second->stringValue);
else GD::out.printError("Error logging in: Unknown error");
}

return false;
}
catch(const std::exception& ex)
{
_out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
}
catch(BaseLib::Exception& ex)
{
_out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
}
catch(...)
{
_out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__);
}
return false;
}

}
69 changes: 69 additions & 0 deletions src/Api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* Copyright 2013-2019 Homegear GmbH
*
* Homegear is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Homegear is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Homegear. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/

#ifndef TAHOMA_API_H
#define TAHOMA_API_H

#include <homegear-base/Sockets/HttpClient.h>
#include <homegear-base/Output/Output.h>
#include <homegear-base/Encoding/JsonEncoder.h>
#include <homegear-base/Encoding/JsonDecoder.h>

namespace MyFamily
{

class Api
{
public:
Api();
virtual ~Api();

void start();
void stop();
private:
BaseLib::Output _out;
std::unique_ptr<BaseLib::HttpClient> _httpClient;
std::unique_ptr<BaseLib::Rpc::JsonEncoder> _jsonEncoder;
std::unique_ptr<BaseLib::Rpc::JsonDecoder> _jsonDecoder;

std::atomic_bool _stopWorkerThread;
std::thread _workerThread;

const std::string _path = "/enduser-mobile-web/externalAPI/json/";
std::mutex _loginMutex;
std::atomic_bool _loggedIn;
std::string _cookie;

bool login();
void worker();
};

}

#endif
2 changes: 1 addition & 1 deletion src/DescriptionCreator.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2013-2017 Homegear UG (haftungsbeschränkt) */
/* Copyright 2013-2019 Homegear GmbH */

#include "DescriptionCreator.h"
#include "GD.h"
Expand Down
2 changes: 1 addition & 1 deletion src/DescriptionCreator.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2013-2017 Homegear UG (haftungsbeschränkt) */
/* Copyright 2013-2019 Homegear GmbH */

#ifndef HOMEGEAR_TAHOMA_DESCRIPTIONCREATOR_H
#define HOMEGEAR_TAHOMA_DESCRIPTIONCREATOR_H
Expand Down
4 changes: 2 additions & 2 deletions src/Factory.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2013-2017 Sathya Laufer
/* Copyright 2013-2019 Homegear GmbH
*
* Homegear is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -31,7 +31,7 @@
#include "../config.h"
#include "GD.h"

BaseLib::Systems::DeviceFamily* MyFactory::createDeviceFamily(BaseLib::SharedObjects* bl, BaseLib::Systems::DeviceFamily::IFamilyEventSink* eventHandler)
BaseLib::Systems::DeviceFamily* MyFactory::createDeviceFamily(BaseLib::SharedObjects* bl, BaseLib::Systems::IFamilyEventSink* eventHandler)
{
return new MyFamily::MyFamily(bl, eventHandler);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Factory.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2013-2017 Sathya Laufer
/* Copyright 2013-2019 Homegear GmbH
*
* Homegear is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -36,7 +36,7 @@
class MyFactory : BaseLib::Systems::SystemFactory
{
public:
virtual BaseLib::Systems::DeviceFamily* createDeviceFamily(BaseLib::SharedObjects* bl, BaseLib::Systems::DeviceFamily::IFamilyEventSink* eventHandler);
virtual BaseLib::Systems::DeviceFamily* createDeviceFamily(BaseLib::SharedObjects* bl, BaseLib::Systems::IFamilyEventSink* eventHandler);
};

extern "C" std::string getVersion();
Expand Down
2 changes: 1 addition & 1 deletion src/GD.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2013-2017 Sathya Laufer
/* Copyright 2013-2019 Homegear GmbH
*
* Homegear is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/GD.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2013-2017 Sathya Laufer
/* Copyright 2013-2019 Homegear GmbH
*
* Homegear is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down
Loading

0 comments on commit 3cba504

Please sign in to comment.