// modules 
import express from 'express'
import BodyParser from 'body-parser'
import http from 'http'
import _ from 'underscore'

// routes
// import index from './routes/index'

import rpio from 'rpio'

const pins = {
	'drive': {
		'forward': 11,
		'backward': 13,
		'left': 12,
		'right': 18
	}
}

// open gpio pins
rpio.open(12, rpio.OUTPUT, rpio.LOW)
rpio.open(18, rpio.OUTPUT, rpio.LOW)

// sockets
import socketio from 'socket.io'

const app = express()

app.use(BodyParser.json())

//enable cors
app.all('*', function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS')
  res.header('Access-Control-Allow-Headers', 'Content-Type')
  next()
})

app.use('/public', express.static(__dirname + '/public'))

// app.use('/', index);

//setup io
var server = http.createServer(app)

var sio = socketio(server)

// io.on('connection', socketioJwt.authorize({
// 	secret: db.secret,
// 	handshae: true
// }))
sio.on('connection', function (socket) {
	/*
		data:
			direction (up, down, left, right)
			speed (1, 0)
	 */
	socket.on('drive', function (data) {
		if (!process.env.production) {
			console.log(data)
		}
		const speed = (data['speed']) ? rpio.HIGH : rpio.LOW
		const pin = pins.drive[data['direction']]

		rpio.write(pin, speed)
	})

	setInterval(function () {
		socket.emit('helloworld', 'hello world')
	}, 2000)
})

server.listen(3000, function () {
	console.log('app is listening on port 3000')
})

export default app

@@ -297,20 +297,19 @@
 %7D%0A%7D%0A%0A// 
-open
+set
  gpio pi
@@ -310,16 +310,40 @@
 pio pins
+ as output%0A// left/right
 %0Arpio.op
@@ -410,16 +410,111 @@
 o.LOW)%0A%0A
+// forward/backward%0Arpio.open(11, rpio.OUTPUT, rpio.LOW)%0Arpio.open(13, rpio.OUTPUT, rpio.LOW)%0A%0A
 // socke
@@ -1421,16 +1421,59 @@
 ion'%5D%5D%0A%0A
+%09%09console.log(pin);%0A%09%09console.log(speed);%0A%0A
 %09%09rpio.w

update server, change ip