Init
This commit is contained in:
81
status.js
Normal file
81
status.js
Normal file
@@ -0,0 +1,81 @@
|
||||
import dotenv from "dotenv";
|
||||
dotenv.config();
|
||||
import axios from "axios";
|
||||
import fs from "fs";
|
||||
|
||||
const BASE_URL = process.env.BASE_URL;
|
||||
const divera_group_id = process.env.divera_status_groupid;
|
||||
// Fahrzeuge die überwacht werden sollen
|
||||
const WATCHED_VEHICLES = [
|
||||
"10-11-1",
|
||||
"10-48-1",
|
||||
"10-30-1",
|
||||
"10-26-1",
|
||||
"10-45-1",
|
||||
"10-17-1",
|
||||
"10-19-1",
|
||||
"10-68-1",
|
||||
"10-64-1",
|
||||
"10-03-1",
|
||||
"10-04-1",
|
||||
];
|
||||
|
||||
// Datei für letzten Zustand
|
||||
const CACHE_FILE = "./vehicle-cache.json";
|
||||
|
||||
// Intervall in ms
|
||||
const POLL_INTERVAL = 15000;
|
||||
|
||||
async function getData() {
|
||||
const data = await axios.get(`${BASE_URL}/v2/pull/all?accesskey=${process.env.divera_accesskey_stadt}`);
|
||||
const watchedVehicles = Object.entries(data.data.data.cluster.vehicle).filter(([key, vehicle]) => {
|
||||
return WATCHED_VEHICLES.includes(vehicle.name);
|
||||
});
|
||||
|
||||
const formatetData = await watchedVehicles.map(([key, vehicle]) => {
|
||||
return {
|
||||
name: vehicle.name,
|
||||
fmsstatus_id: vehicle.fmsstatus_id,
|
||||
fmsstatus_ts: vehicle.fmsstatus_ts,
|
||||
};
|
||||
});
|
||||
|
||||
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
|
||||
console.log("[INFO] Statusabfrage!");
|
||||
if (vehicle.fmsstatus_id == 6 || vehicle.fmsstatus_id == 2 || vehicle.fmsstatus_id == 3) {
|
||||
// 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("Divera News sent successfully:", res.data);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
async function main() {
|
||||
console.log("Ausführung Status");
|
||||
const newData = await getData();
|
||||
|
||||
hasStatusChanged(newData, JSON.parse(fs.readFileSync(CACHE_FILE, "utf8")));
|
||||
|
||||
fs.writeFileSync(CACHE_FILE, JSON.stringify(newData));
|
||||
}
|
||||
|
||||
setInterval(main, 1000 * process.env.intervall);
|
||||
Reference in New Issue
Block a user