Skip to content

Instantly share code, notes, and snippets.

@DaveTD
Last active December 23, 2015 23:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DaveTD/6711158 to your computer and use it in GitHub Desktop.
Save DaveTD/6711158 to your computer and use it in GitHub Desktop.
Gist for NightWatch

About

This is the gist for NightWatch - a somewhat incomplete video security management widget. I've included some stuff that sort of works, but doesn't really, that was originally meant to let the management widget control adding, removing and altering or freezing the pattern that the video feeds take.

Sadly, mjpgs aren't the nicest things, block ajax calls and generally just slow everything down.

The urls I've taken are from opentopia.com (http://www.opentopia.com/hiddencam.php?showmode=animated&country=Canada&seewhat=newest), please look at their disclaimers for legal information.

You'll probably want to use Chrome as your browser for this - mjpgs don't do so well in large numbers in firefox... and so everything ends up looking like garbage and won't load fast, and some mjpgs won't appear etc. In general, the page will load more or less in firefox, but it's just so much better in chrome.

Setup

To handle incoming data, you'll need one widget like so:

`

  • `

    The y size doesn't really matter. It might have at some point if we can get ajax to start working, but it really doesn't right now.

    For each screen, you'll want a widget like this:

    `

  • <iframe src="/nightwatch/widgetBody?start=0" scrolling="no" frameborder=0 style="width:100%;height:100%;"></iframe>

  • `

    The iframe calls for an image container. The x and y sizes are up to you. If you look at the src for the iframe, you'll see a piece which says "start=0", that number represents the number in the array of images sent that will be displayed in this iframe. You can have multiple frames showing the same image, and possibly images sent that don't have a frame.

    Make sure the files go into their correct folders - gist doesn't like slashes in file names, so those have been replaced by dashes with surrounding spaces.

    <div class="gridster">
    <ul>
    <li data-row="1" data-col="1" data-sizex="2" data-sizey="1">
    <iframe src="/nightwatch/widgetBody?start=0" scrolling="no" frameborder=0 style="width:100%;height:100%;"></iframe>
    </li>
    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
    <iframe src="/nightwatch/widgetBody?start=1" scrolling="no" frameborder=0 style="width:100%;height:100%;"></iframe>
    </li>
    <li data-row="1" data-col="1" data-sizex="1" data-sizey="2">
    <div data-id="nightwatch" data-view="Nightwatch"></div>
    </li>
    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
    <iframe src="/nightwatch/widgetBody?start=2" scrolling="no" frameborder=0 style="width:100%;height:100%;"></iframe>
    </li>
    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
    <iframe src="/nightwatch/widgetBody?start=3" scrolling="no" frameborder=0 style="width:100%;height:100%;"></iframe>
    </li>
    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
    <iframe src="/nightwatch/widgetBody?start=4" scrolling="no" frameborder=0 style="width:100%;height:100%;"></iframe>
    </li>
    </ul>
    </div>
    <% idnum = 0 %>
    <% test.each do |text| %>
    <img class="<%= start %>" id="<%= idnum %>" src="<%= text %>" style="width:100%;"/>
    <% idnum = idnum + 1 %>
    <br>
    <% end %>
    class NightWatch
    def initialize
    @urls = Array.new
    @state = ""
    end
    def rotate
    @urls << @urls.first
    @urls.shift
    writeUrls
    end
    def shuffle
    @urls = @urls.shuffle
    writeUrls
    end
    def freeze
    #do nothing
    end
    def nextMove
    getInfo
    if @state == "rotate" or @state == "shuffle" or @state == "freeze"
    eval("#{@state}()")
    else
    #puts "Invalid state found. Rotating."
    rotate
    end
    end
    def writeUrls
    writeString = ""
    @urls.each { |e| writeString << "#{e}" }
    a = File.open('lib/nightwatch/urls.txt', 'w')
    a.syswrite(writeString)
    a.close
    end
    def getInfo
    @state = IO.readlines('lib/nightwatch/state.txt').first
    @urls = IO.readlines('lib/nightwatch/urls.txt')
    end
    def delimitedUrls
    return @urls
    end
    end
    class NightWatchEngage
    theList = NightWatch.new
    SCHEDULER.every '15s', :first_in => 0 do |job|
    theList.nextMove
    send_event('nightwatch', length: theList.delimitedUrls.length)
    puts theList.delimitedUrls.first
    end
    end
    load 'jobs/night_watch_receiver.rb'
    NightWatchEngage.new
    if !File.directory? 'lib'
    Dir.mkdir('lib')
    end
    if !File.directory? 'lib/nightwatch'
    Dir.mkdir('lib/nightwatch')
    end
    if !File.exists? 'lib/nightwatch/state.txt'
    a = File.new('lib/nightwatch/state.txt', 'w')
    a.close
    end
    if !File.exists? 'lib/nightwatch/urls.txt'
    a = File.new('lib/nightwatch/urls.txt', 'w')
    a.close
    end
    get '/nightwatch/addUrl' do
    puts params[:url]
    urlFile = File.open("lib/nightwatch/urls.txt", "a")
    urlFile.syswrite("#{params[:url]}\r\n")
    urlFile.close
    end
    get '/nightwatch/removeUrl' do
    # read the list into an array
    # remove the url from the arry, if it exists
    # write the new array to the list file
    puts params[:url]
    urls = IO.readlines('lib/nightwatch/urls.txt')
    urls.delete("#{params[:url]}\r\n")
    writeString = ""
    urls.each { |e| writeString << "#{e}\r\n" }
    urlFile = File.open("lib/nightwatch/urls.txt", "w")
    urlFile.syswrite(writeString)
    urlFile.close
    end
    get '/nightwatch/widgetBody' do
    urls = IO.readlines('lib/nightwatch/urls.txt')
    erb :nightwatchbody, :locals => { :test => urls, :start => params[:start] }
    end
    get '/alterState/:state' do
    puts "test new state: #{params[:state]}"
    # screw checking if the file exists, just overwrite the state to /lib/nightwatch/state.txt
    # only take the first word in the url - we don't want to eval just anything
    newState = params[:state].split(" ").first
    stateFile = File.open("lib/nightwatch/state.txt", "w")
    stateFile.syswrite(newState)
    stateFile.close
    end
    http://209.17.151.205/mjpg/video.mjpg
    http://209.17.151.206/mjpg/video.mjpg
    http://204.50.231.165/anony/mjpg.cgi
    http://209.17.151.204/mjpg/video.mjpg
    http://24.222.113.226:8082/cgi-bin/faststream.jpg?stream=full
    root = exports ? this
    class Dashing.Nightwatch extends Dashing.Widget
    ready: ->
    $("#addbtn").click ->
    sendAjax("add", $("#newUrl").val())
    return
    $("#removebtn").click ->
    sendAjax("remove", $("#removeUrl").val())
    return
    $("#freeze").click ->
    sendAjax("freeze", null)
    return
    shows()
    return
    onData: (data) ->
    sort(data.length)
    shows()
    sort = (length) ->
    $("iframe").contents().find("img").each ->
    imgid = $(this).attr('id')
    thisid = parseInt(imgid, 10)
    thisid = thisid + 1
    if thisid is length
    thisid = parseInt(0, 10)
    $(this).attr('id', thisid)
    shows = () ->
    $("iframe").contents().find("img").each ->
    if $(this).attr('id') isnt $(this).attr('class')
    $(this).hide()
    else
    $(this).prependTo($(this).parent())
    $(this).show()
    root.sendAjax = (action, words) ->
    switch action
    when "add"
    url = "/nightwatch/addUrl?url=#{words}"
    when "remove"
    url = "/nightwatch/removeUrl?url=#{words}"
    when "freeze"
    url = "/nightwatch/freeze"
    #create the message
    $.ajax
    async: true
    url: "#{url}"
    type: 'GET'
    dataType: 'json'
    error: (jqXHR, textStatus, errorThrown) ->
    $("body").append "failure"
    success: (data, textStatus, jqXHR) ->
    $("body").append "success"
    <!--<form name="newurlinput" action="#">
    Add Url:<br>
    <input type="text" id="newUrl">
    <br>
    <input type="submit" id="addbtn" value="add">
    </form>
    <form name="removeurl" action"#">
    Remove Url:<br>
    <input type="text" id="removeUrl">
    <br>
    <input type="submit" id="removebtn" value="remove" >
    </form>
    <form>
    Freeze: <input type="checkbox" id="freeze">
    <br>
    </form>-->
    Nightwatch cycling...
    <br><br>
    Add more cameras in /lib/nightwatch/urls.txt!
    .widget-nightwatch {
    background: #4B4B4B;
    }
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment