added express endpoints, a settings service and a media info service

This commit is contained in:
Rick van Lieshout 2019-10-30 22:49:04 +01:00
parent e5dd8cb87a
commit d7dab07845
8 changed files with 127 additions and 23 deletions

View File

@ -0,0 +1,9 @@
const globalEvents = {
play: "play",
pause: "pause",
playPause: "playPause",
next: "next",
previous: "previous",
};
module.exports = globalEvents;

View File

@ -0,0 +1,9 @@
const globalEvents = require("./globalEvents");
const mediaKeys = {
MediaPlayPause: globalEvents.playPause,
MediaNextTrack: globalEvents.next,
MediaPreviousTrack: globalEvents.previous,
};
module.exports = mediaKeys;

View File

@ -1,8 +1,13 @@
const { app, BrowserWindow, globalShortcut } = require("electron");
const { app, BrowserWindow, globalShortcut, ipcMain } = require("electron");
const { settings } = require("./scripts/settings");
const { addTray, refreshTray } = require("./scripts/tray");
const path = require("path");
const tidalUrl = "https://listen.tidal.com";
const trayModule = require("./scripts/tray");
const mediaKeysModule = require("./scripts/mediaKeys");
const expressModule = require("./scripts/express");
const mediaKeys = require("./constants/mediaKeys");
const mediaInfoModule = require("./scripts/mediaInfo");
let mainWindow;
let icon = path.join(__dirname, "../assets/icon.png");
@ -47,9 +52,9 @@ function createWindow(options = {}) {
}
function addGlobalShortcuts() {
Object.values(mediaKeysModule).forEach((key) => {
globalShortcut.register(key, () => {
mainWindow.webContents.send("globalKey", key);
Object.keys(mediaKeys).forEach((key) => {
globalShortcut.register(`${key}`, () => {
mainWindow.webContents.send("globalEvent", `${mediaKeys[key]}`);
});
});
}
@ -58,11 +63,11 @@ function addGlobalShortcuts() {
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on("ready", () => {
// window with white backround
createWindow();
addGlobalShortcuts();
trayModule.addTray({ icon });
trayModule.refreshTray();
addTray({ icon });
refreshTray();
settings.api && expressModule.run(mainWindow);
});
app.on("activate", function() {
@ -72,3 +77,9 @@ app.on("activate", function() {
createWindow();
}
});
// IPC
ipcMain.on("update-info", (event, arg) => {
mediaInfoModule.update(arg);
});

View File

@ -1,11 +1,12 @@
const { setTitle, getTitle } = require("./scripts/window-functions");
const { dialog } = require("electron").remote;
const hotkeys = require("./scripts/hotkeys");
const mediaKeysModule = require("./scripts/mediaKeys");
const notifier = require("node-notifier");
const { settings } = require("./scripts/settings");
const { ipcRenderer } = require("electron");
const { app } = require("electron").remote;
const { downloadFile } = require("./scripts/download");
const hotkeys = require("./scripts/hotkeys");
const globalEvents = require("./constants/globalEvents");
const notifier = require("node-notifier");
const notificationPath = `${app.getPath("userData")}/notification.jpg`;
const elements = {
@ -189,15 +190,21 @@ function addIPCEventListeners() {
ipcRenderer.on("globalEvent", (event, args) => {
switch (args) {
case mediaKeysModule.playPause:
case globalEvents.playPause:
playPause();
break;
case mediaKeysModule.next:
case globalEvents.next:
elements.click("next");
break;
case mediaKeysModule.previous:
case globalEvents.previous:
elements.click("previous");
break;
case globalEvents.play:
elements.click("play");
break;
case globalEvents.pause:
elements.click("pause");
break;
}
});
});
@ -236,7 +243,8 @@ setInterval(function() {
}
}).then(
() => {
notifier.notify(options);
ipcRenderer.send("update-info", options);
settings.notifications && notifier.notify(options);
},
() => {}
);

36
src/scripts/express.js Normal file
View File

@ -0,0 +1,36 @@
const express = require("express");
const { mediaInfo } = require("./mediaInfo");
const settingsModule = require("./settings");
const globalEvents = require("./../constants/globalEvents");
const expressModule = {};
var fs = require("fs");
expressModule.run = function(mainWindow) {
function handleGlobalEvent(res, action) {
mainWindow.webContents.send("globalEvent", action);
res.sendStatus(200);
}
const expressApp = express();
expressApp.get("/", (req, res) => res.send("Hello World!"));
expressApp.get("/current", (req, res) => res.json(mediaInfo));
expressApp.get("/play", (req, res) => handleGlobalEvent(res, globalEvents.play));
expressApp.get("/pause", (req, res) => handleGlobalEvent(res, globalEvents.pause));
expressApp.get("/next", (req, res) => handleGlobalEvent(res, globalEvents.next));
expressApp.get("/previous", (req, res) => handleGlobalEvent(res, globalEvents.previous));
expressApp.get("/image", (req, res) => {
var stream = fs.createReadStream(mediaInfo.icon);
stream.on("open", function() {
res.set("Content-Type", "image/png");
stream.pipe(res);
});
stream.on("error", function() {
res.set("Content-Type", "text/plain");
res.status(404).end("Not found");
});
});
expressApp.listen(settingsModule.settings.apiSettings.port, () => {});
};
module.exports = expressModule;

25
src/scripts/mediaInfo.js Normal file
View File

@ -0,0 +1,25 @@
const mediaInfo = {
title: "",
artist: "",
icon: "",
};
const mediaInfoModule = {
mediaInfo,
};
mediaInfoModule.update = function(arg) {
mediaInfo.title = propOrDefault(arg.title);
mediaInfo.artist = propOrDefault(arg.message);
mediaInfo.icon = propOrDefault(arg.icon);
};
/**
* Return the property or a default value
* @param {*} prop property to check
* @param {*} defaultValue defaults to ""
*/
function propOrDefault(prop, defaultValue = "") {
return prop ? prop : defaultValue;
}
module.exports = mediaInfoModule;

View File

@ -1,7 +0,0 @@
const mediaKeysModule = {
play: "MediaPlayPause",
next: "MediaNextTrack",
previous: "MediaPreviousTrack",
};
module.exports = mediaKeysModule;

13
src/scripts/settings.js Normal file
View File

@ -0,0 +1,13 @@
const settings = {
notifications: true,
api: true,
apiSettings: {
port: 47836,
},
};
const settingsModule = {
settings,
};
module.exports = settingsModule;