@mwittig @Icesory
So, after some break I went back to see if I can get this dewpoint plugin running. In fact with your help and some readings I finally managed to read variables from the variable manager and to calculate the dewpoint. The dewpoint value is right now also displayed in my gui, but it is not finished, yet. I would like to have the two variables I read (temperature and humidity) and the dewpoint in one section on the gui including the right @attribute.unit and @attribute.acronym.
There are some points which I until now did not completely understand I guess.
My code is basicly in most parts a copy of the variables device section as you suggested. What I still did not completely understand is the mechanism of return values of these methods.
- The next part which I do not know is the right usage of @_createGetter and @emit. I call @emit inside the constructor and after the main function of the plugin. I guess that’s not right.
This here is my code
module.exports = (env) ->
Promise = env.require 'bluebird'
assert = env.require 'cassert'
class DewpointPlugin extends env.plugins.Plugin
init: (app, @framework, @config) =>
env.logger.info("Hello new World")
deviceConfigDef = require("./device-config-schema")
@framework.deviceManager.registerDeviceClass("DewpointDevice", {
configDef: deviceConfigDef.DewpointDevice,
createCallback: (config, lastState) =>
return new DewpointDevice(config, lastState)
})
Dewpoint = new DewpointPlugin
class DewpointDevice extends env.devices.TemperatureSensor
temperature: null
T: null
H: null
constructor: (@config, lastState) ->
@id = config.id
@name = config.name
@temperature = lastState?.temperature?.value
@T = lastState?.T?.value
@H = lastState?.H?.value
@varManager = Dewpoint.framework.variableManager #so you get the variableManager
@_exprChangeListeners = []
@attributes = {}
for variable in @config.variables
do (variable) =>
name = variable.name
info = null
@attributes[name] = {
description: name
label: (if variable.label? then variable.label else "$#{name}")
type: variable.type or "string"
}
evaluate = ( =>
# wait till veraibelmanager is ready
return Promise.delay(1).then( =>
unless info?
info = @varManager.parseVariableExpression(variable.allocation)
@varManager.notifyOnChange(info.tokens, evaluate)
@_exprChangeListeners.push evaluate
if @attributes[name].type is "number"
unless @attributes[name].unit? and @attributes[name].unit.length > 0
@attributes[name].unit = @varManager.inferUnitOfExpression(info.tokens)
switch info.datatype
when "numeric" then @varManager.evaluateNumericExpression(info.tokens)
when "string" then @varManager.evaluateStringExpression(info.tokens)
else assert false
).then( (val) =>
if val isnt @_attributesMeta[name].value
console.log(name)
console.log(val)
console.log(evaluate)
@emit name, val
switch variable.name
when "humidity" then @H = val
when "temperature" then @T = val
return val
)
)
@_createGetter(name, evaluate)
super()
@doYourStuff()
setInterval( ( => @doYourStuff() ), @config.interval)
doYourStuff: ->
a=7.5
b=237.3
#T=23 #temperature
#H=40 #humidity
sdd=6.1078 * Math.pow(10,(a*@T)/(b+@T))
dd=sdd*(@H/100)
v=Math.log(dd/6.1078)/Math.log(10)
td=(b*v)/(a-v)
@temperature = td
@emit 'temperature', td
getTemperature: -> Promise.resolve(@temperature)
return Dewpoint
I hope I don’t bug you too much with all this questions. But maybe someone can help me here or tell me where I find these informations.