连接到本地HomeGear服务的分机。

homegear的Python项目详细描述


python3家庭用具

python3 homegear是一个python扩展,用于通过unix域套接字连接到homegear。它支持HomeGear的所有RPC方法和实时事件接收。

先决条件

扩展需要安装libhomegear-ipc,并且至少需要python版本3。要安装它,请为您的发行版添加HomeGear APT存储库(请参见https://homegear.eu/downloads.html),然后执行

apt install libhomegear-ipc

或者,在非debian类系统上,您可以手动编译libhomegear ipc:

git clone https://github.com/Homegear/libhomegear-ipc
cd libhomegear-ipc
./makeRelease.sh

设置

如果您有PIP,请执行以下操作:

sudo python3 -m pip install homegear

要手动编译和安装扩展,请从gibhut下载并执行

sudo python3 setup.py install

方法

只有一个对象可用:Homegear。它的构造函数中有两个参数:HomeGear IPC套接字的路径(通常是/var/run/homegear/homegearIPC.sock)和回调方法。在HomeGear中更新设备变量时执行回调方法。在实例化时,类将等待,直到它成功连接到HomeGear。2秒后,即使没有连接,它也会返回。要检查,如果对象仍然连接,可以调用connected()。除了此方法之外,还可以调用HomeGear(see ref.homegear.eu)中可用的所有rpc方法。

不连接时的行为

当没有连接到HomeGear时,构造函数在2秒后返回。它无限期地尝试重新连接,直到能够建立连接。同样的情况也发生在连接丢失上。要检查模块是否已连接,请调用connected()。即使没有连接,也可以毫无例外地调用所有rpc方法。返回值将是None

类型转换

python变量到homegar变量

PythonHomegear
NoneVoid
BoolBoolean
LongInteger
FloatFloat
UnicodeString
BytesBinary
ListArray
TupleArray
DictStruct

HomeGear变量到Python变量

HomegearPython
VoidNone
BooleanBool
IntegerLong
FloatFloat
StringUnicode
BinaryBytes
ArrayList
StructDict

用法示例

一个简单的例子:

fromhomegearimportHomegear# This callback method is called on Homegear variable changesdefeventHandler(eventSource,peerId,channel,variableName,value):# Note that the event handler is called by a different thread than the main thread. I. e. thread synchronization is# needed when you access non local variables.print("Event handler called with arguments: source: "+eventSource+" peerId: "+str(peerId)+"; channel: "+str(channel)+"; variable name: "+variableName+"; value: "+str(value));hg=Homegear("/var/run/homegear/homegearIPC.sock",eventHandler);

请注意,回调方法是从其他线程调用的。访问共享变量时请使用线程同步。

要执行rpc方法,只需键入hg.<method name>。例如,将系统变量“test”设置为“6”并再次检索:

hg.setSystemVariable("TEST",6);print(hg.getSystemVariable("TEST"));

一个完整的例子:

importtimefromhomegearimportHomegear# This callback method is called on Homegear variable changesdefeventHandler(eventSource,peerId,channel,variableName,value):# Note that the event handler is called by a different thread than the main thread. I. e. thread synchronization is# needed when you access non local variables.print("Event handler called with arguments: source: "+eventSource+" peerId: "+str(peerId)+"; channel: "+str(channel)+"; variable name: "+variableName+"; value: "+str(value));hg=Homegear("/var/run/homegear/homegearIPC.sock",eventHandler);# hg waits until the connection is established (but for a maximum of 2 seonds).hg.setSystemVariable("TEST",6);print("getSystemVariable(\"TEST\") after setting \"TEST\" to 6: ",hg.getSystemVariable("TEST"));hg.setSystemVariable("TEST",["One",2,3.3]);print("getSystemVariable(\"TEST\") after setting \"TEST\" to an array: ",hg.getSystemVariable("TEST"));hg.setSystemVariable("TEST",{"One":1,2:"Two",3:[3,3,3]});print("getSystemVariable(\"TEST\") after setting \"TEST\" to a struct: ",hg.getSystemVariable("TEST"));counter=0;while(hg.connected()):time.sleep(1);counter+=1;hg.setSystemVariable("TEST",counter);

链接

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java无法使用Intellij Idea执行Xpath   java为什么springbootstarterdatajpa 2.5.0不能因“未找到表”而初始化数据?   google maps Geoppoint类中的java丢失精度   java fb graph对象中有哪些属性可用?   实现JNI时指针类型的java错误   java使用管理目录API将一个组织单元移动到另一个组织单元?   java局部变量gcd可能尚未初始化   java示例代码未按预期执行   使用持久性的JPA的数据库连接位于何处。xml?   java从AJAX成功函数中检索ArrayList元素   java中的持久文件验证   java编码简约消息的最佳方式   tapestry用java生成站点地图并使其公开   Java文档中使用的哈希集约定   java试图在工作线程上调用join   java有没有时间函数来记录一个方法完成一项工作所需的时间?   如何让计算器在按下等号后接受新数字?JAVA