diff --git a/Makefile b/Makefile index 2b3e65a69ff537307659d32b4f55830d16027938..dc291eff7af2f002c025d2c361e5c6b3362c46c3 100644 --- a/Makefile +++ b/Makefile @@ -6,3 +6,8 @@ prepare: run: $(YARN) start --no-sandbox + +debug: + $(YARN) test --no-sandbox + +.PHONY: test debug run prepare diff --git a/common/db.js b/common/db.js index f82732bc498a461e49cf3a0e889c6d50266fd52e..bfe0f58f194b3d8a118f3b5f2157eb5f6c116288 100644 --- a/common/db.js +++ b/common/db.js @@ -58,7 +58,30 @@ class KaraDatabase { return new Promise(resolv => { db.all(__sqlQuery, [`%${queryString}%`, queryString], (err, rows) => { if (err) { - logger.log("error", err); + logger.error(err); + throw err; + } + rows.forEach((row) => { __ret.push(row); }); + resolv(__ret); + }); + }); + }; + + return __getRecords(this.m_db); + } + + /* List all the kara in the db. + * => a promize, callback with [(id: Int, string: String, cat: String, type: String)] */ + all() { + var __ret = []; + let __sqlQuery = `SELECT id, string, category AS cat, (song_type || song_number) AS type + FROM kara;`; + + function __getRecords(db) { + return new Promise(resolv => { + db.all(__sqlQuery, [], (err, rows) => { + if (err) { + logger.error(err); throw err; } rows.forEach((row) => { __ret.push(row); }); diff --git a/instance/main.js b/instance/main.js index 06f6d974689ae1ee556e0a4d989423a152a05b46..fcd19a39ca20ef8edfb0beaa4c7b9743118ad6de 100644 --- a/instance/main.js +++ b/instance/main.js @@ -1,26 +1,36 @@ const { ipcRenderer } = require('electron'); const logger = require('../common/logger.js'); -const karaJSON = require('../test/dummyKara.json'); const buttonList = [ + [ "left", "Reload", "reloadDb" ], [ "left", "Play", "commandPlay" ], [ "left", "Stop", "commandStop" ], - [ "right", "Quit LektorApp", "closeButton" ] + [ "right", "Quit LektorApp", "closeButton" ], ]; +const karaJSON = require('../test/dummyKara.json'); + window.onload = () => { createButtonList(buttonList); - karaList(karaJSON); + // karaList(karaJSON); document.getElementById("closeButton").addEventListener('click', () => { ipcRenderer.send('close-app') }); document.getElementById("commandPlay").addEventListener('click', () => { logger.debug("Command play !"); + ipcRenderer.send('cmd-play', null); + }); + document.getElementById("commandStop").addEventListener('click', () => { + logger.debug("Command stop !"); + ipcRenderer.send("cmd-stop", null); + }); + document.getElementById("reloadDb").addEventListener("click", () => { + ipcRenderer.send("reload-db-request", document.getElementById("buttonPanelListLeft").innerHTML); }); logger.debug("Window loaded"); -} +}; function createButtonList(list) { var renderHtmlLeft = ""; @@ -43,6 +53,11 @@ function createButtonList(list) { document.getElementById("buttonPanelListLeft").innerHTML = renderHtmlLeft + renderHtmlRight; } +ipcRenderer.on("reload-db-responce", (event, arg) => { + logger.debug(`Web page got reload-db`); + document.getElementById("panelLeft").innerHTML = arg; +}); + function karaList(karas) { var karaList = ""; var playingList = ""; diff --git a/main.js b/main.js index a712539d1301655656d4d401bc2db2f5895db39e..5ce682dba73c2313bab75cb1ca4ed9f24711e00a 100644 --- a/main.js +++ b/main.js @@ -3,19 +3,26 @@ const electron = require('electron') const { app, BrowserWindow } = require('electron') const { ipcMain } = require('electron') const { fork, spawn } = require('child_process'); -const fs = require("fs"); -var tail = require('tail').Tail; -var client; /* Sub process for the express server */ +const fs = require("fs"); +const db = require.main.require("./common/db.js"); +const lkt = require.main.require("./common/lkt.js"); +var tail = require('tail').Tail; +var client; /* Sub process for the express server */ + +var myDb = new db("/home/kara/kara.db"); + +/********************** + * Reinit the logfile * + **********************/ -/* Reinit the logfile */ fs.truncate(logger.logfile, 0, () => {}); var tail = new tail(logger.logfile); +tail.on("line", function(data) { console.log(data); }); -tail.on("line", function(data) { - console.log(data); -}); +/*************************************************************** + * Creates the main window and process for the admin interface * + ***************************************************************/ -/* Creates the main window and process for the admin interface */ function createInstanceWindow () { const win = new BrowserWindow({ width: 1280, @@ -30,18 +37,50 @@ function createInstanceWindow () { // win.webContents.openDevTools(); } +/****************************** + * The end of the application * + ******************************/ + ipcMain.on('close-app', (evt, arg) => { app.quit() }) app.on("quit", () => { - logger.info("Send SIGTERM to express process and tailler process"); + logger.info("Send SIGTERM to express process and tailler process"); client.kill("SIGTERM"); }); +/********************************* + * The beggin of the application * + *********************************/ + app.on('ready', () => { logger.info("Main window is ready"); createInstanceWindow(); client = fork('client/main.js'); }); +/********************************* + * Messages from the main window * + *********************************/ + +ipcMain.on("cmd-play", (event, arg) => { + logger.debug(`${event}: ${arg}`); +}); +ipcMain.on("cmd-stop", (event, arg) => { + logger.debug(`${event}: ${arg}`); +}); + +/* Fill the pannel with the content of the DB. + * The `arg` is the HTML object of the pannel */ +ipcMain.on("reload-db-request", (event, arg) => { + logger.info("Reloading the left panel with all the DB"); + myDb.all().then( karas => { + var karaList = ""; + karas.forEach( kara => { + karaList += `<div class="card shadow" style="margin: 5px; border-radius: 10px; margin: 20px 0px 20px 0px;"> + <div class="card-body">${kara.string} </div></div>`; + }); + event.reply("reload-db-responce", karaList); + }); +}); diff --git a/package.json b/package.json index 3dc218d12c54f485e10f03c678865ec4743efe69..be2a9f47addd1ca6c0652e6ba8651ce7cb9eae58 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "license": "ISC", "scripts": { "start": "electron .", - "test": "ELECTRON_ENABLE_LOGGING=0 ELECTRON_NO_ATTACH_CONSOLE=true electron ." + "test": "ELECTRON_ENABLE_LOGGING=0 ELECTRON_NO_ATTACH_CONSOLE=true electron --trace-uncaught ." }, "dependencies": { "ejs": "^3.1.5",