startpower and config separation

This commit is contained in:
Paolo Asperti 2018-02-19 21:19:38 +01:00
parent ca716802d8
commit c27e242007
5 changed files with 72 additions and 39 deletions

View File

@ -20,7 +20,8 @@ build() {
package() {
mkdir -p "$pkgdir"
install -Dm644 etc/openpdu/openpdu.conf "$pkgdir"/etc/openpdu/openpdu.conf
install -Dm644 etc/openpdu/boards.conf "$pkgdir"/etc/openpdu/boards.conf
install -Dm644 etc/openpdu/outlets.conf "$pkgdir"/etc/openpdu/outlets.conf
install -Dm755 etc/local.d/openpdu.start "$pkgdir"/etc/local.d/openpdu.start
install -Dm755 openpdu "$pkgdir"/usr/bin/openpdu

View File

@ -1,21 +1,4 @@
#!/bin/sh
OUT1=1
OUT2=2
OUT3=3
OUT4=7
OUT5=8
OUT6=9
OUT7=11
OUT8=12
for n in $(seq 1 8) ; do
eval "echo \$OUT$n > /sys/class/gpio/export"
eval "echo out > /sys/class/gpio/gpio\$OUT$n/direction"
# 0=on 1=off :)
VAL=0
eval "echo $VAL > /sys/class/gpio/gpio\$OUT$n/value"
sleep 0.2
done
/usr/bin/openpdu initboards
/usr/bin/openpdu initoutlets

29
etc/openpdu/outlets.conf Normal file
View File

@ -0,0 +1,29 @@
[DEFAULT]
description = Generic outlet
startpower = 1
# [outlet0]
# board = 0
# channel = 0
# description = Router
# [outlet1]
# board = 0
# channel = 1
# description = NAS
# [outlet2]
# board = 1
# channel = 0
# description = Switch
# [outlet3]
# board=1
# channel=1
# description = MailServer
# [outlet4]
# board=1
# channel=2
# description = Extra Fan
# startpower = 0

58
openpdu
View File

@ -12,7 +12,8 @@ import ConfigParser
import json as JSON
VERSION = 0.1
defaults = {}
boardsDefaults = {}
outletsDefaults = {'description': 'Generic outlet','startpower':0}
_boards = []
_outlets = []
@ -40,6 +41,11 @@ def initboards():
board.init()
return
def initoutlets():
'initialize outlets'
for outlet in _outlets:
outlet.init()
return
@arg('-j', '--json', help="output in json")
def outlets(json=False):
@ -187,19 +193,25 @@ class BoardGpioOut(object):
def init(self):
for gpio in self.gpios:
open('/sys/class/gpio/export','w').write(str(gpio))
if not os.path.isdir('/sys/class/gpio/gpio%s/' % gpio):
open('/sys/class/gpio/export','w').write(str(gpio))
fn = '/sys/class/gpio/gpio%s/direction' % gpio
open(fn,'w').write('out')
return
class Outlet(object):
def __init__(self, outletnum, boardnum, channel):
def __init__(self, outletnum, boardnum, channel, startpower=False):
self.outletnum = int(outletnum)
b = [b for b in _boards if b.boardnum==int(boardnum)]
self.board = b[0]
self.channel = int(channel)
self.description = 'Outlet # %s' % self.outletnum
self.startpower = startpower
def init(self):
self.setpower(self.startpower)
return
def setpower(self, power):
self.board.setpower(self.channel,power)
@ -229,38 +241,46 @@ class Outlet(object):
configParser = ConfigParser.SafeConfigParser(defaults=defaults)
boardsConfigParser = ConfigParser.SafeConfigParser(defaults=boardsDefaults)
boardsConfigFile = '/etc/openpdu/boards.conf'
if os.path.isfile(boardsConfigFile) and os.access(boardsConfigFile, os.R_OK):
boardsConfigParser.read(boardsConfigFile)
configFile = '/etc/openpdu/openpdu.conf'
if os.path.isfile(configFile) and os.access(configFile, os.R_OK):
configParser.read(configFile)
for s in configParser.sections():
for s in boardsConfigParser.sections():
if re.match('^board.*',s):
bType = configParser.get(s, 'type')
bType = boardsConfigParser.get(s, 'type')
num = int(re.sub(r'^board','',s))
if bType=='gpio-out':
channels = int(configParser.get(s, 'channels'))
channels = int(boardsConfigParser.get(s, 'channels'))
gpios = []
for g in range(0,channels):
gpios.append(int(configParser.get(s, 'channel%s' % g)))
gpios.append(int(boardsConfigParser.get(s, 'channel%s' % g)))
b = BoardGpioOut(boardnum=num, channels=channels, gpios=gpios)
_boards.append(b)
elif bType=='i2c-out':
channels = int(configParser.get(s, 'channels'))
address = configParser.get(s, 'address')
bus = configParser.get(s, 'bus')
channels = int(boardsConfigParser.get(s, 'channels'))
address = boardsConfigParser.get(s, 'address')
bus = boardsConfigParser.get(s, 'bus')
b = BoardI2COut(boardnum=num, channels=channels, address=address, bus=bus)
_boards.append(b)
outletsConfigParser = ConfigParser.SafeConfigParser(defaults=outletsDefaults)
outletsConfig = '/etc/openpdu/outlets.conf'
if os.path.isfile(outletsConfig) and os.access(outletsConfig, os.R_OK):
outletsConfigParser.read(outletsConfig)
for s in outletsConfigParser.sections():
if re.match('^outlet.*',s):
num = int(re.sub(r'^outlet','',s))
description = configParser.get(s, 'description')
boardnum = configParser.get(s, 'board')
channel = configParser.get(s, 'channel')
o = Outlet(outletnum=num, boardnum=boardnum, channel=channel)
description = outletsConfigParser.get(s, 'description')
boardnum = outletsConfigParser.get(s, 'board')
channel = outletsConfigParser.get(s, 'channel')
startpower = outletsConfigParser.get(s, 'startpower') == 1
o = Outlet(outletnum=num, boardnum=boardnum, channel=channel, startpower=startpower)
o.description = description
_outlets.append(o)
parser = argh.ArghParser()
parser.add_commands([setpower, getpower, outlets, boards, initboards, version])