Skip to content

Commit e968935

Browse files
Merge pull request #14 from OpenDTU-App/information-screens
2 parents 95e3d2b + 7e654cf commit e968935

27 files changed

+1434
-297
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ I am a OpenDTU user, but I am not affiliated with the original project. However,
1919
- [ ] Firmware Upgrade with Firmware Browser
2020
- [ ] Device Reboot
2121
- [ ] Information
22-
- [ ] System Information
23-
- [ ] Network Information
24-
- [ ] NTP Information
25-
- [ ] MQTT Information
22+
- [x] System Information
23+
- [x] Network Information
24+
- [x] NTP Information
25+
- [x] MQTT Information
2626
- [ ] tbd if also console
2727
- [ ] Livedata
2828
- [x] Today's Yield

babel.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = {
1818
shippedProposals: true,
1919
},
2020
],
21+
//['@babel/plugin-transform-private-property-in-object', { loose: true }],
2122
'react-native-reanimated/plugin',
2223
[
2324
'module-resolver',

licenses.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"licenseUrl": "[email protected]:os-team/libs/utils",
66
"parents": "opendtu-react-native"
77
},
8-
"@react-native-async-storage/[email protected].5": {
8+
"@react-native-async-storage/[email protected].8": {
99
"licenses": "MIT",
1010
"repository": "https://github.com/react-native-async-storage/async-storage",
1111
"licenseUrl": "https://github.com/react-native-async-storage/async-storage/raw/master/LICENSE",
@@ -106,7 +106,7 @@
106106
"licenseUrl": "https://github.com/ds300/postinstall-postinstall/raw/master/LICENSE",
107107
"parents": "opendtu-react-native"
108108
},
109-
109+
110110
"licenses": "MIT",
111111
"repository": "https://github.com/samber/prometheus-query-js",
112112
"licenseUrl": "https://github.com/samber/prometheus-query-js/raw/master/LICENSE",
@@ -136,7 +136,7 @@
136136
"licenseUrl": "https://github.com/yamankatby/react-native-flex-layout/raw/master/LICENSE",
137137
"parents": "opendtu-react-native"
138138
},
139-
"react-native-get-random-values@1.9.0": {
139+
"react-native-get-random-values@1.10.0": {
140140
"licenses": "MIT",
141141
"repository": "https://github.com/LinusU/react-native-get-random-values",
142142
"licenseUrl": "https://github.com/LinusU/react-native-get-random-values/raw/master/LICENSE",
@@ -166,7 +166,7 @@
166166
"licenseUrl": "https://github.com/web-ridge/react-native-paper-dates/raw/master/LICENSE",
167167
"parents": "opendtu-react-native"
168168
},
169-
169+
170170
"licenses": "MIT",
171171
"repository": "https://github.com/callstack/react-native-paper",
172172
"licenseUrl": "https://github.com/callstack/react-native-paper/raw/master/LICENSE.md",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"react-native-linear-gradient": "^2.8.3",
5454
"react-native-logs": "^5.0.1",
5555
"react-native-markdown-display": "^7.0.0-alpha.2",
56-
"react-native-paper": "^5.11.0",
56+
"react-native-paper": "^5.11.2",
5757
"react-native-paper-dates": "^0.20.4",
5858
"react-native-reanimated": "^3.5.4",
5959
"react-native-safe-area-context": "^4.7.2",

src/App.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const App = () => {
5454

5555
const LightTheme: MD3Theme = {
5656
...MD3LightTheme,
57-
colors: {
57+
/*colors: {
5858
primary: 'rgb(0, 95, 175)',
5959
onPrimary: 'rgb(255, 255, 255)',
6060
primaryContainer: 'rgb(212, 227, 255)',
@@ -95,12 +95,12 @@ const LightTheme: MD3Theme = {
9595
surfaceDisabled: 'rgba(26, 28, 30, 0.12)',
9696
onSurfaceDisabled: 'rgba(26, 28, 30, 0.38)',
9797
backdrop: 'rgba(45, 49, 56, 0.4)',
98-
},
98+
},*/
9999
};
100100

101101
const DarkTheme: MD3Theme = {
102102
...MD3DarkTheme,
103-
colors: {
103+
/*colors: {
104104
primary: 'rgb(165, 200, 255)',
105105
onPrimary: 'rgb(0, 49, 95)',
106106
primaryContainer: 'rgb(0, 71, 134)',
@@ -141,7 +141,7 @@ const DarkTheme: MD3Theme = {
141141
surfaceDisabled: 'rgba(227, 226, 230, 0.12)',
142142
onSurfaceDisabled: 'rgba(227, 226, 230, 0.38)',
143143
backdrop: 'rgba(45, 49, 56, 0.4)',
144-
},
144+
},*/
145145
};
146146

147147
const _App: FC = () => {
@@ -153,14 +153,21 @@ const _App: FC = () => {
153153

154154
const darkMode = useMemo(() => {
155155
if (appTheme === 'system') {
156+
console.log('systemModeWantsDark', systemModeWantsDark);
156157
return systemModeWantsDark;
157158
}
158159

160+
console.log('appTheme === dark', appTheme === 'dark');
159161
return appTheme === 'dark';
160162
}, [appTheme, systemModeWantsDark]);
161163

162164
const theme = useMemo(() => {
163-
if (darkMode) return DarkTheme;
165+
if (darkMode) {
166+
console.log('darkMode');
167+
return DarkTheme;
168+
}
169+
170+
console.log('lightMode');
164171
return LightTheme;
165172
}, [darkMode]);
166173

src/api/ApiHandler.tsx

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import {
77
setDeviceState,
88
setIsConnected,
99
setLiveData,
10+
setMqttStatus,
11+
setNetworkStatus,
12+
setNtpStatus,
1013
setSystemStatus,
1114
setTriedToConnect,
1215
} from '@/slices/opendtu';
@@ -81,21 +84,39 @@ export const ApiProvider: FC<PropsWithChildren<unknown>> = ({ children }) => {
8184
dispatch(setDeviceState({ deviceState: DeviceState.Connected, index }));
8285
});
8386

84-
api.registerSystemStatusHandler((data, index) => {
85-
if (data.systemStatus) {
86-
dispatch(setSystemStatus({ data: data.systemStatus }));
87-
dispatch(
88-
updateDtuHostname({ hostname: data.systemStatus.hostname, index }),
89-
);
90-
dispatch(
91-
updateDtuCustomNameIfEmpty({
92-
customName: data.systemStatus.hostname,
93-
index,
94-
}),
95-
);
96-
}
97-
setDeviceState({ deviceState: DeviceState.Connected, index });
98-
});
87+
api.registerHttpStatusHandler(
88+
({ systemStatus, networkStatus, ntpStatus, mqttStatus }, index) => {
89+
if (systemStatus) {
90+
dispatch(setSystemStatus({ data: systemStatus }));
91+
dispatch(
92+
updateDtuHostname({
93+
hostname: systemStatus.hostname,
94+
index,
95+
}),
96+
);
97+
dispatch(
98+
updateDtuCustomNameIfEmpty({
99+
customName: systemStatus.hostname,
100+
index,
101+
}),
102+
);
103+
}
104+
105+
if (networkStatus) {
106+
dispatch(setNetworkStatus({ data: networkStatus }));
107+
}
108+
109+
if (ntpStatus) {
110+
dispatch(setNtpStatus({ data: ntpStatus }));
111+
}
112+
113+
if (mqttStatus) {
114+
dispatch(setMqttStatus({ data: mqttStatus }));
115+
}
116+
117+
setDeviceState({ deviceState: DeviceState.Connected, index });
118+
},
119+
);
99120

100121
console.log('Connecting API Handler');
101122

src/api/opendtuapi.ts

Lines changed: 86 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import { logger } from 'react-native-logs';
33

44
import type { OpenDTUAuthenticateResponse } from '@/types/opendtu/authenticate';
55
import { DeviceState } from '@/types/opendtu/state';
6-
import type { LiveData, SystemStatus } from '@/types/opendtu/status';
6+
import type {
7+
LiveData,
8+
MqttStatus,
9+
NetworkStatus,
10+
NtpStatus,
11+
SystemStatus,
12+
} from '@/types/opendtu/status';
713
import type { Index, OpenDTUConfig } from '@/types/settings';
814

915
const log = logger.createLogger();
@@ -19,10 +25,15 @@ export interface GetSystemStatusReturn {
1925
deviceState: DeviceState;
2026
}
2127

22-
export type SystemStatusHandler = (
23-
data: GetSystemStatusReturn,
24-
index: Index,
25-
) => void;
28+
export interface HttpStatusData {
29+
systemStatus?: SystemStatus;
30+
deviceState: DeviceState;
31+
networkStatus: NetworkStatus | null;
32+
ntpStatus: NtpStatus | null;
33+
mqttStatus: MqttStatus | null;
34+
}
35+
36+
export type HttpStatusHandler = (data: HttpStatusData, index: Index) => void;
2637

2738
class OpenDtuApi {
2839
// variables for communication
@@ -32,7 +43,7 @@ class OpenDtuApi {
3243

3344
// handlers
3445
private liveDataHandler: LiveDataHandler | null = null;
35-
private systemStatusHandler: SystemStatusHandler | null = null;
46+
private httpStatusHandler: HttpStatusHandler | null = null;
3647
private onConnectedHandler: ((index: Index) => void) | null = null;
3748
private onDisconnectedHandler: (() => void) | null = null;
3849

@@ -107,12 +118,12 @@ class OpenDtuApi {
107118
this.liveDataHandler = null;
108119
}
109120

110-
public registerSystemStatusHandler(handler: SystemStatusHandler): void {
111-
this.systemStatusHandler = handler;
121+
public registerHttpStatusHandler(handler: HttpStatusHandler): void {
122+
this.httpStatusHandler = handler;
112123
}
113124

114-
public unregisterSystemStatusHandler(): void {
115-
this.systemStatusHandler = null;
125+
public unregisterHttpStatusHandler(): void {
126+
this.httpStatusHandler = null;
116127
}
117128

118129
public registerOnConnectedHandler(handler: (index: Index) => void): void {
@@ -431,20 +442,28 @@ class OpenDtuApi {
431442
public async updateHttpState(): Promise<void> {
432443
log.info('OpenDtuApi.updateHttpState()');
433444

434-
if (this.systemStatusHandler && this.index !== null) {
435-
const systemStatus = await this.getSystemStatus();
445+
if (this.index === null) {
446+
log.warn('OpenDtuApi.updateHttpState() index is null');
447+
return;
448+
}
436449

437-
if (
438-
systemStatus.systemStatus &&
439-
'git_hash' in systemStatus.systemStatus
440-
) {
441-
this.systemStatusHandler(systemStatus, this.index);
442-
} else {
443-
console.log('OpenDtuApi.updateHttpState() systemStatus is invalid');
444-
}
445-
} else {
446-
console.log('OpenDtuApi.updateHttpState() systemStatusHandler is null');
450+
if (!this.httpStatusHandler) {
451+
log.warn('OpenDtuApi.updateHttpState() httpStatusHandler is null');
452+
return;
447453
}
454+
455+
const systemStatus = await this.getSystemStatus();
456+
457+
this.httpStatusHandler(
458+
{
459+
systemStatus: systemStatus.systemStatus,
460+
deviceState: systemStatus.deviceState,
461+
networkStatus: await this.getNetworkStatus(),
462+
ntpStatus: await this.getNtpStatus(),
463+
mqttStatus: await this.getMqttStatus(),
464+
},
465+
this.index,
466+
);
448467
}
449468

450469
private getAuthString(): string {
@@ -463,6 +482,51 @@ class OpenDtuApi {
463482
return '';
464483
}
465484

485+
public async getNetworkStatus(): Promise<NetworkStatus | null> {
486+
if (!this.baseUrl) {
487+
return null;
488+
}
489+
490+
const res = await this.makeAuthenticatedRequest(
491+
'/api/network/status',
492+
'GET',
493+
);
494+
495+
if (res.status === 200) {
496+
return await res.json();
497+
}
498+
499+
return null;
500+
}
501+
502+
public async getNtpStatus(): Promise<NtpStatus | null> {
503+
if (!this.baseUrl) {
504+
return null;
505+
}
506+
507+
const res = await this.makeAuthenticatedRequest('/api/ntp/status', 'GET');
508+
509+
if (res.status === 200) {
510+
return await res.json();
511+
}
512+
513+
return null;
514+
}
515+
516+
public async getMqttStatus(): Promise<MqttStatus | null> {
517+
if (!this.baseUrl) {
518+
return null;
519+
}
520+
521+
const res = await this.makeAuthenticatedRequest('/api/mqtt/status', 'GET');
522+
523+
if (res.status === 200) {
524+
return await res.json();
525+
}
526+
527+
return null;
528+
}
529+
466530
public async makeAuthenticatedRequest(
467531
route: string,
468532
method: string,

0 commit comments

Comments
 (0)