Pizza_boom

indexdotjs

Feb 18th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* set-up server and socket */
  2. const http = require('http');                   // load and initialise http pack
  3. const express = require('express');             // load and initialise expess pack
  4.  
  5. const app = express();                          // create express application
  6.  
  7. const server = http.createServer(app);          // create HTTP server that passes requests to express application
  8. /*
  9. var http = require('http').Server(app);         // initialise app handler
  10. var io = require('socket.io')(http);            // initialise socket.io module
  11. */
  12. const router = express.Router();                // set_up router
  13.     console.log('test');
  14.  
  15. app.get('/', function(req, res){                // make handler getting called at local address
  16.     res.sendFile(__dirname + '/index.html');    // Serve this content to caller when webpage is loaded
  17. });
  18.  
  19. app.get('/imagemap/', function(req, res){               // make handler getting called at local address
  20.     res.sendFile(__dirname + '/image1.png');    // Serve this content to caller when webpage is loaded
  21. });
  22. /*
  23. io.on('connection', function(socket){           // do something when user connects
  24.     socket.on('chat message', function(msg){    // receive message from input
  25.         io.emit('chat message', msg);           // distribute event to all connected systems
  26.     });
  27. });
  28. */
  29.  
  30. // express().use(express.static('/imagemap'));
  31.  
  32. server.listen(3000, function(){                 // Make http server listen on port 3000 for serving content
  33.     console.log('listening on *:3000');         // write message ato console/debugger
  34.     console.log('where the magic is happening');
  35. });
  36.  
  37.  
  38.  
  39. /* set-up socket connection */
  40. const socketIo = require('socket.io');          // set up web socket connections
  41.  
  42. const sio = socketIo(server);                   // Create socket.io server attatched to http server
  43.  
  44. const InputSocket = sio.of('/InputSpace')          
  45.         .on('connection', function (socket) {
  46.             console.log('input connection established');    // tell debugger/logger that connection is established
  47.             socket.on('status', function (data) {           // gets called when cliënt attempt a connection, function listen for events
  48.                 console.log('incomming data: ', data);      // show data on debugger/logger
  49.                 InputSocket.emit ('status', data);          // Transfer input data to cliënt
  50.             });
  51.     });
  52.            
  53. /* ^ Create transferring space for inputs to make cliënt connect to and synchronize cliënts data with existing serverdata ^ */
  54.  
  55. const OutputSocket = sio.of('/OutputSpace')        
  56.         .on('connection', function (socket) {
  57.             console.log('Output connection established');   // tell debugger/logger that connection is established
  58.             socket.on('status', function (data) {           // gets called when cliënt attempt a connection, function listen for events
  59.                 console.log('Transferring data: ', data);       // show data on debugger/logger
  60.                 OutputSocket.emit ('status', data);         // Transfer output data to cliënt
  61.                 OutputPins(data);
  62.             });
  63.     });
  64.            
  65. /* ^ Create transferring space for outputs to make cliënt connect to and synchronize cliënts data with existing serverdata ^ */
  66.  
  67. Inputs = [];
  68. Outputs = [];
  69.  
  70. /* GPIO Handler */
  71.  
  72. const Gpio = require('pigpio').Gpio;                        // initialise pigpio module for further use
  73.  
  74. const GpioInputArray = [0, 1, 2, 3, 4, 5, 6, 7];            // define input array size
  75. const GpioOutputArray = [9, 10, 11, 12, 13, 14, 15];        // define output array size
  76.  
  77. var GpioInputPin = [];                                      // create array variables
  78. var GpioOutputPin = [];                                     // create array variables
  79.  
  80. const button1 = 19;                                         // define input pin for switch1
  81.  
  82. GpioInputArray.map(function (value, key) {                  // creates new array within defined size
  83.     GpioInputPin[1] = new Gpio(button1, {                   // define GpioInputPin[1] as GPIO input, port pulled down and rising and falling edge controlled
  84.                 mode: Gpio.INPUT,
  85.                 pullUpDown: Gpio.PUD_DOWN,
  86.                 edge: Gpio.EITHER_EDGE
  87.             });
  88.     GpioInputPin[1].on('interrupt', function (level) {      // attach interrupt based event handler to GpioInputPin[1] to do routine if gpio state changes
  89.         if (level == 0) {
  90.             Inputs[1] = false;                              // make input state false
  91.         }
  92.         else {
  93.             Inputs[1] = true;                               // make input state true
  94.         }
  95.         InputSocket.emit('status', inputs);                 // transfer changed state to socket
  96.     });
  97. });
  98.  
  99. function OutputPins(data) {
  100.     Outputs = data;
  101.    
  102. }
Advertisement
Add Comment
Please, Sign In to add comment