Skip to content

Instantly share code, notes, and snippets.

@lbrenman
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbrenman/cffb45b8f2b31974d7fc to your computer and use it in GitHub Desktop.
Save lbrenman/cffb45b8f2b31974d7fc to your computer and use it in GitHub Desktop.
ArrowDB Custom APIs Example - Sample custom api's in the apis folder interacting with the same model as the automatic APIs
var Arrow = require('arrow');
var ACS = require('acs-node');
ACS.init(<ACS Key>);
var Alert = Arrow.API.extend({
group: 'iotapis',
path: '/api/alert',
method: 'GET',
description: 'this is an api that shows how to implement an API',
// model: 'testuser',
// before: 'pre_example',
// after: 'post_example',
parameters: {},
action: function (req, resp, next) {
var data = {
login: "a",
password: "1234"
};
ACS.Users.login(data, function(response){
if(response.success) {
console.log("Successful login.");
console.log("UserInfo: " + JSON.stringify(response.users[0], null, 2));
ACS.PushNotifications.notify({
channel: 'iot',
payload: 'iot notification',
to_ids: 'everyone'
}, function (e) {
if (e.success) {
console.log("Successful Push");
} else {
console.log('Error:\n' +
((e.error && e.message) || JSON.stringify(e)));
}
});
} else {
console.log("Error to login: " + response.message);
}
});
}
});
module.exports = Alert;
var Arrow = require('arrow');
var GetVal = Arrow.API.extend({
group: 'iotapis',
path: '/api/getVal/:id',
method: 'GET',
description: 'this is an api that shows how to implement an API',
// model: 'testuser',
// before: 'pre_example',
// after: 'post_example',
parameters: {
id: {description:'the iot device id'}
},
action: function (req, resp, next) {
var model = Arrow.getModel("iotdevice");
model.query({deviceId: req.params.id}, function(err, data){
if(err) {
next(err, null);
} else {
next(null, data);
}
});
}
});
module.exports = GetVal;
var Arrow = require('arrow');
var SetValOff = Arrow.API.extend({
group: 'iotapis',
path: '/api/setValOff/:id',
method: 'GET',
description: 'this is an api that shows how to implement an API',
// model: 'testuser',
// before: 'pre_example',
// after: 'post_example',
parameters: {
id: {description:'the iot device id'}
},
action: function (req, resp, next) {
var model = Arrow.getModel("iotdevice");
model.findAndModify({
where: {
deviceId: req.params.id
}
}, {
deviceOutputVal: 0
}, function(err, data){
if(err) {
next(err, null);
} else {
next(null, data);
}
});
}
});
module.exports = SetValOff;
var Arrow = require('arrow');
var SetValOn = Arrow.API.extend({
group: 'iotapis',
path: '/api/setValOn/:id',
method: 'GET',
description: 'this is an api that shows how to implement an API',
// model: 'testuser',
// before: 'pre_example',
// after: 'post_example',
parameters: {
id: {description:'the iot device id'}
},
action: function (req, resp, next) {
var model = Arrow.getModel("iotdevice");
model.findAndModify({
where: {
deviceId: req.params.id
}
}, {
deviceOutputVal: 1
}, function(err, data){
if(err) {
next(err, null);
} else {
next(null, data);
}
});
}
});
module.exports = SetValOn;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment