-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyrfoutlet.py
63 lines (52 loc) · 1.89 KB
/
pyrfoutlet.py
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
"""
Python bindings for rfoutlet
"""
from ctypes import *
import os
import fnmatch
import sys
import logging
def findlib():
dirname = os.path.dirname(__file__)
for ext in ['so','dylib','dll']:
names = fnmatch.filter(os.listdir(dirname),"*rfoutlet*" + ext)
for name in names:
try:
return cdll.LoadLibrary(dirname +"/"+name)
except OSError:
pass
return None
librfoutlet = findlib()
if librfoutlet is None:
raise OSError("Unable to find rfoutlet library")
librfoutlet.RFOutlet_parseProduct.argtypes = [c_char_p]
librfoutlet.RFOutlet_new.argtypes = [c_int, c_int]
librfoutlet.RFOutlet_new.restype = c_void_p
librfoutlet.RFOutlet_delete.argtypes = [c_void_p]
librfoutlet.RFOutlet_setState.argtypes = [c_void_p, c_int, c_char_p, c_int, c_bool]
librfoutlet.RFOutlet_getState.argtypes = [c_void_p, c_int, c_char_p, c_int]
c_logcb_p = CFUNCTYPE(None, c_char_p)
librfoutlet.RFOutlet_setLog.argtypes = [c_logcb_p]
if sys.version_info >= (3, 0):
def tochar(data):
return bytes(data,'utf-8') if data else None
else:
def tochar(data):
return str(data) if data else None
import logging
logger = logging.getLogger(__name__)
def logcb(data):
logger.info(data.decode('utf-8'))
clogcb = c_logcb_p(logcb)
librfoutlet.RFOutlet_setLog(clogcb)
class RFOutlet(object):
def __init__(self, pin315, pin433):
self.rfoutlet = librfoutlet.RFOutlet_new(pin315, pin433)
def __del__(self):
librfoutlet.RFOutlet_delete(self.rfoutlet)
def setState(self, product, channel, outlet, state):
librfoutlet.RFOutlet_setState(self.rfoutlet, product, tochar(channel), outlet, state)
def getState(self, product, channel, outlet):
return librfoutlet.RFOutlet_getState(self.rfoutlet, product, tochar(channel), outlet)
def parseProduct(product):
return librfoutlet.RFOutlet_parseProduct(tochar(product))