2024-04-13 20:29:31 +02:00
exports . id = 'thermometer' ;
exports . title = 'Thermometer' ;
exports . group = 'Worksys' ;
exports . color = '#5CB36D' ;
2024-12-02 16:57:26 +01:00
exports . input = 1 ;
2024-04-13 20:29:31 +02:00
exports . version = '1.0.3' ;
exports . output = [ "red" , "white" , "blue" ] ;
exports . author = 'Rastislav Kovac' ;
exports . icon = 'thermometer-three-quarters' ;
exports . readme = ` # Getting temperature values for RVO. In case of LM, you need device address. In case of unipi, evok sends values, in case thermometer is installed ` ;
2024-12-02 16:57:26 +01:00
const { errLogger , logger , monitor } = require ( './helper/logger' ) ;
const SEND _TO = {
2024-04-13 20:29:31 +02:00
debug : 0 ,
tb : 1 ,
dido _controller : 2
}
//read temperature - frequency
let timeoutMin = 5 ; //minutes
exports . install = function ( instance ) {
const { exec } = require ( 'child_process' ) ;
2024-12-02 16:57:26 +01:00
const { sendNotification } = require ( './helper/notification_reporter' ) ;
2024-04-13 20:29:31 +02:00
let startRead ;
let counter = 0 ;
2024-12-02 16:57:26 +01:00
let rvoTbName = "" ;
let temperatureAddress = "" ;
2024-04-13 20:29:31 +02:00
logger . debug ( exports . title , "installed" ) ;
instance . on ( "close" , function ( ) {
clearInterval ( startRead ) ;
} )
2024-12-02 16:57:26 +01:00
const main = function ( ) {
2024-04-13 20:29:31 +02:00
try {
2024-12-02 16:57:26 +01:00
if ( FLOW . GLOBALS . settings . controller _type === "unipi" )
2024-04-13 20:29:31 +02:00
{
clearInterval ( startRead ) ;
return ;
}
if ( temperatureAddress === "" ) throw "gettemperature: temperatureAddress is not defined" ;
exec ( ` owread -C ${ temperatureAddress } /temperature ` , ( error , stdout , stderr ) => {
2024-12-02 16:57:26 +01:00
if ( ! error )
2024-04-13 20:29:31 +02:00
{
2024-12-02 16:57:26 +01:00
parseData ( stdout )
2024-04-13 20:29:31 +02:00
return ;
}
2024-12-02 16:57:26 +01:00
sendNotification ( "main" , rvoTbName , "thermometer_is_not_responding" , { } , { "Error" : error } , SEND _TO . tb , instance , "thermometer" ) ;
monitor . info ( "Thermometer is not responding" , error ) ;
instance . send ( SEND _TO . dido _controller , { status : "NOK-thermometer" } ) ;
2024-04-13 20:29:31 +02:00
} ) ;
}
catch ( err ) {
errLogger . error ( exports . title , err ) ;
2024-12-02 16:57:26 +01:00
clearInterval ( startRead ) ;
2024-04-13 20:29:31 +02:00
}
}
const parseData = function ( data ) {
data = parseFloat ( data ) ;
logger . debug ( "gettemperature" , data ) ;
if ( ! isNaN ( data ) ) {
if ( counter > 290 )
{
2024-12-02 16:57:26 +01:00
instance . send ( SEND _TO . debug , "[Get temperature component] - temperature data are comming again from RVO after more than 1 day break" ) ;
sendNotification ( "parseData" , rvoTbName , "thermometer_is_responding_again" , { } , "" , SEND _TO . tb , instance , "thermometer" ) ;
2024-04-13 20:29:31 +02:00
}
logger . debug ( "gettemperature" , data ) ;
2024-12-02 16:57:26 +01:00
2024-04-13 20:29:31 +02:00
const values = {
"temperature" : Number ( data . toFixed ( 2 ) ) ,
}
2024-12-02 16:57:26 +01:00
instance . send ( SEND _TO . dido _controller , { values : values } ) ;
2024-04-13 20:29:31 +02:00
counter = 0 ;
} else {
counter ++ ;
monitor . info ( "gettemperature err" , counter , data ) ;
//ked je problem 1 den
let day = 24 * 60 / timeoutMin ;
if ( counter > day && counter < day + 2 ) {
2024-12-02 16:57:26 +01:00
//sendNotification("parseData", rvoTbName, ERRWEIGHT.WARNING, "Thermometer receives invalid data", "", SEND_TO.tb, instance, "thermometer");
sendNotification ( "parseData" , rvoTbName , "thermometer_sends_invalid_data" , { } , "" , SEND _TO . tb , instance , "thermometer" ) ;
2024-04-13 20:29:31 +02:00
2024-12-02 16:57:26 +01:00
instance . send ( SEND _TO . debug , "[Get temperature component] - no temperature data from RVO for more than 1 day" ) ;
instance . send ( SEND _TO . dido _controller , { status : "NOK-thermometer" } ) ;
2024-04-13 20:29:31 +02:00
}
}
}
2024-12-02 16:57:26 +01:00
instance . on ( "data" , _ => {
temperatureAddress = FLOW . GLOBALS . settings . temperature _address ;
rvoTbName = FLOW . GLOBALS . settings . rvoTbName ;
startRead = setInterval ( main , timeoutMin * 1000 * 60 ) ;
main ( ) ;
} )
2024-09-22 22:18:46 +02:00
} ;