2828import org .openhab .binding .ntfy .internal .network .WebSocketConnectionListener ;
2929import org .openhab .core .library .types .DateTimeType ;
3030import org .openhab .core .library .types .StringType ;
31+ import org .openhab .core .thing .Bridge ;
3132import org .openhab .core .thing .ChannelUID ;
3233import org .openhab .core .thing .Thing ;
3334import org .openhab .core .thing .ThingStatus ;
4849public class NtfyTopicHandler extends BaseThingHandler implements WebSocketConnectionListener {
4950
5051 private NtfyWebSocket ntfyWebSocket ;
51- private NtfySender ntfySender ;
52+ private @ Nullable NtfySender ntfySender ;
5253 private boolean isInitializing ;
5354 private @ Nullable MessageEvent lastMessageEvent ;
55+ private HttpClient httpClient ;
5456
5557 /**
5658 * Creates a new {@link NtfyTopicHandler} for the provided topic thing.
@@ -61,14 +63,19 @@ public class NtfyTopicHandler extends BaseThingHandler implements WebSocketConne
6163 public NtfyTopicHandler (Thing thing , HttpClient httpClient ) {
6264 super (thing );
6365
66+ this .httpClient = httpClient ;
6467 this .ntfyWebSocket = new NtfyWebSocket (this );
65- this .ntfySender = new NtfySender (this .getConfigAs (NtfyTopicConfiguration .class ).topicname , httpClient ,
66- this ::getBridgeHandler );
6768 }
6869
69- private NtfyConnectionHandler getBridgeHandler () {
70- return (NtfyConnectionHandler ) (java .util .Objects
71- .requireNonNull (java .util .Objects .requireNonNull (getBridge ()).getHandler ()));
70+ private @ Nullable NtfyConnectionHandler getBridgeHandler () {
71+ @ Nullable
72+ Bridge bridge = getBridge ();
73+
74+ if (bridge == null ) {
75+ updateStatus (ThingStatus .OFFLINE , ThingStatusDetail .BRIDGE_OFFLINE );
76+ return null ;
77+ }
78+ return (NtfyConnectionHandler ) (bridge .getHandler ());
7279 }
7380
7481 @ Override
@@ -80,12 +87,22 @@ public Collection<Class<? extends ThingHandlerService>> getServices() {
8087 public void thingUpdated (Thing thing ) {
8188 super .thingUpdated (thing );
8289
83- getBridgeHandler ().createAndRegisterWebSocketClient (thing );
90+ @ Nullable
91+ NtfyConnectionHandler bridgeHandler = getBridgeHandler ();
92+
93+ if (bridgeHandler == null ) {
94+ return ;
95+ }
96+
97+ ntfySender = new NtfySender (this .getConfigAs (NtfyTopicConfiguration .class ).topicname , httpClient ,
98+ this ::getBridgeHandler );
99+ bridgeHandler .createAndRegisterWebSocketClient (thing );
100+ startConnection ();
84101 }
85102
86103 @ Override
87104 public void bridgeStatusChanged (ThingStatusInfo bridgeStatusInfo ) {
88- if (bridgeStatusInfo .getStatus () == ThingStatus .ONLINE
105+ if (( bridgeStatusInfo .getStatus () == ThingStatus .ONLINE || bridgeStatusInfo . getStatus () == ThingStatus . UNKNOWN )
89106 && getThing ().getStatusInfo ().getStatusDetail () == ThingStatusDetail .BRIDGE_OFFLINE ) {
90107 updateStatus (ThingStatus .UNKNOWN );
91108 initialize ();
@@ -112,7 +129,14 @@ public void initialize() {
112129
113130 scheduler .execute (() -> {
114131 try {
115- getBridgeHandler ().createAndRegisterWebSocketClient (thing );
132+ ntfySender = new NtfySender (this .getConfigAs (NtfyTopicConfiguration .class ).topicname , httpClient ,
133+ this ::getBridgeHandler );
134+ @ Nullable
135+ NtfyConnectionHandler bridgeHandler = getBridgeHandler ();
136+ if (bridgeHandler == null ) {
137+ return ;
138+ }
139+ bridgeHandler .createAndRegisterWebSocketClient (thing );
116140 startConnection ();
117141 } finally {
118142 this .isInitializing = false ;
@@ -122,7 +146,12 @@ public void initialize() {
122146
123147 private void startConnection () {
124148 if (getThing ().getStatus () != ThingStatus .ONLINE ) {
125- if (!getBridgeHandler ().startWebSocketConnection (thing , ntfyWebSocket )) {
149+ @ Nullable
150+ NtfyConnectionHandler bridgeHandler = getBridgeHandler ();
151+ if (bridgeHandler == null ) {
152+ return ;
153+ }
154+ if (!bridgeHandler .startWebSocketConnection (thing , ntfyWebSocket )) {
126155 updateStatus (ThingStatus .OFFLINE );
127156 }
128157 }
@@ -135,15 +164,22 @@ public void connectionEstablished() {
135164
136165 @ Override
137166 public void connectionLost (String reason ) {
167+ updateStatus (ThingStatus .OFFLINE , ThingStatusDetail .COMMUNICATION_ERROR ,
168+ reason .isBlank () ? "WebSocket connection lost" : reason );
138169 }
139170
140171 @ Override
141172 public void connectionError (Throwable cause ) {
142- getBridgeHandler ().connectionError (cause );
173+ @ Nullable
174+ NtfyConnectionHandler bridgeHandler = getBridgeHandler ();
175+ if (bridgeHandler == null ) {
176+ return ;
177+ }
178+ bridgeHandler .connectionError (cause );
143179 }
144180
145181 @ Override
146- public void messageRecieved (BaseEvent event ) {
182+ public void messageReceived (BaseEvent event ) {
147183 if (event instanceof MessageEvent message ) {
148184 this .lastMessageEvent = message ;
149185 updateChannels ();
@@ -169,19 +205,40 @@ private void updateChannels() {
169205 * @throws URISyntaxException
170206 */
171207 public String sendMessage (NtfyMessage ntfyMessage ) throws URISyntaxException {
208+ final @ Nullable NtfySender sender = ntfySender ;
209+ if (sender == null ) {
210+ return "" ;
211+ }
212+
172213 @ Nullable
173- MessageEvent sendMessage = ntfySender .sendMessage (ntfyMessage );
214+ MessageEvent sendMessage = sender .sendMessage (ntfyMessage );
174215
175216 if (sendMessage == null ) {
176217 return "" ;
177218 }
178219 return sendMessage .getId ();
179220 }
180221
222+ /**
223+ * Uploads a local file to the configured topic via the underlying
224+ * {@link NtfySender#sendFile(String, String, String)} and returns the
225+ * created message id on success.
226+ *
227+ * @param file the filesystem path to the file to upload
228+ * @param filename optional filename to present to recipients (may be null)
229+ * @param sequenceId optional sequence id to associate with the uploaded message
230+ * @return the created message id on success, or an empty string on failure
231+ * @throws URISyntaxException when the constructed request URI is invalid
232+ */
181233 public String sendFile (String file , @ Nullable String filename , @ Nullable String sequenceId )
182234 throws URISyntaxException {
235+ final @ Nullable NtfySender sender = ntfySender ;
236+ if (sender == null ) {
237+ return "" ;
238+ }
239+
183240 @ Nullable
184- MessageEvent sendMessage = ntfySender .sendFile (file , filename , sequenceId );
241+ MessageEvent sendMessage = sender .sendFile (file , filename , sequenceId );
185242
186243 if (sendMessage == null ) {
187244 return "" ;
@@ -197,6 +254,10 @@ public String sendFile(String file, @Nullable String filename, @Nullable String
197254 * @throws URISyntaxException when the underlying request URI could not be constructed
198255 */
199256 public boolean deleteMessage (String sequenceId ) throws URISyntaxException {
200- return ntfySender .deleteMessage (sequenceId );
257+ final @ Nullable NtfySender sender = ntfySender ;
258+ if (sender == null ) {
259+ return false ;
260+ }
261+ return sender .deleteMessage (sequenceId );
201262 }
202263}
0 commit comments