Stairs/web/app.js

160 lines
3.1 KiB
JavaScript
Raw Normal View History

var fs = require('fs');
2017-04-16 12:25:36 +00:00
var md5File = require('md5-file');
var express = require('express');
var semverUtils = require('semver-utils')
var client = require('./client');
var httpPort = 3127;
var stairsHost = '10.138.2.25';
var stairsUdpPort = 3126;
2017-04-16 12:25:36 +00:00
var firmwareFile = './update/firmware.bin';
var alwaysUpdate = true;
function requireNoCache(filename)
{
delete require.cache[require.resolve(filename)];
return require(filename);
}
function isNewer(version1, version2)
{
if (alwaysUpdate) return true;
if (version1.major > version2.major) return true;
if (version1.major < version2.major) return false;
if (version1.minor > version2.minor) return true;
if (version1.minor < version2.minor) return false;
if (version1.patch > version2.patch) return true;
if (version1.patch < version2.patch) return false;
2017-04-16 12:25:36 +00:00
if (parseInt(version1.build, 10) > parseInt(version2.build, 10)) return true;
return false;
}
client.init(stairsHost, stairsUdpPort);
var app = express();
app.get('/ping', function(req, res)
{
client.ping(function(data, error)
{
if (error)
res.status(500);
res.send(data);
});
});
app.get('/getMode', function(req, res)
{
client.getMode(function(data, error)
{
if (error)
res.status(500);
res.send(data);
});
});
app.get('/setMode/:mode', function(req, res)
{
client.setMode(req.params.mode, req.query, function(data, error)
{
if (error)
res.status(500);
res.send(data);
});
});
app.get('/getRange', function(req, res)
{
client.getRange(function(data, error)
{
if (error)
res.status(500);
res.send(data);
});
});
app.get('/setRange', function(req, res)
{
client.setRange(req.query, function(data, error)
{
if (error)
res.status(500);
res.send(data);
});
});
app.get('/updateFirmware', function(req, res)
{
client.updateFirmware(req.query, function(data, error)
{
if (error)
res.status(500);
res.send(data);
});
})
app.get('/checkUpdate', function(req, res)
{
if (!fs.existsSync(firmwareFile))
{
console.log('checkUpdate: ' + firmwareFile + ' not found!');
res.sendStatus(304);
return;
}
var version = requireNoCache('./version.js');
var deviceVersion = semverUtils.parse(req.headers['x-esp8266-version']);
var localVersion = semverUtils.parse(version.Version);
console.log('checkUpdate:');
console.log(' Device version = ' + semverUtils.stringify(deviceVersion));
console.log(' Local version = ' + semverUtils.stringify(localVersion));
if (isNewer(localVersion, deviceVersion))
{
2017-04-16 12:25:36 +00:00
console.log('Sending update');
md5File(firmwareFile, function(err, hash)
{
if (err)
{
res.sendStatus(500);
return;
}
res.set('Content-Length', fs.statSync(firmwareFile).size);
res.set('x-MD5', hash);
res.download(firmwareFile);
});
}
else
{
2017-04-16 12:25:36 +00:00
console.log('No update required');
res.sendStatus(304);
}
});
app.use(express.static(__dirname + '/static'));
app.listen(httpPort, function ()
{
console.log('Stairs ReST service running on port ' + httpPort);
});