This commit is contained in:
Simon Zeyer 2024-09-24 09:38:11 +00:00
parent f6fc542d81
commit 5b923badf7

View File

@ -161,7 +161,10 @@ coreDb.openCoreDatabase((successDatabase) => {
}
});
/**
* authenticate Divera Monitor with Autologin Key
* @returns boolean
*/
async function auth(){
let resp = await client.get(`${AUTH_ENDPOINT}${config.MONITOR_ID}.html?autologin=${config.AUTOLOGIN_KEY}`)
if(resp.status === 200){
@ -170,6 +173,16 @@ async function auth(){
return false
}
/**
* Get Data from Divera pull Endpoint. With ts=0 all data is pulled.
* If ?ts={timestamp}, data changed since this timestamp is pulled.
* We just pull all data.
*
* The Divera Monitor App also connects to a websocket connection where changes are pushed.
* Maybe this is a better idea to pull data in the future than pulling in a while loop
*
* @returns {dict|null}
*/
async function pull(){
let resp = await client.get(`${PULL_ENDPOINT}?ts=0`)
if(resp.status === 200){
@ -178,34 +191,47 @@ async function pull(){
return null
}
/**
* Function handling the state change of an User.
* The feedback-status-id is identified by name and basedata(user)-(uu)id is identified by firstname and lastname
* So Divera feedback-status/firstname & lastname must be the same as in ALARMiator
*
* Feedback-state changes are applyed to the lates active operation
*
* if STATUS_EB/STATUS_NED is set, all groups for this user are muted/unmuted if the name is matching
*
* @param {Number} consumer_id
* @param {Number} status_id
*/
async function onStateChange(consumer_id, status_id){
var d_consumer = pulled_data['consumer'][consumer_id]
var d_status = pulled_data['status'][status_id]
global.logger.info(`${d_consumer['stdformat_name']}: ${d_status['name']}`)
global.logger.info(`${d_consumer.stdformat_name.trim()}: ${d_status.name.trim()}`)
let a_consumer = await getMemberByName(d_consumer.firstname, d_consumer.lastname)
let a_consumer = await getMemberByName(d_consumer.firstname.trim(), d_consumer.lastname.trim())
if(a_consumer === null){
global.logger.debug(`basedata nicht gefunden`)
global.logger.info(`no matching basedata for \'${d_consumer.firstname.trim()}\' \'${d_consumer.lastname.trim()}\'`)
return
}
let a_status = await getFeedbackStateIDbyName(d_status['name'])
let a_status = await getFeedbackStateIDbyName(d_status.name.trim())
if(a_status !== null){
// Status ist eine Rückmeldung
// state is feedback to an operation
let a_operation = await getActiveOperation()
if(a_consumer === null){
global.logger.debug(`kein aktiver alarm`)
global.logger.info(`no active operation running`)
return
}
// set feedback go last operation
setFeedback(a_operation, a_consumer, a_status)
}else{
// Status auf einsatzbereit/nicht einsatzbereit prüfen
if(config.STATUS_EB == d_status['name']){
if(config.STATUS_EB.trim() == d_status.name.trim()){
//Alle Gruppen alamieren
setGroupsMutedStateForMember(a_consumer,false)
}
if(config.STATUS_NEB == d_status['name']){
if(config.STATUS_NEB.trim() == d_status.name.trim()){
//Alle Gruppen alamieren deaktivieren
setGroupsMutedStateForMember(a_consumer,true)
}
@ -228,6 +254,7 @@ async function startAPI() {
if('monitor' in d){
var m = d['monitor'][config.MONITOR_ID]
Object.entries(m).forEach(([key, value]) => {
// wenn 'monitor' nicht in pulled_data ist das der erste pull, dann alle status in ALARMiator abgleichen (betrifft hautsächlich den EB/NEB status)
if(!('monitor' in pulled_data) || ('monitor' in pulled_data && value.status !== pulled_data['monitor'][key]['status'])){
onStateChange(value.id,value.status )
}
@ -238,13 +265,14 @@ async function startAPI() {
}
_pulled_data = await pull()
if(_pulled_data === null){
global.logger.info('_pulled_data === null, reauth')
global.logger.debug('_pulled_data === null, reauth')
_auth = await auth()
_pulled_data = await pull()
if(_pulled_data === null){
global.logger.info('_pulled_data === null after reauth, EXIT!!')
global.logger.debug('_pulled_data === null after reauth, EXIT!!')
}
}
}
}
global.logger.info('auth mit API_KEY war nicht erfolgreich. Service ist gestoppt und startet nicht mehr automatisch??')
}