File size: 5,380 Bytes
cf0f72e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | /**
* Copyright reelyActive 2015-2022
* We believe in an open Internet of Things
*/
const HTTP_STATUS_OK = 200;
const HTTP_STATUS_CREATED = 201;
const HTTP_STATUS_BAD_REQUEST = 400;
const HTTP_STATUS_NOT_FOUND = 404;
/**
* AssociationsManager Class
* Manages the association of wireless identifiers with metadata.
*/
class AssociationsManager {
/**
* AssociationsManager constructor
* @param {Object} options The options as a JSON object.
* @param {DatabaseManager} database The database manager.
* @constructor
*/
constructor(options, database) {
options = options || {};
this.database = database;
}
/**
* Retrieve an existing association.
* @param {String} id The id of the device association.
* @param {String} type The type of id of the device association.
* @param {String} property A specific association property to get.
* @param {callback} callback Function to call on completion.
*/
retrieve(id, type, property, callback) {
let isSpecificAssociation = (id && type);
if(isSpecificAssociation) {
let key = id + '/' + type;
this.database.get(key, function(err, association) {
if(err) {
return callback(HTTP_STATUS_BAD_REQUEST);
}
else if(association === undefined) {
return callback(HTTP_STATUS_NOT_FOUND);
}
else if(property) {
let data = { associations: {} };
data.associations[key] = {};
data.associations[key][property] = association[property];
return callback(HTTP_STATUS_OK, data);
}
else {
let data = { associations: {} };
data.associations[key] = association;
return callback(HTTP_STATUS_OK, data);
}
});
}
else {
let query = {};
this.database.find(query, function(err, result) {
if(err) {
return callback(HTTP_STATUS_BAD_REQUEST);
}
else {
let associations = {};
for(let key of result.keys()) {
associations[key] = {};
}
return callback(HTTP_STATUS_OK, { associations: associations });
}
});
}
}
/**
* Create/replace an association.
* @param {String} id The id of the device association.
* @param {String} type The type of id of the device association.
* @param {String} url The URL associated with the device.
* @param {String} directory The directory associated with the device.
* @param {Array} tags The tags associated with the device.
* @param {Array} position The position associated with the device.
* @param {callback} callback Function to call on completion.
*/
replace(id, type, url, directory, tags, position, callback) {
let self = this;
let key = id + '/' + type;
this.database.get(key, function(err, association) {
if(err) {
return callback(HTTP_STATUS_BAD_REQUEST);
}
let isPresent = (association !== undefined);
association = association || {};
if(url && (typeof url === 'string')) {
association.url = url;
}
if(directory && (typeof directory === 'string')) {
association.directory = directory;
}
if(tags && Array.isArray(tags)) {
association.tags = tags;
}
if(position && Array.isArray(position)) {
association.position = position;
}
self.database.set(key, association, function(err) {
if(err) {
return callback(HTTP_STATUS_BAD_REQUEST);
}
else {
let status = (isPresent ? HTTP_STATUS_OK : HTTP_STATUS_CREATED);
let data = { associations: {} };
data.associations[key] = association;
return callback(status, data);
}
});
});
}
/**
* Remove an existing association.
* @param {String} id The id of the device association.
* @param {String} type The type of id of the device association.
* @param {String} property A specific association property to remove.
* @param {callback} callback Function to call on completion.
*/
remove(id, type, property, callback) {
let self = this;
let key = id + '/' + type;
// Delete only a single property
if(property) {
this.database.get(key, function(err, association) {
if(err) {
return callback(HTTP_STATUS_BAD_REQUEST);
}
else if((association === undefined) ||
!association.hasOwnProperty(property)) {
return callback(HTTP_STATUS_NOT_FOUND);
}
delete association[property];
self.database.set(key, association, function(err) {
if(err) {
return callback(HTTP_STATUS_BAD_REQUEST);
}
else {
let data = { associations: {} };
data.associations[key] = association;
return callback(HTTP_STATUS_OK, { associations: association });
}
});
});
}
// Delete the entire association
else {
this.database.delete(key, function(err, isRemoved) {
if(err) {
return callback(HTTP_STATUS_BAD_REQUEST);
}
else if(!isRemoved) {
return callback(HTTP_STATUS_NOT_FOUND);
}
else {
return callback(HTTP_STATUS_OK);
}
});
}
}
}
module.exports = AssociationsManager;
|