@@ -18,63 +18,66 @@ class ImageController {
18
18
return deviceName ? deviceNameString . replace ( / \u0000 / g, '' ) : 'NA' ;
19
19
}
20
20
21
- getZ2mDeviceImage ( device ) {
21
+ getZ2mDeviceImageModelJPG ( device ) {
22
22
if ( device && device . definition && device . definition . model ) {
23
23
const icoString = `https://www.zigbee2mqtt.io/images/devices/${ this . sanitizeZ2MDeviceName ( device . definition . model ) } .jpg` ;
24
24
// eslint-disable-next-line no-control-regex
25
25
return icoString . replace ( / \u0000 / g, '' ) ;
26
26
}
27
27
}
28
28
29
+ getZ2mDeviceImageModelPNG ( device ) {
30
+ if ( device && device . definition && device . definition . model ) {
31
+ const icoString = `https://www.zigbee2mqtt.io/images/devices/${ this . sanitizeZ2MDeviceName ( device . definition . model ) } .png` ;
32
+ // eslint-disable-next-line no-control-regex
33
+ return icoString . replace ( / \u0000 / g, '' ) ;
34
+ }
35
+ }
36
+
37
+
29
38
getSlsDeviceImage ( device ) {
30
39
if ( device && device . model_id ) {
31
- const icoString = `https://slsys.github .io/Gateway /devices/png /${ this . sanitizeModelIDForImageUrl ( device . model_id ) } .png` ;
40
+ const icoString = `https://www.zigbee2mqtt .io/images /devices/${ this . sanitizeModelIDForImageUrl ( device . model_id ) } .png` ;
32
41
// eslint-disable-next-line no-control-regex
33
42
return icoString . replace ( / \u0000 / g, '' ) ;
34
43
}
35
44
}
36
45
37
46
async getDeviceIcon ( device ) {
38
- if ( this . adapter . config . useDeviceIcons == false ) {
39
- return '' ;
40
- }
47
+ if ( ! this . adapter . config . useDeviceIcons ) return '' ;
41
48
42
- const imageSize = this . adapter . config . deviceIconsSize ;
49
+ const imageSize = this . adapter . config . deviceIconsSize ;
43
50
44
- const z2mIconFileName = `${ this . sanitizeZ2MDeviceName ( device . definition . model ) } .jpg` ;
51
+ const z2mIconFileNameJPG = `${ this . sanitizeZ2MDeviceName ( device . definition . model ) } .jpg` ;
52
+ const z2mIconFileNamePNG = `${ this . sanitizeZ2MDeviceName ( device . definition . model ) } .png` ;
45
53
const slsIconFileName = `${ this . sanitizeModelIDForImageUrl ( device . model_id ) } .png` ;
46
54
47
- let icon ;
48
- let iconFileName ;
49
- // Check whether an image has already been downloaded from Z2M.
50
- if ( await this . adapter . fileExistsAsync ( this . adapter . namespace , z2mIconFileName ) ) {
51
- iconFileName = z2mIconFileName ;
52
- }
53
- // Check whether an image has already been downloaded from SLSys.
54
- else if ( await this . adapter . fileExistsAsync ( this . adapter . namespace , slsIconFileName ) ) {
55
- iconFileName = slsIconFileName ;
56
- }
57
- // If not donwload image
58
- else {
59
- let iconUrl = this . getSlsDeviceImage ( device ) ;
60
-
61
- this . adapter . log . info ( `Download image for device model: ${ device . definition . model } ` ) ;
55
+ let iconFileName = await this . getExistingIconFileName ( z2mIconFileNameJPG , z2mIconFileNamePNG , slsIconFileName ) ;
62
56
63
- try {
64
- await this . downloadIcon ( this . adapter , iconUrl , this . adapter . namespace ) ;
65
- } catch ( err ) {
66
- iconUrl = this . getZ2mDeviceImage ( device ) ;
67
- await this . downloadIcon ( this . adapter , iconUrl , this . adapter . namespace ) ;
68
- }
57
+ if ( ! iconFileName ) {
58
+ const iconUrls = [
59
+ this . getZ2mDeviceImageModelJPG ( device ) ,
60
+ this . getZ2mDeviceImageModelPNG ( device ) ,
61
+ this . getSlsDeviceImage ( device )
62
+ ] ;
69
63
70
- iconFileName = this . getFileNameWithExtension ( iconUrl ) ;
64
+ for ( const iconUrl of iconUrls ) {
65
+ try {
66
+ await this . downloadIcon ( this . adapter , iconUrl , this . adapter . namespace ) ;
67
+ iconFileName = this . getFileNameWithExtension ( iconUrl ) ;
68
+ break ;
69
+ } catch ( err ) {
70
+ this . adapter . log . warn ( `Failed to download image from ${ iconUrl } ` ) ;
71
+ }
72
+ }
71
73
}
72
74
73
- if ( ( await this . adapter . fileExistsAsync ( this . adapter . namespace , iconFileName ) ) == false ) {
75
+ if ( ! iconFileName ) {
74
76
this . adapter . log . warn ( `No image for device model: ${ device . definition . model } found!` ) ;
75
77
return '' ;
76
78
}
77
79
80
+ let icon ;
78
81
try {
79
82
// Load image from the Meta-Store
80
83
icon = await this . adapter . readFileAsync ( this . adapter . namespace , iconFileName ) ;
@@ -122,6 +125,16 @@ class ImageController {
122
125
adapter . log . warn ( ex ) ;
123
126
}
124
127
}
128
+ async getExistingIconFileName ( z2mIconFileNameJPG , z2mIconFileNamePNG , slsIconFileName ) {
129
+ if ( await this . adapter . fileExistsAsync ( this . adapter . namespace , z2mIconFileNameJPG ) ) {
130
+ return z2mIconFileNameJPG ;
131
+ } else if ( await this . adapter . fileExistsAsync ( this . adapter . namespace , z2mIconFileNamePNG ) ) {
132
+ return z2mIconFileNamePNG ;
133
+ } else if ( await this . adapter . fileExistsAsync ( this . adapter . namespace , slsIconFileName ) ) {
134
+ return slsIconFileName ;
135
+ }
136
+ return null ;
137
+ }
125
138
}
126
139
module . exports = {
127
140
ImageController,
0 commit comments