This repository was archived by the owner on Dec 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsend.py
More file actions
109 lines (78 loc) · 2.41 KB
/
send.py
File metadata and controls
109 lines (78 loc) · 2.41 KB
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
"""
A program to send off / on commands to a remote power socket.
author: Alexander Rueedlinger <a.rueedlinger@gmail.com>
date: 02.07.2015
Example:
switch type A:
sudo python send.py -c off -t A -s 11111,11111 -p 0
switch type B:
sudo python send.py -c off -t B -s 1,3 -p 0
switch type C:
sudo python send.py -c off -t C -s a,1,1 -p 0
switch type D:
sudo python send.py -c off -t D -s A,1 -p 0
"""
import argparse
import sys
try:
import pi_switch
except ImportError:
print "pi_switch import error!"
sys.exit()
def create_switch(type, settings, pin):
"""Create a switch.
Args:
type: (str): type of the switch [A,B,C,D]
settings (str): a comma separted list
pin (int): wiringPi pin
Returns:
switch
"""
switch = None
if type == "A":
group, device = settings.split(",")
switch = pi_switch.RCSwitchA(group, device)
elif type == "B":
addr, channel = settings.split(",")
addr = int(addr)
channel = int(channel)
switch = pi_switch.RCSwitchB(addr, channel)
elif type == "C":
family, group, device = settings.split(",")
group = int(group)
device = int(device)
switch = pi_switch.RCSwitchC(family, group, device)
elif type == "D":
group, device = settings.split(",")
device = int(device)
switch = pi_switch.RCSwitchD(group, device)
else:
print "Type %s is not supported!" % type
sys.exit()
switch.enableTransmit(pin)
return switch
def toggle(switch, command):
"""Toggles a switch on or off.
Args:
switch (switch): a switch
command (str): "on" or "off"
"""
if command in ["on"]:
switch.switchOn()
if command in ["off"]:
switch.switchOff()
def main():
parser = argparse.ArgumentParser(description="Send off / on commands to a remote power socket.")
parser.add_argument("-c", dest = "command", metavar = "command", nargs = "?",
help="can be on or off")
parser.add_argument("-t", dest = "type", metavar = "type", nargs = "?",
help="type of the switch: A, B, C or D")
parser.add_argument("-s", dest = "settings", metavar = "settings", nargs = "?",
help="settings as a comma separated list: value1,value2,value2")
parser.add_argument("-p", dest = "pin", metavar = "pin", type = int, nargs = "?",
help="wriningPi pin")
args = parser.parse_args()
switch = create_switch(args.type, args.settings, args.pin)
toggle(switch, args.command)
if __name__ == "__main__":
main()