Ajout de la connexion entre Electron et backend

This commit is contained in:
Quentin Millardet
2024-01-14 00:17:13 +01:00
parent e7488cc5c7
commit 38bc3e7ae5
9 changed files with 330 additions and 81 deletions

View File

@@ -0,0 +1,58 @@
const fs = require('fs');
const LigthshowManager = require('./LightshowService')
const DeviceManager = require('./deviceinformation');
const { join } = require('node:path');
class CopyManagerService{
async copyFromDisk(device, mountPointLabel, lightshowName ){
let lightshowManagerService = new LigthshowManager();
if (!lightshowManagerService.isExistLightshowOnServer(lightshowName)){
throw new Error('Ce ligtshow n\'existe pas')
}
let ligthsowAudioFile = lightshowManagerService.getAudioFileName(lightshowName);
let ligthsowFseqFile = lightshowManagerService.getFseqLigthshow(lightshowName);
let baseDevice = await this.#getDeviceBaseWithLigthShow(device, mountPointLabel);
await this.#moveTeslaCamDir(device, mountPointLabel)
if (this.#isAlreadyExistLightshow(baseDevice, lightshowName)){
throw new Error('Already exist Lighttshow on device, please check file')
}
fs.mkdirSync(baseDevice, { recursive: true })
fs.copyFileSync(ligthsowAudioFile, baseDevice+lightshowName + '.mp3' )
fs.copyFileSync(ligthsowFseqFile, baseDevice + lightshowName + '.fseq' )
}
#isAlreadyExistLightshow(deviceMountPouint, lightshowName){
let ligthshowBase = deviceMountPouint + lightshowName;
let fseq = fs.existsSync( ligthshowBase+ '.fseq')
let wav = fs.existsSync(ligthshowBase + '.wav')
let mp3 = fs.existsSync(ligthshowBase + '.mp3')
return mp3 || wav || fseq;
}
async #moveTeslaCamDir(device, mountPointLabel){
let deviceManager = new DeviceManager();
let deviceMountPoint = await deviceManager.getMountPoint(device, mountPointLabel);
let teslacamDir = deviceMountPoint + '/TeslaCam/'
if(fs.existsSync(teslacamDir)){
let date = new Date();
fs.renameSync(teslacamDir, deviceMountPoint + '/TeslaCam-old/' )
}
return true;
}
async #getDeviceBaseWithLigthShow(device, mountPointLabel){
let deviceManager = new DeviceManager();
let deviceMountPoint = await deviceManager.getMountPoint(device, mountPointLabel);
return deviceMountPoint + '/Lightshow/'
}
}
module.exports = CopyManagerService

View File

@@ -0,0 +1,66 @@
const { PrismaClient } = require('@prisma/client')
const fs = require('fs')
const prisma = new PrismaClient()
const usbLightshow = process.env.LIGHTSHOW_DIR || '';
class LigthshowService{
async getAllLigthshow() {
const posts = await prisma.Lightshow.findMany({
})
return posts;
}
//
// createLigthshow(ligthshow) {
// prisma.Ligthshow.create({
// data: {
// ligthshow,
// content,
// published: false,
// author: { connect: { email: authorEmail } },
// },
// })
// }
isExistLightshowOnServer(ligthshowName){
let audio = this.getAudioFileName(ligthshowName) !== null;
let fseq = this.getFseqLigthshow(ligthshowName) !== null;
return audio && fseq;
}
#getBaseFile(ligthshowName){
return usbLightshow + '/' + ligthshowName
}
getAudioFileName(ligthshowName){
let base = this.#getBaseFile(ligthshowName)
let extensions = [ 'mp3', 'wav']
let filename = null;
extensions.forEach((ext) => {
let tmpfileName = base + '.' + ext
if (fs.existsSync(tmpfileName) && filename === null){
filename = tmpfileName;
}else if (fs.existsSync(tmpfileName) && filename !== null){
throw new Error('Multiple audio file for the same Lightshow')
}
})
return filename;
}
getFseqLigthshow(ligthshowName){
let filename = null;
let base2 = usbLightshow + '/' + ligthshowName
let tmp = base2 + '.fseq'
if (fs.existsSync(tmp)){
filename = tmp;
}
return filename;
}
}
module.exports = LigthshowService

View File

@@ -0,0 +1,50 @@
class Deviceinformation {
async deviceList(){
const drivelist = require('drivelist');
const drives =await drivelist.list()
let element = [];
drives.forEach((drive) => {
element.push({
mountpoint : drive.mountpoints,
device : drive.device
})
})
return element;
}
async deviceInformation( deviceName){
const drivelist = require('drivelist');
const drives =await drivelist.list()
let element = null;
drives.forEach((drive) => {
if (drive.device === deviceName ){
element = drive;
}
})
return element;
}
async getMountPoint( deviceName, pointName){
const drivelist = require('drivelist');
const drives =await drivelist.list()
let element = null;
drives.forEach((drive) => {
if (drive.device === deviceName ){
drive.mountpoints.forEach((mountPoint) => {
if (mountPoint.label === pointName){
element = mountPoint.path
}
})
}
})
return element;
}
}
module.exports = Deviceinformation