@@ -7849,7 +7849,7 @@ export class AdapterClass extends EventEmitter {
7849
7849
aliasId ,
7850
7850
tools . formatAliasValue ( {
7851
7851
sourceCommon : obj ?. common ,
7852
- targetCommon : targetObj ?. common as any ,
7852
+ targetCommon : targetObj ?. common ,
7853
7853
state : stateObj as ioBroker . State ,
7854
7854
logger : this . _logger ,
7855
7855
logNamespace : this . namespaceLog ,
@@ -9600,125 +9600,101 @@ export class AdapterClass extends EventEmitter {
9600
9600
}
9601
9601
}
9602
9602
9603
- private async _addAliasSubscribe (
9604
- aliasObj : ioBroker . StateObject ,
9605
- pattern : string ,
9606
- callback ?: ioBroker . ErrorCallback
9607
- ) : Promise < void > {
9608
- if ( aliasObj ?. common ?. alias ?. id ) {
9609
- if ( aliasObj . type !== 'state' ) {
9610
- this . _logger . warn (
9611
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
9612
- `${ this . namespaceLog } Expected alias ${ aliasObj . _id } to be of type "state", got "${ aliasObj . type } "`
9613
- ) ;
9614
- return tools . maybeCallbackWithError (
9615
- callback ,
9616
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
9617
- new Error ( `Expected alias ${ aliasObj . _id } to be of type "state", got "${ aliasObj . type } "` )
9618
- ) ;
9619
- }
9603
+ /**
9604
+ * Add subscription for given alias, if it is not a state it will be ignored
9605
+ *
9606
+ * @param aliasObj the alias object
9607
+ * @param pattern pattern to subscribe for
9608
+ */
9609
+ private async _addAliasSubscribe ( aliasObj : ioBroker . AnyObject , pattern : string ) : Promise < void > {
9610
+ if ( aliasObj . type !== 'state' ) {
9611
+ // no state types do not need to be subscribed
9612
+ return ;
9613
+ }
9620
9614
9621
- // id can be string or can have attribute read
9622
- const sourceId = tools . isObject ( aliasObj . common . alias . id )
9623
- ? aliasObj . common . alias . id . read
9624
- : aliasObj . common . alias . id ;
9615
+ if ( ! aliasObj . common ?. alias ?. id ) {
9616
+ // if state and no id given
9617
+ this . _logger . warn ( `${ this . namespaceLog } Alias ${ aliasObj . _id } has no target 5` ) ;
9618
+ throw new Error ( `Alias ${ aliasObj . _id } has no target` ) ;
9619
+ }
9625
9620
9626
- // validate here because we use objects/states db directly
9627
- try {
9628
- this . _utils . validateId ( sourceId , true , null ) ;
9629
- } catch ( e ) {
9630
- this . _logger . warn ( `${ this . namespaceLog } Error validating alias id of ${ aliasObj . _id } : ${ e . message } ` ) ;
9631
- return tools . maybeCallbackWithError (
9632
- callback ,
9633
- new Error ( `Error validating alias id of ${ aliasObj . _id } : ${ e . message } ` )
9634
- ) ;
9635
- }
9621
+ // id can be string or can have attribute read
9622
+ const sourceId = tools . isObject ( aliasObj . common . alias . id )
9623
+ ? aliasObj . common . alias . id . read
9624
+ : aliasObj . common . alias . id ;
9636
9625
9637
- let aliasDetails ;
9638
- if ( ! this . aliases . has ( sourceId ) ) {
9639
- aliasDetails = { source : null , targets : [ ] } ;
9640
- this . aliases . set ( sourceId , aliasDetails ) ;
9641
- } else {
9642
- aliasDetails = this . aliases . get ( sourceId ) || { source : null , targets : [ ] } ;
9643
- }
9626
+ // validate here because we use objects/states db directly
9627
+ try {
9628
+ this . _utils . validateId ( sourceId , true , null ) ;
9629
+ } catch ( e ) {
9630
+ throw new Error ( `Error validating alias id of ${ aliasObj . _id } : ${ e . message } ` ) ;
9631
+ }
9644
9632
9645
- const targetEntry = {
9646
- alias : deepClone ( aliasObj . common . alias ) ,
9647
- id : aliasObj . _id ,
9648
- pattern,
9649
- type : aliasObj . common . type ,
9650
- max : aliasObj . common . max ,
9651
- min : aliasObj . common . min ,
9652
- unit : aliasObj . common . unit
9653
- } ;
9633
+ const targetEntry = {
9634
+ alias : deepClone ( aliasObj . common . alias ) ,
9635
+ id : aliasObj . _id ,
9636
+ pattern,
9637
+ type : aliasObj . common . type ,
9638
+ max : aliasObj . common . max ,
9639
+ min : aliasObj . common . min ,
9640
+ unit : aliasObj . common . unit
9641
+ } ;
9654
9642
9655
- aliasDetails . targets . push ( targetEntry ) ;
9643
+ let aliasDetails : AliasDetails ;
9656
9644
9657
- if ( ! aliasDetails . source ) {
9658
- let sourceObj ;
9659
- try {
9660
- await this . #states! . subscribe ( sourceId ) ;
9661
- // we ignore permissions on the source object and thus get it as admin user
9662
- sourceObj = await this . #objects! . getObject ( sourceId , { user : SYSTEM_ADMIN_USER } ) ;
9663
- } catch ( e ) {
9664
- return tools . maybeCallbackWithError ( callback , e ) ;
9665
- }
9645
+ if ( ! this . aliases . has ( sourceId ) ) {
9646
+ aliasDetails = { targets : [ ] } ;
9647
+ // add the alias before doing anything async, so if a delete comes in between we can detect it
9648
+ this . aliases . set ( sourceId , aliasDetails ) ;
9649
+ } else {
9650
+ aliasDetails = this . aliases . get ( sourceId ) ! ;
9651
+ }
9666
9652
9667
- if ( sourceObj ?. common ) {
9668
- if ( ! this . aliases . has ( sourceObj . _id ) ) {
9669
- // TODO what means this, we ensured alias existed, did some async stuff now it's gone -> alias has been deleted?
9670
- this . _logger . error (
9671
- `${
9672
- this . namespaceLog
9673
- } Alias subscription error. Please check your alias definitions: sourceId=${ sourceId } , sourceObj=${ JSON . stringify (
9674
- sourceObj
9675
- ) } `
9676
- ) ;
9677
- } else {
9678
- aliasDetails . source = {
9679
- min : sourceObj . common . min ,
9680
- max : sourceObj . common . max ,
9681
- type : sourceObj . common . type ,
9682
- unit : sourceObj . common . unit
9683
- } ;
9684
- }
9685
- }
9653
+ if ( ! aliasDetails . source ) {
9654
+ await this . #states! . subscribe ( sourceId ) ;
9655
+ // we ignore permissions on the source object and thus get it as admin user
9656
+ const sourceObj = await this . #objects! . getObject ( sourceId , { user : SYSTEM_ADMIN_USER } ) ;
9686
9657
9687
- return tools . maybeCallback ( callback ) ;
9688
- } else {
9689
- return tools . maybeCallback ( callback ) ;
9658
+ // if we have a common and the alias has not been removed in-between
9659
+ if ( sourceObj ?. common && this . aliases . has ( sourceObj . _id ) ) {
9660
+ aliasDetails . source = {
9661
+ min : sourceObj . common . min ,
9662
+ max : sourceObj . common . max ,
9663
+ type : sourceObj . common . type ,
9664
+ unit : sourceObj . common . unit
9665
+ } ;
9690
9666
}
9691
- } else if ( aliasObj && aliasObj . type === 'state' ) {
9692
- // if state and no id given -> if no state just ignore it
9693
- this . _logger . warn ( `${ this . namespaceLog } Alias ${ aliasObj . _id } has no target 5` ) ;
9694
- return tools . maybeCallbackWithError ( callback , new Error ( `Alias ${ aliasObj . _id } has no target` ) ) ;
9695
- } else {
9696
- return tools . maybeCallback ( callback ) ;
9697
9667
}
9668
+
9669
+ // add the alias target after we have ensured that we have the source set
9670
+ aliasDetails . targets . push ( targetEntry ) ;
9698
9671
}
9699
9672
9700
- private async _removeAliasSubscribe (
9701
- sourceId : string ,
9702
- aliasObj : number | AliasTargetEntry ,
9703
- callback ?: ( ) => void
9704
- ) : Promise < void > {
9673
+ /**
9674
+ * Remove an alias subscribe
9675
+ *
9676
+ * @param sourceId id of the source object
9677
+ * @param aliasObjOrIdx the alias target or the index of the targets array
9678
+ */
9679
+ private async _removeAliasSubscribe ( sourceId : string , aliasObjOrIdx : number | AliasTargetEntry ) : Promise < void > {
9705
9680
if ( ! this . aliases . has ( sourceId ) ) {
9706
- return tools . maybeCallback ( callback ) ;
9681
+ return ;
9707
9682
}
9708
9683
9684
+ const alias = this . aliases . get ( sourceId ) ! ;
9685
+
9709
9686
// remove from targets array
9710
- const pos = typeof aliasObj === 'number' ? aliasObj : this . aliases . get ( sourceId ) ! . targets . indexOf ( aliasObj ) ;
9687
+ const pos = typeof aliasObjOrIdx === 'number' ? aliasObjOrIdx : alias . targets . indexOf ( aliasObjOrIdx ) ;
9711
9688
9712
9689
if ( pos !== - 1 ) {
9713
- this . aliases . get ( sourceId ) ! . targets . splice ( pos , 1 ) ;
9690
+ alias . targets . splice ( pos , 1 ) ;
9714
9691
9715
9692
// unsubscribe if no more aliases exists
9716
- if ( ! this . aliases . get ( sourceId ) ! . targets . length ) {
9693
+ if ( ! alias . targets . length ) {
9717
9694
this . aliases . delete ( sourceId ) ;
9718
9695
await this . #states! . unsubscribe ( sourceId ) ;
9719
9696
}
9720
9697
}
9721
- return tools . maybeCallback ( callback ) ;
9722
9698
}
9723
9699
9724
9700
subscribeForeignStates ( pattern : Pattern , callback ?: ioBroker . ErrorCallback ) : void ;
@@ -9852,8 +9828,7 @@ export class AdapterClass extends EventEmitter {
9852
9828
9853
9829
for ( const aliasObj of aliasObjs ) {
9854
9830
if ( aliasObj ) {
9855
- // @ts -expect-error check if alias subscribe also takes non-state objects and then ignores
9856
- promises . push ( new Promise ( resolve => this . _addAliasSubscribe ( aliasObj , aliasObj . _id , resolve ) ) ) ;
9831
+ promises . push ( this . _addAliasSubscribe ( aliasObj , aliasObj . _id ) ) ;
9857
9832
}
9858
9833
}
9859
9834
}
@@ -9891,8 +9866,7 @@ export class AdapterClass extends EventEmitter {
9891
9866
// If alias
9892
9867
if ( id . startsWith ( ALIAS_STARTS_WITH ) ) {
9893
9868
const aliasObj = objs [ id ] ;
9894
- // @ts -expect-error
9895
- promises . push ( new Promise ( resolve => this . _addAliasSubscribe ( aliasObj , pattern , resolve ) ) ) ;
9869
+ promises . push ( this . _addAliasSubscribe ( aliasObj , pattern ) ) ;
9896
9870
}
9897
9871
}
9898
9872
@@ -9929,26 +9903,12 @@ export class AdapterClass extends EventEmitter {
9929
9903
this . #objects. subscribe ( `${ ALIAS_STARTS_WITH } *` ) ;
9930
9904
}
9931
9905
9932
- // aliases['sourceId'] = {
9933
- // source: {common attributes},
9934
- // targets: [
9935
- // {
9936
- // alias: {},
9937
- // id: 'aliasId',
9938
- // pattern: 'some pattern',
9939
- // type: stateType,
9940
- // max: number,
9941
- // min: number,
9942
- // }
9943
- // ]
9944
- // };
9945
-
9946
9906
// just read one alias Object
9947
9907
try {
9948
- const aliasObj = await this . #objects. getObjectAsync ( pattern , options ) ;
9908
+ const aliasObj = await this . #objects. getObject ( pattern , options ) ;
9949
9909
if ( aliasObj ) {
9950
- // cb will be called, but await for catching promisified part
9951
- await this . _addAliasSubscribe ( aliasObj as ioBroker . StateObject , pattern , callback ) ;
9910
+ await this . _addAliasSubscribe ( aliasObj , pattern ) ;
9911
+ return tools . maybeCallback ( callback ) ;
9952
9912
} else {
9953
9913
return tools . maybeCallback ( callback ) ;
9954
9914
}
@@ -10994,19 +10954,19 @@ export class AdapterClass extends EventEmitter {
10994
10954
}
10995
10955
} else if ( ! this . _stopInProgress && this . adapterReady && this . aliases . has ( id ) ) {
10996
10956
// If adapter is ready and for this ID exist some alias links
10997
- const alias = this . aliases . get ( id ) ;
10957
+ const alias = this . aliases . get ( id ) ! ;
10998
10958
/** Prevent multiple publishes if multiple pattern contain this alias id */
10999
10959
const uniqueTargets = new Set < string > ( ) ;
11000
10960
11001
- for ( const target of alias ! . targets ) {
10961
+ for ( const target of alias . targets ) {
11002
10962
const targetId = target . id ;
11003
10963
if ( uniqueTargets . has ( targetId ) ) {
11004
10964
continue ;
11005
10965
}
11006
10966
11007
10967
uniqueTargets . add ( targetId ) ;
11008
10968
11009
- const source = alias ! . source ! ;
10969
+ const source = alias ! . source ;
11010
10970
11011
10971
const aState = state
11012
10972
? tools . formatAliasValue ( {
@@ -11193,18 +11153,14 @@ export class AdapterClass extends EventEmitter {
11193
11153
11194
11154
// if linked ID changed
11195
11155
if ( newSourceId !== sourceId ) {
11196
- this . _removeAliasSubscribe ( sourceId , targetAlias , async ( ) => {
11197
- try {
11198
- await this . _addAliasSubscribe (
11199
- obj as ioBroker . StateObject ,
11200
- targetAlias . pattern
11201
- ) ;
11202
- } catch ( e ) {
11203
- this . _logger . error (
11204
- `${ this . namespaceLog } Could not add alias subscription: ${ e . message } `
11205
- ) ;
11206
- }
11207
- } ) ;
11156
+ await this . _removeAliasSubscribe ( sourceId , targetAlias ) ;
11157
+ try {
11158
+ await this . _addAliasSubscribe ( obj , targetAlias . pattern ) ;
11159
+ } catch ( e ) {
11160
+ this . _logger . error (
11161
+ `${ this . namespaceLog } Could not add alias subscription: ${ e . message } `
11162
+ ) ;
11163
+ }
11208
11164
} else {
11209
11165
// update attributes
11210
11166
targetAlias . min = obj . common . min ;
@@ -11215,13 +11171,13 @@ export class AdapterClass extends EventEmitter {
11215
11171
} else {
11216
11172
// link was deleted
11217
11173
// remove from targets array
11218
- this . _removeAliasSubscribe ( sourceId , targetAlias ) ;
11174
+ await this . _removeAliasSubscribe ( sourceId , targetAlias ) ;
11219
11175
}
11220
11176
}
11221
11177
}
11222
11178
11223
11179
// it's a new alias, we add it to our subscription
11224
- if ( isNewAlias ) {
11180
+ if ( isNewAlias && obj ) {
11225
11181
for ( const aliasPattern of this . aliasPatterns ) {
11226
11182
// check if it's in our subs range, if so add it
11227
11183
const testPattern =
@@ -11234,7 +11190,7 @@ export class AdapterClass extends EventEmitter {
11234
11190
( testPattern instanceof RegExp && testPattern . test ( id ) )
11235
11191
) {
11236
11192
try {
11237
- await this . _addAliasSubscribe ( obj as ioBroker . StateObject , id ) ;
11193
+ await this . _addAliasSubscribe ( obj , id ) ;
11238
11194
} catch ( e ) {
11239
11195
this . _logger . warn (
11240
11196
`${ this . namespaceLog } Could not add alias subscription: ${ e . message } `
0 commit comments