-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.jsx
127 lines (114 loc) · 4.18 KB
/
App.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
import { useState, useEffect } from 'react';
import { Text, View, ScrollView, StatusBar, ImageBackground, SafeAreaView } from 'react-native';
import axios from 'axios';
import TopBar from './components/TopBar';
import AboutModal from './components/AboutModal';
import Cards from './components/Cards';
import CircularLoading from './components/CircularLoading';
import ConnectText from './components/ConnectText';
import EnvChart from './components/EnvChart';
import ChartToggle from './components/ChartToggle';
import URLField from './components/URLBar';
import { colors0, componentStyles } from './components/ComponentStyles';
import utils from './services/WEUtils';
import bgImage from './assets/background.jpg';
const ShowCards = ({ env }) => {
if (env === undefined || env.length === 0) {
return (
<CircularLoading />
);
} else if (!Array.isArray(env)) {
return (
<View style={[componentStyles.appContainer, { margin: 20 }]}>
<Text style={{ fontWeight: 'bold', textAlign: 'center', fontSize: 24, color: colors0.blued, margin: 4 }}>
A backend error has occurred.
</Text>
<Text style={{ fontSize: 20, fontFamily: 'monospace' }}>{`${env.name}: ${env.message}`}</Text>
<SafeAreaView style={{ marginTop: 4, }}>
<ScrollView style={{ height: 100, paddingVertical: 1 }}>
<Text style={{ fontFamily: 'monospace' }}>
{env.stack}
</Text>
</ScrollView></SafeAreaView>
</View>
);
}
else {
return (
<Cards temperature={env[0].envdata.temp} humidity={env[0].envdata.humidity} ppm={env[0].envdata.co2} />
);
}
}
const WebEnv = () => {
const [visible, setModalVisible] = useState(false);
const [chartType, setChartType] = useState(0);
const [env, setEnv] = useState([]);
const [chartData, setChartData] = useState({ labels: [-1], data: [0.0] });
const [URL, setURL] = useState('http://envmon.local:8101/');
useEffect(() => { utils.init(setURL) }, []);
useEffect(() => {
setTimeout(() => {
axios.get(URL)
.then(response => setEnv(response.data))
.catch(e => setEnv({ name: e.name, message: e.message, stack: e.stack }));
}, 2000);
}, [env]);
useEffect(() => {
setEnv([]);
}, [URL]);
useEffect(() => {
if (Array.isArray(env) && env !== undefined && env.length !== 0
&& !(isNaN(env[0].envdata.temp) || isNaN(env[0].envdata.humidity))) {
let newChart = chartData.labels[0] === -1 ? { labels: [], data: [] } : { ...chartData };
if (chartData.data.length >= 5) {
newChart.data.shift();
newChart.labels.shift();
}
newChart.data.push(Number(utils.getChartData(chartType, env)));
newChart.labels.push(
env[0].uptime.hours + ':' + env[0].uptime.minutes + ':' + env[0].uptime.seconds);
setChartData(newChart);
}
}, [env]);
useEffect(() => {
const blankChart = { labels: [], data: [] };
setChartData(blankChart);
}, [chartType]);
return (
<ImageBackground source={bgImage}
style={componentStyles.bgImage}>
<View style={componentStyles.appContainer}>
<StatusBar
animated={true}
backgroundColor={colors0.blued}
hidden={false} />
<TopBar aboutButton={() => setModalVisible(true)} />
<ShowCards env={env} />
<EnvChart chartData={chartData} chartType={chartType} />
<ChartToggle chartType={chartType} setChartType={setChartType} />
<Text style={componentStyles.URLBarTitle}>Backend URL:</Text>
<URLField URL={URL} setURL={setURL} />
<AboutModal visible={visible} setModalVisible={setModalVisible}
envBackend={Array.isArray(env) && env[0] !== undefined ?
env[0].backend : { name: 'unknown', version: 'unknown' }} />
<ConnectText env={env} />
</View>
</ImageBackground>
);
};
const App = () => {
if (utils.isWeb() && !utils.isPortrait()) {
return (
<ImageBackground source={bgImage}
style={componentStyles.bgImage}
blurRadius={10}>
<View style={componentStyles.webWrapper}>
<WebEnv />
</View>
</ImageBackground >
);
} else {
return (<WebEnv />);
}
};
export default App;