-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #244 from prey/geofencing-storage
Geofencing on node client using sqlite storage
- Loading branch information
Showing
13 changed files
with
380 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
var join = require('path').join, | ||
common = require('./../../common'), | ||
storage = require('./../../utils/storage'), | ||
logger = common.logger; | ||
|
||
var storage_path = join(common.system.paths.config, 'commands.db'); | ||
storage.init('geofences', storage_path, function(err) { | ||
if (err) logger.error('Unable to initialize db for geofences: ' + err); | ||
}); | ||
|
||
exports.store = function(geofence, cb) { | ||
var opts = { | ||
id: geofence.id, | ||
name: geofence.name, | ||
state: geofence.state | ||
} | ||
var key = ['geofence', opts.id].join('-'), | ||
obj = {}; | ||
|
||
obj[key] = opts; | ||
var to_add = new Buffer(JSON.stringify(obj, null, 0)).toString('base64'); | ||
storage.set_geofence(key, to_add, cb); | ||
} | ||
|
||
exports.update = function(id, name, del, add, cb) { | ||
var key = ["geofence", id].join("-"); | ||
|
||
if (del != add) { | ||
var fence_del, | ||
fence_add, | ||
obj_del = {}, | ||
obj_add = {}, | ||
to_delete, | ||
to_add; | ||
|
||
fence_del = { | ||
"id": id, | ||
"name": name, | ||
"state": del | ||
} | ||
fence_add = { | ||
"id": id, | ||
"name": name, | ||
"state": add | ||
} | ||
|
||
obj_del[key] = fence_del; | ||
obj_add[key] = fence_add; | ||
|
||
to_delete = new Buffer(JSON.stringify(obj_del, null, 0)).toString('base64'); | ||
to_add = new Buffer(JSON.stringify(obj_add, null, 0)).toString('base64'); | ||
|
||
storage.update(key, to_delete, to_add, cb); | ||
|
||
} else { | ||
return cb(null); | ||
} | ||
} | ||
|
||
exports.get_geofences = function(cb) { | ||
storage.all('geofences', function(err, fences) { | ||
if (err) cb(new Error("Error retrieving geofences from local database")); | ||
|
||
clear_geo(function(err) { | ||
if (err) return cb(new Error("Error deleting geofences from local database"), {}); | ||
cb(null, fences); | ||
}); | ||
|
||
}) | ||
} | ||
|
||
var clear_geo = function(cb) { | ||
storage.clear('geofences', cb); | ||
} | ||
|
||
exports.clear_geo = clear_geo; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"use strict"; | ||
|
||
////////////////////////////////////////// | ||
// (C) 2016 Prey, Inc. | ||
// By Javir Acuña - http://preyproject.com | ||
// GPLv3 Licensed | ||
// Prey File Type discriminate if a file has ASCII or SQLite format, used for | ||
// the commands.db file | ||
//////////////////////////////////////////// | ||
|
||
var os_name = process.platform.replace('darwin', 'mac').replace('win32', 'windows'), | ||
os_functions = require('./' + os_name); | ||
|
||
module.exports.get_file_type = function(path, cb) { | ||
os_functions.get_file_type(path, function(err, file_type) { | ||
if (err) return cb(err); | ||
|
||
return cb(null, file_type); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"use strict"; | ||
|
||
//////////////////////////////////////////// | ||
// (C) 2016 Prey, Inc. | ||
// By Javier Acuña - http://preyproject.com | ||
// GPLv3 Licensed | ||
//////////////////////////////////////////// | ||
|
||
module.exports.get_file_type = function(path, cb) { | ||
var exec = require('child_process').exec, | ||
cmd = 'file ' + path; | ||
|
||
exec(cmd, function(err, stdout) { | ||
var format = "ASCII"; | ||
if (err) return cb(new Error("Error getting file type")) | ||
|
||
if (stdout.includes('SQLite')) format = "SQLite"; | ||
cb(null, format); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"use strict"; | ||
|
||
//////////////////////////////////////////// | ||
// (C) 2016 Prey, Inc. | ||
// By Javier Acuña - http://preyproject.com | ||
// GPLv3 Licensed | ||
//////////////////////////////////////////// | ||
|
||
module.exports.get_file_type = function(path, cb) { | ||
var exec = require('child_process').exec, | ||
cmd = 'file ' + path; | ||
|
||
exec(cmd, function(err, stdout) { | ||
var format = "ASCII"; | ||
if (err) return cb(new Error("Error getting file type")) | ||
|
||
if (stdout.includes('SQLite')) format = "SQLite"; | ||
cb(null, format); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"use strict"; | ||
|
||
//////////////////////////////////////////// | ||
// (C) 2016 Prey, Inc. | ||
// By Javier Acuña - http://preyproject.com | ||
// GPLv3 Licensed | ||
//////////////////////////////////////////// | ||
|
||
module.exports.get_file_type = function(path, cb) { | ||
var exec = require('child_process').exec, | ||
cmd = 'type ' + path; | ||
|
||
exec(cmd, function(err, stdout) { | ||
var format = "ASCII"; | ||
if (err) return cb(new Error("Error getting file type")) | ||
|
||
if (stdout.includes('SQLite')) format = "SQLite"; | ||
cb(null, format); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.