2020-04-07 12:49:49 +00:00
|
|
|
import { createStore, applyMiddleware } from "redux";
|
|
|
|
import thunk from "redux-thunk";
|
2020-04-09 11:59:34 +00:00
|
|
|
import update from "immutability-helper";
|
2020-04-07 10:46:55 +00:00
|
|
|
|
|
|
|
function reducer(previousState, action) {
|
2020-04-11 16:29:32 +00:00
|
|
|
let newState, change;
|
|
|
|
|
2020-04-07 10:46:55 +00:00
|
|
|
const createOrUpdateRoom = (room) => {
|
2020-04-10 15:25:52 +00:00
|
|
|
if (!newState.rooms[room.id]) {
|
2020-04-09 11:59:34 +00:00
|
|
|
newState = update(newState, {
|
2020-04-10 15:25:52 +00:00
|
|
|
rooms: { [room.id]: { $set: { ...room, devices: new Set() } } },
|
2020-04-09 11:59:34 +00:00
|
|
|
});
|
2020-04-07 10:46:55 +00:00
|
|
|
} else {
|
2020-04-09 11:59:34 +00:00
|
|
|
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-11 16:29:32 +00:00
|
|
|
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] };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-07 10:46:55 +00:00
|
|
|
switch (action.type) {
|
|
|
|
case "LOGIN_UPDATE":
|
2020-04-09 11:59:34 +00:00
|
|
|
newState = update(previousState, { login: { $set: action.login } });
|
2020-04-07 10:46:55 +00:00
|
|
|
break;
|
|
|
|
case "USER_INFO_UPDATE":
|
2020-04-11 16:29:32 +00:00
|
|
|
newState = update(previousState, { userInfo: { $set: action.userInfo } });
|
2020-04-07 10:46:55 +00:00
|
|
|
break;
|
|
|
|
case "ROOMS_UPDATE":
|
2020-04-09 11:59:34 +00:00
|
|
|
newState = previousState;
|
2020-04-07 10:46:55 +00:00
|
|
|
for (const room of action.rooms) {
|
|
|
|
createOrUpdateRoom(room);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "DEVICES_UPDATE":
|
2020-04-09 11:59:34 +00:00
|
|
|
change = null;
|
|
|
|
|
2020-04-07 10:46:55 +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) {
|
2020-04-09 11:59:34 +00:00
|
|
|
change = {
|
|
|
|
rooms: { [action.roomId]: { devices: { $set: new Set() } } },
|
|
|
|
devices: { $unset: [] },
|
|
|
|
};
|
|
|
|
|
2020-04-07 10:46:55 +00:00
|
|
|
const room = newState.rooms[action.roomId];
|
|
|
|
for (const deviceId of room.devices) {
|
2020-04-09 11:59:34 +00:00
|
|
|
change.devices.$unset.push(deviceId);
|
2020-04-07 10:46:55 +00:00
|
|
|
}
|
|
|
|
} 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.
|
2020-04-09 11:59:34 +00:00
|
|
|
change = {
|
|
|
|
devices: { $unset: [] },
|
|
|
|
rooms: {},
|
|
|
|
};
|
|
|
|
|
2020-04-07 10:46:55 +00:00
|
|
|
for (const device of action.devices) {
|
2020-04-09 11:59:34 +00:00
|
|
|
const roomId = previousState.devices[device.id].roomId;
|
|
|
|
change.rooms[roomId] = change.rooms[roomId] || {
|
|
|
|
devices: { $remove: [] },
|
|
|
|
};
|
|
|
|
change.rooms[roomId].devices.$remove.push(device.id);
|
|
|
|
change.devices.$unset.push(device.id);
|
2020-04-07 10:46:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// otherwise, just delete all devices and all joins
|
|
|
|
// between rooms and devices
|
2020-04-09 11:59:34 +00:00
|
|
|
change = {
|
|
|
|
devices: { $set: {} },
|
|
|
|
rooms: {},
|
|
|
|
};
|
|
|
|
|
2020-04-10 15:25:52 +00:00
|
|
|
for (const room of Object.values(previousState.rooms)) {
|
2020-04-10 22:42:23 +00:00
|
|
|
if (change.rooms[room.id]) {
|
|
|
|
change.rooms[room.id].devices = { $set: new Set() };
|
|
|
|
}
|
2020-04-07 10:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-10 22:42:23 +00:00
|
|
|
|
2020-04-09 11:59:34 +00:00
|
|
|
newState = update(previousState, change);
|
2020-04-07 10:46:55 +00:00
|
|
|
|
2020-04-09 11:59:34 +00:00
|
|
|
change = {
|
|
|
|
devices: {},
|
|
|
|
rooms: {},
|
2020-04-10 15:52:02 +00:00
|
|
|
pendingJoins: { rooms: {} },
|
2020-04-09 11:59:34 +00:00
|
|
|
};
|
2020-04-07 10:46:55 +00:00
|
|
|
for (const device of action.devices) {
|
2020-04-11 16:29:32 +00:00
|
|
|
if (!newState.devices[device.id]) {
|
|
|
|
change.devices[device.id] = { $set: device };
|
|
|
|
} else {
|
|
|
|
updateDeviceProps(device);
|
|
|
|
}
|
2020-04-07 10:46:55 +00:00
|
|
|
|
|
|
|
if (device.roomId in newState.rooms) {
|
2020-04-10 22:42:23 +00:00
|
|
|
change.rooms[device.roomId] = change.rooms[device.roomId] || {};
|
2020-04-11 16:29:32 +00:00
|
|
|
change.rooms[device.roomId].devices =
|
2020-04-10 22:42:23 +00:00
|
|
|
change.rooms[device.roomId].devices || {};
|
2020-04-09 11:59:34 +00:00
|
|
|
const devices = change.rooms[device.roomId].devices;
|
|
|
|
devices.$add = devices.$add || [];
|
|
|
|
devices.$add.push(device.id);
|
2020-04-07 10:46:55 +00:00
|
|
|
} else {
|
2020-04-10 15:52:02 +00:00
|
|
|
// room does not exist yet, so add to the list of pending
|
|
|
|
// joins
|
|
|
|
|
|
|
|
if (!change.pendingJoins.rooms[device.roomId]) {
|
|
|
|
change.pendingJoins.rooms[device.roomId] = {
|
|
|
|
$set: new Set([device.id]),
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
change.pendingJoins.rooms[device.roomId].$set.add(device.id);
|
|
|
|
}
|
2020-04-07 10:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-10 22:42:23 +00:00
|
|
|
|
2020-04-09 11:59:34 +00:00
|
|
|
newState = update(newState, change);
|
2020-04-07 10:46:55 +00:00
|
|
|
break;
|
|
|
|
case "ROOM_SAVE":
|
2020-04-11 11:57:03 +00:00
|
|
|
newState = previousState;
|
2020-04-07 10:46:55 +00:00
|
|
|
createOrUpdateRoom(action.room);
|
|
|
|
break;
|
2020-04-10 15:25:52 +00:00
|
|
|
case "DEVICE_SAVE":
|
2020-04-11 16:29:32 +00:00
|
|
|
change = {
|
2020-04-10 15:25:52 +00:00
|
|
|
devices: { [action.device.id]: { $set: action.device } },
|
2020-04-11 16:29:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2020-04-10 15:25:52 +00:00
|
|
|
break;
|
2020-04-07 10:46:55 +00:00
|
|
|
case "ROOM_DELETE":
|
2020-04-10 15:25:52 +00:00
|
|
|
if (!(action.roomId in previousState.rooms)) {
|
|
|
|
console.warn(`Room to delete ${action.roomId} does not exist`);
|
2020-04-07 10:46:55 +00:00
|
|
|
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
|
2020-04-09 11:59:34 +00:00
|
|
|
change = { devices: { $unset: [] } };
|
|
|
|
|
|
|
|
for (const id of previousState.rooms[action.roomId].devices) {
|
|
|
|
change.devices.$unset.push(id);
|
2020-04-07 10:46:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 15:25:52 +00:00
|
|
|
change.rooms = { $unset: [action.roomId] };
|
2020-04-11 11:57:03 +00:00
|
|
|
|
|
|
|
if (previousState.active.activeRoom === action.roomId) {
|
2020-04-11 16:29:32 +00:00
|
|
|
change.active = { activeRoom: { $set: -1 } };
|
2020-04-11 11:57:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 11:59:34 +00:00
|
|
|
newState = update(previousState, change);
|
2020-04-07 10:46:55 +00:00
|
|
|
break;
|
|
|
|
case "DEVICE_DELETE":
|
2020-04-10 15:25:52 +00:00
|
|
|
if (!(action.deviceId in previousState.devices)) {
|
|
|
|
console.warn(`Device to delete ${action.deviceId} does not exist`);
|
2020-04-07 10:46:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-04-10 15:25:52 +00:00
|
|
|
change = {
|
|
|
|
devices: { $unset: [action.deviceId] },
|
|
|
|
};
|
|
|
|
|
|
|
|
if (previousState.rooms[previousState.devices[action.deviceId].roomId]) {
|
|
|
|
change.rooms = {
|
|
|
|
[previousState.devices[action.deviceId].roomId]: {
|
|
|
|
devices: { $remove: [action.deviceId] },
|
2020-04-09 11:59:34 +00:00
|
|
|
},
|
2020-04-10 15:25:52 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
newState = update(previousState, change);
|
2020-04-07 10:46:55 +00:00
|
|
|
break;
|
|
|
|
case "LOGOUT":
|
2020-04-09 15:24:30 +00:00
|
|
|
newState = update(initState, {});
|
|
|
|
break;
|
|
|
|
case "SET_ACTIVE_ROOM":
|
|
|
|
newState = update(previousState, {
|
|
|
|
active: {
|
|
|
|
activeRoom: {
|
2020-04-10 15:25:52 +00:00
|
|
|
$set: action.activeRoom,
|
2020-04-09 15:24:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2020-04-07 10:46:55 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.warn(`Action type ${action.type} unknown`, action);
|
2020-04-09 11:59:34 +00:00
|
|
|
return previousState;
|
2020-04-07 10:46:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 12:49:49 +00:00
|
|
|
console.log("new state: ", newState);
|
2020-04-11 16:29:32 +00:00
|
|
|
console.log("active room: ", newState.active.activeRoom);
|
2020-04-07 10:46:55 +00:00
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
|
2020-04-09 15:24:30 +00:00
|
|
|
const initState = {
|
|
|
|
errors: {},
|
2020-04-10 15:52:02 +00:00
|
|
|
pendingJoins: {
|
|
|
|
rooms: {},
|
|
|
|
},
|
2020-04-09 15:24:30 +00:00
|
|
|
active: {
|
|
|
|
activeRoom: -1,
|
|
|
|
},
|
|
|
|
login: {
|
|
|
|
loggedIn: false,
|
|
|
|
token: null,
|
|
|
|
},
|
|
|
|
userInfo: null,
|
|
|
|
/** @type {[integer]Room} */
|
|
|
|
rooms: {},
|
|
|
|
/** @type {[integer]Device} */
|
|
|
|
devices: {},
|
|
|
|
};
|
|
|
|
|
2020-04-07 12:49:49 +00:00
|
|
|
function createSmartHutStore() {
|
|
|
|
const token = localStorage.getItem("token");
|
|
|
|
const exp = localStorage.getItem("exp");
|
|
|
|
|
2020-04-09 15:24:30 +00:00
|
|
|
const initialState = update(initState, {
|
2020-04-07 12:49:49 +00:00
|
|
|
login: {
|
2020-04-09 15:24:30 +00:00
|
|
|
token: { $set: token },
|
|
|
|
loggedIn: { $set: !!(token && exp > new Date().getTime()) },
|
2020-04-07 12:49:49 +00:00
|
|
|
},
|
2020-04-09 15:24:30 +00:00
|
|
|
});
|
2020-04-07 12:49:49 +00:00
|
|
|
|
|
|
|
if (!initialState.login.loggedIn) {
|
|
|
|
localStorage.removeItem("token");
|
|
|
|
localStorage.removeItem("exp");
|
|
|
|
initialState.login.token = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return createStore(reducer, initialState, applyMiddleware(thunk));
|
|
|
|
}
|
2020-04-07 10:46:55 +00:00
|
|
|
|
2020-04-07 12:49:49 +00:00
|
|
|
const smartHutStore = createSmartHutStore();
|
2020-04-07 10:46:55 +00:00
|
|
|
export default smartHutStore;
|