Home of the Squeezebox™ & Transporter® network music players.
Page 3 of 4 FirstFirst 1234 LastLast
Results 21 to 30 of 37
  1. #21
    Quote Originally Posted by paul- View Post
    Can't tell you how many times I wanted to use something like expect, but never wanted the baggage of other languages. Thanks for that link.
    +1.

    Before I read Roland's post on using sexpect (I was already disillusioned with the expect that I was using because it comes with 2mb of tcl) I was tinkering with setting the squeezelite log option in the pCP web interface to "stream=info" and then using inotifywait to monitor /var/log/pcp_squeezelite.log for modification. That actually seems to work pretty well and you can just blank the log after every event so it doesn't continually grow. So if all you want is a way to detect a change and you are getting your actual update information via the jsonrpc call or whatever, then monitoring the log probably works as well as the sexpect method, though it still has the inotifywait dependency.

    I might just "watch tail" the logfile instead of using inotifywait and see how that works. Sort of brute force but it's all local to the box, no extra persistent network connections and the tools are part of busybox already.

    I'm not really digging the jsonrpc.js stuff, mostly because I can't get it to work with just nc and curl wouldn't install when I tried it earlier, "Error on ipv6-4.14.56-pcpCore_v7.tcz", I guess I probably need to hit the pCP repo for that package?? I'm burned out for the night anyway.

    I wish there was an easy to parse http GET you could do with wget. Is there one?

    http://192.168.1.201:9000/status.htm...3A94%3Aae%3A63

    Is fairly uncluttered but it's still 20k worth of unnecessary html. Changing the extension to .m3u gets you the playlist but you can't tell what the currently playing song is with that. Seems like using sexpect for both the monitoring and the status queries would be a good solution rather than tacking on curl too. If I can figure out how to get the cava bar font to look decent outside of tmux I think I'll ditch tmux too. Cava and the image manipulation tools are required but it would be nice to keep the other deps to a minimum. Just because.

    //edit - also, the "newsong" part of "subscribe playlist newsong" doesn't do anything to filter the responses, it's the same as just "subscribe playlist" so far as I can tell.
    Last edited by sodface; 2018-12-03 at 22:37.

  2. #22
    Senior Member
    Join Date
    Aug 2012
    Location
    Austria
    Posts
    780
    Quote Originally Posted by sodface View Post
    Before I read Roland's post on using sexpect (I was already disillusioned with the expect that I was using because it comes with 2mb of tcl) I was tinkering with setting the squeezelite log option in the pCP web interface to "stream=info" and then using inotifywait to monitor /var/log/pcp_squeezelite.log for modification. That actually seems to work pretty well and you can just blank the log after every event so it doesn't continually grow. So if all you want is a way to detect a change and you are getting your actual update information via the jsonrpc call or whatever, then monitoring the log probably works as well as the sexpect method, though it still has the inotifywait dependency.
    sexpect + curl + jq + nc6 = 350K with only zlib as a dependency - that's light-weight enough for me.

    I'm not really digging the jsonrpc.js stuff, mostly because I can't get it to work with just nc and curl wouldn't install when I tried it earlier, "Error on ipv6-4.14.56-pcpCore_v7.tcz", I guess I probably need to hit the pCP repo for that package??
    Compile a local version.

    I wish there was an easy to parse http GET you could do with wget. Is there one?
    Probably not.

    If I can figure out how to get the cava bar font to look decent outside of tmux I think I'll ditch tmux too.
    Works fine on the FB console here.

    //edit - also, the "newsong" part of "subscribe playlist newsong" doesn't do anything to filter the responses, it's the same as just "subscribe playlist" so far as I can tell.
    Yes, the regex used by sexpect takes care of that,

    GraphicsMagick takes ~2 sec to generate the PNG shown in the screenshot above, which isn't too bad. Still, it's noticeable when a cached image is used (but probably only if one is staring at the screen).

  3. #23
    Senior Member paul-'s Avatar
    Join Date
    Jan 2013
    Posts
    1,505
    Yes, just install the kernel modules from our repo first.

    And you can get Json info with wget and parse it with JSON.awk, which is included with pcp.
    piCorePlayer a small player for the Raspberry Pi in RAM.
    Homepage: https://www.picoreplayer.org

    Please donate if you like the piCorePlayer

  4. #24
    Thanks to Roland0 and Paul, I've been testing with sexpect and JSON.awk and it's working fairly well for detecting a "newsong" event and retrieving json playlist info. The operation of sexpect is pretty confusing. Seems like there should be a way to clear the buffer after a match and a way to keep spawned processes from spewing all over stdout but it's probably operator error.

    Code:
    #!/bin/sh
    sexpect -s 9090.sock spawn telnet 192.168.1.201 9090 &
    sleep .5
    sexpect -s 9090.sock send -enter "b8%3A27%3Aeb%3A94%3Aae%3A63 subscribe playlist"
    
    while :
    do
    sexpect -s 9090.sock expect -re "newsong"
    sexpect -s 9090.sock expect -re ".*"
    
    sexpect -s 9000.sock spawn nc 192.168.1.201 9000 &
    sleep .1
    sexpect -s 9000.sock send -f test
    sexpect -s 9000.sock expect -timeout 10 -glob '{"params":*'
    title=$(sexpect -s 9000.sock expect_out -index 0 | awk -f /usr/local/bin/JSON.awk - | grep -m 1 '0,"title"' | cut -f2 | cut -d '"' -f2)
    artist=$(sexpect -s 9000.sock expect_out -index 0 | awk -f /usr/local/bin/JSON.awk - | grep -m 1 '"artist"' | cut -f2 | cut -d '"' -f2)
    album=$(sexpect -s 9000.sock expect_out -index 0 | awk -f /usr/local/bin/JSON.awk - | grep -m 1 '"album"' | cut -f2 | cut -d '"' -f2)
    station=$(sexpect -s 9000.sock expect_out -index 0 | awk -f /usr/local/bin/JSON.awk - | grep -m 1 '"remote_title"' | cut -f2 | cut -d '"' -f2)
    sexpect -s 9000.sock close
    pid=$(sexpect -s 9000.sock get -ppid)
    kill $pid
    
    echo "$title"
    echo "$artist"
    echo "$album"
    echo "$station"
    done
    Oh, and I had to background the "sexpect spawn" commands or the script would just hang - I thought the point of the app was to background itself automagically?

    // where "test" is a separate file:
    Code:
    POST /jsonrpc.js HTTP/1.1
    HOST: 192.168.1.201:9000
    Content-Type: application/json; charset=utf-8
    Content-Length: 97
    
    {"id":1,"method":"slim.request","params":["b8:27:eb:94:ae:63",["status","-","1","tags:acgltys"]]}
    Last edited by sodface; 2018-12-06 at 04:29.

  5. #25
    Senior Member
    Join Date
    Aug 2012
    Location
    Austria
    Posts
    780
    Quote Originally Posted by sodface View Post
    The operation of sexpect is pretty confusing. Seems like there should be a way to clear the buffer after a match and a way to keep spawned processes from spewing all over stdout but it's probably operator error.
    Oh, and I had to background the "sexpect spawn" commands or the script would just hang - I thought the point of the app was to background itself automagically?
    My sexpect spawn backgrounds itself - maybe because I spawn a shell.
    You should do a "wait" after the "close", otherwise sexpect will hang (I guess this is why you kill it?)
    Haven't found a way to suppress the output, so I just redirect stdout to /dev/null

    Code:
    title=$(sexpect -s 9000.sock expect_out -index 0 | awk -f /usr/local/bin/JSON.awk - | grep -m 1 '0,"title"' | cut -f2 | cut -d '"' -f2)
    But doesn't this just always process the first track in the playlist (which isn't necessarily the one playing) ?
    My script reads playlist_cur_index and then uses the playlist_loop record at that index.

    btw, if you use wget to post, you don't need netcat, the second sexpect spawn and the matching sexpect expect/_outs - simply put the result JSON into a variable.

  6. #26
    Quote Originally Posted by Roland0 View Post
    My sexpect spawn backgrounds itself - maybe because I spawn a shell.
    You should do a "wait" after the "close", otherwise sexpect will hang (I guess this is why you kill it?)
    Haven't found a way to suppress the output, so I just redirect stdout to /dev/null
    Yes, I think spawning a shell is the difference though manually backgrounding it in my test script worked ok, just took me a few minutes to figure out what was going on since I didn't think backgrounding was necessary.

    I was killing the port 9000 sexpect process and then respawning it at the top of the loop as an ugly way of clearing the buffer.

    But doesn't this just always process the first track in the playlist (which isn't necessarily the one playing) ?
    No, index 0 is the currently playing song if you use the dash in the query - from the cli page:
    Code:
    Set the parameter to "-" to get the playlist data starting from the current song.
    btw, if you use wget to post, you don't need netcat, the second sexpect spawn and the matching sexpect expect/_outs - simply put the result JSON into a variable.
    The busybox wget doesn't do post. Haha, I know, get a different version that does, and any sane person would, but I seem to be irrationally hung up on not wanting to bring in extra tools, even though I have to for some things.

    Just a few minutes ago I finally figured out how to get the json data just using the busybox nc, that was driving me crazy. I couldn't understand why it was just exiting without returning anything. It's the sleep .1 that made all the difference.

    Here's the little test script I used:
    Code:
    #!/bin/sh
    
    post=$(cat <<'END_HEREDOC'
    POST /jsonrpc.js HTTP/1.1
    HOST: 192.168.1.201:9000
    Content-Type: application/json; charset=utf-8
    Content-Length: 97
    
    {"id":1,"method":"slim.request","params":["b8:27:eb:94:ae:63",["status","-","1","tags:acgltys"]]}
    END_HEREDOC
    )
    
    (echo -e "$post"; sleep .1) | nc 192.168.1.201 9000 | tail --line=+7 > json.txt
    So now I'm thinking about trying the log watching method so I can detect newsong events and retrieve the json with just busybox builtins.
    Last edited by sodface; 2018-12-06 at 21:18.

  7. #27
    Senior Member Greg Erskine's Avatar
    Join Date
    Sep 2006
    Location
    Sydney, Australia
    Posts
    1,491
    hi sodface,

    I like your attitude! The GNU wget in wget.tcz loads 5 or 6 extra extensions, and increases the size of pCP by nearly 2M.

    I must confess, pCP temporarily loads GNU wget to it talks to pastebin, but its only there until the next reboot.

    I noticed you haven't even been tempted to load bash.

    regards
    Greg

  8. #28
    I just did a rough test with squeezelite log level setting set to "stream=info", then running the below script and cycling through some local tracks and internet streams. The script just copies the log file and then compares the log to the copy every .3 seconds and if they are different gets the new track info and writes the title to stdout. Brute force yes but it was really responsive to changes, virtually no lag in detecting the change and writing out the track title. You can see that some titles are repeated though, which is I'm sure to do with timing of the log writing and comparing, so on some loops the log content has changed even though the track hasn't. I'll just have to do a few more checks to minimize false positives. The output below is local tracks at the beginning and then internet streams at the end. The internet streams produced more repeats. I think there is more delay to log writes which triggers more false positives.

    Anyway, I'm sort of liking it!

    Code:
    root@pi3-LMS:/home/tc/sod-npd# cat script.sh
    #!/bin/sh
    
    post=$(cat <<'END_HEREDOC'
    POST /jsonrpc.js HTTP/1.1
    HOST: 192.168.1.201:9000
    Content-Type: application/json; charset=utf-8
    Content-Length: 97
    
    {"id":1,"method":"slim.request","params":["b8:27:eb:94:ae:63",["status","-","1","tags:acgltys"]]}
    END_HEREDOC
    )
    
    cp /var/log/pcp_squeezelite.log last.log
    
    while :
    do
      if `cmp -s /var/log/pcp_squeezelite.log last.log`
      then
        sleep .3
      else
        cp /var/log/pcp_squeezelite.log last.log
        (echo -e "$post"; sleep .1) | nc 192.168.1.201 9000 | tail --line=+7 | awk -f /usr/local/bin/JSON.awk - | grep -m 1 '0,"title"' | cut -f2 | cut -d '"' -f2
      fi
    done
    Code:
    root@pi3-LMS:/home/tc/sod-npd# ./script.sh
    Don’t Go Like That, No
    Every Gauge Is On Empty
    Mathematicians
    Sweaters
    I Just Wanna Go Home
    Green With Envy
    Makin’ Faces Like (You Ate A Lemon)
    Outside, Lookin’ In
    Neon Signs
    Dreaming The Day Away
    Don’t Go Like That, No
    Don’t Go Like That, No
    Every Gauge Is On Empty
    Sweaters
    Sweaters
    Sweaters
    Frontier Ruckus - 27 Dollars
    Frontier Ruckus - 27 Dollars
    Frontier Ruckus - 27 Dollars
    Dead Horses - Red Pony
    Red Pony
    Saban Saulic - Nismo vise tako mladi
    Saban Saulic - Nismo vise tako mladi
    Saban Saulic - Nismo vise tako mladi
    Nihad Alibegovic - Sta Ucini Od Mog Zivota
    Nihad Alibegovic - Sta Ucini Od Mog Zivota
    Nihad Alibegovic - Sta Ucini Od Mog Zivota
    Sta Ucini Od Mog Zivota
    Last edited by sodface; 2018-12-06 at 22:23.

  9. #29
    Quote Originally Posted by Greg Erskine View Post
    hi sodface,

    I like your attitude!


    The GNU wget in wget.tcz loads 5 or 6 extra extensions, and increases the size of pCP by nearly 2M.

    I must confess, pCP temporarily loads GNU wget to it talks to pastebin, but its only there until the next reboot.
    Well, if you need it, you need it, but I sense the extra 2M irritates you a bit?

    I noticed you haven't even been tempted to load bash.
    I'm trying to work with what's there as much as I can, that JSON.awk sure was a nice surprise! I noticed in the list of included applets on the busybox page that there are a few more there then what is available in pCP. I assume that's either a version difference or they were excluded in the busybox build? The inotifyd busybox builtin would have been really nice to have for monitoring the log file instead of a sleepy loop.

  10. #30
    Senior Member
    Join Date
    Aug 2012
    Location
    Austria
    Posts
    780
    Quote Originally Posted by sodface View Post
    Code:
    cp /var/log/pcp_squeezelite.log last.log
    while :
    do
      if `cmp -s /var/log/pcp_squeezelite.log last.log`
      then
        sleep .3
      else
        cp /var/log/pcp_squeezelite.log last.log
        (echo -e "$post"; sleep .1) | nc 192.168.1.201 9000 | tail --line=+7 | awk -f /usr/local/bin/JSON.awk - | grep -m 1 '0,"title"' | cut -f2 | cut -d '"' -f2
      fi
    done
    More efficient way:
    Code:
    LASTT=0
    while true; do
    	MODT=$(stat -c "%Y" /var/log/pcp_squeezelite.log)
    	if [ $MODT -ne $LASTT ]; then
    		LASTT=$MODT
    		echo "file changed at $MODT"
    	fi
    	sleep 1
    done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •