mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-26 13:48:53 +08:00
Add device schema mock-up
This commit is contained in:
parent
a2d57cae38
commit
9d8004ee4c
123
app/models/device.js
Normal file
123
app/models/device.js
Normal file
@ -0,0 +1,123 @@
|
||||
/**
|
||||
* Dependencies
|
||||
*/
|
||||
var mongoose = require('mongoose');
|
||||
|
||||
|
||||
/**
|
||||
* Private variables and functions
|
||||
*/
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
|
||||
/**
|
||||
* Exports
|
||||
*/
|
||||
var DeviceSchema = new Schema({
|
||||
name: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
description: {
|
||||
type: String,
|
||||
required: false
|
||||
},
|
||||
creator: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
owner: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
group: {
|
||||
type: Array,
|
||||
default: []
|
||||
},
|
||||
deviceId: {
|
||||
type: String,
|
||||
required: true,
|
||||
index: true,
|
||||
match: /^[0-9a-f]{10}$/
|
||||
},
|
||||
apiKey: {
|
||||
type: String,
|
||||
required: true,
|
||||
index: true
|
||||
},
|
||||
createdAt: {
|
||||
type: Date,
|
||||
index: true,
|
||||
default: Date.now
|
||||
},
|
||||
isPublic: {
|
||||
type: Boolean,
|
||||
index: true,
|
||||
default: false
|
||||
},
|
||||
online: {
|
||||
type: Boolean,
|
||||
index: true,
|
||||
default: false
|
||||
},
|
||||
lastSeen: {
|
||||
type: Date
|
||||
},
|
||||
updatedAt: {
|
||||
type: Date
|
||||
},
|
||||
manufacturerId: {
|
||||
type: String,
|
||||
required: false,
|
||||
index: true,
|
||||
match: /^[0-9a-f]{10}$/
|
||||
},
|
||||
serialNumber: {
|
||||
type: String,
|
||||
required: false,
|
||||
index: true,
|
||||
match: /^[0-9a-f]{10}$/
|
||||
},
|
||||
productId: {
|
||||
type: String,
|
||||
required: false,
|
||||
index: true,
|
||||
match: /^[0-9a-f]{10}$/
|
||||
},
|
||||
activationCode: {
|
||||
type: String,
|
||||
required: false,
|
||||
index: true,
|
||||
match: /^[0-9a-f]{10}$/
|
||||
},
|
||||
deviceLocation: {
|
||||
type: String,
|
||||
required: false,
|
||||
index: true,
|
||||
match: /^[0-9a-f]{10}$/
|
||||
},
|
||||
firmwareVersion: {
|
||||
type: String,
|
||||
required: false,
|
||||
index: true,
|
||||
match: /^[0-9a-f]{10}$/
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
DeviceSchema.static('exists', function (apikey, deviceid, callback) {
|
||||
this.where({ apiKey: apikey, deviceId: deviceid }).findOne(callback);
|
||||
});
|
||||
|
||||
DeviceSchema.static('getDeviceByDeviceId', function (deviceid, callback) {
|
||||
this.where({ deviceId: deviceid }).findOne(callback);
|
||||
});
|
||||
|
||||
DeviceSchema.static('getDevicesByApikey', function (apikey, callback) {
|
||||
this.where('apiKey', apikey).find(callback);
|
||||
});
|
||||
|
||||
|
||||
module.exports = mongoose.model('Device', DeviceSchema);
|
||||
|
@ -1,10 +0,0 @@
|
||||
// app/models/thing.js
|
||||
|
||||
var mongoose = require('mongoose');
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
var ThingSchema = new Schema({
|
||||
name: String
|
||||
});
|
||||
|
||||
module.exports = mongoose.model('Thing', ThingSchema);
|
85
app/routes/devices.js
Normal file
85
app/routes/devices.js
Normal file
@ -0,0 +1,85 @@
|
||||
var express = require('express');
|
||||
var router = express.Router(); // get an instance of the express Router
|
||||
|
||||
var Device = require('../models/device');
|
||||
|
||||
// on routes that end in /devices
|
||||
// ----------------------------------------------------
|
||||
router.route('/')
|
||||
|
||||
// create a devices (accessed at POST http://localhost:8080/devices)
|
||||
.post(function(req, res) {
|
||||
|
||||
var device = new Device(); // create a new instance of the Bear model
|
||||
device.name = req.body.name; // set the device's name (comes from the request)
|
||||
|
||||
// save the device and check for errors
|
||||
device.save(function(err) {
|
||||
if (err)
|
||||
res.send(err);
|
||||
|
||||
res.json({ message: 'Device created!' });
|
||||
});
|
||||
|
||||
})
|
||||
|
||||
// get all the devices (accessed at GET http://localhost:8080/devices)
|
||||
.get(function(req, res) {
|
||||
Device.find(function(err, devices) {
|
||||
if (err)
|
||||
res.send(err);
|
||||
|
||||
res.json(devices);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// on routes that end in /devices/:device_id
|
||||
// ----------------------------------------------------
|
||||
router.route('/:device_id')
|
||||
|
||||
// get the device with that id (accessed at GET http://localhost:8080/devices/:device_id)
|
||||
.get(function(req, res) {
|
||||
Device.findById(req.params.device_id, function(err, device) {
|
||||
if (err)
|
||||
res.send(err);
|
||||
res.json(device);
|
||||
});
|
||||
})
|
||||
|
||||
// update the device with this id (accessed at PUT http://localhost:8080/devices/:device_id)
|
||||
.put(function(req, res) {
|
||||
|
||||
// use our device model to find the device we want
|
||||
Device.findById(req.params.device_id, function(err, device) {
|
||||
|
||||
if (err)
|
||||
res.send(err);
|
||||
|
||||
device.name = req.body.name; // update the devices info
|
||||
|
||||
// save the device
|
||||
device.save(function(err) {
|
||||
if (err)
|
||||
res.send(err);
|
||||
|
||||
res.json({ message: 'Device updated!' });
|
||||
});
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
// delete the device with this id (accessed at DELETE http://localhost:8080/devices/:device_id)
|
||||
.delete(function(req, res) {
|
||||
Device.remove({
|
||||
_id: req.params.device_id
|
||||
}, function(err, device) {
|
||||
if (err)
|
||||
res.send(err);
|
||||
|
||||
res.json({ message: 'Successfully deleted' });
|
||||
});
|
||||
});
|
||||
|
||||
// export router module
|
||||
module.exports = router;
|
@ -1,85 +0,0 @@
|
||||
var express = require('express');
|
||||
var router = express.Router(); // get an instance of the express Router
|
||||
|
||||
var Thing = require('../models/thing');
|
||||
|
||||
// on routes that end in /things
|
||||
// ----------------------------------------------------
|
||||
router.route('/')
|
||||
|
||||
// create a things (accessed at POST http://localhost:8080/things)
|
||||
.post(function(req, res) {
|
||||
|
||||
var thing = new Thing(); // create a new instance of the Bear model
|
||||
thing.name = req.body.name; // set the thing's name (comes from the request)
|
||||
|
||||
// save the thing and check for errors
|
||||
thing.save(function(err) {
|
||||
if (err)
|
||||
res.send(err);
|
||||
|
||||
res.json({ message: 'Thing created!' });
|
||||
});
|
||||
|
||||
})
|
||||
|
||||
// get all the things (accessed at GET http://localhost:8080/things)
|
||||
.get(function(req, res) {
|
||||
Thing.find(function(err, things) {
|
||||
if (err)
|
||||
res.send(err);
|
||||
|
||||
res.json(things);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// on routes that end in /things/:thing_id
|
||||
// ----------------------------------------------------
|
||||
router.route('/:thing_id')
|
||||
|
||||
// get the thing with that id (accessed at GET http://localhost:8080/things/:thing_id)
|
||||
.get(function(req, res) {
|
||||
Thing.findById(req.params.thing_id, function(err, thing) {
|
||||
if (err)
|
||||
res.send(err);
|
||||
res.json(thing);
|
||||
});
|
||||
})
|
||||
|
||||
// update the thing with this id (accessed at PUT http://localhost:8080/things/:thing_id)
|
||||
.put(function(req, res) {
|
||||
|
||||
// use our thing model to find the thing we want
|
||||
Thing.findById(req.params.thing_id, function(err, thing) {
|
||||
|
||||
if (err)
|
||||
res.send(err);
|
||||
|
||||
thing.name = req.body.name; // update the things info
|
||||
|
||||
// save the thing
|
||||
thing.save(function(err) {
|
||||
if (err)
|
||||
res.send(err);
|
||||
|
||||
res.json({ message: 'Thing updated!' });
|
||||
});
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
// delete the thing with this id (accessed at DELETE http://localhost:8080/things/:thing_id)
|
||||
.delete(function(req, res) {
|
||||
Thing.remove({
|
||||
_id: req.params.thing_id
|
||||
}, function(err, thing) {
|
||||
if (err)
|
||||
res.send(err);
|
||||
|
||||
res.json({ message: 'Successfully deleted' });
|
||||
});
|
||||
});
|
||||
|
||||
// export router module
|
||||
module.exports = router;
|
@ -33,7 +33,7 @@ var port = process.env.PORT || config.port; // set our port
|
||||
// ROUTES FOR OUR API
|
||||
// =============================================================================
|
||||
app.use('/status', require('./app/routes/status'));
|
||||
app.use('/things', require('./app/routes/things'));
|
||||
app.use('/devices', require('./app/routes/devices'));
|
||||
|
||||
|
||||
// START THE SERVER
|
||||
|
Loading…
x
Reference in New Issue
Block a user