Skip to content

Commit

Permalink
First attempt of introducing a REST interface
Browse files Browse the repository at this point in the history
  • Loading branch information
leifbladt committed Jan 1, 2022
1 parent 1d8effa commit d49b7f8
Show file tree
Hide file tree
Showing 9 changed files with 1,033 additions and 16 deletions.
850 changes: 850 additions & 0 deletions optolink.log

Large diffs are not rendered by default.

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>2.21.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);
// }
}
}
27 changes: 27 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,27 @@
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.get("/devices", (req, res) -> thingService.getDevices(), jsonTransformer);
Spark.get("/devices/:id", (req, res) -> thingService.getDevice(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> getDevices() {
LOGGER.debug("get /devices");
return mapThings(things);
}

public String getDevice(String id) {
LOGGER.debug("get /devices");
return "no device found!";
}

private List<ThingDto> mapThings(List<Thing> things) {
return things.stream()
.map(t -> new ThingDto(t.getId(), mapChannels(t.getChannelMap())))
.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());
}
}
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

0 comments on commit d49b7f8

Please sign in to comment.