Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Js cleanup #17

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
<html>
<head>
<title>Switcher</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">

<link href="css/open-sans-condensed-300-700.font" rel="stylesheet" type="text/css">
<link href="css/open-sans-condensed-300-700.font.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="css/jquery.mobile.min.css"><!-- jQuery Mobile 1.4.5 CSS -->

<script src="js/jquery.min.js"></script><!-- jQuery 1.11.3 -->
Expand All @@ -15,7 +16,7 @@
<body>
<div data-role="page" id="index">
<div data-role="header">
<h1>Switcher</h1>
<h1>Switcher</h1>
</div><!-- /header -->

<div role="main" class="ui-content Table" id="switches">
Expand Down
39 changes: 20 additions & 19 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ $(document).ready( function() {
// get the list of switches to poll
var switchesJSON = $
.ajax( { url: 'switches.json', cache: false, dataType: 'json' } )
.fail( function() {
alert( 'Cannot find any switches' );
.fail( function( e, textStatus ) {
alert( 'Cannot find any switches: ' + textStatus );
})
.always( function( data ) {
.done( function( data ) {
// create a section for each switch
$('#switches').html( '<div data-role="collapsible-set" id="switches-set"></div>' );
var menuCollapsed = ( data.switches.length > 1 ? 'true' : 'false' );
Expand All @@ -22,12 +22,13 @@ $(document).ready( function() {
<input type="range" name="slider-fill'+new_id+'" id="slider-fill-'+new_id+'" value="60" min="0" max="300" step="15" data-highlight="true"></div> \
</span></p><table id="infotbl-' + new_id + '"> \
</table><table id="jobtbl-' + new_id + '"></table> \
</div>');
$('#switches-set').collapsibleset().trigger( 'create' );
</div>')
.collapsibleset().trigger( 'create' );

all_switches[new_id] = obj;
all_switches[new_id].id = new_id;
setInterval( UpdateSwitchData( new_id ), 5000 );
UpdateSwitchData( new_id );
setInterval( function() { UpdateSwitchData( new_id ) }, 5000 );
});
});
});
Expand All @@ -43,13 +44,13 @@ function takeAction( url, id ) {
}

function UpdateSwitchData( id ) {
ip = all_switches[id].ip;
var ip = all_switches[id].ip;

var switchinfoJSON = $
.ajax( { url: 'http://' + ip + '/cgi-bin/json.cgi', cache: false, dataType: 'jsonp' } )
.fail( function() {
.fail( function( e, textStatus ) {
$('#imgSignal-' + id).attr( 'src', 'images/wifi_a2.png' );
alert( 'Cannot contact' );
alert( 'Unable to connect: ' + textStatus );
})
.done( function( data ) { // enable switch
//add the data from the device to the array
Expand All @@ -71,17 +72,17 @@ function UpdateSwitchData( id ) {
});

//show network info
$('#infotbl-' + id).empty();
$('#infotbl-' + id).append( '<tr><td>Uptime:</td><td>' + all_switches[id].info.uptime + '</td></tr>' );
$('#infotbl-' + id).append( '<tr><td>IP:</td><td>' + all_switches[id].ip + '</td></tr>' );
$('#infotbl-' + id).append( '<tr><td>MAC:</td><td>' + all_switches[id].info.macaddr + '</td></tr>' );
$('#infotbl-' + id).append( '<tr><td>BSID:</td><td>' + all_switches[id].info.ssid + '</td></tr>' );
$('#infotbl-' + id).append( '<tr><td>Channel:</td><td>' + all_switches[id].info.channel + '</td></tr>' );
$('#infotbl-' + id).append( '<tr><td>Signal:</td><td>' + all_switches[id].info.signal + ' dBm</td></tr>' );
$('#infotbl-' + id).empty()
.append('<tr><td>Uptime:</td><td>' + all_switches[id].info.uptime + '</td></tr>')
.append('<tr><td>IP:</td><td>' + all_switches[id].ip + '</td></tr>')
.append('<tr><td>MAC:</td><td>' + all_switches[id].info.macaddr + '</td></tr>')
.append('<tr><td>BSID:</td><td>' + all_switches[id].info.ssid + '</td></tr>')
.append('<tr><td>Channel:</td><td>' + all_switches[id].info.channel + '</td></tr>')
.append('<tr><td>Signal:</td><td>' + all_switches[id].info.signal + ' dBm</td></tr>');

// show wifi info
$('#right-' + id + ' span').append( '<span>' + all_switches[id].info.ssid + '</span></br>' );
$('#right-' + id + ' span').append( '<span>ch ' + all_switches[id].info.channel + '</span>' );
$('#right-' + id + ' span').append( '<span>' + all_switches[id].info.ssid + '</span></br>' )
.append( '<span>ch ' + all_switches[id].info.channel + '</span>' );

//update switch based on actual reported state
$.getJSON( all_switches[id].links.meta.state + '&callback=?', function( result ) {
Expand Down Expand Up @@ -144,7 +145,7 @@ function slugify( text ) {
return text.toString().toLowerCase()
.replace( /\s+/g, '-' ) // Replace spaces with -
.replace( /[^\w\-]+/g, '' ) // Remove all non-word chars
.replace( /\-\-+/g, '-' ) // Replace multiple - with single -
.replace( /--+/g, '-' ) // Replace multiple - with single -
.replace( /^-+/, '' ) // Trim - from start of text
.replace( /-+$/, '' ); // Trim - from end of text
}