| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- /* *********************************************************** */
- // 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
|