forked from steand/optolink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First attempt of introducing a REST interface
- Loading branch information
Showing
9 changed files
with
1,033 additions
and
16 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/de/myandres/optolink/socket/HttpHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/de/myandres/optolink/socket/config/JsonTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
src/main/java/de/myandres/optolink/socket/model/ChannelDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
src/main/java/de/myandres/optolink/socket/model/ThingDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/de/myandres/optolink/socket/service/ThingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters