Skip to content

Instantly share code, notes, and snippets.

@deversmann
Last active May 1, 2022 19:46
Show Gist options
  • Save deversmann/f4dee0bb7dfec1d63f6de3338856d5c7 to your computer and use it in GitHub Desktop.
Save deversmann/f4dee0bb7dfec1d63f6de3338856d5c7 to your computer and use it in GitHub Desktop.
whos_home_widget

whos_home_widget: Smashing Dashboard Widget listing Who's Home


Contents of this gist licensed as follows:

MIT License

Copyright (c) 2021 Damien Eversmann

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

class Dashing.WhosHomeWidget extends Dashing.Widget
ready: ->
$('.whos_home_widget').css('vertical-align','top')
onData: (data) ->
<h3 class="title">Who's Home?</h3>
<table class="list-nostyle presence">
<tr><th style="width: 50%">IN</th><th>OUT</th></tr>
<tr>
<td>
<ul data-id="res_in" data-foreach-item="res_in">
<li data-bind="item"></li>
</ul>
</td>
<td>
<ul data-id="res_out" data-foreach-item="res_out">
<li data-bind="item"></li>
</ul>
</td>
</tr>
</table>
# encoding: UTF-8
require 'open3'
require 'json'
# json-formatted dictionary or residents and mac-addresses, eg. '{"John":"de:ad:00:be:ef:00","Jane":"12:34:56:78:90:ab"}'
residents = JSON.parse(ENV["WHOS_HOME_RESIDENTS"])
# fully qualified CIDR of local network, eg. "192.168.1.0/24"
network = ENV["WHOS_HOME_NETWORK"]
SCHEDULER.every '60s', :first_in => 0 do |job|
res_in = []
res_out = []
stdout, stderr, status = Open3.capture3("nmap -sP #{network}")
stdout, stderr, status = Open3.capture3("arp -an")
residents.keys.each do |resident|
needle = residents[resident]
if /#{needle}/ =~ stdout
res_in.push resident
else
res_out.push resident
end
end
send_event('whos_home_widget', { res_in: res_in, res_out: res_out })
end
$background-color: #dc5945;
.whos_home_widget {
background-color: $background-color;
padding-left: 5px;
padding-right: 5px;
.updated-at {
color: rgba(0, 0, 0, 0.7);
}
.logo {
background-color: transparent;
float: right;
padding-top: 0;
padding-bottom: 5px;
}
.logo img {
float: right;
}
table.presence {
font-size: 24px;
}
table.presence td {
border-top: white solid 2px;
vertical-align: top;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment