start of light commands
This commit is contained in:
parent
8ee80fecc7
commit
acf7ecbcf1
@ -47,20 +47,30 @@ commands = {'info' : '{"system":{"get_sysinfo":{}}}',
|
|||||||
'reset' : '{"system":{"reset":{"delay":1}}}'
|
'reset' : '{"system":{"reset":{"delay":1}}}'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
light_commands = {
|
||||||
|
'on': '{"smartlife.iot.smartbulb.lightingservice":{"transition_light_state":{"on_off":1,"transition_period":0}}}',
|
||||||
|
'off': '{"smartlife.iot.smartbulb.lightingservice":{"transition_light_state":{"on_off":0,"transition_period":0}}}'
|
||||||
|
}
|
||||||
|
|
||||||
# Encryption and Decryption of TP-Link Smart Home Protocol
|
# Encryption and Decryption of TP-Link Smart Home Protocol
|
||||||
# XOR Autokey Cipher with starting key = 171
|
# XOR Autokey Cipher with starting key = 171
|
||||||
def encrypt(string):
|
def encrypt(string, doHeader=True):
|
||||||
key = 171
|
key = 171
|
||||||
|
if doHeader:
|
||||||
result = "\0\0\0\0"
|
result = "\0\0\0\0"
|
||||||
|
else:
|
||||||
|
result = ""
|
||||||
for i in string:
|
for i in string:
|
||||||
a = key ^ ord(i)
|
a = key ^ ord(i)
|
||||||
key = a
|
key = a
|
||||||
result += chr(a)
|
result += chr(a)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def decrypt(string):
|
def decrypt(string, doHeader=True):
|
||||||
key = 171
|
key = 171
|
||||||
result = ""
|
result = ""
|
||||||
|
if doHeader:
|
||||||
|
string = string[4:]
|
||||||
for i in string:
|
for i in string:
|
||||||
a = key ^ ord(i)
|
a = key ^ ord(i)
|
||||||
key = ord(i)
|
key = ord(i)
|
||||||
@ -70,6 +80,7 @@ def decrypt(string):
|
|||||||
# Parse commandline arguments
|
# Parse commandline arguments
|
||||||
parser = argparse.ArgumentParser(description="TP-Link Wi-Fi Smart Plug Client v" + str(version))
|
parser = argparse.ArgumentParser(description="TP-Link Wi-Fi Smart Plug Client v" + str(version))
|
||||||
parser.add_argument("-t", "--target", metavar="<ip>", required=True, help="Target IP Address", type=validIP)
|
parser.add_argument("-t", "--target", metavar="<ip>", required=True, help="Target IP Address", type=validIP)
|
||||||
|
parser.add_argument("-l", "--lightbulb", dest="lightbulb", help="Enable lightbulb mode", action="store_true")
|
||||||
group = parser.add_mutually_exclusive_group(required=True)
|
group = parser.add_mutually_exclusive_group(required=True)
|
||||||
group.add_argument("-c", "--command", metavar="<command>", help="Preset command to send. Choices are: "+", ".join(commands), choices=commands)
|
group.add_argument("-c", "--command", metavar="<command>", help="Preset command to send. Choices are: "+", ".join(commands), choices=commands)
|
||||||
group.add_argument("-j", "--json", metavar="<JSON string>", help="Full JSON string of command to send")
|
group.add_argument("-j", "--json", metavar="<JSON string>", help="Full JSON string of command to send")
|
||||||
@ -81,19 +92,20 @@ port = 9999
|
|||||||
if args.command is None:
|
if args.command is None:
|
||||||
cmd = args.json
|
cmd = args.json
|
||||||
else:
|
else:
|
||||||
|
if args.lightbulb:
|
||||||
|
cmd = light_commands[args.command]
|
||||||
|
else:
|
||||||
cmd = commands[args.command]
|
cmd = commands[args.command]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Send command and receive reply
|
# Send command and receive reply
|
||||||
try:
|
try:
|
||||||
sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM if args.lightbulb else socket.SOCK_STREAM)
|
||||||
sock_tcp.connect((ip, port))
|
sock.settimeout(1)
|
||||||
sock_tcp.send(encrypt(cmd))
|
sock.sendto(encrypt(cmd, not args.lightbulb), (ip, port))
|
||||||
data = sock_tcp.recv(2048)
|
data = decrypt(sock.recv(2048), not args.lightbulb)
|
||||||
sock_tcp.close()
|
sock.close()
|
||||||
|
|
||||||
print "Sent: ", cmd
|
print "Sent: ", cmd
|
||||||
print "Received: ", decrypt(data[4:])
|
print "Received: ", data
|
||||||
except socket.error:
|
except socket.error:
|
||||||
quit("Cound not connect to host " + ip + ":" + str(port))
|
quit("Cound not connect to host " + ip + ":" + str(port))
|
||||||
|
Loading…
Reference in New Issue
Block a user