@@ -15,6 +15,7 @@ import type {
1515 NetworkSettings ,
1616 NTPSettings ,
1717 NTPTime ,
18+ SecuritySettings ,
1819 TimezoneData ,
1920} from '@/types/opendtu/settings' ;
2021import type { InverterItem } from '@/types/opendtu/state' ;
@@ -126,6 +127,9 @@ class OpenDtuApi {
126127 private onMqttSettingsHandler :
127128 | ( ( data : MqttSettings , index : Index ) => void )
128129 | null = null ;
130+ private onSecuritySettingsHandler :
131+ | ( ( data : SecuritySettings , index : Index ) => void )
132+ | null = null ;
129133
130134 private ws : WebSocket | null = null ;
131135 // communication
@@ -179,21 +183,23 @@ class OpenDtuApi {
179183 public startFetchHttpStateInterval ( ) : void {
180184 log . debug ( 'OpenDtuApi.startFetchHttpStateInterval()' ) ;
181185
182- if ( this . fetchHttpStateInterval ) {
186+ if ( this . fetchHttpStateInterval !== null ) {
183187 log . debug ( 'OpenDtuApi.startFetchHttpStateInterval() already running' ) ;
184188 clearInterval ( this . fetchHttpStateInterval ) ;
185189 }
186190
187- this . fetchHttpStateInterval = setInterval ( ( ) => {
188- this . updateHttpState ( ) ;
191+ if ( this . userString !== null ) {
192+ this . fetchHttpStateInterval = setInterval ( ( ) => {
193+ this . updateHttpState ( ) ;
189194
190- log . debug (
191- 'interval -> OpenDtuApi.updateHttpState()' ,
192- new Date ( ) ,
193- this . index ,
194- this . isConnected ( ) ,
195- ) ;
196- } , 5000 ) ; // 10 seconds
195+ log . debug (
196+ 'interval -> OpenDtuApi.updateHttpState()' ,
197+ new Date ( ) ,
198+ this . index ,
199+ this . isConnected ( ) ,
200+ ) ;
201+ } , 5000 ) ; // 10 seconds
202+ }
197203
198204 this . updateHttpState ( ) ;
199205 }
@@ -383,6 +389,18 @@ class OpenDtuApi {
383389 this . onMqttSettingsHandler = null ;
384390 }
385391
392+ public registerOnSecuritySettingsHandler (
393+ handler : ( data : SecuritySettings , index : Index ) => void ,
394+ ) : void {
395+ log . debug ( 'OpenDtuApi.registerOnSecuritySettingsHandler()' ) ;
396+ this . onSecuritySettingsHandler = handler ;
397+ }
398+
399+ public unregisterOnSecuritySettingsHandler ( ) : void {
400+ log . debug ( 'OpenDtuApi.unregisterOnSecuritySettingsHandler()' ) ;
401+ this . onSecuritySettingsHandler = null ;
402+ }
403+
386404 public async getSystemStatusFromUrl (
387405 url : URL ,
388406 ) : Promise < GetSystemStatusReturn > {
@@ -1583,6 +1601,73 @@ class OpenDtuApi {
15831601 return res . status === 200 && parsed . type === 'success' ;
15841602 }
15851603
1604+ public async getSecurityConfig ( ) : Promise < DtuSettings | null > {
1605+ if ( ! this . baseUrl ) {
1606+ log . error ( 'getSecurityConfig' , 'no base url' ) ;
1607+ return null ;
1608+ }
1609+
1610+ const res = await this . makeAuthenticatedRequest (
1611+ '/api/security/config' ,
1612+ 'GET' ,
1613+ ) ;
1614+
1615+ if ( ! res ) {
1616+ log . error ( 'getSecurityConfig' , 'no response' ) ;
1617+ return null ;
1618+ }
1619+
1620+ if ( res . status === 200 ) {
1621+ const json = await res . json ( ) ;
1622+
1623+ if ( this . onSecuritySettingsHandler && this . index !== null ) {
1624+ this . onSecuritySettingsHandler ( json , this . index ) ;
1625+ }
1626+
1627+ log . debug ( 'getSecurityConfig' , 'success' ) ;
1628+
1629+ return json ;
1630+ }
1631+
1632+ log . error ( 'getSecurityConfig' , 'invalid status' , res . status ) ;
1633+
1634+ return null ;
1635+ }
1636+
1637+ public async setSecurityConfig (
1638+ config : SecuritySettings ,
1639+ ) : Promise < boolean | null > {
1640+ if ( ! this . baseUrl ) {
1641+ log . error ( 'setSecurityConfig' , 'no base url' ) ;
1642+ return null ;
1643+ }
1644+
1645+ const formData = new FormData ( ) ;
1646+ formData . append ( 'data' , JSON . stringify ( config ) ) ;
1647+
1648+ const res = await this . makeAuthenticatedRequest (
1649+ '/api/security/config' ,
1650+ 'POST' ,
1651+ {
1652+ body : formData ,
1653+ } ,
1654+ ) ;
1655+
1656+ if ( ! res ) {
1657+ log . error ( 'setSecurityConfig' , 'no response' ) ;
1658+ return null ;
1659+ }
1660+
1661+ const parsed = await res . json ( ) ;
1662+
1663+ log . debug ( 'setSecurityConfig' , 'success' , {
1664+ status : res . status ,
1665+ parsed,
1666+ } ) ;
1667+
1668+ return res . status === 200 && parsed . type === 'success' ;
1669+ }
1670+
15861671 public async makeAuthenticatedRequest (
15871672 route : string ,
15881673 method : 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'PATCH' ,
0 commit comments