-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-context.jsx
138 lines (127 loc) · 4.66 KB
/
api-context.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import React, { useState, useEffect, useContext, createContext } from 'react';
import { GetBuilding, getDevices, getUnit } from '../Service/api-request';
import { useIsAuthenticated } from "@azure/msal-react";
import { useStorageState } from 'react-storage-hooks';
const ApiContext = createContext();
export const ApiContextProvider = (props) => {
const [buildingId, setBuildingId] = useState([]);
const [deviceData, setDeviceData] = useState([]);
// alarmData with test alarm. Remove when alarmData sets by TelemetryAndAlarm component
const [alarmData, setAlarmData] = useStorageState(localStorage, 'alarmData', []);
const [cardData, setCardData] = useState([]);
const [units, setUnits] = useState([]);
const [valueName, setValueName] = useState(
{
'°C': 'Temperatur',
'%': 'Luftfuktighet'
}
);
const isAuthenticated = useIsAuthenticated();
//Get buildingId, units and store it in state
useEffect(() => {
if (isAuthenticated) {
const getBuildingData = async () => {
const data = await GetBuilding()
if (data) {
setBuildingId(data.id);
}
}
const getUnitData = async () => {
const data = await getUnit()
if (data) {
setUnits(data);
}
}
getBuildingData();
getUnitData();
}
}, [])
//Adding sensors in cardData state
useEffect(() => {
if (isAuthenticated && buildingId.length > 0) {
const getDeviceData = async () => {
const data = await getDevices(buildingId);
if (data) {
setDeviceData(data.devices);
}
}
getDeviceData();
}
}, [buildingId, alarmData]);
// Processing deviceData for applying to cardData. cardData = card to render.
useEffect(() => {
if (deviceData.length > 0) {
var newCardData = [...deviceData];
// Add new keys
newCardData.forEach(obj => {
obj.alarm = false;
obj.alarmTimeStamp = null;
obj.unit = null;
obj.measurementName = null;
});
// Removes the name of the messurement in the name
newCardData = newCardData.map(obj => {
let textArray = obj.name.split(' ');
textArray = textArray.splice(1);
let newText = textArray.join(' ');
if(textArray.length === 3) {
obj.name = newText;
}
return obj;
});
// Unit for each sensor sets to key unit
units.forEach(unit => {
newCardData = newCardData.map(obj => {
if (obj.unitId === unit.id) {
var newObj = {...obj}
newObj.unit = unit.unit;
return newObj;
}
return obj;
});
});
// Add measurementName deprnding on unit
newCardData.forEach(obj => {
obj.measurementName = valueName[obj.unit];
});
// Checks if there is any alarms and changes alarm to true and alarmTimeStamp to timeStamp
alarmData.forEach(alarm => {
newCardData = newCardData.map(obj => {
if ( obj.id === alarm.deviceId) {
var newObj = {...obj}
newObj.alarm = true;
newObj.alarmTimeStamp = alarm.timeStamp;
return newObj;
} else
return obj;
});
});
setCardData(newCardData);
}
}, [deviceData, alarmData]);
const restoreAlarmGlobal = () => {
}
// *** Use for develop ***
// useEffect(() => {
// if (buildingId) {
// console.log('deviceData:', deviceData);
// console.log('buildingId', buildingId);
// console.log('units', units);
// console.log('cardData', cardData);
// console.log('alarmData', alarmData);
// }
// });
return (
<ApiContext.Provider value={{
buildingId: buildingId,
deviceData: deviceData,
alarmData: alarmData,
setAlarmData: setAlarmData,
cardData: cardData,
units: units
}}>
{props.children}
</ApiContext.Provider>
)
}
export default useApi = () => useContext(ApiContext);