Skip to content

Instantly share code, notes, and snippets.

@mdobson
Last active August 29, 2015 14:04
Show Gist options
  • Save mdobson/7796792f8aa7d318d0c2 to your computer and use it in GitHub Desktop.
Save mdobson/7796792f8aa7d318d0c2 to your computer and use it in GitHub Desktop.
New iphone accel page
<!doctype html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(function() {
var x, y, z;
window.ondevicemotion = function(event) {
x = event.accelerationIncludingGravity.x;
y = event.accelerationIncludingGravity.y;
z = event.accelerationIncludingGravity.z;
$('#x').html(x);
$('#y').html(y);
$('#z').html(z);
};
function setupChange(link) {
setInterval(function() {
$.ajax({
url: link,
type: 'POST',
data: { action: 'change', x: x, y: y, z: z }
});
}, 2000);
}
function pair() {
$.ajax({
url: 'http://192.168.1.14:3000/',
type: 'GET',
}).done(function(data){
for(i = 0; i < data.links.length; i++) {
var link = data.links[i];
if(link.title === 'local') {
local = link.href;
}
}
if(local){
$.ajax({
url: local+'/devices',
type: 'POST',
data: 'type=iphone',
statusCode: {
201: function() {
console.log('201');
$.ajax({
url: local,
type: 'GET'
}).done(function(data) {
for(i = 0; i < data.entities.length; i++) {
var entity = data.entities[i];
if(entity.properties.type === 'iphone') {
for(j = 0; j < entity.links.length; j++) {
var link = entity.links[j];
console.log(link);
if(link.rel[0] === 'self') {
setupChange(link.href);
}
}
}
}
});
}
}
}).done(function(){
})
}
});
}
$('#pair').click('click', pair);
});
</script>
</head>
<body>
Hello, world!<br/>
What's my name?
<form>
<input type='text' id='name'/>
<input type='button' value='Pair me!' id='pair'/>
</form>
<div>
X: <span id="x"></span>
</div>
<div>
Y: <span id="y"></span>
</div>
<div>
Z: <span id="z"></span>
</div>
</body>
</html>
var Device = require('../../../../zetta_runtime.js').Device;
var util = require('util');
var Phone = module.exports = function() {
Device.call(this);
this.x = 0;
this.y = 0;
this.z = 0;
};
util.inherits(Phone, Device);
Phone.prototype.init = function(config) {
config
.state('on')
.type('iphone')
.when('off', { allow: 'change' })
.when('on', { allow: 'change' })
.map('change', this.change,
[{ name: 'x', type: 'text' }, { name: 'y', type: 'text' }, { name: 'z', type: 'text' }])
.monitor('x')
.monitor('y')
.monitor('z');
};
Phone.prototype.change = function(x, y, z, cb) {
this.x = x;
this.y = y;
this.z = z;
if (cb) {
cb();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment