diff --git a/package.json b/package.json index 64013c5..136a981 100644 --- a/package.json +++ b/package.json @@ -12,5 +12,6 @@ "dotenv": "^16.3.1", "mqtt": "^5.0.4", "pushover-notifications": "^1.2.2" - } + }, + "type": "module" } diff --git a/src/webhooknotify.js b/src/discordnotify.js similarity index 72% rename from src/webhooknotify.js rename to src/discordnotify.js index 28d42a7..d7643c0 100644 --- a/src/webhooknotify.js +++ b/src/discordnotify.js @@ -1,9 +1,8 @@ -const dotenv = require('dotenv'); +import dotenv from 'dotenv'; dotenv.config(); -let discordUrl = process.env.DISCORD_WEBHOOK - -function sendDiscordOpen() { +let discordUrl = process.env.DISCORD_WEBHOOK; +export function sendDiscordOpen() { let message = {"username": process.env.DISCORD_USERNAME, "content": "Hackeriet is Open " + process.env.DISCORD_ROLE_ID} fetch(discordUrl,{ method: "POST", @@ -15,7 +14,7 @@ function sendDiscordOpen() { },) } -function sendDiscordClosed() { +export function sendDiscordClosed() { let message = {"username": process.env.DISCORD_USERNAME, "content": "Hackeriet is Closed " + process.env.DISCORD_ROLE_ID} fetch(discordUrl,{ method: "POST", @@ -24,6 +23,4 @@ function sendDiscordClosed() { }, body: JSON.stringify(message) },) -} - -module.exports = { sendDiscordOpen: sendDiscordOpen, sendDiscordClosed: sendDiscordClosed }; \ No newline at end of file +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 1804f72..ae8bf8d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,17 +1,12 @@ -var dotenv = require("dotenv"); -var mqtt = require("mqtt"); -const notify = require("./notify"); -const webhooknotify = require("./webhooknotify"); -const sendPushover = notify.sendPushover; -const openmsg = notify.openmsg; -const closedmsg = notify.closedmsg; -const sendDiscordOpen = webhooknotify.sendDiscordOpen; -const sendDiscordClosed = webhooknotify.sendDiscordClosed; +import dotenv from "dotenv"; +import * as mqtt from "mqtt"; +import {sendPushover, openmsg, closedmsg} from "./pushovernotify.js"; +import {sendDiscordClosed, sendDiscordOpen} from "./discordnotify.js"; //dotenv config dotenv.config(); -options = { +let options = { protocol: 'mqtts', host: process.env.MQTT_BROKER, port: 8883, @@ -21,7 +16,7 @@ options = { connectTimeout: 4000, // Timeout period }; console.log(process.env.MQTT_BROKER); -const client = new mqtt.connect(options); +let client = mqtt.connect(options); let previousState = ""; diff --git a/src/notify.js b/src/pushovernotify.js similarity index 67% rename from src/notify.js rename to src/pushovernotify.js index 41e2226..228ab60 100644 --- a/src/notify.js +++ b/src/pushovernotify.js @@ -1,5 +1,6 @@ -var dotenv = require("dotenv"); -var Push = require("pushover-notifications"); +import dotenv from "dotenv"; +import Push from "pushover-notifications"; + //dotenv config dotenv.config(); @@ -9,26 +10,26 @@ const p = new Push({ }); -var openmsg = { +export var openmsg = { // These values correspond to the parameters detailed on https://pushover.net/api // 'message' is required. All other values are optional. - message: "The hacker Space is open ", // required + message: "The HackerSpace is open ", // required title: "Hackerspace is open 👀", sound: "defualt", device: "iphone", priority: 1, }; -var closedmsg = { +export var closedmsg = { // These values correspond to the parameters detailed on https://pushover.net/api // 'message' is required. All other values are optional. - message: "the hackerspace is closed now", // required + message: "the Hackerspace is closed", // required title: "Hackerspace closed 👎", sound: "magic", device: "iphone", priority: 1, }; -function sendPushover(message) { +export function sendPushover(message) { p.send(message, function (err, result) { if (err) { throw err; @@ -38,6 +39,3 @@ function sendPushover(message) { } - - -module.exports = {sendPushover: sendPushover, openmsg : openmsg, closedmsg : closedmsg}; \ No newline at end of file