Files
DataLogger-Generic/www/app.coffee
2016-04-13 17:17:15 -05:00

102 lines
3.8 KiB
CoffeeScript

express = require('express')
path = require('path')
fs = require('fs')
logger = require('morgan')
methodOverride = require('method-override')
bodyParser = require('body-parser')
errorHandler = require('errorhandler')
app = express()
fns = undefined
app.locals.DB_TYPE = 'MySQL'
# or "MySQL"
###*
* Configuration
###
if app.locals.DB_TYPE == 'MySQL'
fns = require('./functions_MySQL.coffee')
mysql = require('mysql')
db_config =
host: 'localhost'
user: 'website'
password: 'henrypump'
database: 'poconsole'
app.locals.pool = mysql.createPool(db_config)
# handleDisconnect = ->
# console.log 'Handling db disconnect gracefully'
# app.locals.db = mysql.createConnection(db_config)
# app.locals.db.connect (err) ->
# if err
# console.log 'error when connecting to db:', err
# setTimeout handleDisconnect, 2000
# return
# app.locals.db.on 'error', (err) ->
# console.log 'db error', err
# if err.code == 'PROTOCOL_CONNECTION_LOST'
# handleDisconnect()
# else
# throw err
# return
# return
# handleDisconnect()
else
fns = require('./functions_SQLite.coffee')
app.set 'port', process.env.PORT or 80
app.set 'views', path.join(__dirname, 'views')
app.engine '.html', require('ejs').renderFile
app.set 'view engine', 'html'
#app.use(favicon(__dirname + '/public/img/favicon.ico'));
app.use logger('dev')
app.use methodOverride()
app.use bodyParser.json()
app.use bodyParser.urlencoded(extended: true)
#app.use(express["static"](path.join(__dirname, 'public')));
app.use express.static(__dirname + '/public')
app.use '/bower_components', express.static(__dirname + '/bower_components')
app.use '/node_modules', express.static(__dirname + '/node_modules')
###*
* Routes
###
angular = (req, res) ->
res.render 'angularIndex'
return
app.post '/json/tag/add', fns.createTag # Adds a tag to the scan list
app.post '/json/tag/update/', fns.updateTag # Updates tag data
app.get '/json/tag/delete/:tag', fns.deleteTag # Removes a tag from the scan list
app.get '/json/tag/:id', fns.getTag # Gets a specific tag in the scan list
app.get '/json/tag', fns.getAllTags # Lists all tags in the scan list
app.get '/json/val/:tag', fns.latestValueSingleTag # Gets the latest value of a single tag
app.get '/json/series/:tag/:hours', fns.seriesTagValues # Gets all the values of a tag for the last X hours
app.get '/json/valBetween/:tag/:startDatetime/:endDatetime', fns.seriesTagValuesBetween # Gets the values of a tag between the start time and end time
app.get '/json/CSV/all', fns.allDataCSV # Gets a CSV of all values stored
app.get '/json/CSV/:tag/:startDatetime/:endDatetime', fns.seriesCSVBetween # Gets a CSV of the values of a tag between the start time and end time
app.get '/json/CSV/:tag/:hours', fns.seriesCSV # Gets a CSV of the values of a tag for the last x hours
app.get '/json/all', fns.latestValueAllTags # Gets the latest values of all tags in the scan list
app.get '/json/config', fns.getSetup # Gets the contents of the config table
app.post '/json/config', fns.updateSetup # Adds a new parameter to the config table
app.get '/json/logger/status', fns.checkLoggerStatus # Gets the status of the data logger
app.get '/json/logger/restart', fns.restartLogger # Restarts the data logger
app.get '/json/clearDatabase/all', fns.clearValues # Removes all tag values from the database
app.get '/json/clearDatabase/:id', fns.clearValues # Removes tag values from the database
app.get '*', angular
###*
* Start Server
###
connectionsArray = []
s_port = 3000
server = app.listen(s_port, ->
host = server.address().address
port = server.address().port
console.log 'POConsole listening at http://%s:%s', host, port
return
)