From c27e2420075b886e592845565699408d88dae03d Mon Sep 17 00:00:00 2001 From: paspo Date: Mon, 19 Feb 2018 21:19:38 +0100 Subject: [PATCH] startpower and config separation --- APKBUILD | 3 +- etc/local.d/openpdu.start | 21 +------- etc/openpdu/{openpdu.conf => boards.conf} | 0 etc/openpdu/outlets.conf | 29 ++++++++++++ openpdu | 58 +++++++++++++++-------- 5 files changed, 72 insertions(+), 39 deletions(-) rename etc/openpdu/{openpdu.conf => boards.conf} (100%) create mode 100644 etc/openpdu/outlets.conf diff --git a/APKBUILD b/APKBUILD index 7fbe4bd..34ac55d 100644 --- a/APKBUILD +++ b/APKBUILD @@ -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 diff --git a/etc/local.d/openpdu.start b/etc/local.d/openpdu.start index de15e1f..d1b52b5 100644 --- a/etc/local.d/openpdu.start +++ b/etc/local.d/openpdu.start @@ -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 diff --git a/etc/openpdu/openpdu.conf b/etc/openpdu/boards.conf similarity index 100% rename from etc/openpdu/openpdu.conf rename to etc/openpdu/boards.conf diff --git a/etc/openpdu/outlets.conf b/etc/openpdu/outlets.conf new file mode 100644 index 0000000..8210655 --- /dev/null +++ b/etc/openpdu/outlets.conf @@ -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 diff --git a/openpdu b/openpdu index e1e2ed9..923f36a 100755 --- a/openpdu +++ b/openpdu @@ -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])