Skip to content

Commit 91800a2

Browse files
committed
Initial commit
0 parents  commit 91800a2

12 files changed

+457
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Intellij
2+
.idea/
3+
*.iml
4+
5+
# Maven
6+
target/

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Luca Bongiovanni
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Mqtt Notifier 🔔
2+
3+
Mqtt Notifier is a simple windows application that subscribes to a topic on a **MQTT** broker and **notifies** incoming messages via **windows notifications**.
4+
5+
<img title="" src="notification.gif" alt="" width="484">
6+
7+
<Insert notification example image>
8+
9+
## Getting Started
10+
11+
### Prerequisites
12+
13+
[Download](https://www.oracle.com/java/technologies/javase-jre8-downloads.html) and install the latest `JRE 8` (Java Runtime Environment) version.
14+
15+
### Installing
16+
17+
- Download the jar executable (`MqttNotifier.jar`) and the sample configuration file and (`sample.settings.json`) of the [latest version](https://github.com/bongijo/MqttNotifier/releases/latest)
18+
19+
- Place them in the same directory anywhere on your machine
20+
21+
- (Optional) Create a shortcut to `MqttNotifier.jar` and move it to `%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup` to have `Mqtt Notifier` run on boot
22+
23+
### Setup configuration file
24+
25+
When the application starts up, it will search for a `settings.json` file in the same directory. So, in order to use the application, rename `sample.settings.json` to`settings.json` and insert the following parameters:
26+
27+
##### Required parameters
28+
29+
| Parameter | Description | Example |
30+
|:---------:|:-------------------------------------------------------------------------- |:---------------------:|
31+
| `broker` | String value needed to connect to the MQTT broker [ *protocol://ip:port* ] | `tcp://hostname:1883` |
32+
| `topic` | Topic name to which the application must subscribe | `topicName` |
33+
34+
##### Optional parameters
35+
36+
| Parameter | Description | Default value |
37+
|:---------------------:|:---------------------------------------------------------------------------------------------- |:----------------------------------------------:|
38+
| `clientId` | MQTT client id for the application | `MqttNotifier` |
39+
| `appName` | The text displayed as *tooltip* in the system tray and as *title* in any INFO or ERROR message | `Mqtt Notifier` |
40+
| `notificationTitle` | The notification title of any incoming message from the subscribed topic | `New Message` |
41+
| `notificationMessage` | The message notification of any incoming message from the subscribed topic | The received message's payload as string value |
42+
43+
Example:
44+
45+
```json
46+
{
47+
"topic": "doorbell",
48+
"broker": "tcp://my-broker:1883",
49+
"clientId": "DoorbellNotifier",
50+
"appName": "Doorbell Notifier",
51+
"notificationTitle": "DOORBELL!",
52+
"notificationMessage": "dlin dlon"
53+
}
54+
```
55+
56+
## Adapt it for your purpose
57+
58+
If this application is too generic for your purpose, you can just do some little changes to the source code and then build it.
59+
60+
For example:
61+
62+
* In case of simple changes, you can modify the behaviour of the `messaggeReceived` method in the `MqttNotifier` class
63+
64+
* If your project gets too complex according to the *observer pattern*, you can create another class that implements the `MqttNotification` interface
65+
66+
```java
67+
public class MyClass implements MqttNotification {
68+
....
69+
70+
@Override
71+
public void messageReceived(String topic, MqttMessage message) {
72+
// What you want do when a new message is received
73+
}
74+
}
75+
```
76+
77+
and connect it to `MqttSubClient` through the `attach()` method
78+
79+
```java
80+
MyClass myClass = new MyClass();
81+
mqttSubClient.attach(myClass);
82+
```
83+
84+
        In this way your method will be called anytime there is a new messagge.
85+
86+
## Roadmap
87+
88+
In future releases I would like to:
89+
90+
* make it multi-platform
91+
92+
* add a feature to subscribe to multiple topics (configurable through the configuration file)
93+
94+
* enable login to broker with username and password
95+
96+
## Built With
97+
98+
* [Eclipse Paho Java Client](https://www.eclipse.org/paho/index.php?page=clients/java/index.php) - MQTT client library
99+
100+
* [Moshi](https://github.com/square/moshi) - JSON library
101+
102+
* [Maven](https://maven.apache.org/) - Dependency Management
103+
104+
## License
105+
106+
This project is released under the [MIT License](https://github.com/bongijo/MqttNotifier/blob/master/LICENSE)

notification.gif

219 KB
Loading

pom.xml

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.bongio.mqttnotifier</groupId>
8+
<artifactId>MqttNotifier</artifactId>
9+
<version>1.0</version>
10+
<packaging>jar</packaging>
11+
<build>
12+
<plugins>
13+
<plugin>
14+
<groupId>org.apache.maven.plugins</groupId>
15+
<artifactId>maven-compiler-plugin</artifactId>
16+
<configuration>
17+
<source>8</source>
18+
<target>8</target>
19+
</configuration>
20+
</plugin>
21+
<plugin>
22+
<artifactId>maven-assembly-plugin</artifactId>
23+
<executions>
24+
<execution>
25+
<phase>package</phase>
26+
<goals>
27+
<goal>single</goal>
28+
</goals>
29+
</execution>
30+
</executions>
31+
<configuration>
32+
<descriptorRefs>
33+
<descriptorRef>jar-with-dependencies</descriptorRef>
34+
</descriptorRefs>
35+
<archive>
36+
<manifest>
37+
<addClasspath>true</addClasspath>
38+
<classpathPrefix>libs/</classpathPrefix>
39+
<mainClass>
40+
com.bongio.mqttnotifier.MqttNotifier
41+
</mainClass>
42+
</manifest>
43+
</archive>
44+
</configuration>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
49+
<repositories>
50+
<repository>
51+
<id>Eclipse Paho Repo</id>
52+
<url>https://repo.eclipse.org/content/repositories/paho-releases/</url>
53+
</repository>
54+
</repositories>
55+
56+
<dependencies>
57+
<dependency>
58+
<groupId>org.eclipse.paho</groupId>
59+
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
60+
<version>1.2.0</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>com.squareup.moshi</groupId>
64+
<artifactId>moshi</artifactId>
65+
<version>1.9.3</version>
66+
</dependency>
67+
</dependencies>
68+
69+
</project>

sample.settings.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"topic": "<topicName>",
3+
"broker": "tcp://<ip>:<port>"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.bongio.mqttnotifier;
2+
3+
public class Configuration {
4+
private final String topic;
5+
private final String broker;
6+
private final String clientId;
7+
private final String appName;
8+
private final String notificationTitle;
9+
private final String notificationMessage;
10+
11+
public Configuration(String topic, String broker, String clientId, String appName, String notificationTitle, String notificationMessage) {
12+
this.topic = topic;
13+
this.broker = broker;
14+
this.clientId = clientId;
15+
this.appName = appName;
16+
this.notificationTitle = notificationTitle;
17+
this.notificationMessage = notificationMessage;
18+
}
19+
20+
public String getTopic() {
21+
return topic;
22+
}
23+
24+
public String getBroker() {
25+
return broker;
26+
}
27+
28+
public String getClientId() {
29+
return clientId;
30+
}
31+
32+
public String getAppName() {
33+
return appName;
34+
}
35+
36+
public String getNotificationTitle() {
37+
return notificationTitle;
38+
}
39+
40+
public String getNotificationMessage() {
41+
return notificationMessage;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.bongio.mqttnotifier;
2+
3+
import org.eclipse.paho.client.mqttv3.MqttMessage;
4+
5+
public interface MqttNotification {
6+
public void messageReceived(String topic, MqttMessage message);
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.bongio.mqttnotifier;
2+
3+
import com.squareup.moshi.JsonAdapter;
4+
import com.squareup.moshi.JsonEncodingException;
5+
import com.squareup.moshi.Moshi;
6+
import org.eclipse.paho.client.mqttv3.MqttException;
7+
import org.eclipse.paho.client.mqttv3.MqttMessage;
8+
9+
import java.awt.*;
10+
import java.io.File;
11+
import java.io.IOException;
12+
import java.nio.file.Files;
13+
import java.nio.file.NoSuchFileException;
14+
import java.util.List;
15+
16+
public class MqttNotifier implements MqttNotification {
17+
private static SystemTrayNotification systemTrayNotification = null;
18+
19+
//Default values
20+
private static String appName = "Mqtt Notifier";
21+
private static String notificationTitle = "New Message";
22+
private static String notificationMessage = "";
23+
private static String clientId = "MqttNotifier";
24+
25+
public static void main(String[] args) {
26+
try {
27+
systemTrayNotification = new SystemTrayNotification(appName);
28+
} catch (AWTException e) {
29+
e.printStackTrace();
30+
System.exit(1);
31+
}
32+
33+
//Start reading configuration file
34+
Configuration configuration = null;
35+
36+
//Check if configuration file exists and if it's a well formed json file
37+
try {
38+
List<String> lines = Files.readAllLines(new File("./settings.json").toPath());
39+
StringBuilder jsonString = new StringBuilder();
40+
lines.forEach(jsonString::append);
41+
42+
Moshi moshi = new Moshi.Builder().build();
43+
JsonAdapter<Configuration> jsonAdapter = moshi.adapter(Configuration.class).nonNull();
44+
configuration = jsonAdapter.nonNull().fromJson(jsonString.toString());
45+
} catch (NoSuchFileException e) {
46+
systemTrayNotification.displayError(appName, "No settings file found!\nPlease add an settings.json file");
47+
e.printStackTrace();
48+
System.exit(2);
49+
} catch (JsonEncodingException e) {
50+
systemTrayNotification.displayError(appName, "Setting file malformed");
51+
e.printStackTrace();
52+
System.exit(3);
53+
}
54+
catch (IOException e) {
55+
systemTrayNotification.displayError(appName, "Error reading configuration file");
56+
e.printStackTrace();
57+
System.exit(4);
58+
}
59+
60+
//Check the presence of optional settings
61+
if(configuration.getAppName() != null) {
62+
appName = configuration.getAppName();
63+
systemTrayNotification.setToolTip(appName);
64+
}
65+
66+
if(configuration.getNotificationTitle() != null)
67+
notificationTitle = configuration.getNotificationTitle();
68+
69+
if(configuration.getNotificationMessage() != null)
70+
notificationMessage = configuration.getNotificationMessage();
71+
72+
if(configuration.getClientId() != null)
73+
clientId = configuration.getClientId();
74+
75+
//Check the presence of required settings
76+
try {
77+
if(configuration.getTopic() == null)
78+
throw new IllegalArgumentException("Missing a topic");
79+
else if(configuration.getBroker() == null)
80+
throw new IllegalArgumentException("Missing a broker");
81+
} catch (IllegalArgumentException e) {
82+
systemTrayNotification.displayError(appName, "Error in configuration file:\n" + e.getMessage());
83+
e.printStackTrace();
84+
System.exit(5);
85+
}
86+
//End reading configuration file
87+
88+
try {
89+
MqttSubClient mqttSubClient = new MqttSubClient(configuration.getBroker(), clientId, configuration.getTopic());
90+
mqttSubClient.attach(new MqttNotifier());
91+
mqttSubClient.connectAndSub();
92+
} catch (MqttException | IllegalArgumentException e) {
93+
systemTrayNotification.displayError(appName, "Error during connection:\n" + e.getMessage());
94+
e.printStackTrace();
95+
System.exit(6);
96+
}
97+
98+
systemTrayNotification.notify(appName, "Successful connection\nBroker: " + configuration.getBroker() + "\nTopic: " + configuration.getTopic());
99+
}
100+
101+
@Override
102+
public void messageReceived(String topic, MqttMessage message) {
103+
systemTrayNotification.notify(notificationTitle, notificationMessage.isEmpty() ? message.toString() : notificationMessage);
104+
}
105+
}

0 commit comments

Comments
 (0)