@@ -9,6 +9,7 @@ import type {
99import type { EventLogData } from '@/types/opendtu/eventlog' ;
1010import type { GridProfileData } from '@/types/opendtu/gridprofile' ;
1111import type { InverterDeviceData } from '@/types/opendtu/inverterDevice' ;
12+ import type { NetworkSettings } from '@/types/opendtu/settings' ;
1213import type { InverterItem } from '@/types/opendtu/state' ;
1314import { DeviceState } from '@/types/opendtu/state' ;
1415import type {
@@ -99,6 +100,9 @@ class OpenDtuApi {
99100 inverterSerial : InverterSerial ,
100101 ) => void )
101102 | null = null ;
103+ private onNetworkSettingsHandler :
104+ | ( ( data : NetworkSettings , index : Index ) => void )
105+ | null = null ;
102106
103107 private ws : WebSocket | null = null ;
104108 // communication
@@ -281,6 +285,16 @@ class OpenDtuApi {
281285 this . onGridProfileHandler = null ;
282286 }
283287
288+ public registerOnNetworkSettingsHandler (
289+ handler : ( data : NetworkSettings , index : Index ) => void ,
290+ ) : void {
291+ this . onNetworkSettingsHandler = handler ;
292+ }
293+
294+ public unregisterOnNetworkSettingsHandler ( ) : void {
295+ this . onNetworkSettingsHandler = null ;
296+ }
297+
284298 public async getSystemStatusFromUrl (
285299 url : URL ,
286300 ) : Promise < GetSystemStatusReturn > {
@@ -1007,6 +1021,64 @@ class OpenDtuApi {
10071021 return res . status === 200 && parsed . type === 'success' ;
10081022 }
10091023
1024+ public async getNetworkConfig ( ) : Promise < NetworkSettings | null > {
1025+ if ( ! this . baseUrl ) {
1026+ return null ;
1027+ }
1028+
1029+ const res = await this . makeAuthenticatedRequest (
1030+ '/api/network/config' ,
1031+ 'GET' ,
1032+ ) ;
1033+
1034+ if ( ! res ) {
1035+ log . error ( 'getNetworkConfig' , 'no response' ) ;
1036+ return null ;
1037+ }
1038+
1039+ if ( res . status === 200 ) {
1040+ const json = await res . json ( ) ;
1041+
1042+ if ( this . onNetworkSettingsHandler && this . index !== null ) {
1043+ this . onNetworkSettingsHandler ( json , this . index ) ;
1044+ }
1045+
1046+ return json ;
1047+ }
1048+
1049+ log . error ( 'getNetworkConfig' , 'invalid status' ) ;
1050+
1051+ return null ;
1052+ }
1053+
1054+ public async setNetworkConfig (
1055+ config : NetworkSettings ,
1056+ ) : Promise < boolean | null > {
1057+ if ( ! this . baseUrl ) {
1058+ return null ;
1059+ }
1060+
1061+ const formData = new FormData ( ) ;
1062+ formData . append ( 'data' , JSON . stringify ( config ) ) ;
1063+
1064+ const res = await this . makeAuthenticatedRequest (
1065+ '/api/network/config' ,
1066+ 'POST' ,
1067+ {
1068+ body : formData ,
1069+ } ,
1070+ ) ;
1071+
1072+ if ( ! res ) {
1073+ log . error ( 'setNetworkConfig' , 'no response' ) ;
1074+ return null ;
1075+ }
1076+
1077+ const parsed = await res . json ( ) ;
1078+
1079+ return res . status === 200 && parsed . type === 'success' ;
1080+ }
1081+
10101082 public async makeAuthenticatedRequest (
10111083 route : string ,
10121084 method : 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'PATCH' ,
0 commit comments