11package com .example ;
22
3+ import io .github .cdimascio .dotenv .Dotenv ;
4+ import javafx .application .Platform ;
5+ import javafx .collections .FXCollections ;
6+ import javafx .collections .ObservableList ;
7+ import tools .jackson .databind .ObjectMapper ;
8+
9+ import java .io .IOException ;
10+ import java .net .URI ;
11+ import java .net .http .HttpClient ;
12+ import java .net .http .HttpRequest ;
13+ import java .net .http .HttpResponse ;
14+ import java .util .Objects ;
15+
316/**
417 * Model layer: encapsulates application data and business logic.
518 */
619public class HelloModel {
20+
21+ private final String hostName ;
22+ private final HttpClient http = HttpClient .newHttpClient ();
23+ private final ObjectMapper mapper = new ObjectMapper ();
24+ private final ObservableList <NtfyMessageDto > messages = FXCollections .observableArrayList ();
25+
26+ private boolean senderMe =false ;
27+
28+ public HelloModel () {
29+ Dotenv dotenv = Dotenv .load ();
30+ hostName = Objects .requireNonNull (dotenv .get ("HOST_NAME" ));
31+ receiveMessage ();
32+ }
33+
34+ public ObservableList <NtfyMessageDto > getMessages () {
35+ return messages ;
36+ }
37+
738 /**
839 * Returns a greeting based on the current Java and JavaFX versions.
940 */
@@ -12,4 +43,55 @@ public String getGreeting() {
1243 String javafxVersion = System .getProperty ("javafx.version" );
1344 return "Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + "." ;
1445 }
46+
47+ public void sendMessage (String text ) {
48+
49+ senderMe =true ;
50+
51+ HttpRequest httpRequest = HttpRequest .newBuilder ()
52+ .POST (HttpRequest .BodyPublishers .ofString ("Hello world" ))
53+ .uri (URI .create (hostName + "/mytopic" ))
54+ .build ();
55+ http .sendAsync (httpRequest , HttpResponse .BodyHandlers .discarding ())
56+ .exceptionally (ex -> {
57+ System .out .println ("Error sending message: " + ex .getMessage ());
58+ return null ;
59+ });
60+ try {
61+ var response = http .send (httpRequest , HttpResponse .BodyHandlers .ofString ());
62+ } catch (IOException e ) {
63+ System .out .println ("Error sending message" );
64+ } catch (InterruptedException e ) {
65+ System .out .println ("Interrupted sending request" );
66+ }
67+
68+ }
69+
70+ public void receiveMessage (){
71+ HttpRequest httpRequest = HttpRequest .newBuilder ()
72+ .GET ()
73+ .uri (URI .create (hostName + "/mytopic/json" ))
74+ .build ();
75+
76+ http .sendAsync (httpRequest , HttpResponse .BodyHandlers .ofLines ())
77+ .thenAccept (response -> response .body ()
78+ .map (s -> {
79+ try {
80+ return mapper .readValue (s , NtfyMessageDto .class );
81+ } catch (Exception e ) {
82+ System .err .println ("Failed to parse message: " + e .getMessage ());
83+ return null ;
84+ }
85+ })
86+ .filter (Objects ::nonNull )
87+ .filter (msg -> "message" .equals (msg .event ()))
88+ .forEach (msg ->{
89+ if (senderMe ) {
90+ senderMe =false ;
91+ return ;
92+ }
93+ Platform .runLater (() -> messages .add (msg ));
94+ }));
95+ }
96+
1597}
0 commit comments