frontend/smart-hut/src/store.js

642 lines
18 KiB
JavaScript
Raw Normal View History

2020-05-12 13:18:33 +00:00
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import update from 'immutability-helper';
import reduxWebSocket, { connect } from '@giantmachines/redux-websocket';
import { socketURL } from './endpoint';
2020-04-07 10:46:55 +00:00
function reducer(previousState, action) {
2020-05-12 13:18:33 +00:00
let newState; let
change;
2020-04-11 16:29:32 +00:00
2020-04-07 10:46:55 +00:00
const createOrUpdateRoom = (room) => {
2020-04-10 15:25:52 +00:00
if (!newState.rooms[room.id]) {
newState = update(newState, {
2020-04-10 15:25:52 +00:00
rooms: { [room.id]: { $set: { ...room, devices: new Set() } } },
});
2020-04-07 10:46:55 +00:00
} else {
newState = update(newState, {
rooms: {
[room.id]: {
name: { $set: room.name },
image: { $set: room.image },
icon: { $set: room.icon },
},
},
});
2020-04-07 10:46:55 +00:00
}
2020-04-10 15:52:02 +00:00
if (newState.pendingJoins.rooms[room.id]) {
newState = update(newState, {
pendingJoins: { rooms: { $unset: [room.id] } },
rooms: {
[room.id]: {
devices: {
$add: [...newState.pendingJoins.rooms[room.id]],
},
},
},
});
}
2020-04-07 10:46:55 +00:00
};
2020-04-18 14:26:12 +00:00
const createOrUpdateScene = (scene) => {
if (!newState.scenes[scene.id]) {
newState = update(newState, {
scenes: { [scene.id]: { $set: { ...scene, sceneStates: new Set() } } },
2020-04-18 14:26:12 +00:00
});
} else {
newState = update(newState, {
scenes: {
[scene.id]: {
name: { $set: scene.name },
2020-05-04 13:08:17 +00:00
icon: { $set: scene.icon },
2020-05-08 11:45:13 +00:00
guestAccessEnabled: { $set: scene.guestAccessEnabled },
2020-04-18 14:26:12 +00:00
},
},
});
}
2020-05-08 13:45:08 +00:00
if (newState.pendingJoins.scenes[scene.id]) {
newState = update(newState, {
pendingJoins: { scenes: { $unset: [scene.id] } },
scenes: {
[scene.id]: {
sceneStates: {
$add: [...newState.pendingJoins.scenes[scene.id]],
},
},
},
});
}
};
const updateDeviceProps = (device) => {
// In some updates the information regarding a device is incomplete
// due to a fault in the type system and JPA repository management
// in the backend. Therefore to solve this avoid to delete existing
// attributes of this device in the previous state, but just update
// the new ones.
change.devices[device.id] = {};
for (const key in device) {
change.devices[device.id][key] = { $set: device[key] };
}
};
const updateSceneStateProps = (state) => {
change.sceneStates[state.id] = {};
for (const key in state) {
change.sceneStates[state.id][key] = { $set: state[key] };
}
};
switch (action.type) {
2020-05-12 13:18:33 +00:00
case 'LOGIN_UPDATE':
2020-05-08 13:45:08 +00:00
newState = update(previousState, { login: { $set: action.login } });
break;
2020-05-12 13:18:33 +00:00
case 'USER_INFO_UPDATE':
2020-05-08 13:45:08 +00:00
newState = update(previousState, {
userInfo: { $set: action.userInfo },
});
break;
2020-05-12 13:18:33 +00:00
case 'ROOMS_UPDATE':
2020-05-08 13:45:08 +00:00
newState = previousState;
for (const room of action.rooms) {
createOrUpdateRoom(room);
}
break;
2020-05-12 13:18:33 +00:00
case 'HOST_ROOMS_UPDATE':
2020-05-08 13:45:08 +00:00
change = {
hostRooms: {
[action.hostId]: { $set: {} },
},
};
const rooms = change.hostRooms[action.hostId].$set;
for (const room of action.rooms) {
rooms[room.id] = room;
}
newState = update(previousState, change);
break;
2020-05-12 13:18:33 +00:00
case 'SCENES_UPDATE':
2020-05-08 13:45:08 +00:00
newState = previousState;
for (const scene of action.scenes) {
createOrUpdateScene(scene);
}
break;
2020-05-12 13:18:33 +00:00
case 'HOST_SCENES_UPDATE':
2020-05-08 13:45:08 +00:00
change = {
hostScenes: {
[action.hostId]: { $set: action.scenes }, // stored as array
},
};
newState = update(previousState, change);
break;
2020-05-12 13:18:33 +00:00
case 'STATES_UPDATE':
// console.log(action.sceneStates);
2020-05-08 13:45:08 +00:00
change = null;
// if scene is given, delete all sceneStates in that scene
// and remove any join between that scene and deleted
// sceneStates
change = {
scenes: {
[action.sceneId]: { sceneStates: { $set: new Set() } },
},
sceneStates: { $unset: [] },
};
const scene = previousState.scenes[action.sceneId];
for (const stateId of scene.sceneStates) {
change.sceneStates.$unset.push(stateId);
}
newState = update(previousState, change);
change = {
sceneStates: {},
scenes: {},
pendingJoins: { scenes: {} },
};
for (const sceneState of action.sceneStates) {
if (!newState.sceneStates[sceneState.id]) {
change.sceneStates[sceneState.id] = {
$set: sceneState,
};
} else {
updateSceneStateProps(sceneState);
2020-04-07 10:46:55 +00:00
}
2020-05-08 13:45:08 +00:00
if (sceneState.sceneId in newState.scenes) {
2020-05-12 13:18:33 +00:00
change.scenes[sceneState.sceneId] = change.scenes[sceneState.sceneId] || {};
change.scenes[sceneState.sceneId].sceneStates = change.scenes[sceneState.sceneId].sceneStates || {};
const { sceneStates } = change.scenes[sceneState.sceneId];
2020-05-08 13:45:08 +00:00
sceneStates.$add = sceneStates.$add || [];
sceneStates.$add.push(sceneState.id);
} else {
// room does not exist yet, so add to the list of pending
// joins
if (!change.pendingJoins.scenes[sceneState.sceneId]) {
change.pendingJoins.scenes[sceneState.sceneId] = {
$set: new Set([sceneState.id]),
};
2020-05-08 13:45:08 +00:00
} else {
change.pendingJoins.scenes[sceneState.sceneId].$set.add(
2020-05-12 13:18:33 +00:00
sceneState.id,
2020-05-08 13:45:08 +00:00
);
}
}
}
2020-04-07 10:46:55 +00:00
2020-05-08 13:45:08 +00:00
newState = update(newState, change);
2020-04-07 10:46:55 +00:00
2020-05-08 13:45:08 +00:00
break;
2020-05-12 13:18:33 +00:00
case 'DEVICES_UPDATE':
2020-05-08 13:45:08 +00:00
change = null;
2020-04-10 15:52:02 +00:00
2020-05-08 13:45:08 +00:00
// if room is given, delete all devices in that room
// and remove any join between that room and deleted
// devices
if (action.roomId) {
change = {
rooms: {
[action.roomId]: { devices: { $set: new Set() } },
},
devices: { $unset: [] },
};
2020-05-08 13:45:08 +00:00
const room = newState.rooms[action.roomId];
for (const deviceId of room.devices) {
change.devices.$unset.push(deviceId);
}
} else if (action.partial) {
// if the update is partial and caused by an operation on an input
// device (like /switch/operate), iteratively remove deleted
// devices and their join with their corresponding room.
change = {
devices: { $unset: [] },
rooms: {},
};
for (const device of action.devices) {
if (!previousState.devices[device.id]) continue;
change.devices.$unset.push(device.id);
2020-05-12 13:18:33 +00:00
const { roomId } = previousState.devices[device.id];
2020-05-08 13:45:08 +00:00
if (roomId in previousState.rooms) {
change.rooms[roomId] = change.rooms[roomId] || {
devices: { $remove: [] },
};
2020-05-08 13:45:08 +00:00
change.rooms[roomId].devices.$remove.push(device.id);
}
}
} else {
// otherwise, just delete all devices and all joins
// between rooms and devices
change = {
devices: { $set: {} },
rooms: {},
};
for (const room of Object.values(previousState.rooms)) {
if (change.rooms[room.id]) {
change.rooms[room.id].devices = { $set: new Set() };
}
}
}
2020-05-08 13:45:08 +00:00
newState = update(previousState, change);
2020-05-08 13:45:08 +00:00
change = {
devices: {},
rooms: {},
pendingJoins: { rooms: {} },
};
for (const device of action.devices) {
if (!newState.devices[device.id]) {
change.devices[device.id] = { $set: device };
} else {
updateDeviceProps(device);
}
2020-05-08 13:45:08 +00:00
if (device.roomId in newState.rooms) {
change.rooms[device.roomId] = change.rooms[device.roomId] || {};
2020-05-12 13:18:33 +00:00
change.rooms[device.roomId].devices = change.rooms[device.roomId].devices || {};
const { devices } = change.rooms[device.roomId];
2020-05-08 13:45:08 +00:00
devices.$add = devices.$add || [];
devices.$add.push(device.id);
} else {
// room does not exist yet, so add to the list of pending
// joins
2020-05-08 13:45:08 +00:00
if (!change.pendingJoins.rooms[device.roomId]) {
change.pendingJoins.rooms[device.roomId] = {
$set: new Set([device.id]),
};
2020-05-08 13:45:08 +00:00
} else {
change.pendingJoins.rooms[device.roomId].$set.add(device.id);
}
}
}
newState = update(newState, change);
break;
2020-05-12 13:18:33 +00:00
case 'HOST_DEVICES_UPDATE':
2020-05-08 13:45:08 +00:00
newState = action.partial
? previousState
: update(previousState, {
hostDevices: { [action.hostId]: { $set: {} } },
});
2020-05-12 13:18:33 +00:00
newState.hostDevices[action.hostId] = newState.hostDevices[action.hostId] || {};
2020-05-08 13:45:08 +00:00
change = {
hostDevices: {
[action.hostId]: {},
},
};
const deviceMap = change.hostDevices[action.hostId];
for (const device of action.devices) {
deviceMap[device.id] = { $set: device };
}
newState = update(newState, change);
break;
2020-05-12 13:18:33 +00:00
case 'AUTOMATION_UPDATE':
2020-05-08 13:45:08 +00:00
const automations = {};
for (const automation of action.automations) {
automations[automation.id] = automation;
}
change = {
automations: { $set: automations },
};
newState = update(previousState, change);
break;
2020-05-12 13:18:33 +00:00
case 'ROOM_SAVE':
2020-05-08 13:45:08 +00:00
newState = previousState;
createOrUpdateRoom(action.room);
break;
2020-05-12 13:18:33 +00:00
case 'SCENE_SAVE':
2020-05-08 13:45:08 +00:00
newState = previousState;
createOrUpdateScene(action.scene);
break;
2020-05-12 13:18:33 +00:00
case 'DEVICE_SAVE':
2020-05-08 13:45:08 +00:00
change = {
devices: { [action.device.id]: { $set: action.device } },
};
if (previousState.rooms[action.device.roomId]) {
change.rooms = {
[action.device.roomId]: {
devices: {
$add: [action.device.id],
},
},
};
} else {
change.pendingJoins = {
rooms: {
[action.device.roomId]: {
$add: [action.device.id],
},
},
};
}
newState = update(previousState, change);
break;
2020-05-12 13:18:33 +00:00
case 'HOST_DEVICE_SAVE':
2020-05-08 13:45:08 +00:00
change = {
hostDevices: {
[action.hostId]: {
[action.device.id]: {
$set: action.device,
},
},
},
};
newState = update(previousState, change);
break;
2020-05-12 13:18:33 +00:00
case 'HOST_DEVICES_DELETE':
2020-05-08 13:45:08 +00:00
change = {
hostDevices: {
[action.hostId]: {
$unset: [action.deviceIds],
},
},
};
newState = update(previousState, change);
break;
2020-05-12 13:18:33 +00:00
case 'AUTOMATION_SAVE':
2020-05-08 13:45:08 +00:00
change = {
automations: {
[action.automation.id]: { $set: action.automation },
},
};
2020-05-08 13:45:08 +00:00
newState = update(previousState, change);
2020-05-08 13:45:08 +00:00
break;
2020-05-12 13:18:33 +00:00
case 'STATE_SAVE':
2020-05-08 13:45:08 +00:00
change = {
sceneStates: {
[action.sceneState.id]: { $set: action.sceneState },
},
};
2020-05-08 13:45:08 +00:00
if (previousState.scenes[action.sceneState.sceneId]) {
change.scenes = {
[action.sceneState.sceneId]: {
sceneStates: {
$add: [action.sceneState.id],
},
},
};
} else {
change.pendingJoins = {
scenes: {
[action.sceneState.sceneId]: {
$add: [action.sceneState.id],
},
},
};
}
newState = update(previousState, change);
break;
2020-05-12 13:18:33 +00:00
case 'ROOM_DELETE':
2020-05-08 13:45:08 +00:00
if (!(action.roomId in previousState.rooms)) {
console.warn(`Room to delete ${action.roomId} does not exist`);
break;
}
// This update does not ensure the consistent update of switchId/dimmerId properties
// on output devices connected to an input device in this room. Please manually request
// all devices again if consistent update is desired
change = { devices: { $unset: [] } };
for (const id of previousState.rooms[action.roomId].devices) {
change.devices.$unset.push(id);
}
change.rooms = { $unset: [action.roomId] };
if (previousState.active.activeRoom === action.roomId) {
change.active = { activeRoom: { $set: -1 } };
}
newState = update(previousState, change);
break;
2020-05-12 13:18:33 +00:00
case 'AUTOMATION_DELETE':
2020-05-08 13:45:08 +00:00
change = {
automations: { $unset: [action.id] },
};
newState = update(previousState, change);
break;
2020-05-12 13:18:33 +00:00
case 'SCENE_DELETE':
2020-05-08 13:45:08 +00:00
if (!(action.sceneId in previousState.scenes)) {
console.warn(`Scene to delete ${action.sceneId} does not exist`);
break;
}
// This update does not ensure the consistent update of switchId/dimmerId properties
// on output devices connected to an input device in this room. Please manually request
// all devices again if consistent update is desired
change = { sceneStates: { $unset: [] } };
for (const id of previousState.scenes[action.sceneId].sceneStates) {
change.sceneStates.$unset.push(id);
}
change.scenes = { $unset: [action.sceneId] };
if (previousState.active.activeScene === action.sceneId) {
change.active = { activeScene: { $set: -1 } };
}
newState = update(previousState, change);
break;
2020-05-12 13:18:33 +00:00
case 'STATE_DELETE':
2020-05-08 13:45:08 +00:00
if (!(action.stateId in previousState.sceneStates)) {
console.warn(`State to delete ${action.stateId} does not exist`);
break;
}
change = {
sceneStates: { $unset: [action.stateId] },
};
if (
previousState.scenes[previousState.sceneStates[action.stateId].sceneId]
) {
change.scenes = {
[previousState.sceneStates[action.stateId].sceneId]: {
sceneStates: { $remove: [action.stateId] },
},
};
}
newState = update(previousState, change);
break;
2020-05-12 13:18:33 +00:00
case 'DEVICE_DELETE':
2020-05-08 13:45:08 +00:00
if (!(action.deviceId in previousState.devices)) {
console.warn(`Device to delete ${action.deviceId} does not exist`);
break;
}
change = {
devices: { $unset: [action.deviceId] },
};
if (previousState.rooms[previousState.devices[action.deviceId].roomId]) {
change.rooms = {
[previousState.devices[action.deviceId].roomId]: {
devices: { $remove: [action.deviceId] },
},
};
}
newState = update(previousState, change);
break;
2020-05-12 13:18:33 +00:00
case 'LOGOUT':
2020-05-08 13:45:08 +00:00
newState = update(initState, {});
break;
2020-05-12 13:18:33 +00:00
case 'SET_ACTIVE':
2020-05-08 13:45:08 +00:00
newState = update(previousState, {
active: {
[action.key]: {
$set: action.value,
},
},
});
break;
2020-05-12 13:18:33 +00:00
case 'REDUX_WEBSOCKET::MESSAGE':
2020-05-08 13:45:08 +00:00
const allDevices = JSON.parse(action.payload.message);
const devices = allDevices.filter(
2020-05-12 13:18:33 +00:00
(d) => (d.fromHostId === null || d.fromHostId === undefined) && !d.deleted,
2020-05-08 13:45:08 +00:00
);
const hostDevicesMapByHostId = allDevices
.filter((d) => d.fromHostId)
.reduce((a, e) => {
const hostId = e.fromHostId;
2020-05-12 13:18:33 +00:00
// delete e.fromHostId;
2020-05-08 13:45:08 +00:00
a[hostId] = a[hostId] || { updated: [], deletedIds: [] };
if (e.deleted) {
a[hostId].deletedIds.push(e.id);
} else {
a[hostId].updated.push(e);
}
return a;
}, {});
newState = reducer(previousState, {
2020-05-12 13:18:33 +00:00
type: 'DEVICES_UPDATE',
2020-05-08 13:45:08 +00:00
partial: true,
devices,
});
for (const hostId in hostDevicesMapByHostId) {
2020-05-12 13:18:33 +00:00
if (hostDevicesMapByHostId[hostId].updated.length > 0) {
newState = reducer(newState, {
type: 'HOST_DEVICES_UPDATE',
2020-05-08 13:45:08 +00:00
devices: hostDevicesMapByHostId[hostId].updated,
partial: true,
hostId,
});
2020-05-12 13:18:33 +00:00
}
2020-05-08 13:45:08 +00:00
if (hostDevicesMapByHostId[hostId].deletedIds.length > 0) {
newState = reducer(newState, {
2020-05-12 13:18:33 +00:00
type: 'HOST_DEVICES_DELETE',
2020-05-08 13:45:08 +00:00
deviceIds: hostDevicesMapByHostId[hostId].deletedIds,
partial: true,
hostId,
});
}
}
break;
2020-05-12 13:18:33 +00:00
case 'HG_UPDATE':
2020-05-08 13:45:08 +00:00
newState = update(previousState, {
[action.key]: { $set: action.value },
});
break;
default:
console.warn(`Action type ${action.type} unknown`, action);
return previousState;
}
return newState;
2020-04-07 10:46:55 +00:00
}
2020-04-09 15:24:30 +00:00
const initState = {
2020-05-08 13:45:08 +00:00
pendingJoins: {
2020-04-10 15:52:02 +00:00
rooms: {},
scenes: {},
automations: {},
2020-05-08 13:45:08 +00:00
},
active: {
activeRoom: -1,
2020-05-12 13:18:33 +00:00
activeTab: 'Devices',
2020-05-08 13:45:08 +00:00
activeScene: -1,
activeAutomation: -1,
activeHost: -1,
},
login: {
loggedIn: false,
token: null,
},
userInfo: null,
/** @type {[integer]Room} */
rooms: {},
/** @type {[integer]Scene} */
scenes: {},
hostScenes: {},
/** @type {[integer]Automation} */
automations: {},
/** @type {[integer]Device} */
devices: {},
/** @type {[integer]SceneState} */
sceneStates: {},
/** @type {User[]} */
guests: [],
/** @type {User[]} */
hosts: [],
/** @type {[integer]Device} */
hostDevices: {},
/** @type {[integer]Eoom} */
hostRooms: {},
2020-04-09 15:24:30 +00:00
};
function createSmartHutStore() {
2020-05-12 13:18:33 +00:00
const token = localStorage.getItem('token');
const exp = localStorage.getItem('exp');
2020-05-08 13:45:08 +00:00
const initialState = update(initState, {
login: {
token: { $set: token },
loggedIn: { $set: !!(token && exp > new Date().getTime()) },
},
});
if (!initialState.login.loggedIn) {
2020-05-12 13:18:33 +00:00
localStorage.removeItem('token');
localStorage.removeItem('exp');
2020-05-08 13:45:08 +00:00
initialState.login.token = null;
}
const store = createStore(
reducer,
initialState,
2020-05-12 13:18:33 +00:00
compose(applyMiddleware(thunk), applyMiddleware(reduxWebSocket())),
2020-05-08 13:45:08 +00:00
);
if (initialState.login.loggedIn) {
store.dispatch(connect(socketURL(token)));
}
return store;
}
2020-04-07 10:46:55 +00:00
const smartHutStore = createSmartHutStore();
2020-04-07 10:46:55 +00:00
export default smartHutStore;