Alarmos Intigration
This commit is contained in:
1 parent
5c3cf9d2be
commit
87fac97c12
2 files changed
+90
-31
No files matched your search
+70
-31
@@ -42,50 +42,89 @@ async function getData() {
|
||||
name: vehicle.name,
|
||||
fmsstatus_id: vehicle.fmsstatus_id,
|
||||
fmsstatus_ts: vehicle.fmsstatus_ts,
|
||||
issi: vehicle.issi,
|
||||
};
|
||||
});
|
||||
|
||||
return formatetData;
|
||||
}
|
||||
|
||||
async function hasStatusChanged(newData, oldData) {
|
||||
await newData.forEach(async (vehicle) => {
|
||||
const oldVehicle = oldData.find((x) => x.name == vehicle.name);
|
||||
|
||||
if (vehicle.fmsstatus_id != oldVehicle.fmsstatus_id || !oldVehicle) {
|
||||
// Divera Nachricht
|
||||
if (
|
||||
vehicle.fmsstatus_id == 6 ||
|
||||
vehicle.fmsstatus_id == 2 ||
|
||||
vehicle.fmsstatus_id == 3 ||
|
||||
vehicle.fmsstatus_id == 4
|
||||
) {
|
||||
// console.log(`Status Änderung: FL. AUR ${vehicle.name} Status ${vehicle.fmsstatus_id}`);
|
||||
await axios
|
||||
.post("https://app.divera247.com/api/v2/news?accesskey=" + process.env.divera_accesskey_aurich, {
|
||||
News: {
|
||||
title: `FL. AUR ${vehicle.name} Status ${oldVehicle.fmsstatus_id}->${vehicle.fmsstatus_id}`,
|
||||
ts_publish: vehicle.fmsstatus_ts,
|
||||
notification_type: 3,
|
||||
group: divera_group_id,
|
||||
send_push: true,
|
||||
},
|
||||
})
|
||||
.then((res) => {
|
||||
console.log(
|
||||
`[INFO] Divera News sent successfully: FL. AUR ${vehicle.name} Status ${oldVehicle.fmsstatus_id}->${vehicle.fmsstatus_id}`,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
function getChangedVehicles(newData, oldData = []) {
|
||||
return newData.filter((vehicle) => {
|
||||
const oldVehicle = oldData.find((x) => x.name === vehicle.name);
|
||||
return !oldVehicle || oldVehicle.fmsstatus_id !== vehicle.fmsstatus_id;
|
||||
});
|
||||
}
|
||||
|
||||
function toIsoTimestamp(unixTimestamp) {
|
||||
if (!unixTimestamp) return new Date().toISOString();
|
||||
const date = new Date(Number(unixTimestamp) * 1000);
|
||||
return date.toISOString();
|
||||
}
|
||||
|
||||
async function sendAlarmosPost(vehicle) {
|
||||
return axios.post("https://alamos.feuerwehr-stadt-aurich.de/rest/external/http/status/v2", {
|
||||
type: "STATUS",
|
||||
timestamp: toIsoTimestamp(vehicle.fmsstatus_ts),
|
||||
authorization: "%u7RB#gk^cYG64VMT5WF*0$Lg",
|
||||
sender: "Divera247",
|
||||
data: {
|
||||
status: vehicle.fmsstatus_id,
|
||||
address: vehicle.issi,
|
||||
radioName: vehicle.name,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function hasStatusChangedDiveraNachricht(newData, oldData) {
|
||||
// Divera Nachrichten
|
||||
await Promise.all(
|
||||
newData.map(async (vehicle) => {
|
||||
const oldVehicle = oldData.find((x) => x.name === vehicle.name);
|
||||
const oldStatus = oldVehicle ? oldVehicle.fmsstatus_id : "neu";
|
||||
|
||||
if (!oldVehicle || vehicle.fmsstatus_id !== oldVehicle.fmsstatus_id) {
|
||||
if (
|
||||
vehicle.fmsstatus_id == 6 ||
|
||||
vehicle.fmsstatus_id == 2 ||
|
||||
vehicle.fmsstatus_id == 3 ||
|
||||
vehicle.fmsstatus_id == 4
|
||||
) {
|
||||
await axios
|
||||
.post(
|
||||
"https://app.divera247.com/api/v2/news?accesskey=" + process.env.divera_accesskey_aurich,
|
||||
{
|
||||
News: {
|
||||
title: `FL. AUR ${vehicle.name} Status ${oldStatus}->${vehicle.fmsstatus_id}`,
|
||||
ts_publish: vehicle.fmsstatus_ts,
|
||||
notification_type: 3,
|
||||
group: divera_group_id,
|
||||
send_push: true,
|
||||
},
|
||||
},
|
||||
)
|
||||
.then(() => {
|
||||
console.log(
|
||||
`[INFO] Divera News sent successfully: FL. AUR ${vehicle.name} Status ${oldStatus}->${vehicle.fmsstatus_id}`,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
async function main() {
|
||||
console.log("[INFO] Statusabfrage - " + new Date().toLocaleString("de-DE"));
|
||||
|
||||
const oldData = JSON.parse(fs.readFileSync(CACHE_FILE, "utf8") || "[]");
|
||||
const newData = await getData();
|
||||
const changedVehicles = getChangedVehicles(newData, oldData);
|
||||
|
||||
hasStatusChanged(newData, JSON.parse(fs.readFileSync(CACHE_FILE, "utf8")));
|
||||
for (const vehicle of changedVehicles) {
|
||||
await sendAlarmosPost(vehicle);
|
||||
}
|
||||
|
||||
await hasStatusChangedDiveraNachricht(newData, oldData);
|
||||
|
||||
fs.writeFileSync(CACHE_FILE, JSON.stringify(newData));
|
||||
}
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
import dotenv from "dotenv";
|
||||
dotenv.config();
|
||||
import axios from "axios";
|
||||
const BASE_URL = process.env.BASE_URL;
|
||||
const divera_group_id = process.env.divera_status_groupid;
|
||||
|
||||
axios
|
||||
.post("https://app.divera247.com/api/v2/news?accesskey=" + process.env.divera_accesskey_aurich, {
|
||||
News: {
|
||||
title: `TestNachricht für 10Sekunden `,
|
||||
ts_publish: Math.floor(Date.now() / 1000),
|
||||
ts_archive: Math.floor((Date.now() + 10000) / 1000),
|
||||
notification_type: 3,
|
||||
group: divera_group_id,
|
||||
send_push: true,
|
||||
},
|
||||
})
|
||||
.then((res) => {
|
||||
console.log(`[INFO] Divera News sent successfully:`);
|
||||
});
|
||||
Reference in new issue
Block a user