Thu, 04 Jun 2015 14:50:33 +0200
Genesis of lecture sources for Droidcon Berlin 2015 in Postbahnhof.
michael@0 | 1 | /*jslint unparam: true */ |
michael@0 | 2 | |
michael@0 | 3 | /* jshint strict: true, -W097, unused:false */ |
michael@0 | 4 | /*global window, document, d3, $, io, navigator, setTimeout */ |
michael@0 | 5 | |
michael@0 | 6 | //Set the size of the Notifier Circle |
michael@0 | 7 | $("#notifier_circle").width(0.8 * window.innerWidth); |
michael@0 | 8 | $("#notifier_circle").height(0.8 * window.innerWidth); |
michael@0 | 9 | |
michael@0 | 10 | |
michael@0 | 11 | /* |
michael@0 | 12 | Function: validateIP() |
michael@0 | 13 | Parameter: none |
michael@0 | 14 | Description: Attempt to connect to server/Intel IoT platform |
michael@0 | 15 | */ |
michael@0 | 16 | function validateIP() { |
michael@0 | 17 | 'use strict'; |
michael@0 | 18 | var socket, |
michael@0 | 19 | //Get values from text fields |
michael@0 | 20 | ip_addr = $("#ip_address").val(), |
michael@0 | 21 | port = $("#port").val(), |
michael@0 | 22 | script = document.createElement("script"); |
michael@0 | 23 | |
michael@0 | 24 | //create script tag for socket.io.js file located on your IoT platform (development board) |
michael@0 | 25 | script.setAttribute("src", "http://" + ip_addr + ":" + port + "/socket.io/socket.io.js"); |
michael@0 | 26 | document.head.appendChild(script); |
michael@0 | 27 | |
michael@0 | 28 | //Wait 1 second before connecting |
michael@0 | 29 | setTimeout(function () { |
michael@0 | 30 | try { |
michael@0 | 31 | //Connect to Server |
michael@0 | 32 | socket = io.connect("http://" + ip_addr + ":" + port); |
michael@0 | 33 | |
michael@0 | 34 | //Attach a 'connected' event handler to the socket |
michael@0 | 35 | socket.on("connected", function (message) { |
michael@0 | 36 | navigator.notification.alert( |
michael@0 | 37 | 'Welcome', // message |
michael@0 | 38 | "", // callback |
michael@0 | 39 | 'Hi There!', // title |
michael@0 | 40 | 'Ok' // buttonName |
michael@0 | 41 | ); |
michael@0 | 42 | }); |
michael@0 | 43 | |
michael@0 | 44 | //Set all Back button to not show |
michael@0 | 45 | $.ui.showBackButton = false; |
michael@0 | 46 | //Load page with transition |
michael@0 | 47 | $.ui.loadContent("#main", false, false, "fade"); |
michael@0 | 48 | |
michael@0 | 49 | socket.on("message", function (message) { |
michael@0 | 50 | //alert("Is anyone there? "+message); |
michael@0 | 51 | if (message === "present") { |
michael@0 | 52 | $("#notifier_circle").attr("class", "green"); |
michael@0 | 53 | //Update log |
michael@0 | 54 | $("#feedback_log").append(Date().substr(0, 21) + " Someone is Present!<br>"); |
michael@0 | 55 | //Prompt user with Cordova notification alert |
michael@0 | 56 | navigator.notification.alert( |
michael@0 | 57 | 'Someone is Present!', // message |
michael@0 | 58 | "", // callback |
michael@0 | 59 | 'Check Your Door', // title |
michael@0 | 60 | 'Ok' // buttonName |
michael@0 | 61 | ); |
michael@0 | 62 | //Wait 2 seconds then turn back to gray |
michael@0 | 63 | setTimeout(function () { |
michael@0 | 64 | $("#notifier_circle").attr("class", "gray"); |
michael@0 | 65 | }, 3000); |
michael@0 | 66 | } |
michael@0 | 67 | }); |
michael@0 | 68 | } catch (e) { |
michael@0 | 69 | navigator.notification.alert( |
michael@0 | 70 | "Server Not Available!", // message |
michael@0 | 71 | "", // callback |
michael@0 | 72 | 'Connection Error!', // title |
michael@0 | 73 | 'Ok' // buttonName |
michael@0 | 74 | ); |
michael@0 | 75 | } |
michael@0 | 76 | }, 1000); |
michael@0 | 77 | } |