Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First attempt of introducing a REST interface #6

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@
<version>4.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
</dependencies>

<version>1.1.0-SNAPSHOT</version>
Expand Down
28 changes: 16 additions & 12 deletions src/main/java/de/myandres/optolink/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
*******************************************************************************/
package de.myandres.optolink;

import de.myandres.optolink.socket.HttpHandler;
import de.myandres.optolink.socket.SocketHandler;
import de.myandres.optolink.socket.service.ThingService;
import de.myandres.optolink.viessmann.ViessmannHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -32,11 +34,11 @@ public static void main(String[] args) {

try {

// config = new Config("src/main/resources/optolink.xml");
config = new Config("conf/optolink.xml");
config = new Config("src/main/resources/optolink.xml");
// config = new Config("conf/optolink.xml");

// Init ViessmannHandler
viessmannHandler = new ViessmannHandler(config);
// viessmannHandler = new ViessmannHandler(config);

} catch (Exception e) {
log.error("Something is wrong not init", e);
Expand All @@ -56,14 +58,16 @@ public static void main(String[] args) {
log.info("Programm normal terminated by Signal (Kill)");
}));

try {

// Start SocketHandler
SocketHandler socketHandler = new SocketHandler(config, viessmannHandler);
socketHandler.start();

} catch (Exception e) {
log.error("Programm abnormal terminated.", e);
}
ThingService thingService = new ThingService(config.getThingList());
HttpHandler httpHandler = new HttpHandler(thingService);
// try {
//
// // Start SocketHandler
// SocketHandler socketHandler = new SocketHandler(config, viessmannHandler);
// socketHandler.start();
//
// } catch (Exception e) {
// log.error("Programm abnormal terminated.", e);
// }
}
}
28 changes: 28 additions & 0 deletions src/main/java/de/myandres/optolink/socket/HttpHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.myandres.optolink.socket;

import de.myandres.optolink.socket.config.JsonTransformer;
import de.myandres.optolink.socket.service.ThingService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import spark.Spark;

public class HttpHandler {

private static final Logger LOGGER = LogManager.getLogger(HttpHandler.class);

private final ThingService thingService;
private final JsonTransformer jsonTransformer;

public HttpHandler(ThingService thingService) {
this.thingService = thingService;
jsonTransformer = new JsonTransformer();

Spark.port(8080);
Spark.get("/things", (req, res) -> thingService.getThings(), jsonTransformer);
Spark.get("/things/:id", (req, res) -> thingService.getThing(req.params("id")), jsonTransformer);
Spark.after((req, res) -> {
res.header("Content-Type", "application/json");
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package de.myandres.optolink.socket.config;

import com.google.gson.Gson;
import spark.ResponseTransformer;

public class JsonTransformer implements ResponseTransformer {

private Gson gson = new Gson();

@Override
public String render(Object model) {
return gson.toJson(model);
}
}
33 changes: 33 additions & 0 deletions src/main/java/de/myandres/optolink/socket/model/ChannelDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package de.myandres.optolink.socket.model;

import java.io.Serializable;

public class ChannelDto implements Serializable {

private String id;

private String value;

public ChannelDto() {}

public ChannelDto(String id, String value) {
this.id = id;
this.value = value;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}
34 changes: 34 additions & 0 deletions src/main/java/de/myandres/optolink/socket/model/ThingDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.myandres.optolink.socket.model;

import java.io.Serializable;
import java.util.List;

public class ThingDto implements Serializable {

private String id;

private List<ChannelDto> channels;

public ThingDto() {}

public ThingDto(String id, List<ChannelDto> channels) {
this.id = id;
this.channels = channels;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public List<ChannelDto> getChannels() {
return channels;
}

public void setChannels(List<ChannelDto> channels) {
this.channels = channels;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package de.myandres.optolink.socket.service;

import de.myandres.optolink.openhab.Channel;
import de.myandres.optolink.openhab.Thing;
import de.myandres.optolink.socket.model.ChannelDto;
import de.myandres.optolink.socket.model.ThingDto;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;
import java.util.stream.Collectors;

public class ThingService {

private static final Logger LOGGER = LogManager.getLogger(ThingService.class);

private final List<Thing> things;

public ThingService(List<Thing> things) {
this.things = things;
}

public List<ThingDto> getThings() {
LOGGER.debug("get /things");
return mapThings(things);
}

public String getThing(String id) {
LOGGER.debug("get /thing");
return "no thing found!";
}

private List<ThingDto> mapThings(List<Thing> things) {
return things.stream()
.map(t -> new ThingDto(t.getId(), mapChannels(t.getChannelList())))
.collect(Collectors.toList());
}

private List<ChannelDto> mapChannels(List<Channel> channels) {
return channels.stream()
.filter(c -> !c.getId().startsWith("*"))
.map(c -> new ChannelDto(c.getId(), null))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public String toString() {
return "SerialLog{" +
"timestamp=" + timestamp +
", operation=" + operation +
", message=" + data +
", data=" + String.format("0x%02X", data) +
'}';
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/optolink.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ For more information about addresses and types see: http://openv.wikispaces.com/

<optolink device="2033" id="300P" protocol="300">

<adapterID>VITOLIGNO</adapterID> <!-- Unique ID on the Network-->
<tty>/dev/ttyAMA0</tty> <!-- serial port of the optolink adapter-->
<ttytimeout>2000</ttytimeout> <!-- milliseconds for reading timeout tty -->
<port>31113</port> <!-- port for incoming communication. See also: http://www.iana.org -->
<adapterID>VITOLIGNO</adapterID> <!-- Unique ID on the Network-->
<tty>/dev/ttyUSB0</tty> <!-- serial port of the optolink adapter-->
<ttytimeout>2000</ttytimeout> <!-- milliseconds for reading timeout tty -->
<port>31113</port> <!-- port for incoming communication. See also: http://www.iana.org -->

<!-- Addressen die von diesem 'device' unterst�tzt werden -->

Expand Down