15
15
import static org .openhab .binding .squeezebox .internal .SqueezeBoxBindingConstants .*;
16
16
17
17
import java .util .Dictionary ;
18
- import java .util .HashMap ;
19
18
import java .util .Hashtable ;
20
19
import java .util .Map ;
21
20
import java .util .Set ;
22
21
import java .util .concurrent .ConcurrentHashMap ;
23
22
import java .util .stream .Collectors ;
24
23
import java .util .stream .Stream ;
25
24
26
- import org .openhab . binding . squeezebox . internal . discovery . SqueezeBoxPlayerDiscoveryParticipant ;
27
- import org .openhab . binding . squeezebox . internal . handler . SqueezeBoxPlayerEventListener ;
25
+ import org .eclipse . jdt . annotation . NonNullByDefault ;
26
+ import org .eclipse . jdt . annotation . Nullable ;
28
27
import org .openhab .binding .squeezebox .internal .handler .SqueezeBoxPlayerHandler ;
29
28
import org .openhab .binding .squeezebox .internal .handler .SqueezeBoxServerHandler ;
30
29
import org .openhab .core .audio .AudioHTTPServer ;
31
30
import org .openhab .core .audio .AudioSink ;
32
- import org .openhab .core .config .discovery .DiscoveryService ;
33
31
import org .openhab .core .net .HttpServiceUtil ;
34
32
import org .openhab .core .net .NetworkAddressService ;
35
33
import org .openhab .core .thing .Bridge ;
36
34
import org .openhab .core .thing .Thing ;
37
35
import org .openhab .core .thing .ThingTypeUID ;
38
- import org .openhab .core .thing .ThingUID ;
39
36
import org .openhab .core .thing .binding .BaseThingHandlerFactory ;
40
37
import org .openhab .core .thing .binding .ThingHandler ;
41
38
import org .openhab .core .thing .binding .ThingHandlerFactory ;
56
53
* @author Mark Hilbush - Add callbackUrl
57
54
*/
58
55
@ Component (service = ThingHandlerFactory .class , configurationPid = "binding.squeezebox" )
56
+ @ NonNullByDefault
59
57
public class SqueezeBoxHandlerFactory extends BaseThingHandlerFactory {
60
58
private final Logger logger = LoggerFactory .getLogger (SqueezeBoxHandlerFactory .class );
61
59
@@ -64,21 +62,19 @@ public class SqueezeBoxHandlerFactory extends BaseThingHandlerFactory {
64
62
SqueezeBoxPlayerHandler .SUPPORTED_THING_TYPES_UIDS .stream ())
65
63
.collect (Collectors .toSet ());
66
64
67
- private Map <ThingUID , ServiceRegistration <?>> discoveryServiceRegs = new HashMap <>();
68
-
69
65
private final AudioHTTPServer audioHTTPServer ;
70
66
private final NetworkAddressService networkAddressService ;
71
67
private final SqueezeBoxStateDescriptionOptionsProvider stateDescriptionProvider ;
72
68
73
69
private Map <String , ServiceRegistration <AudioSink >> audioSinkRegistrations = new ConcurrentHashMap <>();
74
70
75
71
// Callback url (scheme+server+port) to use for playing notification sounds
76
- private String callbackUrl = null ;
72
+ private @ Nullable String callbackUrl = null ;
77
73
78
74
@ Activate
79
- public SqueezeBoxHandlerFactory (@ Reference AudioHTTPServer audioHTTPServer ,
80
- @ Reference NetworkAddressService networkAddressService ,
81
- @ Reference SqueezeBoxStateDescriptionOptionsProvider stateDescriptionProvider ) {
75
+ public SqueezeBoxHandlerFactory (final @ Reference AudioHTTPServer audioHTTPServer ,
76
+ final @ Reference NetworkAddressService networkAddressService ,
77
+ final @ Reference SqueezeBoxStateDescriptionOptionsProvider stateDescriptionProvider ) {
82
78
this .audioHTTPServer = audioHTTPServer ;
83
79
this .networkAddressService = networkAddressService ;
84
80
this .stateDescriptionProvider = stateDescriptionProvider ;
@@ -97,101 +93,53 @@ protected void activate(ComponentContext componentContext) {
97
93
}
98
94
99
95
@ Override
100
- protected ThingHandler createHandler (Thing thing ) {
96
+ protected @ Nullable ThingHandler createHandler (Thing thing ) {
101
97
ThingTypeUID thingTypeUID = thing .getThingTypeUID ();
102
98
103
- if (thingTypeUID .equals (SQUEEZEBOXSERVER_THING_TYPE )) {
104
- logger .trace ("creating handler for bridge thing {}" , thing );
105
- SqueezeBoxServerHandler bridge = new SqueezeBoxServerHandler ((Bridge ) thing );
106
- registerSqueezeBoxPlayerDiscoveryService (bridge );
107
- return bridge ;
99
+ if (SQUEEZEBOXSERVER_THING_TYPE .equals (thingTypeUID )) {
100
+ logger .trace ("Creating handler for bridge thing {}" , thing );
101
+ return new SqueezeBoxServerHandler ((Bridge ) thing );
108
102
}
109
103
110
- if (thingTypeUID .equals (SQUEEZEBOXPLAYER_THING_TYPE )) {
111
- logger .trace ("creating handler for player thing {}" , thing );
104
+ if (SQUEEZEBOXPLAYER_THING_TYPE .equals (thingTypeUID )) {
105
+ logger .trace ("Creating handler for player thing {}" , thing );
112
106
SqueezeBoxPlayerHandler playerHandler = new SqueezeBoxPlayerHandler (thing , createCallbackUrl (),
113
107
stateDescriptionProvider );
114
108
115
109
// Register the player as an audio sink
116
110
logger .trace ("Registering an audio sink for player thing {}" , thing .getUID ());
117
111
SqueezeBoxAudioSink audioSink = new SqueezeBoxAudioSink (playerHandler , audioHTTPServer , callbackUrl );
118
- @ SuppressWarnings ("unchecked" )
119
- ServiceRegistration <AudioSink > reg = (ServiceRegistration <AudioSink >) bundleContext
120
- .registerService (AudioSink .class .getName (), audioSink , new Hashtable <>());
121
- audioSinkRegistrations .put (thing .getUID ().toString (), reg );
112
+ ServiceRegistration <AudioSink > audioSinkRegistration = bundleContext .registerService (AudioSink .class ,
113
+ audioSink , new Hashtable <>());
114
+ audioSinkRegistrations .put (thing .getUID ().toString (), audioSinkRegistration );
122
115
123
116
return playerHandler ;
124
117
}
125
118
126
119
return null ;
127
120
}
128
121
129
- /**
130
- * Adds SqueezeBoxServerHandlers to the discovery service to find SqueezeBox
131
- * Players
132
- *
133
- * @param squeezeBoxServerHandler
134
- */
135
- private synchronized void registerSqueezeBoxPlayerDiscoveryService (
136
- SqueezeBoxServerHandler squeezeBoxServerHandler ) {
137
- logger .trace ("registering player discovery service" );
138
-
139
- SqueezeBoxPlayerDiscoveryParticipant discoveryService = new SqueezeBoxPlayerDiscoveryParticipant (
140
- squeezeBoxServerHandler );
141
-
142
- // Register the PlayerListener with the SqueezeBoxServerHandler
143
- squeezeBoxServerHandler .registerSqueezeBoxPlayerListener (discoveryService );
144
-
145
- // Register the service, then add the service to the ServiceRegistration map
146
- discoveryServiceRegs .put (squeezeBoxServerHandler .getThing ().getUID (),
147
- bundleContext .registerService (DiscoveryService .class .getName (), discoveryService , new Hashtable <>()));
148
- }
149
-
150
122
@ Override
151
123
protected synchronized void removeHandler (ThingHandler thingHandler ) {
152
- if (thingHandler instanceof SqueezeBoxServerHandler serverHandler ) {
153
- logger .trace ("removing handler for bridge thing {}" , thingHandler .getThing ());
154
-
155
- ServiceRegistration <?> serviceReg = this .discoveryServiceRegs .get (thingHandler .getThing ().getUID ());
156
- if (serviceReg != null ) {
157
- logger .trace ("unregistering player discovery service" );
158
-
159
- // Get the discovery service object and use it to cancel the RequestPlayerJob
160
- SqueezeBoxPlayerDiscoveryParticipant discoveryService = (SqueezeBoxPlayerDiscoveryParticipant ) bundleContext
161
- .getService (serviceReg .getReference ());
162
- discoveryService .cancelRequestPlayerJob ();
163
-
164
- // Unregister the PlayerListener from the SqueezeBoxServerHandler
165
- serverHandler .unregisterSqueezeBoxPlayerListener (
166
- (SqueezeBoxPlayerEventListener ) bundleContext .getService (serviceReg .getReference ()));
167
-
168
- // Unregister the PlayerListener service
169
- serviceReg .unregister ();
170
-
171
- // Remove the service from the ServiceRegistration map
172
- discoveryServiceRegs .remove (thingHandler .getThing ().getUID ());
173
- }
174
- }
175
-
176
124
if (thingHandler instanceof SqueezeBoxPlayerHandler playerHandler ) {
177
125
SqueezeBoxServerHandler bridge = playerHandler .getSqueezeBoxServerHandler ();
178
126
if (bridge != null ) {
179
127
// Unregister the player's audio sink
180
128
logger .trace ("Unregistering the audio sync service for player thing {}" ,
181
129
thingHandler .getThing ().getUID ());
182
- ServiceRegistration <AudioSink > reg = audioSinkRegistrations
183
- .get (thingHandler .getThing ().getUID ().toString ());
184
- if (reg != null ) {
185
- reg .unregister ();
130
+ ServiceRegistration <AudioSink > audioSinkRegistration = audioSinkRegistrations
131
+ .remove (thingHandler .getThing ().getUID ().toString ());
132
+ if (audioSinkRegistration != null ) {
133
+ audioSinkRegistration .unregister ();
186
134
}
187
135
188
- logger .trace ("removing handler for player thing {}" , thingHandler .getThing ());
136
+ logger .trace ("Removing handler for player thing {}" , thingHandler .getThing ());
189
137
bridge .removePlayerCache (playerHandler .getMac ());
190
138
}
191
139
}
192
140
}
193
141
194
- private String createCallbackUrl () {
142
+ private @ Nullable String createCallbackUrl () {
195
143
final String ipAddress = networkAddressService .getPrimaryIpv4HostAddress ();
196
144
if (ipAddress == null ) {
197
145
logger .warn ("No network interface could be found." );
0 commit comments