Commit 8b9a113e authored by Peter Müller's avatar Peter Müller

Added PresenceSensor device

parent e2d5fa72
......@@ -44,6 +44,24 @@ module.exports = {
type: "boolean"
}
PCF8574PresenceSensor: {
title: "PCF8574PresenceSensor config options"
type: "object"
extensions: ["xLink", "xClosedLabel", "xOpenedLabel"]
properties:
PCF8574IC:
description: "The ID of PCF8574IC device to which this device is connected. This device must be created first. If the PCF8574IC device gets deleted, this PCF8574PresenceSensor device will be deleted too."
type: "string"
enum: []
pinNumber:
description: "The pin number at the PCF8574 IC."
type: "integer"
enum: [0,1,2,3,4,5,6,7]
inverted:
description: "If this pin should be handled inverted or not. (inverted means true=low and false=high)"
type: "boolean"
}
PCF8574Switch: {
title: "PCF8574Switch config options"
type: "object"
......
......@@ -21,7 +21,7 @@ module.exports = (env) ->
env.i2cBus = require('i2c-bus').openSync(1) if !env.i2cBus?
# enum of devices which need a PCF8574IC device
pcf8574Devices = ['PCF8574ContactSensor', 'PCF8574Switch']
pcf8574Devices = ['PCF8574ContactSensor', 'PCF8574PresenceSensor', 'PCF8574Switch']
# ###PimaticPCF8574 class
class PimaticPCF8574 extends env.plugins.Plugin
......@@ -55,6 +55,13 @@ module.exports = (env) ->
return device
})
@framework.deviceManager.registerDeviceClass('PCF8574PresenceSensor', {
configDef: deviceConfigDef.PCF8574PresenceSensor,
createCallback: (deviceConfig, lastState) =>
device = new PCF8574PresenceSensor(deviceConfig, lastState, @framework)
return device
})
@framework.deviceManager.registerDeviceClass('PCF8574Switch', {
configDef: deviceConfigDef.PCF8574Switch,
createCallback: (deviceConfig, lastState) =>
......@@ -158,6 +165,42 @@ module.exports = (env) ->
destroy: ->
super()
# ###PCF8574PresenceSensor class
class PCF8574PresenceSensor extends env.devices.PresenceSensor
constructor: (@config, lastState, @framework) ->
env.logger.debug('PCF8574PresenceSensor', @config, lastState)
@id = @config.id
@name = @config.name
@_presence = lastState?.presence?.value or false
# get the PCF8574IC instance and check it
@pcf8574ic = @framework.deviceManager.getDeviceById(@config.PCF8574IC)
assert @pcf8574ic? and @pcf8574ic instanceof PCF8574IC, "No PCF8574IC device found with the given ID"
# check the pin number
assert typeof @config.pinNumber is 'number'
assert 7 >= @config.pinNumber >= 0, "The pinNumber must be between 0 and 7"
# set pin as input
@pcf8574ic.ic.inputPin @config.pinNumber, @config.inverted? or false
# handle input changes
@pcf8574ic.ic.on('input', inputListener = (data) =>
return if data.pin != @config.pinNumber
env.logger.debug "#{@id} input event", data
@_setPresence data.value
)
@on 'destroy', () => @pcf8574ic.ic.removeListener('input', inputListener)
super()
destroy: ->
super()
# ###PCF8574Switch class
class PCF8574Switch extends env.devices.PowerSwitch
constructor: (@config, lastState, @framework) ->
env.logger.debug('PCF8574ContactSensor', @config, lastState)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment