abstractDevice.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* *********************************************************** */
  2. // Created by Macheng on 2019/05/18.
  3. // Description:
  4. // This is a struct of abstract device. Any device either real or virtual
  5. // must inherit from this struct, so that they have uniform properties like
  6. // AVs and DVs and general functions like AddAV and AddDV etc.
  7. // In order to this, when an occurrence of changing come we can only
  8. // modify this struct to adjust new situation.
  9. /* *********************************************************** */
  10. import BaseModel from './baseModel'
  11. class AbstractDevice extends BaseModel {
  12. avs = []
  13. dvs = []
  14. parentId = 0
  15. constructor(opts) {
  16. if (!opts) opts = {}
  17. super(opts)
  18. this.init(opts)
  19. }
  20. get avsStr() {
  21. return JSON.stringify(this.avs)
  22. }
  23. get dvsStr() {
  24. return JSON.stringify(this.dvs)
  25. }
  26. set avsStr(val) {
  27. try {
  28. const vec = JSON.parse(val)
  29. this.avs.splice(0, this.avs.length)
  30. this.avs.push(...vec)
  31. } catch (e) {
  32. return
  33. }
  34. }
  35. set dvsStr(val) {
  36. try {
  37. const vec = JSON.parse(val)
  38. this.dvs.splice(0, this.dvs.length)
  39. this.dvs.push(...vec)
  40. } catch (e) {
  41. return
  42. }
  43. }
  44. addAV(vid) {
  45. if (this.avs.indexOf(vid) >= 0) {
  46. return false
  47. }
  48. this.avs.push(vid)
  49. }
  50. addDV(vid) {
  51. if (this.dvs.indexOf(vid) >= 0) {
  52. return false
  53. }
  54. this.dvs.push(vid)
  55. }
  56. delAV(vid) {
  57. let range = this.avs.length
  58. let idx = this.avs.indexOf(vid)
  59. while (idx >= 0 && this.avs.length && range >= 0) {
  60. this.avs.splice(idx, 1)
  61. idx = this.avs.indexOf(vid)
  62. range--
  63. }
  64. }
  65. delDV(vid) {
  66. let range = this.dvs.length
  67. let idx = this.dvs.indexOf(vid)
  68. while (idx >= 0 && this.dvs.length && range >= 0) {
  69. this.dvs.splice(idx, 1)
  70. idx = this.dvs.indexOf(vid)
  71. range--
  72. }
  73. }
  74. init(opts) {
  75. if (!opts) opts = {}
  76. super.init(opts)
  77. this.parentId = Number(opts['parentId']) || this.parentId
  78. }
  79. toJson() {
  80. const obj1 = super.toJson()
  81. const obj2 = {
  82. parentId: this.parentId,
  83. avs: this.avs.slice(0),
  84. dvs: this.dvs.slice(0)
  85. }
  86. return Object.assign({}, obj1, obj2)
  87. }
  88. }
  89. export default AbstractDevice