@@ -3,7 +3,13 @@ import { logger } from 'react-native-logs';
33
44import type { OpenDTUAuthenticateResponse } from '@/types/opendtu/authenticate' ;
55import { 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' ;
713import type { Index , OpenDTUConfig } from '@/types/settings' ;
814
915const 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
2738class 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