Skip to content

add command line interface #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions OrviboHTTPServer.py
Original file line number Diff line number Diff line change
@@ -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()
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ip to bind to> <port to listen to>
```
Commands can then be given through HTTP GET requests in the openHAB http binding using the following IP Address and path:
```
<ip to bind to>:<port to listen to>/STATUS/<ip of plug>
<ip to bind to>:<port to listen to>/ON/<ip of plug>
<ip to bind to>:<port to listen to>/OFF/<ip of plug>
```

## Contributions

Pull requests are welcome. Possible areas for improvement:
Expand Down
20 changes: 20 additions & 0 deletions cmd.py
Original file line number Diff line number Diff line change
@@ -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")