thingsboard_api
Version:
Thingsboard REST API implementation
216 lines (187 loc) • 8.71 kB
JavaScript
const postgres = require("postgres")
sqlConfig = {};
async function createPostgresConnection() {
sqlConfig =
{
timeout: 1, // get idle connection
evict: 2000, // it actualy removes the idle connection
host: process.env.PG_HOST, // Postgres ip address or domain name
port: process.env.PG_PORT, // Postgres server port
database: process.env.PG_DATABASE, // Name of database to connect to
username: process.env.PG_USERNAME, // Username of database user
password: process.env.PG_PASSWORD,
}
return
}
function toUUID(id) {
id = id.substring(7, 15) + "-" +
id.substring(3, 7) + "-1" +
id.substring(0, 3) + "-" +
id.substring(15, 19) + "-" +
id.substring(19);
return id;
}
// get postgres id from thingsboard uuid
function toPostgresID(tb_uuid) {
id = tb_uuid.substring(15, 18) +
tb_uuid.substring(9, 13) +
tb_uuid.substring(0, 8) +
tb_uuid.substring(19, 23) +
tb_uuid.substring(24, tb_uuid.length)
return id;
}
// Get access token for device
async function getEntityToken(entityId) {
if ((typeof entityId === "undefined") || (entityId === null)) {
return { "error": true, "message": "getEntitytoken(), Error: entityId is not defined or null!" }
}
const sql = postgres('postgres://username:password@host:port/database', sqlConfig);
try {
const pgId = toPostgresID(entityId);
const response = await sql`SELECT credentials_id FROM device_credentials WHERE device_id = ${pgId}`
return response[0].credentials_id;
} catch (err) {
return { "error": true, "message": `getEntityToken(), ${err}` }
}
}
// Get attributes according to attributeKeys (array of keys)
async function getAttrsAndValuesById(entityId, attributeKeys) {
if (((typeof entityId === "undefined") || (entityId === null)) || ((typeof attributeKeys === "undefined") || (attributeKeys === null))) {
return { "error": true, "message": "getAttrsAndValuesById(), Error: entityId or attributeKys are not defined or null!" }
}
const sql = postgres('postgres://username:password@host:port/database', sqlConfig);
try {
const pgId = toPostgresID(entityId);
const response = await sql`SELECT entity_type, entity_id, attribute_type, attribute_key, bool_v, str_v, long_v, dbl_v, last_update_ts
FROM attribute_kv where entity_id = ${pgId}
AND attribute_key in (${attributeKeys})
ORDER BY attribute_type DESC`;
return response;
} catch (err) {
return { "error": true, "message": `getAttrsAndValuesById(), ${err}` }
}
}
// Keys are hardcoded in db request!
async function insertIntoAttrsKeysVals(dataToWrite) {
if ((typeof dataToWrite === "undefined") || (dataToWrite === null)) {
return { "error": true, "message": "insertIntoAttrsKeysVals(), Error: input data is not defined or null!" }
}
const sql = postgres('postgres://username:password@host:port/database', sqlConfig);
try {
const response = await sql`insert into attribute_kv ${sql(dataToWrite,
'entity_type', 'entity_id', 'attribute_type', 'attribute_key', 'bool_v', 'str_v', 'long_v', 'dbl_v', 'last_update_ts'
)}`;
return response;
} catch (err) {
return { "error": true, "message": `insertIntoAttrsKeysVals(), ${err}` }
}
}
// Update child attributes keys and values based on parent attributes
async function updateAttrsKeysAndVals(attributeObj) {
if ((typeof attributeObj === "undefined") || (attributeObj === null)) {
return { "error": true, "message": "updateAttrsKeysAndVals(), Error: input data is not defined or null!" }
}
const sql = postgres('postgres://username:password@host:port/database', sqlConfig);
try {
const response = await sql`update attribute_kv set ${
sql(attributeObj, 'entity_type', 'entity_id', 'attribute_type', 'attribute_key', 'bool_v', 'str_v', 'long_v', 'dbl_v', 'last_update_ts')
} where entity_id = ${attributeObj["entity_id"]} and attribute_key = ${attributeObj.attribute_key}`;
return response;
} catch (err) {
return { "error": true, "message": `updateAttrsKeysAndVals(), ${err}` }
}
}
async function getAllObjectsIDbyType(type, entity_type) {
if (((typeof type === "undefined") || (type === null)) || ((typeof entity_type === "undefined") || (entity_type === null))) {
return { "error": true, "message": "getAllObjectsIDbyType(), Error: custom type or entity type are not defined or null!" }
}
const sql = postgres('postgres://username:password@host:port/database', sqlConfig)
try {
const data = await sql`
SELECT * from ${sql(entity_type)}
WHERE type = ${type} ORDER BY id DESC `
for (let i = 0; i < data.length; i++) {
data[i].id = toUUID(data[i].id)
}
return data
} catch (err) {
return { "error": true, "message": `getAllObjectsIDbyType(), ${err}` }
}
}
async function getAllObjectsIDandKeysByType(type, entity_type, keys = null, timezoneOffset = null) {
if (((typeof type === "undefined") || (type === null)) || ((typeof entity_type === "undefined") || (entity_type === null))) {
return { "error": true, "message": "getAllObjectsIDandKeysByType(), Error: custom type or entity type are not defined or null!" }
}
const sql = postgres('postgres://username:password@host:port/database', sqlConfig)
try {
const data = await sql`
SELECT * from ${sql(entity_type)}
WHERE type = ${type} ORDER BY id DESC `
let entities = []
for (let i = 0; i < data.length; i++) {
entities.push(data[i].id)
}
let result = [];
if (keys != null) {
result = await sql`
SELECT entity_id, entity_type, long_v, str_v, attribute_key from attribute_kv
WHERE entity_id in (${entities}) and
attribute_key in (${keys})
ORDER by entity_id DESC
`;
}
else {
result = await sql`
SELECT entity_id, entity_type, long_v, str_v, attribute_key from attribute_kv
WHERE entity_id in (${entities})
ORDER by entity_id DESC
`;
}
let target = []
let obj = {}
if ((typeof timezoneOffset == 'undefined') || (timezoneOffset === null)) {
var timezoneOffset = 0
}
for (let i = 0; i < result.length; i++) {
if (result[i]["attribute_key"] == 'ts') {
result[i]["long_v"] = new Date(result[i]["long_v"] - timezoneOffset * 60 * 1000)
}
if (i === result.length - 1) {
obj[result[i]["attribute_key"]] = result[i]["long_v"] || result[i]["str_v"];
obj["entity_id"] = result[i]["entity_id"].toString();
obj["entity_type"] = result[i]["entity_type"].toString();
target.push(obj);
break;
}
if (result[i]["entity_id"].toString() === result[i + 1]["entity_id"].toString()) {
obj[result[i]["attribute_key"]] = result[i]["long_v"] || result[i]["str_v"];
if (result[i]["long_v"] === 0)
obj[result[i]["attribute_key"]] = 0;
}
if (result[i]["entity_id"].toString() !== result[i + 1]["entity_id"].toString()) {
obj[result[i]["attribute_key"]] = result[i]["long_v"] || result[i]["str_v"];
obj["entity_id"] = result[i]["entity_id"].toString();
obj["entity_type"] = result[i]["entity_type"].toString();
obj["entity_name"] = data[target.length].name
target.push(obj)
obj = {};
}
}
return target
} catch (err) {
return { "error": true, "message": `getAllObjectsIDandKeysByType(), ${err}` }
}
}
module.exports =
{
createPostgresConnection: createPostgresConnection,
toPostgresID: toPostgresID,
insertIntoAttrsKeysVals: insertIntoAttrsKeysVals,
updateAttrsKeysAndVals: updateAttrsKeysAndVals,
get: {
allObjectsIDbyType: getAllObjectsIDbyType,
allObjectsIDandKeysByType: getAllObjectsIDandKeysByType,
getAttrsAndValuesById: getAttrsAndValuesById,
getEntityToken: getEntityToken,
}
}