PageRenderTime 19ms CodeModel.GetById 12ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/srv.socket.js

https://github.com/thomasdavis/noduino
JavaScript | 98 lines | 60 code | 13 blank | 25 comment | 23 complexity | b653973b8bbe24ab06ed6e449954d3d6 MD5 | raw file
 1/**
 2 * srv.web.js – Handling Socket.IO Requests
 3 * This file is part of noduino (c) 2012 Sebastian Müller <c@semu.mp>
 4 *
 5 * @package     noduino
 6 * @author      Sebastian Müller <c@semu.mp>
 7 * @license     MIT License – http://www.opensource.org/licenses/mit-license.php 
 8 * @url         https://github.com/semu/noduino
 9 */
10 
11define(['socket.io', 'public/scripts/libs/Noduino', 'public/scripts/libs/Noduino.Serial', 'public/scripts/libs/Logger'], function(io, Noduino, Connector, Logger) {
12  
13  /**
14   * Define SocketHandler
15   * @param object socket Socket.IO
16   */
17  var SocketHandler = function(socket) {
18    this.sockets  = {};
19    this.arduinos = {};
20    this.handler  = socket.listen(8090);
21    this.handler.set('origin', '*');
22    this.pinCache = {};
23    
24    this.bindings();
25  };
26  
27  /**
28   * Get selected Arduino
29   */
30  SocketHandler.prototype.current = function() {
31    return this.arduinos[0];
32  };
33  
34  /**
35   * Connect Bindings
36   */
37  SocketHandler.prototype.bindings = function() {
38    var io = this.handler, that = this;
39
40    io.sockets.on('connection', function(socket) {
41      that.sockets[socket.id] = socket;
42
43      /**
44       * Incoming Serial Request
45       */
46      that.sockets[socket.id].on('serial', function(data) {
47        switch (data.type) {
48          case 'write':
49            that.current().write(data.write);
50            break;
51          case 'analogRead':
52            var curPin = data.pin;
53            that.current().watchAnalogIn({'pin': data.pin}, function(m) {
54              if (!m.pin || m.pin == null || m.pin == NaN) {
55                return; }
56              
57              if (m.state != that.pinCache[m.pin] && curPin == m.pin) {
58                socket.emit('response', {'type': 'analogRead', 'pin': m.pin, 'value': m.state});
59                that.pinCache[m.pin] = m.state;
60              }
61            });
62          break;
63          case 'digitalRead':
64            var curPin = data.pin;
65            that.current().watchDigitalIn({'pin': data.pin}, function(m) {
66              if (!m.pin || m.pin == null || m.pin == NaN) {
67                return; }
68              
69              if (m.state != that.pinCache[m.pin] && curPin == m.pin) {
70                socket.emit('response', {'type': 'digitalRead', 'pin': m.pin, 'value': m.state});
71                that.pinCache[m.pin] = m.state;
72              }
73            });
74          break;
75        }
76      });
77
78      /**
79       * Connect to Arduino
80       */      
81      that.sockets[socket.id].on('board.connect', function(data) {
82        if (that.current() && that.current().connected == true) {
83          return socket.emit('response', {'msg': 'board.connect', 'response': 'ready'}); }
84        
85        that.arduinos[0] = new Noduino({'debug': true}, Connector, Logger);
86        that.current().connect(function(err, board) {
87          that.current().connected = false;
88          if (err) { return socket.emit('response', {'msg': 'board.connect', 'response': 'failed'}); }
89          
90          that.current().connected = true;
91          return socket.emit('response', {'msg': 'board.connect', 'response': 'ready'});
92        });
93      });
94    });
95  };
96
97  return new SocketHandler(io, Noduino, Connector, Logger);
98});