0

Thanks for click on this post, I have a Dashboard powered by Dashing on my RPI 3 where I have add a widget for my camera IP (D-link DSC 4201).

Camera-widget Link

There is the ruby jobs who catch the snapshot.jpeg of my camera :

require 'net/http'

@cameraDelay = 1 # Needed for image sync. 
@fetchNewImageEvery = '3s'

@camera1Host = "192.168.0.20"  ## CHANGE
@camera1Port = "80"  ## CHANGE
@camera1Username = 'admin' ## CHANGE
@camera1Password ='*****'
@camera1URL = "/dms?nowprofileid=1&"
@newFile1 = "assets/images/cameras/snapshot1_new.jpg"
@oldFile1 = "assets/images/cameras/snapshot1_old.jpg"


def fetch_image(host,old_file,new_file, cam_user, cam_pass, cam_url)
    `rm #{old_file}` 
    `mv #{new_file} #{old_file}`    
    Net::HTTP.start(host) do |http|
        req = Net::HTTP::Get.new(cam_url)
        if cam_user != "None" ## if username for any particular camera is set to 'None' then assume auth not required.
            req.basic_auth cam_user, cam_pass
        end
        response = http.request(req)
        open(new_file, "wb") do |file|
            file.write(response.body)
        end

        end
    new_file
end

def make_web_friendly(file)
  "/" + File.basename(File.dirname(file)) + "/" + File.basename(file)
end

SCHEDULER.every @fetchNewImageEvery do

        new_file1 = fetch_image(@camera1Host,@oldFile1,@newFile1,@camera1Username,@camera1Password,@camera1URL)

    if not File.exists?(@newFile1)
        warn "Failed to Get Camera Image"
    end
    send_event('camera1', image: make_web_friendly(@oldFile1))
    sleep(@cameraDelay)
    send_event('camera1', image: make_web_friendly(new_file1))

end

Actually my jobs display only the first two images (in @oldFile1,@newFile1) and after he was stock in a loop where he display only the first two images catch on my dashing dashboard.

So, I have looked in the /assets and I see my two snapshots.jpg refresh in real time like my jobs as to did it, But the dashing dashboard doesn't display it.

So why the dashboard don't took the refresh image..?

Thibz
  • 3
  • 4

1 Answers1

0

just ran into this issue today. Ended up rewriting things a bit. Pretty sure this is a browser caching issue in that the file name is the same. Just appended the datetime and pushing it seemed to work.

require 'net/http'
require 'open-uri'

@url = 'http://172.1.1.16/image.jpg'

SCHEDULER.every '4s', :first_in => 0 do |job|

    `find '/dashing/f12dash/assets/images/cameras/' -type f -mmin +1 -print0 | xargs -0 rm -f`

    @currentTime = Time.now.strftime("%Y-%m-%d_%H-%M-%S")
    @newFile1 = "assets/images/cameras/snapshot-" + @currentTime + "_new.jpeg"

    open(@url, :http_basic_authentication => ['root', 'CamPW']) do |f|
      open(@newFile1,'wb') do |file|
        file.puts f.read
      end
    end

    send_event('camera1', image: ("/" + File.basename(File.dirname(@newFile1)) + "/" + File.basename(@newFile1)))

end
mikeyeg
  • 28
  • 5
  • Thanks a lot your script worked find but for an unknown reason the image.jpeg sometimes wasn't display on the dashboard like maybe every 5 images, at start, i thinks it's because they are too heavy but obviously it's not the fact, any idea ? did you have the same issue ? – Thibz Jun 27 '17 at 09:03