From c439e83183739c7f38b8cf9438757dcda7f460ba Mon Sep 17 00:00:00 2001 From: Richard Beales Date: Sat, 21 Nov 2015 22:30:31 +0000 Subject: [PATCH 1/6] add command line interface --- README.md | 9 +++++++++ cmd.py | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 cmd.py diff --git a/README.md b/README.md index 6d2cb7b..2cf6d53 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,15 @@ s20.on = True # Turn it on. s20.on = False # Turn it off. ``` +There is also a command line version to achieve the same, e.g. + +``` +python cmd.py --server 1.2.3.4 --switch off +python cmd.py --server 1.2.3.4 --switch on +python cmd.py --server 1.2.3.4 --status + Switch is on +``` + ## Contributions Pull requests are welcome. Possible areas for improvement: diff --git a/cmd.py b/cmd.py new file mode 100644 index 0000000..7d12f71 --- /dev/null +++ b/cmd.py @@ -0,0 +1,20 @@ +from orvibo.s20 import S20 +import argparse + +parser = argparse.ArgumentParser(description='Control the S20 switch.') +parser.add_argument('--server', dest='server', type=str, help='the IP address of the switch to control') +parser.add_argument('--switch', dest='operation', help='what to do (ON or OFF)') +parser.add_argument('--status', dest='status', help='the current switch state', action="store_true") + +args = parser.parse_args() + +s20 = S20(args.server) +if args.status: + print("Switch is on" if s20.on else "Switch is off") +else: + if args.operation and args.operation.upper() == 'ON': + s20.on = True + elif args.operation and args.operation.upper() == 'OFF': + s20.on = False + else: + print("unrecognised command") \ No newline at end of file From 61841c9284a14670b8859063333df46a21b60300 Mon Sep 17 00:00:00 2001 From: Richard Beales Date: Mon, 23 Nov 2015 09:10:19 +0000 Subject: [PATCH 2/6] change command line output to be compatible with openhab --- README.md | 1 - cmd.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 2cf6d53..ac104e4 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,6 @@ There is also a command line version to achieve the same, e.g. python cmd.py --server 1.2.3.4 --switch off python cmd.py --server 1.2.3.4 --switch on python cmd.py --server 1.2.3.4 --status - Switch is on ``` ## Contributions diff --git a/cmd.py b/cmd.py index 7d12f71..d51f979 100644 --- a/cmd.py +++ b/cmd.py @@ -10,7 +10,7 @@ s20 = S20(args.server) if args.status: - print("Switch is on" if s20.on else "Switch is off") + print("ON" if s20.on else "OFF") else: if args.operation and args.operation.upper() == 'ON': s20.on = True From dae3bbf5df2c2c8c07601d102ed4376df010f665 Mon Sep 17 00:00:00 2001 From: DomQuixote Date: Thu, 7 Apr 2016 02:02:14 +0100 Subject: [PATCH 3/6] Create OrviboHTTPServer.py Simple HTTP Server that discovers the plugs and accepts STATUS, ON, OFF as HTTP GET commands. Example use: openHAB HTTP binding. Advantage versus CLI: no errors if multiple requests one after the other. Usage: python3 OrviboHTTPServer.py --- OrviboHTTPServer.py | 110 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 OrviboHTTPServer.py diff --git a/OrviboHTTPServer.py b/OrviboHTTPServer.py new file mode 100644 index 0000000..79f059e --- /dev/null +++ b/OrviboHTTPServer.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python +from http.server import BaseHTTPRequestHandler, HTTPServer +from orvibo.s20 import S20, discover +import argparse +global args +global httpd + +class S(BaseHTTPRequestHandler): + def _set_headers(self): + self.send_response(200) + self.send_header('Content-type', 'text/html') + self.end_headers() + + def do_GET(self): + global ObjectList + self._set_headers() + slash,command,address=self.path.split('/') + if command=='STATUS': + notknown=True + for (x,y) in ObjectList: + if x==address: + notknown=False + if y.on: + self.wfile.write(bytes("ON", 'UTF-8')) + print(x, " status is ON") + else: + self.wfile.write(bytes("OFF", 'UTF-8')) + print(x," status is OFF") + elif command=='ON': + notknown=True + for (x,y) in ObjectList: + if x==address: + notknown=False + y.on=True + self.wfile.write(bytes("ON", 'UTF-8')) + print(x," switched to ON.") + elif command=='OFF': + notknown=True + for (x,y) in ObjectList: + if x==address: + notknown=False + y.on=False + self.wfile.write(bytes("OFF", 'UTF-8')) + print(x," switched to OFF") + else: + print("Error, GET command not recognised") + self.wfile.write(bytes("ERROR 1", 'UTF-8')) + if notknown: + self.wfile.write(bytes("ERROR 2", 'UTF-8')) + print("Error, address not active or invalid") + + def do_HEAD(self): + self._set_headers() + + def do_POST(self): + ''' + No POST Implemented + ''' + +def init_s20(): + global ObjectList + ObjectList=[] + print('Discovering Orvibo S20 plugs....') + try: + hosts=discover() + except: + print('Unexpected Error in initialisation plugs') + return(False) + else: + + for i in hosts.keys(): + print('Discovered Orvibo S20 plug on',i) + TempObject=S20(i) + TempTuple=(i,TempObject) + ObjectList.append(TempTuple) + print(len(ObjectList),' plugs found in total') + return(True) + + + +def init_server(server_class=HTTPServer, handler_class=S): + global httpd + global args + print('Starting HTTP Server on', args.IP[0],args.port[0]) + server_address = (args.IP[0], args.port[0]) + try: + httpd = server_class(server_address, handler_class) + print('HTTP server started on',args.IP[0],args.port[0]) + return(True) + except: + print('Unexpected Error in starting server') + return(False) + +def run(): + global httpd + httpd.serve_forever() + +parser = argparse.ArgumentParser(description='Control Orvibo plugs on local network through HTTP GET Requests') +parser.add_argument('IP', metavar='IP Address', type=str, nargs=1,help='IP Address to bind to') +parser.add_argument('port', metavar='port', type=int, nargs=1,help='Port to listen on') +args=parser.parse_args() +if not init_s20(): + exit() +if not init_server(): + exit() +try: + run() +except KeyboardInterrupt: + print('^C received, shutting down the web server') + httpd.socket.close() From 1e88948dbc56d71167796b28989661649dbab8d6 Mon Sep 17 00:00:00 2001 From: DomQuixote Date: Thu, 7 Apr 2016 02:15:57 +0100 Subject: [PATCH 4/6] Update README.md Described HTTP Server usage --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index ac104e4..0ff7c7b 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,17 @@ python cmd.py --server 1.2.3.4 --switch on python cmd.py --server 1.2.3.4 --status ``` +There is also a HTTP Server version . + +OrviboHTTPServer + +Commands can then be given through HTTP GET requests in the openHAB http binding: + +:/STATUS/ +:/ON/ +:/OFF/ + + ## Contributions Pull requests are welcome. Possible areas for improvement: From 0ed84e26e1b2be61e25d8405ba6a617423446f91 Mon Sep 17 00:00:00 2001 From: DomQuixote Date: Thu, 7 Apr 2016 02:17:24 +0100 Subject: [PATCH 5/6] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0ff7c7b..26aa2db 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ python cmd.py --server 1.2.3.4 --status ``` There is also a HTTP Server version . - +``` OrviboHTTPServer Commands can then be given through HTTP GET requests in the openHAB http binding: @@ -30,7 +30,7 @@ Commands can then be given through HTTP GET requests in the openHAB http binding :/STATUS/ :/ON/ :/OFF/ - +``` ## Contributions From b746515c8b59e263d62e081c62c2c8b9397db344 Mon Sep 17 00:00:00 2001 From: DomQuixote Date: Thu, 7 Apr 2016 02:19:39 +0100 Subject: [PATCH 6/6] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 26aa2db..4785695 100644 --- a/README.md +++ b/README.md @@ -23,10 +23,10 @@ python cmd.py --server 1.2.3.4 --status There is also a HTTP Server version . ``` -OrviboHTTPServer - -Commands can then be given through HTTP GET requests in the openHAB http binding: - +python3 OrviboHTTPServer.py +``` +Commands can then be given through HTTP GET requests in the openHAB http binding using the following IP Address and path: +``` :/STATUS/ :/ON/ :/OFF/