Created
January 2, 2022 18:47
-
-
Save OsvaldoFrias/d206bd6f16d55af0b9c87c3a95924ca4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { format, compareAsc, addSeconds } = require('date-fns'); | |
| const CronJob = require('cron').CronJob; | |
| const ONE_MINUTE = 60; // seconds | |
| const cameras = ['3', '6', '10']; | |
| const initDate = new Date(); | |
| const job = createJob(initDate); | |
| function createJob(initDate) { | |
| let afterDate = initDate; | |
| let consecutivo = 0; | |
| let step = 0; | |
| return new CronJob('*/5 * * * * *', async function () { | |
| console.log('You will see this message every second'); | |
| console.log('Initial date', initDate); | |
| console.log('Processing date', afterDate); | |
| const cameraIndex = step % cameras.length; | |
| step++; | |
| consecutivo++; // Incremento inicial del consecutivo para las imágenes | |
| seprobanBeforeMinuteProcess(new Date(), cameras[cameraIndex], consecutivo); | |
| consecutivo++; // Incremento de consecutivo para abarcar las 2 imágenes creadas | |
| }, null, false, 'America/Mexico_City'); | |
| } | |
| job.start(); | |
| function seprobanBeforeMinuteProcess(afterDate, camId, consecutivo) { | |
| let beforeDate = addSeconds(afterDate, - ONE_MINUTE); | |
| // const sucursal = '00123'; | |
| // const evento = '001'; | |
| // const tipoArchivo = 'I'; | |
| // let camId = '5'; | |
| let momento = 'A'; | |
| // let consecutivo = 1; | |
| const filenameData = { | |
| sucursal: '00123', | |
| evento: '001', | |
| tipoArchivo: 'I', | |
| momento, | |
| camId, | |
| consecutivo | |
| } | |
| console.log(genImage(filenameData.camId, beforeDate, genFileName(filenameData))); | |
| // consecutivo++; | |
| filenameData.consecutivo = consecutivo + 1; | |
| filenameData.momento = 'D'; | |
| console.log(genImage(filenameData.camId, afterDate, genFileName(filenameData))); | |
| } | |
| // Formato de nombre de archivo | |
| // 00123 - Sucursal 5 dígitos | |
| // 001 - Tipo de evento 3 dígitos ("001 ASALTO" o "501 Solicitud de Pruebas") | |
| // 05 - Número de cámara 2 dígitos | |
| // 142012 - Horas minutos y segundos 6 dígitos | |
| // D - Momento de la imagen (Valores: (A) - Antes, (D) – Durante) | |
| // . - Delimitador | |
| // I - Tipo de archivo (Valores: (I) - Imagen, (H) - Archivo de Texto) | |
| // 1 - Consecutivo de imagen | |
| function genFileName(data) { | |
| return `${data.sucursal}${data.evento}${data.camId.length == 2 ? data.camId : '0' + data.camId}DDDDDD${data.momento}.${data.tipoArchivo}${data.consecutivo}`; | |
| } | |
| function genImage(camId, dateImage, filename) { | |
| const formatted = format(dateImage, 'yyyy-MM-dd HH:mm:ss'); | |
| const image1 = format(dateImage, 'HHmmss'); | |
| console.log('Generate image for cam', camId, 'in time', formatted); | |
| return filename.replace('DDDDDD', image1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment