local app_base =require'app.base'local app = app_base:subclass('EXAMPLE_CALC_APP_IN_GUIDE')app.static.API_VER =5
初始化计算模块
跟采集应用相比,我们在应用初始化时需要初始化计算模块:
function app:on_init()-- 计算帮助模块初始化local calc = self:create_calc()end
数据计算
计算模块提供了监听方式的计算回调:
当所有所需数据就绪时,进行逻辑回调
当任一数据发生变化时,进行逻辑回调
function app:on_start()local source_device_sn ='xxxxxxxxxxxxx.xxx' self._calc:add('unique_name_for_calc', { { sn = source_device_sn, input ='temperature', prop='value'} }, function(temperature)if temperature >40then--- turn the fan on self:control_fan(true) self._fan_on =truereturnendif self._fan_on and temperature <30then--- turn the fan off self:control_fan(false) self._fan_on =falsereturnendend)end
示例代码
local app_base =require'app.base'local app = app_base:subclass('EXAMPLE_CALC_APP_IN_GUIDE')app.static.API_VER =5function app:on_init()-- 计算帮助模块初始化local calc = self:create_calc() self._fan_on =falseendfunction app:on_start()local source_device_sn ='xxxxxxxxxxxxx.xxx' self._calc:add('unique_name_for_calc', { { sn = source_device_sn, input ='temperature', prop='value'} }, function(temperature)if temperature >40then--- turn the fan on self:control_fan(true) self._fan_on =truereturnendif self._fan_on and temperature <30then--- turn the fan off self:control_fan(false) self._fan_on =falsereturnendend)endfunction app:control_fan(on_off)local control_device_sn ='xxxxxxxxxxxxxxxx.xxx'local device = self._api:get_device(control_device_sn)if device thenlocal out_value = on_off and1or0return device:set_output_prop('set_f', 'value', out_value)endendreturn app