Skip to content

Commit

Permalink
Remove scene execution concurrency, fix camera issues & fix MQTT new …
Browse files Browse the repository at this point in the history
…value parsing (GladysAssistant#769)

* Remove scene queue concurrency

* Fix GladysAssistant#749 : Camera should not create tons of folders + improve image compression

* Fix camera tests

* Fix scene UI, automatically add actions blocks

* Add tests to cameraService.start

* Fix GladysAssistant#767: MQTT service should parseFloat received device value

* Fix MQTT tests
  • Loading branch information
Pierre-Gilles authored May 4, 2020
1 parent 23a2d29 commit 0177908
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 22 deletions.
2 changes: 1 addition & 1 deletion front/src/routes/scene/edit-scene/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class EditScene extends Component {
}
}
});
if (newState.scene.actions[columnIndex].length === 0) {
if (columnIndex + 1 === newState.scene.actions.length && newState.scene.actions[columnIndex].length === 1) {
newState = update(newState, {
scene: {
actions: {
Expand Down
5 changes: 3 additions & 2 deletions server/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function Gladys(params = {}) {
const device = new Device(event, message, stateManager, service, room, variable);
const scene = new Scene(stateManager, event, device, message);
const scheduler = new Scheduler(event);
const system = new System(db.sequelize, event);
const system = new System(db.sequelize, event, config);
const weather = new Weather(service, event, message, house);
const gateway = new Gateway(variable, event, system, db.sequelize, config, user);

Expand Down Expand Up @@ -91,6 +91,8 @@ function Gladys(params = {}) {
// Execute DB migrations
await db.umzug.up();

await system.init();

if (!params.disableBrainLoading) {
await brain.load();
}
Expand All @@ -114,7 +116,6 @@ function Gladys(params = {}) {
scheduler.init();
}
gateway.init();
system.init();
},
};

Expand Down
1 change: 0 additions & 1 deletion server/lib/scene/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const SceneManager = function SceneManager(stateManager, event, device, message)
// @ts-ignore
this.queue = queue({
autostart: true,
concurrency: 1,
});
this.event.on(EVENTS.TRIGGERS.CHECK, eventFunctionWrapper(this.checkTrigger.bind(this)));
this.event.on(EVENTS.ACTION.TRIGGERED, eventFunctionWrapper(this.executeSingleAction.bind(this)));
Expand Down
3 changes: 2 additions & 1 deletion server/lib/system/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ const { getDiskSpace } = require('./system.getDiskSpace');
const { saveLatestGladysVersion } = require('./system.saveLatestGladysVersion');
const { shutdown } = require('./system.shutdown');

const System = function System(sequelize, event) {
const System = function System(sequelize, event, config) {
this.downloadUpgradeError = null;
this.downloadUpgradeFinished = null;
this.downloadUpgradeLastEvent = null;
this.Docker = Docker;
this.sequelize = sequelize;
this.event = event;
this.config = config;
this.dockerode = null;
this.event.on(EVENTS.SYSTEM.DOWNLOAD_UPGRADE, eventFunctionWrapper(this.downloadUpgrade.bind(this)));
};
Expand Down
4 changes: 4 additions & 0 deletions server/lib/system/system.init.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const logger = require('../../utils/logger');
* init();
*/
async function init() {
// Clean temp folder
await fse.emptyDir(this.config.tempFolder);
// Ensure temp directory exists
await fse.ensureDir(this.config.tempFolder);
// we get Gladys version from package.json
const packageJsonString = await fse.readFile(path.join(__dirname, '../../../package.json'), 'utf8');
const packageJson = JSON.parse(packageJsonString);
Expand Down
2 changes: 1 addition & 1 deletion server/services/mqtt/lib/handler/handleGladysMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function handleGladysMessage(topic, message) {
}
const event = {
device_feature_external_id: parsedTopic[5],
state: message,
state: parseFloat(message),
};
this.gladys.event.emit(EVENTS.DEVICE.NEW_STATE, event);
} else {
Expand Down
3 changes: 3 additions & 0 deletions server/services/rtsp-camera/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fse = require('fs-extra');
const logger = require('../../utils/logger');
const RtspCameraHandler = require('./lib');
const RtspCameraController = require('./api/rtspCamera.controller');
Expand All @@ -13,6 +14,8 @@ module.exports = function RtspCameraService(gladys, serviceId) {
*/
async function start() {
logger.log('starting RTSP service');
// make sure the tempFolder exists
await fse.ensureDir(gladys.config.tempFolder);
}

/**
Expand Down
13 changes: 7 additions & 6 deletions server/services/rtsp-camera/lib/getImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const logger = require('../../../utils/logger');
const { NotFoundError } = require('../../../utils/coreErrors');

const DEVICE_PARAM_CAMERA_URL = 'CAMERA_URL';
const CAMERA_FILENAME = 'output.jpg';

/**
* @description Get camera image.
Expand All @@ -25,19 +24,20 @@ async function getImage(device) {
}
// we create a temp folder
const now = new Date();
const tempFolder = path.join(
const filePath = path.join(
this.gladys.config.tempFolder,
`camera-${device.id}-${now.getMilliseconds()}-${now.getSeconds()}-${now.getMinutes()}-${now.getHours()}`,
`camera-${device.id}-${now.getMilliseconds()}-${now.getSeconds()}-${now.getMinutes()}-${now.getHours()}.jpg`,
);
await fse.ensureDir(tempFolder);
const filePath = path.join(tempFolder, CAMERA_FILENAME);
// we create a writestream
const writeStream = fse.createWriteStream(filePath);
// and send a camera thumbnail to this stream
this.ffmpeg(cameraUrlParam.value)
.format('image2')
.outputOptions('-vframes 1')
// resize the image with max width = 640
.outputOptions('-vf scale=640:-1')
// Effective range for JPEG is 2-31 with 31 being the worst quality.
.outputOptions('-qscale:v 15')
.output(writeStream)
.on('end', async () => {
const image = await fse.readFile(filePath);
Expand All @@ -47,10 +47,11 @@ async function getImage(device) {
resolve(cameraImage);
await fse.remove(filePath);
})
.on('error', (err, stdout, stderr) => {
.on('error', async (err, stdout, stderr) => {
logger.debug(`Cannot process video: ${err.message}`);
logger.debug(stderr);
reject(err.message);
await fse.remove(filePath);
})
.run();
return null;
Expand Down
18 changes: 11 additions & 7 deletions server/test/lib/system/system.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ const sequelize = {
close: fake.resolves(null),
};

const config = {
tempFolder: '/tmp/gladys',
};

const event = new EventEmitter();

describe('system', () => {
it('should get infos', async () => {
const system = new System(sequelize, event);
const system = new System(sequelize, event, config);
await system.init();
const infos = await system.getInfos();
expect(infos).to.have.property('hostname');
Expand All @@ -38,7 +42,7 @@ describe('system', () => {
expect(infos.gladys_version.substr(0, 1)).to.equal('v');
});
it('should return disk space', async () => {
const system = new System(sequelize, event);
const system = new System(sequelize, event, config);
const diskSpace = await system.getDiskSpace();
expect(diskSpace).to.have.property('filesystem');
expect(diskSpace).to.have.property('size');
Expand All @@ -48,27 +52,27 @@ describe('system', () => {
expect(diskSpace).to.have.property('mountpoint');
});
it('should return if process is running inside docker or not', async () => {
const system = new System(sequelize, event);
const system = new System(sequelize, event, config);
const isDocker = await system.isDocker();
expect(typeof isDocker).to.equal('boolean');
});
it('should list containers', async () => {
const system = new System(sequelize, event);
const system = new System(sequelize, event, config);
await system.init();
const containers = await system.getContainers();
expect(containers).be.instanceOf(Array);
});
it('should init system', async () => {
const system = new System(sequelize, event);
const system = new System(sequelize, event, config);
await system.init();
});
it('should download upgrade', async () => {
const system = new System(sequelize, event);
const system = new System(sequelize, event, config);
await system.init();
await system.downloadUpgrade('latest');
});
it('should install upgrade', async () => {
const system = new System(sequelize, event);
const system = new System(sequelize, event, config);
await system.init();
await system.installUpgrade();
});
Expand Down
2 changes: 1 addition & 1 deletion server/test/services/mqtt/lib/handleNewMessage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('Mqtt handle message', () => {

assert.calledWith(gladys.event.emit, EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: 'my_feature_external_id',
state: '19.8',
state: 19.8,
});
});

Expand Down
2 changes: 1 addition & 1 deletion server/test/services/mqtt/mqttHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('MqttHandler', () => {

assert.calledWith(gladys.event.emit, EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: 'my_feature_external_id',
state: '19.8',
state: 19.8,
});
});
});
11 changes: 10 additions & 1 deletion server/test/services/rtsp-camera/rtspCamera.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const { expect } = require('chai');
const fse = require('fs-extra');
const assertChai = require('chai').assert;
const { fake, assert } = require('sinon');
const FfmpegMock = require('./FfmpegMock.test');
const RtspCameraManager = require('../../../services/rtsp-camera/lib');
const RtspCameraService = require('../../../services/rtsp-camera');

const gladys = {
config: {
tempFolder: './.tmp',
tempFolder: '/tmp/gladys',
},
device: {
camera: {
Expand Down Expand Up @@ -39,6 +41,13 @@ const brokenDevice = {

describe('RtspCameraManager commands', () => {
const rtspCameraManager = new RtspCameraManager(gladys, FfmpegMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc');
before(async () => {
await fse.ensureDir(gladys.config.tempFolder);
});
it('should start service', async () => {
const rtspCameraService = RtspCameraService(gladys, 'de051f90-f34a-4fd5-be2e-e502339ec9bc');
await rtspCameraService.start();
});
it('should getImage', async () => {
const image = await rtspCameraManager.getImage(device);
expect(image).to.equal('image/png;base64,aW1hZ2U=');
Expand Down

0 comments on commit 0177908

Please sign in to comment.