Skip to content

Commit

Permalink
location of device offline notification changed
Browse files Browse the repository at this point in the history
  • Loading branch information
BenAhrdt committed May 10, 2024
1 parent b88ecfe commit 444a2a7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 63 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ For now there is documentation in English here: https://wiki.hafenmeister.de
Placeholder for the next version (at the beginning of the line):
### **WORK IN PROGRESS**
-->
### **WORK IN PROGRESS**
* (BenAhrdt) device offline location changed

### 1.0.4 (2024-05-10)
* (BenAhrdt) changed icon and offline time

Expand Down
67 changes: 67 additions & 0 deletions lib/modules/directorieshandler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { isDeepStrictEqual } = require("util");
const schedule = require("node-schedule");

class directorieshandlerClass {
constructor(adapter) {
Expand Down Expand Up @@ -32,6 +33,14 @@ class directorieshandlerClass {
wifi: "wifi"
};

this.cronJobs = {};
this.cronJobIds = {
checkTimestamp: "checkTimestamp"
};
this.cronJosValues = {
checkTimestamp: "* * * * *"
};

/*********************************************************************
* ********************* Definition of Assigns **********************
* ******************************************************************/
Expand Down Expand Up @@ -189,6 +198,64 @@ class directorieshandlerClass {
objectNative: "objectNative",
objectIconPath: "objectIconPath"
};

// Create cronjob to check timestamps (for device offline notification)
this.cronJobs[this.cronJobIds.checkTimestamp] = schedule.scheduleJob(this.cronJosValues.checkTimestamp,this.checkTimestamps.bind(this));

}

/*********************************************************************
* ************************ Check Timestamp *****************************
* ******************************************************************/

async checkTimestamps(){
const activeFunction = "checkTimestamps";
try{
const adapterObjects = await this.adapter.getAdapterObjectsAsync();
// Generate Infos of all defices and decoded folders
for(const adapterObject of Object.values(adapterObjects)){
if(adapterObject._id.endsWith(`${this.reachableSubfolders.uplinkRaw}.json`)){
const uplinkState = await this.adapter.getStateAsync(adapterObject._id);
if(uplinkState){
const msInOneHour = 1000 * 60 * 60;
const maxDifferenceMin = 0 * msInOneHour;
const maxDifferenceMax = 1 * msInOneHour;
const timestampNow = Date.now();
const timestampData = uplinkState.ts;
const difference = timestampNow - timestampData;
if(difference >= maxDifferenceMin && difference <= maxDifferenceMax){
const changeInfo = await this.adapter.getChangeInfo(adapterObject._id);
if(changeInfo){
this.adapter.registerNotification("lorawan", "LoRaWAN device offline", `The LoRaWAN device ${changeInfo.usedDeviceId} in the application ${changeInfo.usedApplicationName} is offline`);
const deviceFolderId = adapterObject._id.substring(0,adapterObject._id.indexOf(".uplink"));
const deviceFolderObject = await this.adapter.getObjectAsync(deviceFolderId);
// Set foldericon to low / no connection
if(deviceFolderObject){
deviceFolderObject.common.icon = "icons/wifiSfX_0.png";
await this.adapter.extendObjectAsync(deviceFolderId,{
common: deviceFolderObject.common,
});
}

//const devicefolder = await this.getObjectAsync()
}
}
}
}
}
}
catch(error){
this.adapter.log.error(`error at ${activeFunction}: ` + error);
}
}

// Clear all schedules, if there are some
clearAllSchedules(){
for(const cronJob in this.cronJobs)
{
schedule.cancelJob(this.cronJobs[cronJob]);
delete this.cronJobs[cronJob];
}
}

/*********************************************************************
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/messagehandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class messagehandlerClass {
const activeFunction = "generateDeviceinfosAtStartup";
try{
const adapterObjectsAtStart = await this.adapter.getAdapterObjectsAsync();
// Generate Infos of all defices and decoded folders
// Generate Infos of all devices and decoded folders
for(const adapterObject of Object.values(adapterObjectsAtStart)){
if(adapterObject._id.endsWith(`${this.directoryhandler.reachableSubfolders.uplinkRaw}.json`)){
const uplinkState = await this.adapter.getStateAsync(adapterObject._id);
Expand Down
67 changes: 5 additions & 62 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require("@iobroker/adapter-core");
const schedule = require("node-schedule");
const mqttClientClass = require("./lib/modules/mqttclient");
const messagehandlerClass = require("./lib/modules/messagehandler");
const downlinkConfighandlerClass = require("./lib/modules/downlinkConfighandler");
Expand All @@ -33,14 +32,6 @@ class Lorawan extends utils.Adapter {
chirpstack: "chirpstack"
};

this.cronJobs = {};
this.cronJobIds = {
checkTimestamp: "checkTimestamp"
};
this.cronJosValues = {
checkTimestamp: "0 * * * *"
};

// Simulation variables
this.simulation = {};
}
Expand Down Expand Up @@ -75,9 +66,6 @@ class Lorawan extends utils.Adapter {
this.log.silly(`the adapter starts with downlinkconfigs: ${JSON.stringify(this.config.downlinkConfig)}.`);
this.log.silly(`the active downlinkconfigs are: ${JSON.stringify(this.downlinkConfighandler.activeDownlinkConfigs)}`);

// Create cronjob to check timestamps (for device offline notification)
this.cronJobs[this.cronJobIds.checkTimestamp] = schedule.scheduleJob(this.cronJosValues.checkTimestamp,this.checkTimestamps.bind(this));

/*setTimeout(async () => {
await this.startSimulation();
}, 5000);*/
Expand All @@ -93,47 +81,6 @@ class Lorawan extends utils.Adapter {
}
}

async checkTimestamps(){
const activeFunction = "checkTimestamps";
try{
const adapterObjects = await this.getAdapterObjectsAsync();
// Generate Infos of all defices and decoded folders
for(const adapterObject of Object.values(adapterObjects)){
if(adapterObject._id.endsWith(`${this.messagehandler?.directoryhandler.reachableSubfolders.uplinkRaw}.json`)){
const uplinkState = await this.getStateAsync(adapterObject._id);
if(uplinkState){
const msInOneHour = 1000 * 60 * 60;
const maxDifferenceMin = 25 * msInOneHour;
const maxDifferenceMax = 26 * msInOneHour;
const timestampNow = Date.now();
const timestampData = uplinkState.ts;
const difference = timestampNow - timestampData;
if(difference >= maxDifferenceMin && difference <= maxDifferenceMax){
const changeInfo = await this.getChangeInfo(adapterObject._id);
if(changeInfo){
this.registerNotification("lorawan", "LoRaWAN device offline", `The LoRaWAN device ${changeInfo.usedDeviceId} in the application ${changeInfo.usedApplicationName} is offline`);
const deviceFolderId = adapterObject._id.substring(0,adapterObject._id.indexOf(".uplink"));
const deviceFolderObject = await this.getObjectAsync(deviceFolderId);
// Set foldericon to low / no connection
if(deviceFolderObject){
deviceFolderObject.common.icon = "icons/wifiSfX_0.png";
await this.extendObjectAsync(deviceFolderId,{
common: deviceFolderObject.common,
});
}

//const devicefolder = await this.getObjectAsync()
}
}
}
}
}
}
catch(error){
this.log.error(`error at ${activeFunction}: ` + error);
}
}

async startSimulation(){

// TTN
Expand Down Expand Up @@ -197,6 +144,11 @@ class Lorawan extends utils.Adapter {
this.clearTimeout(this.simulation.timeout);
delete this.simulation.timeout;
}

// Clear Schedules im directoriehandler
this.messagehandler?.directoryhandler.clearAllSchedules();

// Destroy mqtt client
this.mqttClient?.destroy();
callback();
} catch (e) {
Expand Down Expand Up @@ -563,15 +515,6 @@ class Lorawan extends utils.Adapter {
this.log.error(`error at ${activeFunction}: ` + error);
}
}

// Clear all schedules, if there are some
clearAllSchedules(){
for(const cronJob in this.cronJobs)
{
schedule.cancelJob(this.cronJobs[cronJob]);
delete this.cronJobs[cronJob];
}
}
}

if (require.main !== module) {
Expand Down

0 comments on commit 444a2a7

Please sign in to comment.