Skip to content

Commit

Permalink
Wait for connection before returning in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
hfedcba committed May 29, 2018
1 parent 5163e2a commit 9748818
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ local.properties
autom4te.cache
*.in~
config.log
homegear-management
homegear.so
core
vgcore.*

Expand Down Expand Up @@ -87,4 +87,4 @@ cmake-build-debug
#Python setup
build/
homegear.egg-info
dist/
dist/
3 changes: 1 addition & 2 deletions Example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ def eventHandler(peerId, channel, variableName, value):

hg = Homegear("/var/run/homegear/homegearIPC.sock", eventHandler);

while(not hg.connected()):
time.sleep(1);
# hg waits until the connection is established (but for a maximum of 2 seonds).

print(hg.logLevel());
print(hg.listDevices());
Expand Down
1 change: 1 addition & 0 deletions IpcClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ IpcClient::~IpcClient()

void IpcClient::onConnect()
{
if(_onConnect) _onConnect();
}

// {{{ RPC methods
Expand Down
2 changes: 2 additions & 0 deletions IpcClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ class IpcClient : public Ipc::IIpcClient
IpcClient(std::string socketPath);
virtual ~IpcClient();

void setOnConnect(std::function<void(void)> value) { _onConnect.swap(value); }
void setBroadcastEvent(std::function<void(uint64_t peerId, int32_t channel, std::string& variableName, Ipc::PVariable value)> value) { _broadcastEvent.swap(value); }
private:
std::function<void(void)> _onConnect;
std::function<void(uint64_t peerId, int32_t channel, std::string& variableName, Ipc::PVariable value)> _broadcastEvent;

virtual void onConnect();
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ To compile and install the extension manually, execute
sudo python3 setup.py install
```

## Methods

There is only one object available: `Homegear`. It takes two parameters in it's constructor: The path to the Homegear IPC socket (`/var/run/homegear/homegearIPC.sock` by default) and a callback method. The callback method is executed when a device variable is updated in Homegear. On instantiation the class waits until it is connected succesfully to Homegear. After 2 seconds it returns even if there is no connection. To check, if the object is still connected, you can call `connected()`. Apart from this method, you can call all RPC methods available in Homegear.

## Usage example

Basically there is the Homegear object which takes two parameters on instantiation: The path to the Homegear IPC socket (`/var/run/homegear/homegearIPC.sock` by default) and a callback method. The callback method is executed when a device variable is updated in Homegear. A minimal example:
A minimal example:

```
from homegear import Homegear
Expand Down
17 changes: 17 additions & 0 deletions homegear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

std::shared_ptr<IpcClient> _ipcClient;
static PyObject* _eventCallback = nullptr;
static std::mutex _onConnectWaitMutex;
static std::condition_variable _onConnectConditionVariable;

typedef struct
{
Expand Down Expand Up @@ -192,6 +194,13 @@ static PyObject* HomegearRpcMethod_call(PyObject* object, PyObject* args, PyObje
return PythonVariableConverter::getPythonVariable(result);
}

static void Homegear_onConnect()
{
std::unique_lock<std::mutex> waitLock(_onConnectWaitMutex);
waitLock.unlock();
_onConnectConditionVariable.notify_all();
}

static void Homegear_broadcastEvent(uint64_t peerId, int32_t channel, std::string& variableName, Ipc::PVariable value)
{
if(!_eventCallback) return;
Expand Down Expand Up @@ -258,7 +267,15 @@ static int Homegear_init(HomegearObject* self, PyObject* arg)
{
_ipcClient = std::make_shared<IpcClient>(self->socketPath);
if(_eventCallback) _ipcClient->setBroadcastEvent(std::function<void(uint64_t, int32_t, std::string&, Ipc::PVariable)>(std::bind(&Homegear_broadcastEvent, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)));
_ipcClient->setOnConnect(std::function<void(void)>(std::bind(&Homegear_onConnect)));
_ipcClient->start();
std::unique_lock<std::mutex> waitLock(_onConnectWaitMutex);
int64_t startTime = Ipc::HelperFunctions::getTime();
while (!_onConnectConditionVariable.wait_for(waitLock, std::chrono::milliseconds(2000), [&]
{
if(Ipc::HelperFunctions::getTime() - startTime > 2000) return true;
else return _ipcClient->connected();
}));
}

return 0;
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

setuptools.setup(
name="homegear",
version="1.0.2",
version="1.0.3",
description = 'Extension to connect to a local Homegear service.',
long_description=long_description,
long_description_content_type="text/markdown",
author="Homegear GmbH",
author_email="[email protected]",
url="https://github.com/Homegear/libhomegear-python",
download_url = 'https://github.com/Homegear/libhomegear-python/archive/1.0.2.tar.gz',
download_url = 'https://github.com/Homegear/libhomegear-python/archive/1.0.3.tar.gz',
keywords = ['homegear', 'smart home'],
ext_modules=[
Extension("homegear", ["homegear.cpp", "IpcClient.cpp", "PythonVariableConverter.cpp"],
Expand Down

0 comments on commit 9748818

Please sign in to comment.