PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms 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. define(['socket.io', 'public/scripts/libs/Noduino', 'public/scripts/libs/Noduino.Serial', 'public/scripts/libs/Logger'], function(io, Noduino, Connector, Logger) {
  11. /**
  12. * Define SocketHandler
  13. * @param object socket Socket.IO
  14. */
  15. var SocketHandler = function(socket) {
  16. this.sockets = {};
  17. this.arduinos = {};
  18. this.handler = socket.listen(8090);
  19. this.handler.set('origin', '*');
  20. this.pinCache = {};
  21. this.bindings();
  22. };
  23. /**
  24. * Get selected Arduino
  25. */
  26. SocketHandler.prototype.current = function() {
  27. return this.arduinos[0];
  28. };
  29. /**
  30. * Connect Bindings
  31. */
  32. SocketHandler.prototype.bindings = function() {
  33. var io = this.handler, that = this;
  34. io.sockets.on('connection', function(socket) {
  35. that.sockets[socket.id] = socket;
  36. /**
  37. * Incoming Serial Request
  38. */
  39. that.sockets[socket.id].on('serial', function(data) {
  40. switch (data.type) {
  41. case 'write':
  42. that.current().write(data.write);
  43. break;
  44. case 'analogRead':
  45. var curPin = data.pin;
  46. that.current().watchAnalogIn({'pin': data.pin}, function(m) {
  47. if (!m.pin || m.pin == null || m.pin == NaN) {
  48. return; }
  49. if (m.state != that.pinCache[m.pin] && curPin == m.pin) {
  50. socket.emit('response', {'type': 'analogRead', 'pin': m.pin, 'value': m.state});
  51. that.pinCache[m.pin] = m.state;
  52. }
  53. });
  54. break;
  55. case 'digitalRead':
  56. var curPin = data.pin;
  57. that.current().watchDigitalIn({'pin': data.pin}, function(m) {
  58. if (!m.pin || m.pin == null || m.pin == NaN) {
  59. return; }
  60. if (m.state != that.pinCache[m.pin] && curPin == m.pin) {
  61. socket.emit('response', {'type': 'digitalRead', 'pin': m.pin, 'value': m.state});
  62. that.pinCache[m.pin] = m.state;
  63. }
  64. });
  65. break;
  66. }
  67. });
  68. /**
  69. * Connect to Arduino
  70. */
  71. that.sockets[socket.id].on('board.connect', function(data) {
  72. if (that.current() && that.current().connected == true) {
  73. return socket.emit('response', {'msg': 'board.connect', 'response': 'ready'}); }
  74. that.arduinos[0] = new Noduino({'debug': true}, Connector, Logger);
  75. that.current().connect(function(err, board) {
  76. that.current().connected = false;
  77. if (err) { return socket.emit('response', {'msg': 'board.connect', 'response': 'failed'}); }
  78. that.current().connected = true;
  79. return socket.emit('response', {'msg': 'board.connect', 'response': 'ready'});
  80. });
  81. });
  82. });
  83. };
  84. return new SocketHandler(io, Noduino, Connector, Logger);
  85. });