macosshlibgphoto2

get current status of gphoto2


I have gphoto running with a --wait-event-and-download argument, so that the pictures I take using my infrared remote are saved to the computer.

I have a second script set up to interrupt the wait process and take a picture programatically, like so:

#!/bin/sh
# shootnow.sh - stop the current gphoto2 process (if it exists),
# shoot a new image, then start a new wait-event process.

pkill -INT gphoto2     #send interrupt (i.e. ctrl+c) to gphoto2
sleep 0.1              #avoid the process ownership error
gphoto2 --capture-image-and-download  #take a picture now
gphoto2 --wait-event-and-download   #start a new wait-event process

But I want to make sure the first wait-event process is not currently downloading an image before I go interrupting it (which causes a messy situation where images fill up the camera's ram, preventing further operation). So the second script should be something more like this:

#!/bin/sh
# shootnow-with-check.sh - stop the current gphoto2 process (if it exists 
# and isn't currently downloading an image), shoot a new image, then start 
# a new wait-event process.

shootnow() {  # same as previously, but now in a function
    pkill -INT gphoto2
    sleep 0.1
    gphoto2 --capture-image-and-download
    gphoto2 --wait-event-and-download
}

if [ ***current output line of gphoto2 process doesnt start with "Downloading"*** ] then
    shootnow
else
    echo "Capture aborted - a picture was just taken and is being saved."
fi

Can anyone help me with that if statement? Can I read the current output line of a running gphoto process?


Solution

  • I eventually managed this with a script like so:

    #!/bin/bash
    # gphoto2-expect.sh
    # use expect to monitor gphoto2 during --capture-image-and-download with
    # --interval=-1, adding in SIGUSR1 functionality except during a
    # download event.
    
    echo "Prepping system for camera"
    killall PTPCamera
    expect << 'EOS'
    puts "Starting capture..."
    if [catch "spawn gphoto2 --capture-image-and-download --interval=-1" gp_pid] {
      Log $ERROR "Unable to start gphoto2.\n$gp_pid\n"
      return 0
    }
    
    trap {exec kill -SIGUSR1 $gp_pid} SIGUSR1
    set timeout -1
    expect {
      -i $spawn_id
      "Downloading" {
        trap {send_user "\n Ignoring request as currently downloading"} SIGUSR1 ; exp_continue
      }
      "Saving file as" {
        sleep 0.1
        trap {exec kill -SIGUSR1 $gp_pid} SIGUSR1 ; exp_continue
      }
    }
    
    EOS
    

    which could be triggered with another script:

    #!/bin/bash
    # trigger.sh - trigger an immediate capture
    var=$(pidof expect)
    kill -SIGUSR1 "$var"