Advertisement
Guest User

sispmctlcups 0.0.1

a guest
Nov 26th, 2014
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.63 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # sispmctlcups 0.0.1 -- CUPS backend for controlling SiS-PM devices
  4. #
  5. # This script saved as a CUPS backend (on Linux/FreeBSD normally in
  6. # /usr/lib/cups/backend and on OS X in /usr/libexec/cups/backend/)
  7. # provides an interface to GEMBIRD (m)SiS-PM devices: These are
  8. # cheap advanced surge protectors with 1 or 4 sockets manageable
  9. # via USB.
  10. #
  11. # Please compare with http://sispmctl.sourceforge.net
  12. #
  13. # The script creates two pseudo printers for each found SiS-PM socket.
  14. # One to switch the socket on and the other so shut the power down.
  15. # In case you attach two 4-port SiS-PMs to your system then 'sispmctl -s'
  16. # would list for example:
  17. #
  18. # root@bananas:~# sispmctl -s
  19. # Gembird #0
  20. # USB information:  bus 005, device 002
  21. # device type:      4-socket SiS-PM
  22. # serial number:    01:01:51:45:0b
  23. #
  24. # Gembird #1
  25. # USB information:  bus 003, device 002
  26. # device type:      4-socket SiS-PM
  27. # serial number:    01:01:51:23:cb
  28. #
  29. # And the backend would list 16 pseudo printers that can be used to
  30. # switch on/off the specific socket on the SiS-PM in question. Therefore
  31. # the script uses the serial number of each Gembird device and encodes it
  32. # in the so called device URI. For example the first and the last socket
  33. # of the first Gembird and the third of the second:
  34. #
  35. # root@bananas:~# lpinfo -v
  36. # ...
  37. # file sispmctlcups://01_01_51_45_0b-1-on
  38. # file sispmctlcups://01_01_51_45_0b-1-off
  39. # ...
  40. # file sispmctlcups://01_01_51_45_0b-4-on
  41. # file sispmctlcups://01_01_51_45_0b-4-off
  42. # ...
  43. # file sispmctlcups://01_01_51_23_cb-3-on
  44. # file sispmctlcups://01_01_51_23_cb-3-off
  45. #
  46. # This means that once you're done with the setup of your printers you can
  47. # disconnect and reconnect your Gembird USB devices in different order and
  48. # will still be able to switch the right socket as long as you take care  
  49. # that the same devices are plugged into the same socket!
  50. #
  51. # Attention: Normally only root is allowed to use sispmctl on linux. So
  52. # unless you adjust permissions as it's outlined on the sispmctl page
  53. # http://sispmctl.sourceforge.net (eg. simply using the setuid bit:
  54. # "chmod u+s /usr/bin/sispmctl") you won't be able to access the SiS-PM
  55. # devices from within CUPS since the daemons are running under an
  56. # unprivileged account like 'lp'.
  57. #
  58. # If you suceeded setting up the pseudo printer devices to switch on and
  59. # off specific sockets on one of your SiS-PMs (you won't need a PPD -- just
  60. # use 'raw' as printer type) then you could simply send empty print jobs
  61. # to switch the sockets:
  62. #
  63. # root@bananas:~# echo "" | lp -d switch_on_laserjet
  64. #
  65. # The name of the printer queue doesn't matter. All what's important is
  66. # the correct device URI (that contains the serial number of your device):
  67. #
  68. # root@bananas:~# lpstat -t | grep switch_on_laserjet
  69. # device for switch_on_laserjet: sispmctlcups://01_01_51_45_0b-4-on
  70. # switch_on_laserjet accepting requests since Di 25 Nov 2014 22:21:00 CET
  71. # printer switch_on_laserjet is idle.  enabled since Di 25 Nov 2014 22:21:00 CET
  72. #
  73. # In case something does not work as expected, try executing sispmctl
  74. # manually under the account the CUPS daemons run on your system (very
  75. # important to nail down permission related problems) and temporarely
  76. # change 'LogLevel warn' to 'LogLevel debug' in /etc/cups/cupsd.conf,
  77. # restart cupsd and have a look into CUPS' error_log while accessing
  78. # CUPS' administrative interfaces or printing to the pseudo printers.
  79. #
  80. #############################################################################
  81. #
  82. # You get these devices with or without surge protection / insurance:
  83. # http://energenie.com/item.aspx?id=7556
  84. # http://energenie.com/item.aspx?id=7025
  85. # http://energenie.com/item.aspx?id=7415
  86. #
  87. # http://geizhals.at/eu/231255382 vs. http://geizhals.at/eu/a732667.html
  88. #
  89. #############################################################################
  90. #
  91. # author: Thomas Kaiser <mailto:print-to-pdf@kaiser-edv.de>
  92. # license: GPL - http://www.gnu.org/copyleft/gpl.html
  93. #
  94. #############################################################################
  95.  
  96. export PATH=/usr/bin:/sbin:/bin
  97.  
  98. case $# in
  99.     0)
  100.         # CUPS is in 'device discovery' mode. We list our capabilities that
  101.         # means all found SiS-PM with their serial numbers and the count of
  102.         # sockets they provide:
  103.        
  104.         # check version
  105.         SiSPMVersion=$(sispmctl -v 2>&1 | awk -F" " '/SiS PM Control/ {print $6}')
  106.         case ${SiSPMVersion} in
  107.             3.*)
  108.                 # ok
  109.                 :
  110.                 ;;
  111.             *)
  112.                 # too old or not existing
  113.                 echo "ERROR: sispmctl is not installed, too old or not accessible" >&2
  114.                 ;;
  115.         esac
  116.        
  117.         # generate debug output:
  118.         echo -e "DEBUG: Output from sispmctl device discovery\nDEBUG: " >&2
  119.         sispmctl -s 2>&1 | while read ; do      
  120.             echo "DEBUG: ${REPLY}" >&2
  121.         done
  122.  
  123.         # define counter
  124.         SiSPM=0
  125.        
  126.         # parse sispmctl output to get serial numbers and device types (1/4 sockets)
  127.         sispmctl -s | awk -F": " '/^serial number:/ {print $2}' | sed 's/\ //g' | while read ; do
  128.             SerialNumber=$(echo "${REPLY}" | sed 's/\:/_/g')
  129.             DeviceType=$(sispmctl -s | grep -B1 "${REPLY}" | awk -F": " '/^device type:/ {print $2}')
  130.             case ${DeviceType} in
  131.                 *1*)
  132.                     # 1-socket mSiS-PM
  133.                     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\""
  134.                     ;;
  135.                 *4*)
  136.                     # 4-socket SiS-PM
  137.                     for i in 1 2 3 4 ; do
  138.                         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}\""
  139.                     done
  140.                     ;;
  141.             esac
  142.             let SiSPM++
  143.         done
  144.         ;;
  145.     5|6)
  146.         # we have to process a switch on/off request since this is a print job
  147.         CUPSDeviceString=$(echo "${DEVICE_URI}" | awk -F'/' '{print $3}')
  148.        
  149.         # parse device URI
  150.         OIFS=${IFS}
  151.         IFS="-"
  152.         set ${CUPSDeviceString}
  153.         SiSPMSerialnumber=$(echo ${1} | tr '_' ':')
  154.         SiSPMSocket=${2}
  155.         SiSPMMode=${3}
  156.         IFS=${OIFS}
  157.         echo "DEBUG: Switch request for ${DEVICE_URI}: SiS-PM ${SiSPMSerial} socket ${SiSPMSocket} to ${SiSPMMode}" >&2
  158.        
  159.         case ${SiSPMMode} in
  160.             on)
  161.                 echo "DEBUG: Executing \"sispmctl -D${SiSPMSerialnumber} -o${SiSPMSocket}\"" >&2
  162.                 sispmctl -D${SiSPMSerialnumber} -o${SiSPMSocket} 2>&1 | while read ; do      
  163.                     echo "DEBUG: ${REPLY}" >&2
  164.                 done
  165.                 ;;
  166.             off)
  167.                 echo "DEBUG: Executing \"sispmctl -D${SiSPMSerialnumber} -f${SiSPMSocket}\"" >&2
  168.                 sispmctl -D${SiSPMSerialnumber} -f${SiSPMSocket} 2>&1 | while read ; do      
  169.                     echo "DEBUG: ${REPLY}" >&2
  170.                 done
  171.                 ;;
  172.         esac
  173.         ;;
  174. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement