-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccessoryiport.py
executable file
·171 lines (149 loc) · 6.04 KB
/
accessoryiport.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3
"""
accessoryiport.py
Copyright (C) 2016 [email protected]
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
""" iport tcp 192.168.0.20:10001
topic:
/iport/out
push: {
"deviceid":"SurfaceMount",
"model":"iPortSM10B",
"macaddr":"DC82F600100C",
"version":"V6",
"uptime":"1875573",
"eventtime":"40565",
"events":[{"label":"key 1","state":"1"}]}
release: {
"deviceid":"SurfaceMount",
"model":"iPortSM10B",
"macaddr":"DC82F600100C",
"version":"V6",
"uptime":"1875594",
"eventtime":"40586",
"events":[{"label":"key 1","state":"0"}]} """
import statdb
import time
import math
import datetime as dt
class Start(object):
""" add homekit name in database if its not exist
"""
def __init__(self, acc, prefix='/homekit/'):
self.prefix = prefix
self.name = acc['acc']
self.comm = acc['comm']
self.stat = acc['stat']
self.comm1 = acc['comm1']
self.stat1 = acc['stat1']
self.comm2 = acc['comm2']
self.stat2 = acc['stat2']
self.comm3 = acc['comm3']
self.stat3 = acc['stat3']
self.push = None
statdb.create(self.name)
if statdb.select(self.name) is None:
statdb.insert(self.name, 0)
def get_status(self, pause=.1):
time.sleep(pause)
stat = list(statdb.select(self.name).values())[0]
out = (self.prefix+self.name+'-stat', stat)
return out
def event(self, top, msg):
if top=="/iport/out":
#try:
if type(msg["events"]) is list:
return self.handle(msg["events"][0], int(msg["eventtime"]))
#except Exception as e:
# print('iPort ERR:', e)
if top==self.prefix+self.name:
statdb.insert(self.name, msg)
return (self.prefix+self.name+'-stat', msg)
class Short(Start):
""" no homekit """
def __init__(self, acc):
super().__init__(acc)
try:
self.comm = self.comm.split('.')
except Exception as e:
print('iPort ERR:', e)
def handle(self, msg, etime):
#print('e', msg, msg['label'].replace(' ',''), self.stat, etime)
if msg["label"].replace(' ', '') == self.stat:
if msg["state"] == "1" and self.push is None:
self.push = etime
if msg["state"] == "0" and self.push is not None:
if etime < self.push + 555:
self.push = None
stat = list(statdb.select(self.name).values())[0]
out, new = (), None
if stat == 0:
new = 1
if stat == 1:
new = 0
statdb.insert(self.name, new)
for comm in self.comm:
if comm[0] is "/":
out += (comm, new)
else:
out += ("/knx/in",'{"dstgad":"'+comm+'","value":"'+str(new)+'","dpt":"1"}')
return out
if msg["state"] == "0" and etime >= self.push + 555:
self.push = None
class Scene(Short):
def handle(self, msg, etime):
if msg["label"].replace(' ','') == self.stat:
if msg["state"] == "1" and self.push is None:
self.push = etime
if msg["state"] == "0" and self.push is not None:
if etime < self.push + 666:
self.push = None
stat = list(statdb.select(self.name).values())[0]
out, val = (), None
if stat == 0:
val = str(self.comm1)
statdb.insert(self.name, 1)
if stat == 1:
val = str(self.stat1)
statdb.insert(self.name, 0)
for comm in self.comm:
if comm[0] is "/":
out += (comm, val)
else:
out += ("/knx/in",'{"dstgad":"'+comm+'","value":"'+val+'","dpt":"5"}')
return out
if msg["state"] == "0" and etime >= self.push + 666:
self.push = None
class Switch(Short):
def handle(self, msg, etime):
if msg["label"].replace(' ','') == self.stat:
if msg["state"] == "1" and self.push is None:
self.push = etime
if msg["state"] == "1" and self.push is not None:
if etime >= self.push + 999:
self.push = etime
stat = list(statdb.select(self.name).values())[0]
if stat == 0:
statdb.insert(self.name, 1)
return (self.prefix+self.name+'-stat', 1)
if stat == 1:
statdb.insert(self.name, 0)
return (self.prefix+self.name+'-stat', 0)
if msg["state"] == "0" and self.push is not None:
self.push = None