Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.86 KB | None | 0 0
  1. module.exports = (env) ->
  2.  
  3. Promise = env.require 'bluebird'
  4. assert = env.require 'cassert'
  5. _ = env.require 'lodash'
  6. OTGWConnection = require 'OTGW-Control'
  7. Promise.promisifyAll(OTGWConnection.prototype)
  8. M = env.matcher
  9. settled = (promise) -> Promise.settle([promise])
  10.  
  11. class OTGWThermostatPlugin extends env.plugins.Plugin
  12.  
  13. init: (app, @framework, @config) =>
  14.  
  15. # Promise that is resolved when the connection is established
  16. @_lastAction = new Promise( (resolve, reject) =>
  17. @otgw = new OTGWConnection(@config.host, @config.port)
  18. @otgw.once("connected", resolve)
  19. @otgw.once('error', reject)
  20. return
  21. ).timeout(60000).catch( (error) ->
  22. env.logger.error "Error on connecting to OTGW relay: #{error.message}"
  23. env.logger.debug error.stack
  24. return
  25. )
  26.  
  27. @otgw.on('error', (error) =>
  28. env.logger.error "connection error: #{error}"
  29. env.logger.debug error.stack
  30. )
  31.  
  32. @otgw.on("control_setpoint", (data) =>
  33. env.logger.debug "got control_setpoint: ", data
  34.  
  35. )
  36. @otgw.on("max_relative_modulation_level", (data) =>
  37. env.logger.debug "got max_relative_modulation_level: ", data
  38.  
  39. )
  40. @otgw.on("room_setpoint", (data) =>
  41. env.logger.debug "got room_setpoint: ", data
  42.  
  43. )
  44. @otgw.on("relative_modulation_level", (data) =>
  45. env.logger.debug "got relative_modulation_level: ", data
  46.  
  47. )
  48. @otgw.on("ch_water_pressure", (data) =>
  49. env.logger.debug "got ch_water_pressure: ", data
  50.  
  51. )
  52. @otgw.on("room_temperature", (data) =>
  53. env.logger.debug "got room_temperature: ", data
  54.  
  55. )
  56. @otgw.on("boiler_water_temperature", (data) =>
  57. env.logger.debug "got boiler_water_temperature: ", data
  58.  
  59. )
  60. @otgw.on("dhw_temperature", (data) =>
  61. env.logger.debug "got dhw_temperature: ", data
  62.  
  63. )
  64. @otgw.on("outside_temperature", (data) =>
  65. env.logger.debug "got outside_temperature: ", data
  66.  
  67. )
  68. @otgw.on("return_water_temperature", (data) =>
  69. env.logger.debug "got return_water_temperature: ", data
  70.  
  71. )
  72. @otgw.on("dhw_setpoint", (data) =>
  73. env.logger.debug "got dhw_setpoint: ", data
  74.  
  75. )
  76. @otgw.on("max_ch_water_setpoint", (data) =>
  77. env.logger.debug "got max_ch_water_setpoint: ", data
  78.  
  79. )
  80. @otgw.on("remote_override_setpoint", (data) =>
  81. env.logger.debug "got remote_override_setpoint: ", data
  82. )
  83. @otgw.on("flame_status" , (data) =>
  84. env.logger.debug "Flame status: ", data
  85. )
  86. @otgw.on("burner_starts" , (data) =>
  87. env.logger.debug "burner_starts: ", data
  88. )
  89. @otgw.on("ch_pump_starts" , (data) =>
  90. env.logger.debug "ch_pump_starts: ", data
  91. )
  92. @otgw.on("dhw_pump_starts" , (data) =>
  93. env.logger.debug "dhw_pump_starts: ", data
  94. )
  95. @otgw.on("dhw_burner_starts" , (data) =>
  96. env.logger.debug "dhw_burner_starts: ", data
  97. )
  98. @otgw.on("burner_operation_hours" , (data) =>
  99. env.logger.debug "burner_operation_hours: ", data
  100. )
  101. @otgw.on("ch_pump_operation_hours" , (data) =>
  102. env.logger.debug "ch_pump_operation_hours: ", data
  103. )
  104. @otgw.on("dhw_pump_operation_hours" , (data) =>
  105. env.logger.debug "dhw_pump_operation_hours: ", data
  106. )
  107. @otgw.on("dhw_burner_operation_hours" , (data) =>
  108. env.logger.debug "dhw_burner_operation_hours: ", data
  109. )
  110.  
  111.  
  112. deviceConfigDef = require("./device-config-schema")
  113. @framework.deviceManager.registerDeviceClass("OTGWHeatingThermostat", {
  114. configDef: deviceConfigDef.OTGWHeatingThermostat,
  115. createCallback: (config, lastState) -> new OTGWHeatingThermostat(config, lastState)
  116. })
  117.  
  118. @framework.deviceManager.registerDeviceClass("OTGWMainThermostat", {
  119. configDef: deviceConfigDef.OTGWMainThermostat,
  120. createCallback: (config, lastState) -> new OTGWMainThermostat(config, lastState)
  121. })
  122.  
  123. @framework.deviceManager.registerDeviceClass("OTGWMainThermostat1", {
  124. configDef: deviceConfigDef.OTGWMainThermostat1,
  125. createCallback: (config, lastState) -> new OTGWMainThermostat1(config, lastState)
  126. })
  127.  
  128. @framework.deviceManager.registerDeviceClass("OTGWMainThermostat2", {
  129. configDef: deviceConfigDef.OTGWMainThermostat2,
  130. createCallback: (config, lastState) -> new OTGWMainThermostat2(config, lastState)
  131. })
  132.  
  133. @framework.deviceManager.registerDeviceClass("OTGWFlamePresenceSensor", {
  134. configDef: deviceConfigDef.OTGWFlamePresenceSensor,
  135. createCallback: (config) -> new OTGWFlamePresenceSensor(config)
  136. })
  137.  
  138. @framework.deviceManager.registerDeviceClass("OTGWDomesticHotWater", {
  139. configDef: deviceConfigDef.OTGWDomesticHotWater,
  140. createCallback: (config) -> new OTGWDomesticHotWater(config)
  141. })
  142.  
  143. @framework.deviceManager.registerDeviceClass("OTGWCentralHeating", {
  144. configDef: deviceConfigDef.OTGWCentralHeating,
  145. createCallback: (config) -> new OTGWCentralHeating(config)
  146. })
  147.  
  148. @framework.deviceManager.registerDeviceClass("OTGWThermostat", {
  149. configDef: deviceConfigDef.OTGWThermostat,
  150. createCallback: (config) -> new OTGWThermostat(config)
  151. })
  152.  
  153. # @framework.deviceManager.registerDeviceClass("MaxContactSensor", {
  154. # configDef: deviceConfigDef.MaxContactSensor,
  155. # createCallback: (config, lastState) -> new MaxContactSensor(config, lastState)
  156. # })
  157.  
  158. # @framework.deviceManager.registerDeviceClass("MaxCube", {
  159. # configDef: deviceConfigDef.MaxCube,
  160. # createCallback: (config, lastState) -> new MaxCube(config, lastState)
  161. # })
  162.  
  163. #setTemperatureSetpoint: (rfAddress, mode, value) ->
  164. # setTemperatureSetpoint(mode,value)
  165. setTemperatureSetpoint: (mode, value) ->
  166. @_lastAction = settled(@_lastAction).then( =>
  167. @otgw.setTemperatureAsync(mode, value)
  168. )
  169. return @_lastAction
  170.  
  171.  
  172. plugin = new OTGWThermostatPlugin
  173.  
  174. class OTGWThermostat extends env.devices.Device
  175. _fault = false
  176. _chmode = false
  177. _dhwmode = false
  178. _flame = false
  179. _coolingstatus = false
  180. _ch2mode = false
  181. _diag = false
  182. _chenable = false
  183. _dhwenable = false
  184. _coolingenable = false
  185. _otcstate = false
  186. _ch2enable = false;
  187.  
  188. attributes:
  189. Flame:
  190. description: "Flame status"
  191. type: "boolean"
  192. labels: ["aan", "uit"]
  193. acronym: 'Vlam status'
  194. CentralHeatingMode:
  195. description: "Central Heating Mode"
  196. type: "boolean"
  197. labels: ["aan", "uit"]
  198. acronym: 'Centrale Verwarming Modus'
  199. Fault:
  200. description: "Fault indication"
  201. type: "boolean"
  202. labels: ["aan", "uit"]
  203. acronym: 'Fout indicatie'
  204. DomesticHotWaterMode:
  205. description: "Domestic Hot Water Mode"
  206. type: "boolean"
  207. labels: ["aan", "uit"]
  208. acronym: 'Warm water'
  209. CoolingStatus:
  210. description: "Cooling Status"
  211. type: "boolean"
  212. labels: ["aan", "uit"]
  213. acronym: 'Afkoeling status'
  214. CentralHeating2Mode:
  215. description: "Central Heating 2 Mode"
  216. type: "boolean"
  217. labels: ["aan", "uit"]
  218. acronym: 'Centrale verwarming II'
  219. Diagnostics:
  220. description: "Diagnostics"
  221. type: "boolean"
  222. labels: ["aan", "uit"]
  223. acronym: 'Diagnose'
  224. CentralHeatingEnable:
  225. description: "Central Heating Enable"
  226. type: "boolean"
  227. labels: ["aan", "uit"]
  228. acronym: 'Centrale verwarming aan'
  229. DomesticHotWaterEnable:
  230. description: "Domestic Hot Water Enable"
  231. type: "boolean"
  232. labels: ["aan", "uit"]
  233. acronym: 'Warm water aan'
  234. CoolingEnable:
  235. description: "Cooling Enable"
  236. type: "boolean"
  237. labels: ["aan", "uit"]
  238. acronym: 'Afkoeling aan'
  239. OTCState:
  240. description: "OTC State"
  241. type: "boolean"
  242. labels: ["aan", "uit"]
  243. acronym: 'OTC status'
  244. CentralHeating2Enable:
  245. description: "Central Heating 2 Enable"
  246. type: "boolean"
  247. labels: ["aan", "uit"]
  248. acronym: 'Centrale verwarming II aan'
  249.  
  250. constructor: (@config) ->
  251. @id = @config.id
  252. @name = @config.name
  253.  
  254. plugin.otgw.on("flame_status" , (data) =>
  255. if data.length = 16
  256. @_setFault(@_bitToBool(data.slice(15,16)))
  257. @_setCHMode(@_bitToBool(data.slice(14,15)))
  258. @_setDHWMode(@_bitToBool(data.slice(13,14)))
  259. @_setFlame(@_bitToBool(data.slice(12,13)))
  260. @_setCoolingStatus(@_bitToBool(data.slice(11,12)))
  261. @_setCH2Mode(@_bitToBool(data.slice(10,11)))
  262. @_setDiagnostics(@_bitToBool(data.slice(9,10)))
  263. @_setCHEnable(@_bitToBool(data.slice(8,9)))
  264. @_setDHWEnable(@_bitToBool(data.slice(7,8)))
  265. @_setCoolingEnable(@_bitToBool(data.slice(6,7)))
  266. @_setOTCState(@_bitToBool(data.slice(5,6)))
  267. @_setCH2Enable(@_bitToBool(data.slice(4,5)))
  268. )
  269.  
  270. super()
  271.  
  272. #Getters
  273. getFlame: () -> return Promise.resolve @_flame
  274. getCentralHeatingMode: () -> return Promise.resolve @_chmode
  275. getFault: () -> return Promise.resolve @_fault
  276. getDomesticHotWaterMode: () -> return Promise.resolve @_dhwmode
  277. getCoolingStatus: () -> return Promise.resolve @_coolingstatus
  278. getCentralHeating2Mode: () -> return Promise.resolve @_ch2mode
  279. getDiagnostics: () -> return Promise.resolve @_diag
  280. getCentralHeatingEnable: () -> return Promise.resolve @_chenable
  281. getDomesticHotWaterEnable: () -> return Promise.resolve @_dhwenable
  282. getCoolingEnable: () -> return Promise.resolve @_coolingenable
  283. getOTCState: () -> return Promise.resolve @_otcstate
  284. getCentralHeating2Enable: () -> return Promise.resolve @_ch2enable
  285.  
  286. #Setters
  287. _setFlame: (state) ->
  288. if @_flame isnt state
  289. @_flame = state
  290. @emit 'Flame', state
  291. _setCHMode: (state) ->
  292. if @_chmode isnt state
  293. @_chmode = state
  294. @emit 'CentralHeatingMode', state
  295. _setFault: (state) ->
  296. if @_fault isnt state
  297. @_fault = state
  298. @emit 'Fault', state
  299. _setDHWMode: (state) ->
  300. if @_dhwmode isnt state
  301. @_dhwmode = state
  302. @emit 'DomesticHotWaterMode', state
  303. _setCoolingStatus: (state) ->
  304. if @_coolingstatus isnt state
  305. @_coolingstatus = state
  306. @emit 'CoolingStatus', state
  307. _setCH2Mode: (state) ->
  308. if @_ch2mode isnt state
  309. @_ch2mode = state
  310. @emit 'CentralHeating2Mode', state
  311. _setDiagnostics: (state) ->
  312. if @_diag isnt state
  313. @_diag = state
  314. @emit 'Diagnostics', state
  315. _setCHEnable: (state) ->
  316. if @_chenable isnt state
  317. @_chenable = state
  318. @emit 'CentralHeatingEnable', state
  319. _setDHWEnable: (state) ->
  320. if @_dhwenable isnt state
  321. @_dhwenable = state
  322. @emit 'DomesticHotWaterEnable', state
  323. _setCoolingEnable: (state) ->
  324. if @_coolingenable isnt state
  325. @_coolingenable = state
  326. @emit 'CoolingEnable', state
  327. _setOTCState: (state) ->
  328. if @_otcstate isnt state
  329. @_otcstate = state
  330. @emit 'OTCState', state
  331. _setCH2Enable: (state) ->
  332. if @_ch2enable isnt state
  333. @_ch2enable = state
  334. @emit 'CentralHeating2Enable', state
  335.  
  336. #Functions
  337. _bitToBool: (value) ->
  338. return (value is "1")
  339.  
  340. class OTGWHeatingThermostat extends env.devices.HeatingThermostat
  341.  
  342. constructor: (@config, lastState) ->
  343. @id = @config.id
  344. @name = @config.name
  345. @_temperatureSetpoint = lastState?.temperatureSetpoint?.value
  346. @_mode = lastState?.mode?.value or "auto"
  347. @_battery = lastState?.battery?.value or "ok"
  348. @_lastSendTime = 0
  349.  
  350. plugin.otgw.on("room_setpoint", (data) =>
  351. if data?
  352. data = Number(data)
  353. now = new Date().getTime()
  354. ###
  355. Give the gateway some time to handle the changes. If we send new values to the cube
  356. we set _lastSendTime to the current time. We consider the values as succesfull set, when
  357. the command was not rejected.
  358.  
  359. In the case that the gateway did not react to our the send commands, the values will be
  360. overwritten with the internal state (old ones) of the gateway after 30 seconds.
  361. ###
  362. if @_mode is "auto"
  363. @_setSetpoint(data) #override from gateway
  364. @_setSynced(true)
  365.  
  366. ###
  367. if now - @_lastSendTime < 30*1000
  368. # only if values match, we are synced
  369. if data is @_temperatureSetpoint
  370. @_setSynced(true)
  371. else
  372. # more then 30 seconds passed, set the values anyway
  373. @_setSetpoint(data) #override from gateway
  374.  
  375. @_setSynced(true)
  376. ###
  377.  
  378. return
  379. )
  380. plugin.otgw.on("remote_override_setpoint", (data) =>
  381. if data?
  382. data = Number(data)
  383. now = new Date().getTime()
  384. ###
  385. Give the gateway some time to handle the changes. If we send new values to the cube
  386. we set _lastSendTime to the current time. We consider the values as succesfull set, when
  387. the command was not rejected.
  388.  
  389. In the case that the gateway did not react to our the send commands, the values will be
  390. overwritten with the internal state (old ones) of the gateway after 30 seconds.
  391. ###
  392.  
  393.  
  394. if @_mode is "manu"
  395. if data < 1
  396. env.logger.debug "setting to auto"
  397. @_setMode("auto")
  398. else
  399. if now - @_lastSendTime < 30*1000
  400. # only if values match, we are synced
  401. if data is @_temperatureSetpoint
  402. @_setSynced(true)
  403. else
  404. # more then 30 seconds passed, set the values anyway
  405. @_setSetpoint(data)
  406. @_setSynced(true)
  407. if @_mode is "auto" and data > 0.00
  408. @_setMode("manu")
  409.  
  410. return
  411. )
  412. super()
  413.  
  414. changeModeTo: (mode) ->
  415. temp = @_temperatureSetpoint
  416. if mode is "auto"
  417. temp = null
  418. return plugin.setTemperatureSetpoint(mode, temp).then( =>
  419. @_lastSendTime = new Date().getTime()
  420. @_setSynced(false)
  421. @_setMode(mode)
  422. )
  423.  
  424. changeTemperatureTo: (temperatureSetpoint) ->
  425. if @temperatureSetpoint is temperatureSetpoint then return
  426. return plugin.setTemperatureSetpoint(@_mode, temperatureSetpoint).then( =>
  427. @_lastSendTime = new Date().getTime()
  428. @_setSynced(false)
  429. @_setSetpoint(temperatureSetpoint)
  430. )
  431.  
  432. class OTGWMainThermostat extends env.devices.TemperatureSensor
  433. _temperature: null
  434.  
  435. constructor: (@config, lastState) ->
  436. @id = @config.id
  437. @name = @config.name
  438. @_temperature = lastState?.temperature?.value
  439. super()
  440.  
  441. plugin.otgw.on("room_temperature", (data) =>
  442. if data?
  443. @_temperature = Number(data)
  444. @emit 'temperature', @_temperature
  445. )
  446.  
  447. getTemperature: -> Promise.resolve(@_temperature)
  448.  
  449. class OTGWMainThermostat1 extends env.devices.Device
  450.  
  451. temperature: null
  452. temperature1: null
  453. temperature2: null
  454. temperature3: null
  455. temperature4: null
  456. temperature5: null
  457. temperature6: null
  458. temperature7: null
  459. temperature8: null
  460. temperature9: null
  461. temperature10: null
  462. temperature11: null
  463. temperature12: null
  464.  
  465. attributes:
  466. temperature:
  467. description: "Temperature"
  468. type: "number"
  469. unit: '°C'
  470. acronym: 'Control Setpoint'
  471. temperature1:
  472. description: "Temperature1"
  473. type: "number"
  474. unit: '°C'
  475. acronym: 'Remote Override Setpoint'
  476. temperature2:
  477. description: "Temperature2"
  478. type: "number"
  479. unit: '%'
  480. acronym: 'Max Relative Modulation Level'
  481. temperature3:
  482. description: "Temperature3"
  483. type: "number"
  484. unit: '°C'
  485. acronym: 'Room Setpoint'
  486. temperature4:
  487. description: "Temperature4"
  488. type: "number"
  489. unit: '%'
  490. acronym: 'Relative Modulation Level'
  491. temperature5:
  492. description: "Temperature5"
  493. type: "number"
  494. unit: 'Bar'
  495. acronym: 'Water Pressure'
  496. temperature6:
  497. description: "Temperature6"
  498. type: "number"
  499. unit: '°C'
  500. acronym: 'Room Temperature'
  501. temperature7:
  502. description: "Temperature7"
  503. type: "number"
  504. unit: '°C'
  505. acronym: 'Boiler Water Temperature'
  506. temperature8:
  507. description: "Temperature8"
  508. type: "number"
  509. unit: '°C'
  510. acronym: 'DWH Temperature'
  511. temperature9:
  512. description: "Temperature9"
  513. type: "number"
  514. unit: '°C'
  515. acronym: 'Outside Temperature'
  516. temperature10:
  517. description: "Temperature10"
  518. type: "number"
  519. unit: '°C'
  520. acronym: 'Return Water Temperature'
  521. temperature11:
  522. description: "Temperature11"
  523. type: "number"
  524. unit: '°C'
  525. acronym: 'DWH Setpoint'
  526. temperature12:
  527. description: "Temperature12"
  528. type: "number"
  529. unit: '°C'
  530. acronym: 'Max CH Water Setpoint'
  531.  
  532. constructor: (@config, lastState) ->
  533. @id = @config.id
  534. @name = @config.name
  535. @temperature = lastState?.temperature?.value
  536. @temperature1 = lastState?.temperature1?.value
  537. @temperature2 = lastState?.temperature2?.value
  538. @temperature3 = lastState?.temperature3?.value
  539. @temperature4 = lastState?.temperature4?.value
  540. @temperature5 = lastState?.temperature5?.value
  541. @temperature6 = lastState?.temperature6?.value
  542. @temperature7 = lastState?.temperature7?.value
  543. @temperature8 = lastState?.temperature8?.value
  544. @temperature9 = lastState?.temperature9?.value
  545. @temperature10 = lastState?.temperature10?.value
  546. @temperature11 = lastState?.temperature11?.value
  547. @temperature12 = lastState?.temperature12?.value
  548. super()
  549.  
  550. plugin.otgw.on("control_setpoint", (data) =>
  551. if data?
  552. @temperature = Number(data)
  553. @emit 'temperature', @temperature
  554. )
  555. plugin.otgw.on("remote_override_setpoint", (data) =>
  556. if data?
  557. @temperature1 = Number(data)
  558. @emit 'temperature1', @temperature1
  559. )
  560. plugin.otgw.on("max_relative_modulation_level", (data) =>
  561. if data?
  562. @temperature2 = Number(data)
  563. @emit 'temperature2', @temperature2
  564. )
  565. plugin.otgw.on("room_setpoint", (data) =>
  566. if data?
  567. @temperature3 = Number(data)
  568. @emit 'temperature3', @temperature3
  569. )
  570. plugin.otgw.on("relative_modulation_level", (data) =>
  571. if data?
  572. @temperature4 = Number(data)
  573. @emit 'temperature4', @temperature4
  574. )
  575. plugin.otgw.on("ch_water_pressure", (data) =>
  576. if data?
  577. @temperature5 = Number(data)
  578. @emit 'temperature5', @temperature5
  579. )
  580. plugin.otgw.on("room_temperature", (data) =>
  581. if data?
  582. @temperature6 = Number(data)
  583. @emit 'temperature6', @temperature6
  584. )
  585. plugin.otgw.on("boiler_water_temperature", (data) =>
  586. if data?
  587. @temperature7 = Number(data)
  588. @emit 'temperature7', @temperature7
  589. )
  590. plugin.otgw.on("dhw_temperature", (data) =>
  591. if data?
  592. @temperature8 = Number(data)
  593. @emit 'temperature8', @temperature8
  594. )
  595. plugin.otgw.on("outside_temperature", (data) =>
  596. if data?
  597. @temperature9 = Number(data)
  598. @emit 'temperature9', @temperature9
  599. )
  600. plugin.otgw.on("return_water_temperature", (data) =>
  601. if data?
  602. @temperature10 = Number(data)
  603. @emit 'temperature10', @temperature10
  604. )
  605. plugin.otgw.on("dhw_setpoint", (data) =>
  606. if data?
  607. @temperature11 = Number(data)
  608. @emit 'temperature11', @temperature11
  609. )
  610. plugin.otgw.on("max_ch_water_setpoint", (data) =>
  611. if data?
  612. @temperature12 = Number(data)
  613. @emit 'temperature12', @temperature12
  614. )
  615.  
  616. getTemperature: -> Promise.resolve(@temperature)
  617. getTemperature1: -> Promise.resolve(@temperature1)
  618. getTemperature2: -> Promise.resolve(@temperature2)
  619. getTemperature3: -> Promise.resolve(@temperature3)
  620. getTemperature4: -> Promise.resolve(@temperature4)
  621. getTemperature5: -> Promise.resolve(@temperature5)
  622. getTemperature6: -> Promise.resolve(@temperature6)
  623. getTemperature7: -> Promise.resolve(@temperature7)
  624. getTemperature8: -> Promise.resolve(@temperature8)
  625. getTemperature9: -> Promise.resolve(@temperature9)
  626. getTemperature10: -> Promise.resolve(@temperature10)
  627. getTemperature11: -> Promise.resolve(@temperature11)
  628. getTemperature12: -> Promise.resolve(@temperature12)
  629.  
  630. class OTGWDomesticHotWater extends env.devices.Device
  631.  
  632. temperature: null
  633. temperature1: null
  634. temperature2: null
  635. temperature3: null
  636.  
  637. attributes:
  638. temperature:
  639. description: "Temperature"
  640. type: "number"
  641. acronym: 'Pump Starts'
  642. temperature1:
  643. description: "Temperature1"
  644. type: "number"
  645. unit: 'H'
  646. acronym: 'Pump Hours'
  647. temperature2:
  648. description: "Temperature2"
  649. type: "number"
  650. acronym: 'Burner Starts'
  651. temperature3:
  652. description: "Temperature3"
  653. type: "number"
  654. unit: 'H'
  655. acronym: 'Burner Hours'
  656.  
  657. constructor: (@config, lastState) ->
  658. @id = @config.id
  659. @name = @config.name
  660. @temperature = lastState?.temperature?.value
  661. @temperature1 = lastState?.temperature1?.value
  662. @temperature2 = lastState?.temperature2?.value
  663. @temperature3 = lastState?.temperature3?.value
  664. super()
  665.  
  666. plugin.otgw.on("dhw_pump_starts", (data) =>
  667. if data?
  668. @temperature = Number(data)
  669. @emit 'temperature', @temperature
  670. )
  671. plugin.otgw.on("dhw_pump_operation_hours", (data) =>
  672. if data?
  673. @temperature1 = Number(data)
  674. @emit 'temperature1', @temperature1
  675. )
  676. plugin.otgw.on("dhw_burner_starts", (data) =>
  677. if data?
  678. @temperature2 = Number(data)
  679. @emit 'temperature2', @temperature2
  680. )
  681. plugin.otgw.on("dhw_burner_operation_hours", (data) =>
  682. if data?
  683. @temperature3 = Number(data)
  684. @emit 'temperature3', @temperature3
  685. )
  686.  
  687. getTemperature: -> Promise.resolve(@temperature)
  688. getTemperature1: -> Promise.resolve(@temperature1)
  689. getTemperature2: -> Promise.resolve(@temperature2)
  690. getTemperature3: -> Promise.resolve(@temperature3)
  691.  
  692. class OTGWCentralHeating extends env.devices.Device
  693.  
  694. temperature: null
  695. temperature1: null
  696. temperature2: null
  697. temperature3: null
  698.  
  699. attributes:
  700. temperature:
  701. description: "Temperature"
  702. type: "number"
  703. acronym: 'Pump Starts'
  704. temperature1:
  705. description: "Temperature1"
  706. type: "number"
  707. unit: 'H'
  708. acronym: 'Pump Hours'
  709. temperature2:
  710. description: "Temperature2"
  711. type: "number"
  712. acronym: 'Burner Starts'
  713. temperature3:
  714. description: "Temperature3"
  715. type: "number"
  716. unit: 'H'
  717. acronym: 'Burner Hours'
  718.  
  719. constructor: (@config, lastState) ->
  720. @id = @config.id
  721. @name = @config.name
  722. @temperature = lastState?.temperature?.value
  723. @temperature1 = lastState?.temperature1?.value
  724. @temperature2 = lastState?.temperature2?.value
  725. @temperature3 = lastState?.temperature3?.value
  726. super()
  727.  
  728. plugin.otgw.on("ch_pump_starts", (data) =>
  729. if data?
  730. @temperature = Number(data)
  731. @emit 'temperature', @temperature
  732. )
  733. plugin.otgw.on("ch_pump_operation_hours", (data) =>
  734. if data?
  735. @temperature1 = Number(data)
  736. @emit 'temperature1', @temperature1
  737. )
  738. plugin.otgw.on("burner_starts", (data) =>
  739. if data?
  740. @temperature2 = Number(data)
  741. @emit 'temperature2', @temperature2
  742. )
  743. plugin.otgw.on("burner_operation_hours", (data) =>
  744. if data?
  745. @temperature3 = Number(data)
  746. @emit 'temperature3', @temperature3
  747. )
  748.  
  749. getTemperature: -> Promise.resolve(@temperature)
  750. getTemperature1: -> Promise.resolve(@temperature1)
  751. getTemperature2: -> Promise.resolve(@temperature2)
  752. getTemperature3: -> Promise.resolve(@temperature3)
  753.  
  754. class OTGWFlamePresenceSensor extends env.devices.PresenceSensor
  755.  
  756. @_presence = false
  757.  
  758. constructor: (@config) ->
  759. @id = @config.id
  760. @name = @config.name
  761.  
  762. plugin.otgw.on("flame_status" , (data) =>
  763. if data.length = 16
  764. @_setPresence(@_bitToBool(data.slice(12,13)))
  765. )
  766. super()
  767.  
  768. #Getters
  769. getPresence: () -> return Promise.resolve @_flame
  770.  
  771. #No need for setter, _setPresence() is implemented by super class
  772.  
  773. #Functions
  774. _bitToBool: (value) ->
  775. return (value is "1")
  776.  
  777. class OTGWMainThermostat2 extends env.devices.Device
  778.  
  779. attributeTemplates =
  780. ch_pump_starts:
  781. description: "Temperature"
  782. type: "number"
  783. acronym: 'Pump Starts'
  784. ch_pump_operation_hours:
  785. description: "Temperature1"
  786. type: "number"
  787. unit: 'H'
  788. acronym: 'Pump Hours'
  789. dhw_burner_starts:
  790. description: "Temperature2"
  791. type: "number"
  792. acronym: 'Burner Starts'
  793. dhw_burner_operation_hours:
  794. description: "Temperature3"
  795. type: "number"
  796. unit: 'H'
  797. acronym: 'Burner Hours'
  798.  
  799. constructor: (@config, @plugin, lastState) ->
  800. @debug = @plugin.config.debug ? false
  801. @base = commons.base @, @config.class unless @base?
  802. @attributeValues = new AttributeContainer()
  803. @attributes = _.cloneDeep(@attributes)
  804.  
  805. for attributeName in @config.attributes
  806. do (attributeName) =>
  807. if attributeTemplates.hasOwnProperty attributeName
  808. properties = attributeTemplates[attributeName]
  809. @attributes[attributeName] =
  810. description: properties.description || attributeName.replace /(^[a-z])|([A-Z])/g, ((match, p1, p2, offset) =>
  811. (if offset>0 then " " else "") + match.toUpperCase())
  812. type: properties.type
  813. unit: properties.unit if properties.unit?
  814. acronym: properties.acronym if properties.acronym?
  815.  
  816. @attributeValues.values[attributeName] = 0
  817.  
  818. @attributeValues.on properties.key, ((value) =>
  819. @base.debug "Received update for", properties.key, value
  820. if value.value? and @attributeValues.values[attributeName] isnt value.value
  821. @attributeValues.values[attributeName] = value.value
  822. @emit attributeName, value.value
  823. )
  824.  
  825. @_createGetter(attributeName, =>
  826. return Promise.resolve @attributeValues[attributeName]
  827. )
  828. else
  829. @base.error "Configuration Error. No such attribute: #{attributeName} - skipping."
  830.  
  831. super(@config, @plugin, "GetComponentsData")
  832.  
  833. @on 'data', ((error, values) =>
  834. if error or not values
  835. if error
  836. @base.error error
  837. else
  838. data = values.Body.Data
  839. for key,value of data
  840. @attributeValues.emit key, value if value?
  841. )
  842.  
  843. destroy: () ->
  844. super()
  845.  
  846. return plugin
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement