Skip to content

Commit

Permalink
Adding in the source code
Browse files Browse the repository at this point in the history
  • Loading branch information
tckb committed Sep 7, 2019
0 parents commit 9b45567
Show file tree
Hide file tree
Showing 29 changed files with 1,462 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target/
*.iml
.idea/
.DS_Store
661 changes: 661 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# busylight_rest
a rest interface for busylight device
71 changes: 71 additions & 0 deletions pom.xml
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 src/main/java/com/fyayc/essen/busylight/server/ServerApp.java
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 src/main/java/com/fyayc/essen/busylight/server/ServerConfig.java
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());
}
}
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());
}
}
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");
}
}
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());
}
}
}
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";
}
}
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);
}
}
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");
}
}
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);
}
Loading

0 comments on commit 9b45567

Please sign in to comment.