-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9b45567
Showing
29 changed files
with
1,462 additions
and
0 deletions.
There are no files selected for viewing
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,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
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,4 @@ | ||
target/ | ||
*.iml | ||
.idea/ | ||
.DS_Store |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# busylight_rest | ||
a rest interface for busylight device |
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,71 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<artifactId>busylight-server</artifactId> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
<groupId>org.springframework.boot</groupId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<dependencies> | ||
<dependency> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
<exclusions> | ||
<exclusion> | ||
<artifactId>spring-boot-starter-tomcat</artifactId> | ||
<groupId>org.springframework.boot</groupId> | ||
</exclusion> | ||
</exclusions> | ||
<groupId>org.springframework.boot</groupId> | ||
</dependency> | ||
<dependency> | ||
<artifactId>spring-boot-starter-jetty</artifactId> | ||
<groupId>org.springframework.boot</groupId> | ||
<version>2.0.0.RELEASE</version> | ||
</dependency> | ||
<dependency> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<groupId>org.springframework.boot</groupId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<artifactId>springdoc-openapi-ui</artifactId> | ||
<groupId>org.springdoc</groupId> | ||
<version>1.1.6</version> | ||
</dependency> | ||
<dependency> | ||
<artifactId>busylight-core</artifactId> | ||
<groupId>com.fyayc.essen</groupId> | ||
<version>1.0.1-v2.2</version> | ||
</dependency> | ||
<dependency> | ||
<artifactId>spring-boot-starter-security</artifactId> | ||
<groupId>org.springframework.boot</groupId> | ||
</dependency> | ||
<dependency> | ||
<artifactId>spring-boot-test</artifactId> | ||
<groupId>org.springframework.boot</groupId> | ||
</dependency> | ||
</dependencies> | ||
<description>A Rest interface for controlling the busylight device</description> | ||
<groupId>com.fyayc.essen</groupId> | ||
<modelVersion>4.0.0</modelVersion> | ||
<name>busylight-server</name> | ||
|
||
<parent> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<groupId>org.springframework.boot</groupId> | ||
<relativePath/> | ||
<version>2.1.7.RELEASE</version> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<java.version>8</java.version> | ||
</properties> | ||
<version>1.1</version> | ||
|
||
</project> |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/fyayc/essen/busylight/server/ServerApp.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,22 @@ | ||
package com.fyayc.essen.busylight.server; | ||
|
||
import io.swagger.v3.oas.annotations.OpenAPIDefinition; | ||
import io.swagger.v3.oas.annotations.info.Contact; | ||
import io.swagger.v3.oas.annotations.info.Info; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@OpenAPIDefinition( | ||
info = | ||
@Info( | ||
title = "BusyLight REST Api", | ||
version = "0.5", | ||
description = "A Rest interface for controlling the busylight device", | ||
contact = @Contact(name = "Chandra", email = "[email protected]"))) | ||
@SpringBootApplication | ||
public class ServerApp { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(ServerApp.class, args); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/fyayc/essen/busylight/server/ServerConfig.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,16 @@ | ||
package com.fyayc.essen.busylight.server; | ||
|
||
import com.fyayc.essen.busylight.core.Driver; | ||
import com.fyayc.essen.busylight.server.driver.BusylightDriver; | ||
import com.fyayc.essen.busylight.server.driver.BusylightDriverSpec; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class ServerConfig { | ||
|
||
@Bean | ||
public BusylightDriverSpec driver() { | ||
return new BusylightDriver(Driver.tryAndAcquire()); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/fyayc/essen/busylight/server/controller/GlobalExceptionHandler.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,26 @@ | ||
package com.fyayc.essen.busylight.server.controller; | ||
|
||
import com.fyayc.essen.busylight.server.controller.models.Response; | ||
import io.swagger.v3.oas.annotations.Hidden; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(IllegalArgumentException.class) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@Hidden | ||
public Response badRequest(IllegalArgumentException ex) { | ||
return new Response(ex.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(Throwable.class) | ||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
@Hidden | ||
public Response otherException(Throwable ex) { | ||
return new Response(ex.getMessage()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/fyayc/essen/busylight/server/controller/LightControllerImpl.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,25 @@ | ||
package com.fyayc.essen.busylight.server.controller; | ||
|
||
import com.fyayc.essen.busylight.server.controller.intf.LightController; | ||
import com.fyayc.essen.busylight.server.controller.models.Response; | ||
import com.fyayc.essen.busylight.server.service.DriverService; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class LightControllerImpl implements LightController { | ||
|
||
private final DriverService driverService; | ||
|
||
public LightControllerImpl(DriverService driverService) { | ||
this.driverService = driverService; | ||
} | ||
|
||
public Response light(String hex, String type) { | ||
if (!hex.toUpperCase().startsWith("0X")) { | ||
throw new IllegalArgumentException( | ||
"Illegal hex format. Hex code must start with 0x[rr][gg][[bb]"); | ||
} | ||
driverService.setColor(hex, type); | ||
return new Response("updated"); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/com/fyayc/essen/busylight/server/controller/ParseControllerImpl.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,82 @@ | ||
package com.fyayc.essen.busylight.server.controller; | ||
|
||
import com.fyayc.essen.busylight.server.controller.intf.LightController; | ||
import com.fyayc.essen.busylight.server.controller.intf.ParseController; | ||
import com.fyayc.essen.busylight.server.controller.intf.StatusController; | ||
import com.fyayc.essen.busylight.server.controller.intf.ToneController; | ||
import com.fyayc.essen.busylight.server.controller.models.Response; | ||
import com.google.common.collect.ImmutableMap; | ||
import java.awt.Color; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class ParseControllerImpl implements ParseController { | ||
private final StatusController statusController; | ||
private final LightController lightController; | ||
private final ToneController toneControllerImpl; | ||
private final Pattern pattern = | ||
Pattern.compile("(set my)(\\s)+(status|light|tone)(\\s)+(to|for|as|ass)(\\s)+(.*)"); | ||
private final Map<String, Color> colorHashMap; | ||
private Logger logger = LogManager.getLogger(this.getClass()); | ||
|
||
@Autowired | ||
public ParseControllerImpl( | ||
StatusController statusController, | ||
LightController lightController, | ||
ToneController toneController) { | ||
this.statusController = statusController; | ||
this.lightController = lightController; | ||
this.toneControllerImpl = toneController; | ||
colorHashMap = | ||
ImmutableMap.<String, Color>builder() | ||
.put("red", Color.RED) | ||
.put("green", Color.GREEN) | ||
.put("blue", Color.BLUE) | ||
.put("magenta", Color.MAGENTA) | ||
.put("orange", Color.ORANGE) | ||
.put("pink", Color.pink) | ||
.put("yellow", Color.yellow) | ||
.put("cyan", Color.cyan) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public Response parse(String text) { | ||
logger.info("Parsing request {}", text); | ||
try { | ||
Matcher matcher = pattern.matcher(text); | ||
Response response = new Response("Sorry, this is not available yet"); | ||
if (matcher.matches()) { | ||
switch (matcher.group(3)) { | ||
case "status": | ||
response = statusController.status(matcher.group(7)); | ||
break; | ||
case "light": | ||
Color color = colorHashMap.get(matcher.group(7)); | ||
if (color != null) { | ||
response = | ||
lightController.light( | ||
"0x" + Integer.toHexString(color.getRGB()).substring(2), "none"); | ||
} else { | ||
response = lightController.light(matcher.group(7), "none"); | ||
} | ||
break; | ||
case "tone": | ||
break; | ||
} | ||
return response; | ||
} else { | ||
return new Response("Sorry, I'm unable to understand this. Can you try again?"); | ||
} | ||
|
||
} catch (IllegalArgumentException exception) { | ||
return new Response(exception.getMessage()); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/fyayc/essen/busylight/server/controller/RootController.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,15 @@ | ||
package com.fyayc.essen.busylight.server.controller; | ||
|
||
import io.swagger.v3.oas.annotations.Hidden; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
|
||
@Controller | ||
public class RootController { | ||
|
||
@GetMapping("/") | ||
@Hidden | ||
public String doRedirect() { | ||
return "redirect:/api/console"; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/fyayc/essen/busylight/server/controller/StatusControllerImpl.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,21 @@ | ||
package com.fyayc.essen.busylight.server.controller; | ||
|
||
import com.fyayc.essen.busylight.server.controller.intf.StatusController; | ||
import com.fyayc.essen.busylight.server.controller.models.Response; | ||
import com.fyayc.essen.busylight.server.service.DriverService; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class StatusControllerImpl implements StatusController { | ||
|
||
private final DriverService driverService; | ||
|
||
public StatusControllerImpl(DriverService driverService) { | ||
this.driverService = driverService; | ||
} | ||
|
||
public Response status(String status) { | ||
driverService.setStatus(status); | ||
return new Response("Setting status as " + status); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/fyayc/essen/busylight/server/controller/ToneControllerImpl.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,24 @@ | ||
package com.fyayc.essen.busylight.server.controller; | ||
|
||
import com.fyayc.essen.busylight.server.controller.intf.ToneController; | ||
import com.fyayc.essen.busylight.server.controller.models.Response; | ||
import com.fyayc.essen.busylight.server.service.DriverService; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class ToneControllerImpl implements ToneController { | ||
|
||
private final DriverService driverService; | ||
|
||
public ToneControllerImpl(DriverService driverService) { | ||
this.driverService = driverService; | ||
} | ||
|
||
public Response tone(String toneName, Integer volume, Integer duration) { | ||
if (duration < 0) { | ||
duration = -1; | ||
} | ||
driverService.tone(toneName.toUpperCase(), volume, duration * 1000); | ||
return new Response("updated"); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/fyayc/essen/busylight/server/controller/intf/LightController.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,61 @@ | ||
package com.fyayc.essen.busylight.server.controller.intf; | ||
|
||
import com.fyayc.essen.busylight.server.controller.models.Response; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.enums.ParameterIn; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.ExampleObject; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import javax.validation.constraints.NotEmpty; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
public interface LightController { | ||
|
||
@GetMapping(value = "/light/{hex}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) | ||
@Operation( | ||
summary = "Sets the color of the busylight device", | ||
responses = { | ||
@ApiResponse( | ||
responseCode = "200", | ||
description = "successfully adjusted the color of the device", | ||
content = | ||
@Content( | ||
mediaType = "application/json", | ||
schema = @Schema(implementation = Response.class))), | ||
@ApiResponse(responseCode = "500", description = "Server-side issue") | ||
}) | ||
Response light( | ||
@Parameter( | ||
description = "Hex code of the color to set, preceding with '0x'", | ||
required = true, | ||
in = ParameterIn.PATH, | ||
examples = { | ||
@ExampleObject(value = "0xff0000", name = "Red"), | ||
}) | ||
@PathVariable | ||
@NotEmpty(message = "Color can not be empty") | ||
String hex, | ||
@Parameter( | ||
description = "Sets the type of light", | ||
required = false, | ||
in = ParameterIn.QUERY, | ||
examples = { | ||
@ExampleObject(value = "none", name = "Normal light", summary = "Only light"), | ||
@ExampleObject( | ||
value = "blink", | ||
name = "Blinking light with the desired color", | ||
summary = "Blink light"), | ||
@ExampleObject( | ||
value = "pulse", | ||
name = "Pulsating light with the desired color", | ||
summary = "Pulse light"), | ||
}) | ||
@NotEmpty | ||
@RequestParam(required = false, defaultValue = "none") | ||
String type); | ||
} |
Oops, something went wrong.