/* *********************************************************** */ // Created by Macheng on 2019/05/18. // Description: // This is a struct of abstract device. Any device either real or virtual // must inherit from this struct, so that they have uniform properties like // AVs and DVs and general functions like AddAV and AddDV etc. // In order to this, when an occurrence of changing come we can only // modify this struct to adjust new situation. /* *********************************************************** */ import BaseModel from './baseModel' class AbstractDevice extends BaseModel { avs = [] dvs = [] parentId = 0 constructor(opts) { if (!opts) opts = {} super(opts) this.init(opts) } get avsStr() { return JSON.stringify(this.avs) } get dvsStr() { return JSON.stringify(this.dvs) } set avsStr(val) { try { const vec = JSON.parse(val) this.avs.splice(0, this.avs.length) this.avs.push(...vec) } catch (e) { return } } set dvsStr(val) { try { const vec = JSON.parse(val) this.dvs.splice(0, this.dvs.length) this.dvs.push(...vec) } catch (e) { return } } addAV(vid) { if (this.avs.indexOf(vid) >= 0) { return false } this.avs.push(vid) } addDV(vid) { if (this.dvs.indexOf(vid) >= 0) { return false } this.dvs.push(vid) } delAV(vid) { let range = this.avs.length let idx = this.avs.indexOf(vid) while (idx >= 0 && this.avs.length && range >= 0) { this.avs.splice(idx, 1) idx = this.avs.indexOf(vid) range-- } } delDV(vid) { let range = this.dvs.length let idx = this.dvs.indexOf(vid) while (idx >= 0 && this.dvs.length && range >= 0) { this.dvs.splice(idx, 1) idx = this.dvs.indexOf(vid) range-- } } init(opts) { if (!opts) opts = {} super.init(opts) this.parentId = Number(opts['parentId']) || this.parentId } toJson() { const obj1 = super.toJson() const obj2 = { parentId: this.parentId, avs: this.avs.slice(0), dvs: this.dvs.slice(0) } return Object.assign({}, obj1, obj2) } } export default AbstractDevice