Plan 9 from Bell Labs’s /usr/web/sources/contrib/cinap_lenrek/apc.rc

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#!/bin/rc

bind -a '#t' /dev
com=/dev/eia1
echo 'b2400 e0 d0 l8 pn' >$com^'ctl'

fn onpowfail {
	log 'syncing fossil/venti...'

	sleep 1
	f=`{ls /srv/fscons*>[2]/dev/null}
	for(i in $f){
		{
			echo
			echo fsys all sync
		} < $i >> $i
	}
	sleep 2
	venti/sync >/dev/null >[2=1]
	sleep 1
}

fn onpowret {
	# should unhalt fossil here
}

fn onlowbat {
}

fn onlowbatret {
}


# some helper stuff

fn send {
	echo -n $*
}

fn recvn {
	dd -bs $1 -count 1 >[2] /dev/null
	echo
}

fn recvln {
	read | sed 's/
//g'
}

fn log {
	echo `{date} APC: $* >[1=2]
}

# 

fn reset {
	log 'reseting...'

	send Y
	e=`{recvln}
	if(! ~ $e SM){
		log 'setting smartmode failed: '^$"e
		exit
	}

	send A
	e=`{recvln}
	if(! ~ $e OK){
		log 'frontpanel test failed: '^$"e
	}

	log 'done'
}

fn info {
	for(t in $*){
		send $t
		e=`{recvln}
		d=$t
		switch($t){
		case B
			d='battery voltage'
		case C
			d='internal temperature'
		case F
			d='line frequency'
		case L
			d='input line voltage'
		case O
			d='output line voltage'
		case P
			d='power load (%)'
		case f
			d='battery level (%)'
		case n
			d='serial number'
		case b
			d='firmware revision'
		}
		log $"d^': '^$"e
	}
}

{
	reset
	info n b B C F L O P f

	while(){
		c=`{recvn 1}
		switch($c){
		case '!'
			log 'power failure'
			info f O P
			onpowfail
		case '$'
			log 'power returned'
			info L O
			onpowret
		case '%'
			log 'low battery'
			info f
			onlowbat
		case '+'
			log 'return from low battery'
			info f
			onlowbatret
		case '?'
			log 'abnormal condition'

			log 'will reset in 10 seconds...'
			sleep 10
			reset
		case '='
			log 'return from abnormal condition'
		case '#'
			log 'replace battery'
		case '&'
			log 'check alarm'
		case '|'
			log 'variable changed'
		case *
			log 'unknown status code: '^$"c
		}
	}
} <$com >$com

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].