2022-08-01 05:59:04 +00:00
|
|
|
const axios = require('axios');
|
2022-08-03 12:57:39 +00:00
|
|
|
const https = require('https');
|
2022-08-01 05:59:04 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('getVideo', {
|
|
|
|
description: 'Get packing video of ticketId',
|
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'id',
|
|
|
|
type: 'number',
|
2022-08-01 13:03:17 +00:00
|
|
|
required: false,
|
2022-08-01 05:59:04 +00:00
|
|
|
description: 'Ticket id'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'time',
|
|
|
|
type: 'number',
|
2022-08-01 13:03:17 +00:00
|
|
|
required: false,
|
2022-08-01 05:59:04 +00:00
|
|
|
description: 'Time to add'
|
2022-08-02 13:15:05 +00:00
|
|
|
},
|
|
|
|
{
|
2022-08-03 12:57:39 +00:00
|
|
|
arg: 'req',
|
|
|
|
type: 'object',
|
|
|
|
http: {source: 'req'}
|
2022-08-02 13:15:05 +00:00
|
|
|
},
|
|
|
|
{
|
2022-08-03 12:57:39 +00:00
|
|
|
arg: 'res',
|
|
|
|
type: 'object',
|
|
|
|
http: {source: 'res'}
|
2022-08-02 13:15:05 +00:00
|
|
|
}
|
|
|
|
],
|
2022-08-01 05:59:04 +00:00
|
|
|
http: {
|
|
|
|
path: `/getVideo`,
|
|
|
|
verb: 'GET',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-08-03 12:57:39 +00:00
|
|
|
Self.getVideo = async(id, time, req, res, options) => {
|
2022-08-01 05:59:04 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
const myOptions = {};
|
2022-08-03 12:57:39 +00:00
|
|
|
// console.log('ENTRY');
|
2022-08-01 05:59:04 +00:00
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
const expedition = await models.Expedition.findById(id, null, myOptions);
|
|
|
|
const packingSiteConfig = await models.PackingSiteConfig.findOne({}, myOptions);
|
2022-08-03 12:57:39 +00:00
|
|
|
const monitorId = 'Xme2hiqz1f';
|
|
|
|
// console.log(packingSiteConfig);
|
2022-08-01 05:59:04 +00:00
|
|
|
|
2022-08-02 13:15:05 +00:00
|
|
|
const addTime = (time ? time : packingSiteConfig.avgBoxingTime) * 1000;
|
|
|
|
const start = new Date(expedition.created);// '2022-07-27T09:00:00';// new Date('2022-07-27'); //
|
|
|
|
const end = new Date(start.getTime() + addTime);
|
2022-08-03 12:57:39 +00:00
|
|
|
// console.log(addTime);
|
|
|
|
// console.log(start.toString().split('.')[0]);
|
|
|
|
// console.log(end.toString().split('.')[0]);
|
2022-08-01 05:59:04 +00:00
|
|
|
|
2022-08-02 13:15:05 +00:00
|
|
|
const videoUrl = `/${packingSiteConfig.shinobiToken}/videos/${packingSiteConfig.shinobiGroupKey}/${monitorId}`;
|
2022-08-03 12:57:39 +00:00
|
|
|
const timeUrl = `?start=${start.toString().split('.')[0]}&end=${end.toString().split('.')[0]}`;
|
2022-08-02 13:15:05 +00:00
|
|
|
const url = `${packingSiteConfig.shinobiUrl}${videoUrl}${timeUrl}&endIsStartTo`;
|
2022-08-01 05:59:04 +00:00
|
|
|
|
2022-08-03 12:57:39 +00:00
|
|
|
// console.log(url);
|
2022-08-02 13:15:05 +00:00
|
|
|
const videoList = await axios.post(url);
|
2022-08-03 12:57:39 +00:00
|
|
|
// console.log('videoList.data', videoList.data);
|
|
|
|
// console.log(videoList.data.videos);
|
|
|
|
// console.log(videoList.data.videos.length);
|
|
|
|
const length = videoList.data.videos.length;
|
|
|
|
// console.log(length);
|
|
|
|
if (!length) return [];
|
|
|
|
|
|
|
|
const lastVideo = videoList.data.videos[length - 1].href;
|
|
|
|
|
|
|
|
const headers = Object.assign({}, req.headers, {
|
|
|
|
host: 'shinobi.verdnatura.es'
|
2022-08-02 13:15:05 +00:00
|
|
|
});
|
2022-08-03 12:57:39 +00:00
|
|
|
const httpOptions = {
|
|
|
|
host: 'shinobi.verdnatura.es',
|
|
|
|
path: '/BW4o7FZyJ85she1hlRDe6QnwW3jwEP/videos/xVqft9LFXg/Xme2hiqz1f/2022-08-02T11-45-01.mp4',
|
|
|
|
port: 443,
|
|
|
|
headers
|
|
|
|
};
|
2022-08-02 13:15:05 +00:00
|
|
|
|
2022-08-03 12:57:39 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const req = https.request(httpOptions,
|
|
|
|
shinobiRes => {
|
|
|
|
shinobiRes.pause();
|
|
|
|
res.writeHeader(shinobiRes.statusCode, shinobiRes.headers);
|
|
|
|
shinobiRes.pipe(res);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
req.end();
|
|
|
|
});
|
2022-08-01 05:59:04 +00:00
|
|
|
};
|
|
|
|
};
|