151 lines
4.8 KiB
JavaScript
151 lines
4.8 KiB
JavaScript
console.loggerName = 'js.equipment';
|
|
console.log('Load equipment modules');
|
|
|
|
class Equipment {
|
|
constructor(equipmentItem) {
|
|
|
|
// Fetch item if provided equipmentItem is a string
|
|
if (typeof equipmentItem === 'string' || equipmentItem instanceof String) {
|
|
this.equipmentItem = items[equipmentItem];
|
|
} else {
|
|
this.equipmentItem = equipmentItem;
|
|
}
|
|
|
|
// Throw error if item is not existing
|
|
if (this.equipmentItem === null) {
|
|
throw(`Item ${equipmentItem} not existing`);
|
|
}
|
|
|
|
// Throw error if item is not an equipment type
|
|
if (this.equipmentItem.semantics.semanticType != 'Equipment') {
|
|
throw(`Item ${this.equipmentItem.name} is not an equipment type`);
|
|
}
|
|
|
|
console.info(`Initialization of equipment ${this.equipmentItem.name} with type ${this.constructor.name}`);
|
|
|
|
// Initialization of properties
|
|
this.name = this.equipmentItem.name;
|
|
this.stateItem = items[this.getPropertyItemName('State')];
|
|
this.watch = new Object();
|
|
|
|
// Check if equipment has LowBat item
|
|
if (this.hasProperty('LowBat')) {
|
|
this.watch['lowBat'] = new watch.Watch(this.getPropertyItemName('LowBat'));
|
|
this.watch['lowBat'].add({
|
|
targetState: 'ON',
|
|
alertFunc: () => { this.#notifyLowBat(); },
|
|
alertRepeat: 'PT23H'
|
|
});
|
|
}
|
|
|
|
// Check if equipment has Unreach item
|
|
if (this.hasProperty('Unreach')) {
|
|
this.watch['Unreach'] = new watch.Watch(this.getPropertyItemName('Unreach'));
|
|
this.watch['Unreach'].add({
|
|
targetState: 'ON',
|
|
alertFunc: () => { this.#notifyUnreach(); },
|
|
alertDelay: 'PT15M',
|
|
alertRepeat: 'PT1H'
|
|
});
|
|
}
|
|
}
|
|
|
|
getValue(propertyName, defaultValue = '') {
|
|
let valueItemName = this.name + '_' + propertyName;
|
|
let returnValue = defaultValue;
|
|
|
|
if (items[valueItemName] == null) { // Return default value if item is missing
|
|
console.warn('Item ' + valueItemName + ' is missing');
|
|
} else if (items[valueItemName]['state'] == 'NULL') { // Return default value if item state is null
|
|
console.warn('Item ' + valueItemName + ' is unset')
|
|
} else { // Return value from item
|
|
if (items[valueItemName].quantityState == null && items[valueItemName].numericState == null) {
|
|
returnValue = items[valueItemName]['state'];
|
|
} else if (items[valueItemName].quantityState == null) {
|
|
returnValue = items[valueItemName]['numericState']
|
|
} else {
|
|
returnValue = items[valueItemName].quantityState;
|
|
}
|
|
}
|
|
|
|
console.debug(`Return value ${valueName} for ${this.name}: ${returnValue}`);
|
|
return returnValue;
|
|
}
|
|
|
|
getPropertyItemName(propertyName) {
|
|
if (this.hasProperty(propertyName)) {
|
|
return items[this.name + '_' + propertyName].name;
|
|
}
|
|
}
|
|
|
|
hasProperty(propertyName) {
|
|
if (items[this.name + '_' + propertyName] == null) {
|
|
console.debug(`Eqipment ${this.name} has no property ${propertyName}`)
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
#notifyLowBat() {
|
|
console.warn(`${this.name} has a low battery level.`);
|
|
}
|
|
|
|
#notifyUnreach() {
|
|
console.warn(`${this.name} is offline.`);
|
|
}
|
|
|
|
gc() {
|
|
console.log('Denitialization of eqipment ' + this.name);
|
|
|
|
// Delete all watchObjects
|
|
for (let watchItem of Object.keys(this.watch)) {
|
|
this.watch[watchItem].deleteAll();
|
|
}
|
|
}
|
|
}
|
|
|
|
class Irrigation extends Equipment {
|
|
constructor(equipmentItem) {
|
|
super(equipmentItem);
|
|
}
|
|
}
|
|
|
|
class IrrigationValve extends Equipment {
|
|
constructor(equipmentItem) {
|
|
super(equipmentItem);
|
|
|
|
this.watch['state'] = new watch.Watch(this.stateItem.name);
|
|
this.watch['state'].add({
|
|
targetState: 'ON',
|
|
alertFunc: () => { this.stateItem.sendCommand('OFF'); },
|
|
alertDelay: 'PT59M'
|
|
});
|
|
}
|
|
}
|
|
|
|
class TowelRadiator extends Equipment {
|
|
constructor(equipmentItem) {
|
|
super(equipmentItem);
|
|
|
|
this.watch['state'] = new watch.Watch(this.stateItem.name);
|
|
this.autoOff = this.watch['state'].add({
|
|
targetState: 'ON',
|
|
alertFunc: () => { this.stateItem.sendCommand('OFF'); },
|
|
alertDelay: 'PT59M'
|
|
});
|
|
}
|
|
}
|
|
|
|
class WeatherService extends Equipment {
|
|
constructor(equipmentItem) {
|
|
super(equipmentItem);
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
Equipment,
|
|
Irrigation,
|
|
IrrigationValve,
|
|
TowelRadiator,
|
|
WeatherService
|
|
}; |