@trusthab/composable-resources
Version:
migrating https://github.com/knetikmedia/hab-api/tree/integration/app/resources/composable
195 lines (182 loc) • 4.86 kB
JavaScript
const _ = require('lodash');
const composer = require('../mixin_loader');
module.exports = (App) => {
class DeviceResource {
static mixins() {
return [
// Functionality
'Validatable',
'Templatable',
// ORMish
'Persistable',
'KnetikCloudPersistence',
'PrometheusHooks',
// Mapping and helpers
'Common',
'Nameable',
'Alertable',
'Sourceable'
];
}
static fields() {
return {
serial: {
type: 'text',
minLength: 1
},
device_type: {
type: 'text',
enum: ['thermostat', 'gateway', 'audio_detector', 'water_sensor'],
minLength: 1
},
make: {
type: 'text',
minLength: 1
},
model: {
type: 'text',
minLength: 1
},
mac_address: {
type: 'text',
or: ['null']
},
ip_address: {
type: 'text',
or: ['null'],
path: 'additional_properties.ip_address.value'
},
os: {
type: 'text',
or: ['null']
},
status: {
type: 'text',
or: ['null'],
enum: ['offline', 'idle', 'cooling', 'heating', 'off', 'unknown', 'online', null],
path: 'additional_properties.status.value',
// Get device offline from prometheus, update status to offline if
// value is less than 1
promExp: 'device_online{ app_id="{{app_id}}", device_id="{{id}}", {{location_type}}_id="{{location_id}}" }',
promEval: (device, { result }) => {
if (!_.includes(['thermostat', 'gateway'], device.device_type)) {
return;
}
const val = _.get(result, '0.value.1');
const deviceStatus = parseInt(val);
if (!deviceStatus) {
device.status = 'offline';
}
}
},
connected: {
type: 'boolean',
or: ['null'],
path: 'additional_properties.connected.value'
},
disconnected_since: {
type: 'integer',
or: ['null'],
path: 'additional_properties.disconnected_since.value'
},
battery_level: {
type: 'integer',
or: ['null'],
path: 'additional_properties.battery_level.value'
},
signal: {
type: 'integer',
or: ['null'],
path: 'additional_properties.signal.value'
},
last_seen: {
type: 'integer',
or: ['null'],
path: 'additional_properties.last_seen.value'
},
registered: {
type: 'boolean',
or: ['null'],
path: 'additional_properties.registered.value'
},
coordinates: {
type: 'list',
path: 'additional_properties.coordinates.values'
},
working_status: {
type: 'text',
or: ['null'],
path: 'additional_properties.working_status.value'
},
command_source: {
type: 'text',
or: ['null'],
path: 'additional_properties.command_source.value'
},
gateway_serial: {
type: 'text',
or: ['null'],
path: 'additional_properties.gateway_serial.value'
},
home_id: {
type: 'text',
or: ['null', 'integer'],
path: 'additional_properties.home_id.value'
},
node_id: {
type: 'text',
or: ['null', 'integer'],
path: 'additional_properties.node_id.value'
},
template: {
type: 'text',
schema_ignore: true
},
created_at: {
weight: 1000,
type: 'integer',
or: ['null'],
path: 'created_date'
},
updated_at: {
weight: 1001,
type: 'integer',
or: ['null'],
path: 'updated_date'
}
};
}
static knetikCloudMethods() {
return {
search: {
index: 'devices',
template: 'hab'
},
find: {
api: 'DevicesApi',
method: 'getDevice'
},
create: {
api: 'DevicesApi',
method: 'createDevice',
paramsBuilder: (params) => {
params.template = 'hab';
return [params];
}
},
update: {
api: 'DevicesApi',
method: 'updateDevice',
paramsBuilder: (id, params) => {
params.template = 'hab';
return [params, id];
}
}
};
}
static customFilters() {
return require('./device_filters')(App);
}
}
return composer(DeviceResource, App);
};