feat: simple remote control support on http

This commit is contained in:
Serkan committed 2024-10-09 18:21:53 +03:00
1 parent 5a4cdd848f
commit d6a07cc0cd
14 files changed
+146 -19

No files matched your search

+13 -1
View File
@@ -32,6 +32,7 @@ import {
PLAYLIST_UPDATE_RESPONSE,
SET_MPV_PLAYER_PATH,
SET_VLC_PLAYER_PATH,
SETTINGS_UPDATE,
STALKER_REQUEST,
STALKER_RESPONSE,
XTREAM_REQUEST,
@@ -40,6 +41,7 @@ import {
import { Playlist } from '../shared/playlist.interface';
import { createPlaylistObject } from '../shared/playlist.utils';
import { ParsedPlaylist } from '../src/typings.d';
import { Server } from './server';
const fs = require('fs');
const https = require('https');
@@ -88,7 +90,12 @@ export class Api {
mpv;
settings;
server;
constructor(store) {
this.server = new Server(this);
this.store = store;
this.mpv = this.createMpvInstance();
this.mpv
@@ -286,7 +293,11 @@ export class Api {
.on(SET_VLC_PLAYER_PATH, (_event, vlcPlayerPath) => {
console.log('... setting vlc player path', vlcPlayerPath);
store.set(VLC_PLAYER_PATH, vlcPlayerPath);
});
})
.on(SETTINGS_UPDATE, (_event, arg) => {
this.settings = arg;
this.server.updateSettings();
}) ;
// listeners for EPG events
ipcMain
@@ -625,4 +636,5 @@ export class Api {
}
});
}
}
+1 -3
View File
@@ -4,7 +4,6 @@ import * as path from 'path';
import * as url from 'url';
import { Api } from './api';
import { AppMenu } from './menu';
import { Server } from './server';
const fixPath = require('fix-path');
const {
@@ -121,8 +120,7 @@ try {
const menu = new AppMenu(win);
Menu.setApplicationMenu(menu.getMenu());
api.setMainWindow(win);
new Server(3000, api);
// create hidden window for epg worker
createEpgWorkerWindow();
});
+40 -12
View File
@@ -1,4 +1,5 @@
import { IncomingMessage, ServerResponse } from "http";
import { REMOTE_CONTROL_CHANGE_CHANNEL } from "../shared/ipc-commands";
const http = require('http');
const Sqrl = require('squirrelly');
@@ -8,47 +9,74 @@ const path = require('path');
export class Server {
/** Server instance */
server: any;
/** Port number */
port: number;
/** Api instance */
api: any;
/**
* Creates a new server instance
* @param port port number
* @param api application instance
*/
constructor(_port: number, _api: any) {
constructor(_api: any) {
this.api = _api;
this.port = _port;
this.server = http.createServer(this.requestListener.bind(this));
this.server.listen(this.port);
}
updateSettings() {
if (this.api.settings.remoteControl && !this.server.listening) {
this.server.listen(this.api.settings.remoteControlPort);
} else if (this.server.listening && !this.api.settings.remoteControl) {
this.server.close();
}
}
async getTranslation() {
const language = this.api.settings && this.api.settings.language ? this.api.settings.language : 'en';
const languageFileContent = await fs.promises.readFile(path.join(__dirname, `../src/assets/i18n/${language}.json`), 'utf-8');
return JSON.parse(languageFileContent);
}
getTranslationValue(key: string, translation: any, defaultValue: string) {
if (translation && translation.REMOTE_CONTROL) {
const translationValue = translation.REMOTE_CONTROL[key];
if (translationValue) {
return translationValue;
}
}
return defaultValue;
}
async requestListener(request: IncomingMessage, response: ServerResponse) {
const currentPage = request.url?.split('?')[0]?.split('/').splice(-1)[0];
console.log(this.api.store);
const translation = await this.getTranslation();
switch (currentPage) {
case '': {
const indexFile = await fs.promises.readFile(path.join(__dirname, './remote-control/', 'index.sqrl'), 'utf-8');
const renderedHtml = Sqrl.render(indexFile, {
headerText: 'My Dynamic Header',
upChannelText: 'Up Channel',
downChannelText: 'Down Channel',
headerText: this.getTranslationValue('HEADER', translation, 'Remote Control'),
upChannelText: this.getTranslationValue('UP_CHANNEL', translation, 'Up Channel'),
downChannelText: this.getTranslationValue('DOWN_CHANNEL', translation, 'Down Channel'),
upChannelUrl: '/upChannel',
downChannelUrl: '/downChannel',
title: 'IPTVNator'
title: this.getTranslationValue('TITLE', translation, 'IPTVNator'),
footer: this.getTranslationValue('FOOTER', translation, 'IPTVNator')
});
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(renderedHtml);
break;
}
case 'upChannel': {
// this.api.upChannel();
this.api.mainWindow.webContents.send(REMOTE_CONTROL_CHANGE_CHANNEL, { type: 'up' });
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end('Up Channel');
break;
}
case 'downChannel': {
this.api.mainWindow.webContents.send(REMOTE_CONTROL_CHANGE_CHANNEL, { type: 'down' });
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end('Down Channel');
break;
}
default:
response.writeHead(404, { 'Content-Type': 'text/html' });
response.end('404');