f.zz.de
posts /

cups notifier - another overengineered feature

Posted Sat 25 Oct 2008 11:06:30 AM CEST
in

I bought a usb power-bar to switch individual devices on an off. We have a central laserjet printer at home which i wanted to turn on automatically after somebody prints and turn off after some time. Using the SIS PM tools is straight forward and turns on/off the printer fine with the power-bar. Now - when do you turn the printer on? The easy stuff would have been a cronjob counting the jobs in the queue. One would have needed to run this every minute or something to get a quick result. I didnt like that approach and found the cups notifier. The notifier may run your programm whenever something happens on the cups server e.g. a job gets created. The problem is - The information you get passed is binary bullshit and fully unsuitable for scripting. I was very close to drop the notifiers when i encountered the testnotify notifier. Its for debugging only and its sole purpose is to decode the binary bullshit. Some shell scripts later everything was done:

`

!/bin/sh

TESTNOTIFY=/usr/lib/cups/notifier/testnotify
POWERFILE=/var/cache/cups/printerpoweron
TMP=tempfile
PRINTERPOWERON="sudo /usr/local/bin/sispmctl -o 4"

In:

DEBUG: notify-subscribed-event (keyword): "printer-state-changed"

Out:

notify-subscribed-event: printer-state-changed

${TESTNOTIFY} \
testnotify://nowait 2>&1 |\
sed -ne '/^DEBUG: / { s/^DEBUG: *//; s/ ([^)]):/:/; /: "/ { s/: "/: /; s/"$//; }; p;}' >${TMP}

event=sed -ne '/notify-subscribed-event:/ { s/^.*: **//; p; }' <${TMP}

case ${event} in
job-created)
date >>${POWERFILE}
${PRINTERPOWERON}
;;
esac
exit 0
`

Now the same for poweroff - We want to only poweroff if there were no new print jobs within a certain amount of time and no printjob in the queue left ...

`

!/bin/sh

POWERFILE=/var/cache/cups/printerpoweron
PRINTERPOWEROFF="/usr/local/bin/sispmctl -f 4"
POWEROFFDELAY=300

Is the printer turned on?

[ -e ${POWERFILE} ] || exit 0

Is there something still in the queue?

[ $((lpq | wc -l - 2)) -eq 0 ] || exit 0

Poweroff delay

age=$((date "+%s" - find ${POWERFILE} -printf "%C@"))

if [ ${age} -gt ${POWEROFFDELAY} ]; then
${PRINTERPOWEROFF} >/dev/null 2>&1
rm ${POWERFILE}
fi
`

Have a look at the power-bar which is really nice - Reichelt has them as Gembird SIS-PM Silver Shield and Pearl as revolt "Intelli-Plug". The linux software is on sourceforge http://sispmctl.sf.net