Merge pull request #49 from rickcarlino/master

May 24th
pull/51/head^2
Rick Carlino 2014-05-24 11:11:59 -07:00
commit 312615afa9
5 changed files with 121 additions and 0 deletions

View File

@ -1,5 +1,6 @@
//= require lodash
//= require foundation/jquery
//= require skynet
//= require foundation/foundation
//= require_tree ./foundation
//= require angular/angular

View File

@ -0,0 +1 @@
angular.module('FarmBot').provider('device', ->)

View File

@ -0,0 +1,57 @@
function skynet (config, cb) {
if (!cb && typeof config === 'function') {
cb = config
config = {}
}
function loadScript(url, callback)
{
// Adding the script tag to the head as suggested before
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
// Then bind the event to the callback function.
// There are several events for cross browser compatibility.
script.onreadystatechange = callback;
script.onload = callback;
// Fire the loading
head.appendChild(script);
}
var authenticate = function() {
var socket = io.connect(config.host || "http://skynet.im", {
port: config.port || 80
});
socket.on('connect', function(){
console.log('Requesting websocket connection to Skynet');
socket.on('identify', function(data){
console.log('Websocket connecting to Skynet with socket id: ' + data.socketid);
//console.log('Sending device uuid: ' + config.uuid);
if (config.uuid && config.token) socket.emit('identity', {uuid: config.uuid, socketid: data.socketid, token: config.token});
else socket.emit('register', config, function (ident) {
config = ident
socket.emit('identity', {uuid: config.uuid, socketid: data.socketid, token: config.token});
console.log(config)
})
});
socket.on('notReady', function(data){
cb(new Error('Authentication Error'), socket);
});
socket.on('ready', function(data){
// cb(null, socket);
cb(null, socket, data);
});
});
};
loadScript("//skynet.im/socket.io/socket.io.js", authenticate);
};

View File

@ -0,0 +1,57 @@
# Devices 1 and 2 are just some SkyNet devices I registered. Go nuts.
window.device1 =
type: "farmbot"
uuid: "713c69b1-e36a-11e3-93f8-f3e7e8d1cce9"
token: "0waw1l97lbwc23xrh0oem7d8rbai3sor"
protocol: "websocket"
window.device2 =
type: "farmbot"
uuid: "77d51a61-e36a-11e3-93f8-f3e7e8d1cce9"
token: "00r57sfqx8orms4i6m28ldac6su7hkt9"
protocol: "websocket"
window.fake_message1 =
devices: device2.uuid
payload:
message_type: 'single_command'
time_stamp: new Date()
command:
action: 'MOVE RELATIVE'
x: 100
y: 0
z: 0
speed: 100
amount: null
delay: 0
window.fake_message2 =
devices: device1.uuid
payload:
message_type: 'single_command'
time_stamp: new Date()
command:
action: 'MOVE RELATIVE'
x: 100
y: 0
z: 0
speed: 100 # Not sure about this one.
amount: null # Is this for "DOSE WATER"?
delay: 0
window.start_skynet = ->
device = prompt 'Which device are you using? 1 or 2'
device = eval("device#{device}")
skynet device, (e, socket) ->
throw e if e
window.skynet_socket = socket
socket.on "message", (message) ->
console.log "message received", message
# # Send and receive messages
# socket.emit "message",
# devices: "0d3a53a0-2a0b-11e3-b09c-ff4de847b2cc"
# payload:
# red: "on"
# , (data) ->
# console.log data

View File

@ -0,0 +1,5 @@
= javascript_include_tag "skynet_experiment"
%h1
DANGER, WARNING!
%p
This page is a sandbox for testing SkyNet connections. Try running the start_skynet() command in the console.