@@ -138,6 +138,13 @@ interface RepoRequester {
138138 callback : ioBroker . MessageCallbackInfo ;
139139}
140140
141+ interface SendResponseToOptions {
142+ /** The message we want to respond to */
143+ receivedMsg : ioBroker . SendableMessage ;
144+ /** The response payload */
145+ payload : Record < string , unknown > ;
146+ }
147+
141148/** Host information including host id and running version */
142149type HostInformation = ioBroker . HostCommon & { host : string ; runningVersion : string } ;
143150
@@ -272,7 +279,7 @@ function getConfig(): ioBroker.IoBrokerJson | never {
272279 * @param _config
273280 * @param secret
274281 */
275- async function _startMultihost ( _config : Record < string , any > , secret : string | false ) : Promise < void > {
282+ async function _startMultihost ( _config : ioBroker . IoBrokerJson , secret : string | false ) : Promise < void > {
276283 const MHService = await import ( './lib/multihostServer.js' ) ;
277284 const cpus = os . cpus ( ) ;
278285 mhService = new MHService . MHServer (
@@ -297,7 +304,7 @@ async function _startMultihost(_config: Record<string, any>, secret: string | fa
297304 *
298305 * @param __config - the iobroker config object
299306 */
300- async function startMultihost ( __config ?: Record < string , any > ) : Promise < boolean | void > {
307+ async function startMultihost ( __config ?: ioBroker . IoBrokerJson ) : Promise < boolean | void > {
301308 if ( compactGroupController ) {
302309 return ;
303310 }
@@ -341,7 +348,7 @@ async function startMultihost(__config?: Record<string, any>): Promise<boolean |
341348 let obj : ioBroker . SystemConfigObject | null | undefined ;
342349 let errText ;
343350 try {
344- obj = await objects ! . getObjectAsync ( SYSTEM_CONFIG_ID ) ;
351+ obj = await objects ! . getObject ( SYSTEM_CONFIG_ID ) ;
345352 } catch ( e ) {
346353 // will log error below
347354 errText = e . message ;
@@ -1405,7 +1412,7 @@ async function collectDiagInfo(type: DiagInfoType): Promise<void | Record<string
14051412 let err ;
14061413
14071414 try {
1408- systemConfig = await objects ! . getObjectAsync ( SYSTEM_CONFIG_ID ) ;
1415+ systemConfig = await objects ! . getObject ( SYSTEM_CONFIG_ID ) ;
14091416 } catch ( e ) {
14101417 err = e ;
14111418 }
@@ -2074,8 +2081,7 @@ async function processMessage(msg: ioBroker.SendableMessage): Promise<null | voi
20742081
20752082 // Collect statistics (only if license has been confirmed - user agreed)
20762083 if (
2077- systemConfig ?. common &&
2078- systemConfig . common . diag &&
2084+ systemConfig ?. common ?. diag &&
20792085 systemConfig . common . licenseConfirmed &&
20802086 ( ! lastDiagSend || Date . now ( ) - lastDiagSend > 30_000 ) // prevent sending of diagnostics by multiple admin instances
20812087 ) {
@@ -2849,9 +2855,8 @@ async function processMessage(msg: ioBroker.SendableMessage): Promise<null | voi
28492855 const configFile = tools . getConfigFileName ( ) ;
28502856 if ( fs . existsSync ( configFile ) ) {
28512857 try {
2852- let config = fs . readFileSync ( configFile ) . toString ( 'utf8' ) ;
2858+ const config : ioBroker . IoBrokerJson = fs . readJsonSync ( configFile ) ;
28532859 const stat = fs . lstatSync ( configFile ) ;
2854- config = JSON . parse ( config ) ;
28552860 sendTo ( msg . from , msg . command , { config, isActive : uptimeStart > stat . mtimeMs } , msg . callback ) ;
28562861 } catch {
28572862 const error = `Cannot parse file ${ configFile } ` ;
@@ -2871,56 +2876,58 @@ async function processMessage(msg: ioBroker.SendableMessage): Promise<null | voi
28712876 break ;
28722877
28732878 case 'writeBaseSettings' : {
2874- let error ;
2875- if ( msg . message ) {
2876- const configFile = tools . getConfigFileName ( ) ;
2877- if ( fs . existsSync ( configFile ) ) {
2878- let config ;
2879- if ( typeof msg . message === 'string' ) {
2880- try {
2881- config = JSON . parse ( msg . message ) ;
2882- } catch {
2883- error = `Cannot parse data ${ msg . message } ` ;
2884- }
2885- } else {
2886- config = msg . message ;
2887- }
2879+ if ( ! msg . message ) {
2880+ const error = `No data found on writeBaseSettings from "${ msg . from } "` ;
2881+ logger . error ( `${ hostLogPrefix } ${ error } ` ) ;
2882+ return sendResponseTo ( { receivedMsg : msg , payload : { error } } ) ;
2883+ }
28882884
2889- if ( ! error ) {
2890- // todo validate structure, because very important
2891- if ( ! config . system ) {
2892- error = 'Cannot find "system" in data' ;
2893- } else if ( ! config . objects ) {
2894- error = 'Cannot find "objects" in data' ;
2895- } else if ( ! config . states ) {
2896- error = 'Cannot find "states" in data' ;
2897- } else if ( ! config . log ) {
2898- error = 'Cannot find "log" in data' ;
2899- }
2900- }
2885+ const configFile = tools . getConfigFileName ( ) ;
29012886
2902- if ( ! error ) {
2903- try {
2904- fs . writeFileSync ( configFile , JSON . stringify ( config , null , 2 ) ) ;
2905- } catch {
2906- error = `Cannot write file ${ configFile } ` ;
2907- }
2908- }
2909- }
2910- } else {
2911- error = `No data found for writeBaseSettings ${ msg . from } ` ;
2887+ if ( ! fs . existsSync ( configFile ) ) {
2888+ const error = `No config file exists on writeBaseSettings from "${ msg . from } "` ;
2889+ logger . error ( `${ hostLogPrefix } ${ error } ` ) ;
2890+ return sendResponseTo ( { receivedMsg : msg , payload : { error } } ) ;
29122891 }
29132892
2914- if ( error ) {
2915- logger . error ( `${ hostLogPrefix } ${ error } ` ) ;
2916- if ( msg . callback && msg . from ) {
2917- sendTo ( msg . from , msg . command , { error } , msg . callback ) ;
2893+ let config : ioBroker . IoBrokerJson | undefined ;
2894+ if ( typeof msg . message === 'string' ) {
2895+ try {
2896+ config = JSON . parse ( msg . message ) ;
2897+ } catch {
2898+ return sendResponseTo ( {
2899+ receivedMsg : msg ,
2900+ payload : { error : `Cannot parse data: "${ msg . message } "` }
2901+ } ) ;
29182902 }
29192903 } else {
2920- msg . callback && msg . from && sendTo ( msg . from , msg . command , { result : 'ok' } , msg . callback ) ;
2904+ config = msg . message ;
29212905 }
29222906
2923- break ;
2907+ if ( ! config ) {
2908+ return sendResponseTo ( { receivedMsg : msg , payload : { error : 'Empty config' } } ) ;
2909+ }
2910+
2911+ if ( ! config . system ) {
2912+ return sendResponseTo ( { receivedMsg : msg , payload : { error : 'Cannot find "system" in data' } } ) ;
2913+ }
2914+ if ( ! config . objects ) {
2915+ return sendResponseTo ( { receivedMsg : msg , payload : { error : 'Cannot find "objects" in data' } } ) ;
2916+ }
2917+ if ( ! config . states ) {
2918+ return sendResponseTo ( { receivedMsg : msg , payload : { error : 'Cannot find "states" in data' } } ) ;
2919+ }
2920+ if ( ! config . log ) {
2921+ return sendResponseTo ( { receivedMsg : msg , payload : { error : 'Cannot find "log" in data' } } ) ;
2922+ }
2923+
2924+ try {
2925+ fs . writeFileSync ( configFile , JSON . stringify ( config , null , 2 ) ) ;
2926+ } catch {
2927+ return sendResponseTo ( { receivedMsg : msg , payload : { error : `Cannot write file ${ configFile } ` } } ) ;
2928+ }
2929+
2930+ return sendResponseTo ( { receivedMsg : msg , payload : { result : 'ok' } } ) ;
29242931 }
29252932
29262933 case 'addNotification' :
@@ -3039,6 +3046,19 @@ async function processMessage(msg: ioBroker.SendableMessage): Promise<null | voi
30393046 }
30403047}
30413048
3049+ /**
3050+ * Wrapper around sendTo for message responses
3051+ *
3052+ * @param options The received message and response payload
3053+ */
3054+ async function sendResponseTo ( options : SendResponseToOptions ) : Promise < void > {
3055+ const { receivedMsg, payload } = options ;
3056+
3057+ if ( receivedMsg . callback && receivedMsg . from ) {
3058+ await sendTo ( receivedMsg . from , receivedMsg . command , payload , receivedMsg . callback ) ;
3059+ }
3060+ }
3061+
30423062/**
30433063 * Collect all instances on this host and call `initInstances`
30443064 */
@@ -5242,7 +5262,7 @@ export async function init(compactGroupId?: number): Promise<void> {
52425262 stopTimeout += 5_000 ;
52435263 }
52445264
5245- // If bootstrap file detected, it must be deleted, but give time for a bootstrap process to use this file
5265+ // If a bootstrap file detected, it must be deleted, but give time for a bootstrap process to use this file
52465266 if ( fs . existsSync ( VENDOR_BOOTSTRAP_FILE ) ) {
52475267 setTimeout ( ( ) => {
52485268 try {
0 commit comments