Skip to content

Instantly share code, notes, and snippets.

@shanehsu
Created March 29, 2017 17:10
Show Gist options
  • Save shanehsu/47bed9c39588e72e9a5237a4027f0dc1 to your computer and use it in GitHub Desktop.
Save shanehsu/47bed9c39588e72e9a5237a4027f0dc1 to your computer and use it in GitHub Desktop.
Updated
let express = require('express');
let router = express.Router();
// route that devices will automatically connect and reqister their current ip:port
router.use('/', (req, res) => {
// Device information object
// {
// serial: <string> product serial number
// mac: <string> mac address of remote device
// ip: <string> ip address
// port: <int> port
// regTime: <Date> time that remote device registered
// belongTo: <string>
// }
// record device information
var deviceInfo = {
serial: 1,
mac: 1,
ip: req.connection.remoteAddress,
port: req.connection.remotePort,
regTime: Date.now(),
belongTo: null
};
console.log(deviceInfo); // log incoming information
// insert into database and return result
let resData;
db.insertDocument('cimpsDB', 'devices', deviceInfo)
.then(() => {
resData = {
result: 1,
message: 'Device register successfully'
};
console.log('Success\n\n');
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(resData));
}, err => {
resData = {
result: 0,
message: err
};
console.log(err);
});
});
//export this router to use in our main.js
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment