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() diff --git a/README.md b/README.md index 6d2cb7b..4785695 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,25 @@ 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 +``` + +There is also a HTTP Server version . +``` +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/ +``` + ## Contributions Pull requests are welcome. Possible areas for improvement: diff --git a/cmd.py b/cmd.py new file mode 100644 index 0000000..d51f979 --- /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("ON" if s20.on else "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