Skip to content

Commit

Permalink
Changing /actions to /thngs/actions to accomodate new security restri…
Browse files Browse the repository at this point in the history
…ction on EVRYTHNG
  • Loading branch information
domguinard committed May 28, 2018
1 parent 1aa9dc6 commit 70d419a
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions chapter7-implementation/part3-cloud/client/plug.html
Expand Up @@ -32,7 +32,7 @@
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
console.log(" Loading geolocation")
} else {
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
Expand Down Expand Up @@ -130,9 +130,9 @@ <h5>Response</h5>
<div class="panel-heading">
<h4 class="panel-title">DEBUG (Show HTTP Response)</h4>
</div>
<div id="response" class="panel-body">
<pre></pre>
</div>
<div id="response" class="panel-body">
<pre></pre>
</div>
</div>
</div>

Expand Down Expand Up @@ -207,11 +207,11 @@ <h3>Properties</h3>

x = d3.scale.linear()
.domain([0, n - 1])
.range([0, width]);
.range([0, width]);

// Removes the existing graph
svg.selectAll("*").remove();


// Redraws the existing graph
svg.attr("width", width + margin.left + margin.right)
Expand Down Expand Up @@ -243,7 +243,7 @@ <h3>Properties</h3>

}

d3.select(window).on('resize', resize);
d3.select(window).on('resize', resize);

function updateCharts(key, value, timestamp) {

Expand Down Expand Up @@ -285,51 +285,51 @@ <h3>Properties</h3>

<script>
var url = 'wss://ws.evrythng.com:443/thngs/'+thngId+'/properties?access_token='+key;
var socket = new WebSocket(url); //#A
var socket = new WebSocket(url); //#A

socket.onmessage = function (message) { //#B
var content = JSON.parse(message.data);
console.log('Property update : ', content[0]); //#C
if (content[0].key=="power"){
updateCharts(content[0].key, content[0].value, content[0].timestamp); //#D
}
$('#value-'+content[0].key).html( content[0].value ); //#E
$('#value-'+content[0].key).html( content[0].value ); //#E
};

socket.onerror = function (error) {
console.log('An error occurred while trying to connect to a WebSocket!');
console.log(error);
};
//#A Subscribes to all properties of this Thng (via secure WebSockets)
//#B Each time we receive a WS message, this will be called
//#A Subscribes to all properties of this Thng (via secure WebSockets)
//#B Each time we receive a WS message, this will be called
//#C We parse the message content (note: it's an array!)
//#D Call this to update the power graph (function not shown)
//#E Use the property name to update the badge
//#E Use the property name to update the badge
</script>

<script>

function sendAction(type,value){
function sendAction(type,value){
console.log(" You are here: ("+latitude+","+longitude+")");

$.ajax({
url: 'https://api.evrythng.com/actions/'+type+'?access_token='+key,//#A
$.ajax({
url: 'https://api.evrythng.com/thngs/'+thngId+'/actions/'+type+'?access_token='+key,//#A
dataType: 'json', //#B
type: 'post', //#B
contentType: 'application/json', //#B
data: JSON.stringify({"type": type, "thng": thngId, "customFields":value,"location":{"position":{"type": "Point","coordinates": [longitude, latitude]}},"locationSource":"sensor"}), //#C
processData: false,
processData: false,
success: function( data, textStatus, jQxhr ){ //#D
$('#response pre').html( JSON.stringify( data ) );
},
error: function( jqXhr, textStatus, errorThrown ){//#E
console.log( errorThrown );
}
$('#response pre').html( JSON.stringify( data ) );
},
error: function( jqXhr, textStatus, errorThrown ){//#E
console.log( errorThrown );
}
});
}
//#A The custom actions endpoint in EVRYTHNG
//#B We POST a JSON document
//#C The payload to POST
//#C The payload to POST
//#D If successful, update the "response" html element
//#E If an error, display in the console

Expand Down Expand Up @@ -357,4 +357,4 @@ <h3>Properties</h3>



</body></html>
</body></html>

0 comments on commit 70d419a

Please sign in to comment.