Guest User

expect mpd

a guest
Feb 22nd, 2014
289
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/expect --
  2.  
  3.  
  4. set timeoff 5
  5. set server localhost
  6. set port 6600
  7. #set onplay [lindex $argv 2]
  8. set onplay /home/pi/gpio/poweron.sh
  9. #set noplay [lindex $argv 3]
  10. set noplay /home/pi/gpio/poweroff.sh
  11. #set onstop [lindex $argv 3]
  12.  
  13.  
  14. set timeout -1
  15. log_user 0
  16.  
  17. trap {send "close\r"
  18. exit} {SIGINT SIGTERM}
  19.  
  20. send_user "Starting connection to MPD [lrange $argv 0 1]\r\n"
  21. send_user "On play: $onplay\r\n"
  22. send_user "On no play: $noplay\r\n"
  23. #send_user "On stop: $onstop\r\n"
  24.  
  25. #spawn -noecho telnet [lindex $argv 0] [lindex $argv 1]
  26. spawn -noecho telnet $server $port
  27.  
  28. expect {
  29. "OK MPD" {
  30. send_user "Connected\r\n"
  31. }
  32. }
  33.  
  34. send "status\r"
  35. expect {
  36. "state: play" {
  37. set currentState "play"
  38. }
  39. "state: pause" {
  40. set currentState "noplay"
  41. }
  42. "state: stop" {
  43. set currentState "noplay"
  44. }
  45. }
  46. send_user "Initial state $currentState\r\n"
  47. send "idle player\r"
  48.  
  49. for {} 1 {} {
  50. expect {
  51. timeout {
  52. if { "$noplay" != "" } {
  53. exec "$noplay"
  54. }
  55. send_user "Timeout, off speakers\r\n"
  56. set timeout -1
  57. }
  58. "changed: player" {
  59. send_user "Changed status player\r\n"
  60. send "status\r"
  61. }
  62. "state: play" {
  63. send_user "Status: play\r\n"
  64. if { "$currentState" != "play" } {
  65. send_user "State changed to play\r\n"
  66. if { "$onplay" != "" } {
  67. exec $onplay
  68. }
  69. set timeout -1
  70. set currentState "play"
  71. }
  72. send "idle player\r"
  73. }
  74. "state: pause" {
  75. send_user "Status: pause\r\n"
  76. if { "$currentState" != "noplay" } {
  77. send_user "State changed to pause\r\n"
  78. set timeout $timeoff
  79. set currentState "stop"
  80. }
  81. send "idle player\r"
  82. }
  83. "state: stop" {
  84. send_user "Status: stop\r\n"
  85. if { "$currentState" != "noplay" } {
  86. send_user "State changed to stop\r\n"
  87. set timeout $timeoff
  88. set currentState "stop"
  89. }
  90. send "idle player\r"
  91. }
  92. }
  93. }
RAW Paste Data