Skip to content

Commit

Permalink
Esm mgration (#16)
Browse files Browse the repository at this point in the history
* renamed the file and started the migration to ES6

* changed the package.json file to use ESM

* renamed the file and changed to use esm

* uppdated the imports to use es6

* renamed the file and migrated to esm
  • Loading branch information
bernes1 authored Sep 11, 2023
1 parent 7839e81 commit 9fc7ec7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 30 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"dotenv": "^16.3.1",
"mqtt": "^5.0.4",
"pushover-notifications": "^1.2.2"
}
},
"type": "module"
}
13 changes: 5 additions & 8 deletions src/webhooknotify.js → src/discordnotify.js
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand All @@ -24,6 +23,4 @@ function sendDiscordClosed() {
},
body: JSON.stringify(message)
},)
}

module.exports = { sendDiscordOpen: sendDiscordOpen, sendDiscordClosed: sendDiscordClosed };
}
17 changes: 6 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 = "";

Expand Down
18 changes: 8 additions & 10 deletions src/notify.js → src/pushovernotify.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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;
Expand All @@ -38,6 +39,3 @@ function sendPushover(message) {
}




module.exports = {sendPushover: sendPushover, openmsg : openmsg, closedmsg : closedmsg};

0 comments on commit 9fc7ec7

Please sign in to comment.