Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # sispmctlcups 0.0.1 -- CUPS backend for controlling SiS-PM devices
- #
- # This script saved as a CUPS backend (on Linux/FreeBSD normally in
- # /usr/lib/cups/backend and on OS X in /usr/libexec/cups/backend/)
- # provides an interface to GEMBIRD (m)SiS-PM devices: These are
- # cheap advanced surge protectors with 1 or 4 sockets manageable
- # via USB.
- #
- # Please compare with http://sispmctl.sourceforge.net
- #
- # The script creates two pseudo printers for each found SiS-PM socket.
- # One to switch the socket on and the other so shut the power down.
- # In case you attach two 4-port SiS-PMs to your system then 'sispmctl -s'
- # would list for example:
- #
- # root@bananas:~# sispmctl -s
- # Gembird #0
- # USB information: bus 005, device 002
- # device type: 4-socket SiS-PM
- # serial number: 01:01:51:45:0b
- #
- # Gembird #1
- # USB information: bus 003, device 002
- # device type: 4-socket SiS-PM
- # serial number: 01:01:51:23:cb
- #
- # And the backend would list 16 pseudo printers that can be used to
- # switch on/off the specific socket on the SiS-PM in question. Therefore
- # the script uses the serial number of each Gembird device and encodes it
- # in the so called device URI. For example the first and the last socket
- # of the first Gembird and the third of the second:
- #
- # root@bananas:~# lpinfo -v
- # ...
- # file sispmctlcups://01_01_51_45_0b-1-on
- # file sispmctlcups://01_01_51_45_0b-1-off
- # ...
- # file sispmctlcups://01_01_51_45_0b-4-on
- # file sispmctlcups://01_01_51_45_0b-4-off
- # ...
- # file sispmctlcups://01_01_51_23_cb-3-on
- # file sispmctlcups://01_01_51_23_cb-3-off
- #
- # This means that once you're done with the setup of your printers you can
- # disconnect and reconnect your Gembird USB devices in different order and
- # will still be able to switch the right socket as long as you take care
- # that the same devices are plugged into the same socket!
- #
- # Attention: Normally only root is allowed to use sispmctl on linux. So
- # unless you adjust permissions as it's outlined on the sispmctl page
- # http://sispmctl.sourceforge.net (eg. simply using the setuid bit:
- # "chmod u+s /usr/bin/sispmctl") you won't be able to access the SiS-PM
- # devices from within CUPS since the daemons are running under an
- # unprivileged account like 'lp'.
- #
- # If you suceeded setting up the pseudo printer devices to switch on and
- # off specific sockets on one of your SiS-PMs (you won't need a PPD -- just
- # use 'raw' as printer type) then you could simply send empty print jobs
- # to switch the sockets:
- #
- # root@bananas:~# echo "" | lp -d switch_on_laserjet
- #
- # The name of the printer queue doesn't matter. All what's important is
- # the correct device URI (that contains the serial number of your device):
- #
- # root@bananas:~# lpstat -t | grep switch_on_laserjet
- # device for switch_on_laserjet: sispmctlcups://01_01_51_45_0b-4-on
- # switch_on_laserjet accepting requests since Di 25 Nov 2014 22:21:00 CET
- # printer switch_on_laserjet is idle. enabled since Di 25 Nov 2014 22:21:00 CET
- #
- # In case something does not work as expected, try executing sispmctl
- # manually under the account the CUPS daemons run on your system (very
- # important to nail down permission related problems) and temporarely
- # change 'LogLevel warn' to 'LogLevel debug' in /etc/cups/cupsd.conf,
- # restart cupsd and have a look into CUPS' error_log while accessing
- # CUPS' administrative interfaces or printing to the pseudo printers.
- #
- #############################################################################
- #
- # You get these devices with or without surge protection / insurance:
- # http://energenie.com/item.aspx?id=7556
- # http://energenie.com/item.aspx?id=7025
- # http://energenie.com/item.aspx?id=7415
- #
- # http://geizhals.at/eu/231255382 vs. http://geizhals.at/eu/a732667.html
- #
- #############################################################################
- #
- # author: Thomas Kaiser <mailto:print-to-pdf@kaiser-edv.de>
- # license: GPL - http://www.gnu.org/copyleft/gpl.html
- #
- #############################################################################
- export PATH=/usr/bin:/sbin:/bin
- case $# in
- 0)
- # CUPS is in 'device discovery' mode. We list our capabilities that
- # means all found SiS-PM with their serial numbers and the count of
- # sockets they provide:
- # check version
- SiSPMVersion=$(sispmctl -v 2>&1 | awk -F" " '/SiS PM Control/ {print $6}')
- case ${SiSPMVersion} in
- 3.*)
- # ok
- :
- ;;
- *)
- # too old or not existing
- echo "ERROR: sispmctl is not installed, too old or not accessible" >&2
- ;;
- esac
- # generate debug output:
- echo -e "DEBUG: Output from sispmctl device discovery\nDEBUG: " >&2
- sispmctl -s 2>&1 | while read ; do
- echo "DEBUG: ${REPLY}" >&2
- done
- # define counter
- SiSPM=0
- # parse sispmctl output to get serial numbers and device types (1/4 sockets)
- sispmctl -s | awk -F": " '/^serial number:/ {print $2}' | sed 's/\ //g' | while read ; do
- SerialNumber=$(echo "${REPLY}" | sed 's/\:/_/g')
- DeviceType=$(sispmctl -s | grep -B1 "${REPLY}" | awk -F": " '/^device type:/ {print $2}')
- case ${DeviceType} in
- *1*)
- # 1-socket mSiS-PM
- echo -e "file sispmctlcups://${SerialNumber}-1-on \"Unknown\" \"Power on mSiS-PM #${SiSPM}/${REPLY} socket 1\"\nfile sispmctlcups://${SerialNumber}-1-off \"Unknown\" \"Switch off mSiS-PM #${SiSPM} socket 1\""
- ;;
- *4*)
- # 4-socket SiS-PM
- for i in 1 2 3 4 ; do
- echo -e "file sispmctlcups://${SerialNumber}-${i}-on \"Unknown\" \"Power on SiS-PM #${SiSPM}/${REPLY} socket ${i}\"\nfile sispmctlcups://${SerialNumber}-${i}-off \"Unknown\" \"Switch off SiS-PM #${SiSPM}/${REPLY} socket ${i}\""
- done
- ;;
- esac
- let SiSPM++
- done
- ;;
- 5|6)
- # we have to process a switch on/off request since this is a print job
- CUPSDeviceString=$(echo "${DEVICE_URI}" | awk -F'/' '{print $3}')
- # parse device URI
- OIFS=${IFS}
- IFS="-"
- set ${CUPSDeviceString}
- SiSPMSerialnumber=$(echo ${1} | tr '_' ':')
- SiSPMSocket=${2}
- SiSPMMode=${3}
- IFS=${OIFS}
- echo "DEBUG: Switch request for ${DEVICE_URI}: SiS-PM ${SiSPMSerial} socket ${SiSPMSocket} to ${SiSPMMode}" >&2
- case ${SiSPMMode} in
- on)
- echo "DEBUG: Executing \"sispmctl -D${SiSPMSerialnumber} -o${SiSPMSocket}\"" >&2
- sispmctl -D${SiSPMSerialnumber} -o${SiSPMSocket} 2>&1 | while read ; do
- echo "DEBUG: ${REPLY}" >&2
- done
- ;;
- off)
- echo "DEBUG: Executing \"sispmctl -D${SiSPMSerialnumber} -f${SiSPMSocket}\"" >&2
- sispmctl -D${SiSPMSerialnumber} -f${SiSPMSocket} 2>&1 | while read ; do
- echo "DEBUG: ${REPLY}" >&2
- done
- ;;
- esac
- ;;
- esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement