forked from kk7ds/pynx584
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nx584_client
executable file
·175 lines (143 loc) · 5.04 KB
/
nx584_client
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
171
172
173
174
175
#!/usr/bin/env python
import argparse
import pprint
import prettytable
from nx584 import client
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('command')
parser.add_argument('--zone', default=None, type=int,
help='Zone number for zone-related commands')
parser.add_argument('--partition', default=None, type=int,
help='Partition number for partition-related commands')
parser.add_argument('--user', default=None, type=int,
help='User number for user-related commands')
parser.add_argument('--master', default=None,
help='Master PIN for commands that require it')
parser.add_argument('--pin', default=None,
help='User PIN to set (or `x` to disable)')
parser.add_argument('--host', default='localhost:5007',
help='Host and port (localhost:5007)')
args = parser.parse_args()
return args
def do_list(clnt, args):
for zone in clnt.list_zones():
pprint.pprint(zone)
def do_list_partitions(clnt, args):
for part in clnt.list_partitions():
pprint.pprint(part)
def do_arm(clnt, args):
if args.command == 'arm-stay':
mode = 'stay'
elif args.command == 'arm-exit':
mode = 'exit'
else:
mode = 'auto'
clnt.arm(mode)
def do_disarm(clnt, args):
if not args.master:
print('Master pin required')
return
clnt.disarm(args.master)
def do_summary(clnt, args):
t = prettytable.PrettyTable(['Zone', 'Name', 'Bypass', 'Status'])
for zone in clnt.list_zones():
t.add_row(['%i' % zone['number'], zone['name'],
zone['bypassed'] and 'bypass' or '-',
'%s' % (zone['state'] or False)])
print(t)
for i, part in enumerate(clnt.list_partitions()):
status = 'Partition %i %s' % (part['number'],
part['armed'] and 'armed' or 'disarmed')
if part['armed'] or i == 0:
print(status)
def do_bypass(clnt, args):
if args.command == 'bypass':
clnt.set_bypass(args.zone, True)
elif args.command == 'unbypass':
clnt.set_bypass(args.zone, False)
def do_show(clnt, args):
if args.zone:
for zone in clnt.list_zones():
if zone['number'] == args.zone:
pprint.pprint(zone)
return
print('No such zone `%s\'' % args.zone)
elif args.partition:
for part in clnt.list_partitions():
if part['number'] == args.partition:
pprint.pprint(part)
return
print('No such partition `%s\'' % args.partition)
else:
print('Zone or Partition required')
def do_get_user(clnt, args):
if not (args.user and args.master):
print('User number and master pin required')
return
user = clnt.get_user(args.master, args.user)
if user:
pprint.pprint(user)
else:
print('Unable to fetch user information')
def do_user_summary(clnt, args):
t = prettytable.PrettyTable(['User', 'PIN', 'Master'])
if not (args.user and args.master):
print('User number (max) and master pin required')
return
for i in range(0, args.user):
user = clnt.get_user(args.master, i + 1)
t.add_row([user['number'], user['pin'],
'Master / Program' in user['authority_flags']])
print(t)
def do_set_user_pin(clnt, args):
if not (args.user and args.master and args.pin):
print('User number and master pin and user pin required')
return
user = {
'number': args.user,
'pin': None if args.pin.lower() == 'x' else args.pin,
}
user = clnt.put_user(args.master, user)
if user:
pprint.pprint(user)
else:
print('Unable to set user information')
def do_events(clnt, args):
while True:
events = clnt.get_events()
if events:
for event in events:
print(event)
def do_version(clnt, args):
print(clnt.get_version())
def main():
args = parse_args()
url = 'http://%s:%s' % tuple(args.host.split(':', 1))
clnt = client.Client(url)
if args.command == 'list':
do_list(clnt, args)
elif args.command in ['arm', 'arm-stay', 'arm-exit', 'arm-auto']:
do_arm(clnt, args)
elif args.command == 'disarm':
do_disarm(clnt, args)
elif args.command == 'partitions':
do_list_partitions(clnt, args)
elif args.command == 'summary':
do_summary(clnt, args)
elif args.command in ['bypass', 'unbypass']:
do_bypass(clnt, args)
elif args.command == 'show':
do_show(clnt, args)
elif args.command == 'getuser':
do_get_user(clnt, args)
elif args.command == 'setuserpin':
do_set_user_pin(clnt, args)
elif args.command == 'users':
do_user_summary(clnt, args)
elif args.command == 'events':
do_events(clnt, args)
elif args.command == 'version':
do_version(clnt, args)
if __name__ == '__main__':
main()