@@ -10,6 +10,7 @@ import type { EventLogData } from '@/types/opendtu/eventlog';
1010import type { GridProfileData } from '@/types/opendtu/gridprofile' ;
1111import type { InverterDeviceData } from '@/types/opendtu/inverterDevice' ;
1212import type {
13+ DtuSettings ,
1314 NetworkSettings ,
1415 NTPSettings ,
1516 NTPTime ,
@@ -118,6 +119,9 @@ class OpenDtuApi {
118119 private onNtpSettingsHandler :
119120 | ( ( data : NTPSettings , index : Index ) => void )
120121 | null = null ;
122+ private onDtuSettingsHandler :
123+ | ( ( data : DtuSettings , index : Index ) => void )
124+ | null = null ;
121125
122126 private ws : WebSocket | null = null ;
123127 // communication
@@ -351,6 +355,18 @@ class OpenDtuApi {
351355 this . onNtpSettingsHandler = null ;
352356 }
353357
358+ public registerOnDtuSettingsHandler (
359+ handler : ( data : DtuSettings , index : Index ) => void ,
360+ ) : void {
361+ log . debug ( 'OpenDtuApi.registerOnDtuSettingsHandler()' ) ;
362+ this . onDtuSettingsHandler = handler ;
363+ }
364+
365+ public unregisterOnDtuSettingsHandler ( ) : void {
366+ log . debug ( 'OpenDtuApi.unregisterOnDtuSettingsHandler()' ) ;
367+ this . onDtuSettingsHandler = null ;
368+ }
369+
354370 public async getSystemStatusFromUrl (
355371 url : URL ,
356372 ) : Promise < GetSystemStatusReturn > {
@@ -1401,6 +1417,64 @@ class OpenDtuApi {
14011417 return res . status === 200 && parsed . type === 'success' ;
14021418 }
14031419
1420+ public async getDtuConfig ( ) : Promise < DtuSettings | null > {
1421+ if ( ! this . baseUrl ) {
1422+ log . error ( 'getDtuConfig' , 'no base url' ) ;
1423+ return null ;
1424+ }
1425+
1426+ const res = await this . makeAuthenticatedRequest ( '/api/dtu/config' , 'GET' ) ;
1427+
1428+ if ( ! res ) {
1429+ log . error ( 'getDtuConfig' , 'no response' ) ;
1430+ return null ;
1431+ }
1432+
1433+ if ( res . status === 200 ) {
1434+ const json = await res . json ( ) ;
1435+
1436+ if ( this . onDtuSettingsHandler && this . index !== null ) {
1437+ this . onDtuSettingsHandler ( json , this . index ) ;
1438+ }
1439+
1440+ log . debug ( 'getDtuConfig' , 'success' ) ;
1441+
1442+ return json ;
1443+ }
1444+
1445+ log . error ( 'getDtuConfig' , 'invalid status' , res . status ) ;
1446+
1447+ return null ;
1448+ }
1449+
1450+ public async setDtuConfig ( config : DtuSettings ) : Promise < boolean | null > {
1451+ if ( ! this . baseUrl ) {
1452+ log . error ( 'setDtuConfig' , 'no base url' ) ;
1453+ return null ;
1454+ }
1455+
1456+ const formData = new FormData ( ) ;
1457+ formData . append ( 'data' , JSON . stringify ( config ) ) ;
1458+
1459+ const res = await this . makeAuthenticatedRequest ( '/api/dtu/config' , 'POST' , {
1460+ body : formData ,
1461+ } ) ;
1462+
1463+ if ( ! res ) {
1464+ log . error ( 'setDtuConfig' , 'no response' ) ;
1465+ return null ;
1466+ }
1467+
1468+ const parsed = await res . json ( ) ;
1469+
1470+ log . debug ( 'setDtuConfig' , 'success' , {
1471+ status : res . status ,
1472+ parsed,
1473+ } ) ;
1474+
1475+ return res . status === 200 && parsed . type === 'success' ;
1476+ }
1477+
14041478 public async makeAuthenticatedRequest (
14051479 route : string ,
14061480 method : 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'PATCH' ,
0 commit comments