Skip to content

Commit

Permalink
Fix #1316: Update temperature/humidity in room box live when changes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato authored Jul 21, 2022
1 parent 432f2c5 commit 3ba5dd8
Show file tree
Hide file tree
Showing 10 changed files with 419 additions and 11 deletions.
148 changes: 148 additions & 0 deletions front/cypress/integration/routes/dashboard/DashboardHumidityBox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
describe('Dashboard Humidity Box', () => {
beforeEach(() => {
cy.login();

// Create dashboard
const serverUrl = Cypress.env('serverUrl');
const { rooms } = Cypress.env('house');
const roomSelector = rooms[0].selector;
cy.request({
method: 'POST',
url: `${serverUrl}/api/v1/dashboard`,
body: {
name: 'Test',
type: 'test',
selector: 'test',
boxes: [
[],
[
{
type: 'humidity-in-room',
room: roomSelector
}
],
[]
]
}
});

cy.request({
method: 'GET',
url: `${serverUrl}/api/v1/room/${roomSelector}`
}).then(res => {
// Create humidity device in room
const device1 = {
name: 'First device',
external_id: 'first-device',
selector: 'first-device',
room_id: res.body.id,
features: [
{
name: 'Temp sensor',
category: 'humidity-sensor',
type: 'decimal',
external_id: 'first-humidity',
selector: 'first-humidity',
read_only: true,
keep_history: true,
has_feedback: false,
min: -50,
max: 100
}
]
};
cy.createDevice(device1, 'example');
});

// Create another humidity device without room
const otherRoomDevice = {
name: 'Second device',
external_id: 'second-device',
selector: 'second-device',
features: [
{
name: 'Temp sensor',
category: 'humidity-sensor',
type: 'decimal',
external_id: 'second-humidity',
selector: 'second-humidity',
read_only: true,
keep_history: true,
has_feedback: false,
min: -50,
max: 100
}
]
};
cy.createDevice(otherRoomDevice, 'example');

cy.intercept({
method: 'GET',
url: `${serverUrl}/api/v1/dashboard/test`
}).as('loadDashboard');

cy.intercept({
method: 'GET',
url: `${serverUrl}/api/v1/room/${roomSelector}?expand=humidity,devices`
}).as('loadBox');

cy.visit('/dashboard/test');

cy.wait('@loadDashboard');
cy.wait('@loadBox');
});
afterEach(() => {
// Delete all devices
cy.deleteDevices('example');
// Delete dashboard
const serverUrl = Cypress.env('serverUrl');
cy.request({
method: 'DELETE',
url: `${serverUrl}/api/v1/dashboard/test`
});
});
it('Should have no value box', () => {
cy.contains('p', 'dashboard.boxes.humidityInRoom.noHumidityRecorded').should('exist');

const { rooms } = Cypress.env('house');
const roomName = rooms[0].name;
cy.contains('small', roomName).should('exist');
});

it('Should update humidity on update device value', () => {
const serverUrl = Cypress.env('serverUrl');
const { rooms } = Cypress.env('house');
const roomSelector = rooms[0].selector;

cy.intercept({
method: 'GET',
url: `${serverUrl}/api/v1/room/${roomSelector}?expand=humidity,devices`
}).as('reloadBox');

cy.request({
method: 'POST',
url: `${serverUrl}/api/v1/device/first-device/humidity-sensor/decimal/value`,
body: { value: 24 }
});

cy.sendWebSocket({ type: 'device.new-state', payload: { device_feature_selector: 'first-humidity' } });

cy.wait('@reloadBox');

cy.contains('h4', '24%').should('exist');
});

it('Should not update humidity on update device value', () => {
const serverUrl = Cypress.env('serverUrl');

cy.request({
method: 'POST',
url: `${serverUrl}/api/v1/device/first-device/humidity-sensor/decimal/value`,
body: { value: 24 }
});

cy.sendWebSocket({ type: 'device.new-state', payload: { device_feature_selector: 'second-humidity' } });

cy.contains('p', 'dashboard.boxes.humidityInRoom.noHumidityRecorded').should('exist');
});
});
150 changes: 150 additions & 0 deletions front/cypress/integration/routes/dashboard/DashboardTemperatureBox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
describe('Dashboard Temperature Box', () => {
beforeEach(() => {
cy.login();

// Create dashboard
const serverUrl = Cypress.env('serverUrl');
const { rooms } = Cypress.env('house');
const roomSelector = rooms[0].selector;
cy.request({
method: 'POST',
url: `${serverUrl}/api/v1/dashboard`,
body: {
name: 'Test',
type: 'test',
selector: 'test',
boxes: [
[],
[
{
type: 'temperature-in-room',
room: roomSelector
}
],
[]
]
}
});

cy.request({
method: 'GET',
url: `${serverUrl}/api/v1/room/${roomSelector}`
}).then(res => {
// Create temperature device in room
const device1 = {
name: 'First device',
external_id: 'first-device',
selector: 'first-device',
room_id: res.body.id,
features: [
{
name: 'Temp sensor',
category: 'temperature-sensor',
type: 'decimal',
external_id: 'first-temperature',
selector: 'first-temperature',
unit: 'celsius',
read_only: true,
keep_history: true,
has_feedback: false,
min: -50,
max: 100
}
]
};
cy.createDevice(device1, 'example');
});

// Create another temperature device without room
const otherRoomDevice = {
name: 'Second device',
external_id: 'second-device',
selector: 'second-device',
features: [
{
name: 'Temp sensor',
category: 'temperature-sensor',
type: 'decimal',
external_id: 'second-temperature',
selector: 'second-temperature',
unit: 'celsius',
read_only: true,
keep_history: true,
has_feedback: false,
min: -50,
max: 100
}
]
};
cy.createDevice(otherRoomDevice, 'example');

cy.intercept({
method: 'GET',
url: `${serverUrl}/api/v1/dashboard/test`
}).as('loadDashboard');

cy.intercept({
method: 'GET',
url: `${serverUrl}/api/v1/room/${roomSelector}?expand=temperature,devices`
}).as('loadBox');

cy.visit('/dashboard/test');

cy.wait('@loadDashboard');
cy.wait('@loadBox');
});
afterEach(() => {
// Delete all devices
cy.deleteDevices('example');
// Delete dashboard
const serverUrl = Cypress.env('serverUrl');
cy.request({
method: 'DELETE',
url: `${serverUrl}/api/v1/dashboard/test`
});
});
it('Should have no value box', () => {
cy.contains('p', 'dashboard.boxes.temperatureInRoom.noTemperatureRecorded').should('exist');

const { rooms } = Cypress.env('house');
const roomName = rooms[0].name;
cy.contains('small', roomName).should('exist');
});

it('Should update temperature on update device value', () => {
const serverUrl = Cypress.env('serverUrl');
const { rooms } = Cypress.env('house');
const roomSelector = rooms[0].selector;

cy.intercept({
method: 'GET',
url: `${serverUrl}/api/v1/room/${roomSelector}?expand=temperature,devices`
}).as('reloadBox');

cy.request({
method: 'POST',
url: `${serverUrl}/api/v1/device/first-device/temperature-sensor/decimal/value`,
body: { value: 24 }
});

cy.sendWebSocket({ type: 'device.new-state', payload: { device_feature_selector: 'first-temperature' } });

cy.wait('@reloadBox');

cy.contains('h4', '24°C').should('exist');
});

it('Should not update temperature on update device value', () => {
const serverUrl = Cypress.env('serverUrl');

cy.request({
method: 'POST',
url: `${serverUrl}/api/v1/device/first-device/temperature-sensor/decimal/value`,
body: { value: 24 }
});

cy.sendWebSocket({ type: 'device.new-state', payload: { device_feature_selector: 'second-temperature' } });

cy.contains('p', 'dashboard.boxes.temperatureInRoom.noTemperatureRecorded').should('exist');
});
});
20 changes: 19 additions & 1 deletion front/src/actions/dashboard/boxes/humidityInRoom.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import get from 'get-value';

import { RequestStatus } from '../../../utils/consts';
import createBoxActions from '../boxActions';

Expand All @@ -10,14 +12,30 @@ function createActions(store) {
async getHumidityInRoom(state, box, x, y) {
boxActions.updateBoxStatus(state, BOX_KEY, x, y, RequestStatus.Getting);
try {
const room = await state.httpClient.get(`/api/v1/room/${box.room}?expand=humidity`);
const room = await state.httpClient.get(`/api/v1/room/${box.room}?expand=humidity,devices`);
boxActions.mergeBoxData(state, BOX_KEY, x, y, {
room
});
boxActions.updateBoxStatus(state, BOX_KEY, x, y, RequestStatus.Success);
} catch (e) {
boxActions.updateBoxStatus(state, BOX_KEY, x, y, RequestStatus.Error);
}
},
async deviceFeatureWebsocketEvent(state, box, x, y, payload) {
const data = boxActions.getBoxData(state, BOX_KEY, x, y);
const devices = get(data, 'room.devices', { default: [] });

// Search if feature is in room
const featureIndex = devices.findIndex(device => {
const featureIndex = device.features.findIndex(feature => feature.selector === payload.device_feature_selector);
return featureIndex !== -1;
});

// If feature is in room
if (featureIndex !== -1) {
// Refresh box value
this.getHumidityInRoom(box, x, y);
}
}
};
return Object.assign({}, actions);
Expand Down
20 changes: 19 additions & 1 deletion front/src/actions/dashboard/boxes/temperatureInRoom.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import get from 'get-value';

import { RequestStatus } from '../../../utils/consts';
import createBoxActions from '../boxActions';

Expand All @@ -10,14 +12,30 @@ function createActions(store) {
async getTemperatureInRoom(state, box, x, y) {
boxActions.updateBoxStatus(state, BOX_KEY, x, y, RequestStatus.Getting);
try {
const room = await state.httpClient.get(`/api/v1/room/${box.room}?expand=temperature`);
const room = await state.httpClient.get(`/api/v1/room/${box.room}?expand=temperature,devices`);
boxActions.mergeBoxData(state, BOX_KEY, x, y, {
room
});
boxActions.updateBoxStatus(state, BOX_KEY, x, y, RequestStatus.Success);
} catch (e) {
boxActions.updateBoxStatus(state, BOX_KEY, x, y, RequestStatus.Error);
}
},
async deviceFeatureWebsocketEvent(state, box, x, y, payload) {
const data = boxActions.getBoxData(state, BOX_KEY, x, y);
const devices = get(data, 'room.devices', { default: [] });

// Search if feature is in room
const featureIndex = devices.findIndex(device => {
const featureIndex = device.features.findIndex(feature => feature.selector === payload.device_feature_selector);
return featureIndex !== -1;
});

// If feature is in room
if (featureIndex !== -1) {
// Refresh box value
this.getTemperatureInRoom(box, x, y);
}
}
};
return Object.assign({}, actions);
Expand Down
Loading

0 comments on commit 3ba5dd8

Please sign in to comment.