Skip to content
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
target
*.iml
11 changes: 8 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@

<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>1.1</version>
<groupId>fr.ybonnel</groupId>
<artifactId>simpleweb4j</artifactId>
<version>0.0.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies>

<build>
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2013- Yan Bonnel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

public enum Command {
UP,
DOWN,
OPEN,
CLOSE,
NOTHING
}
41 changes: 23 additions & 18 deletions src/main/java/Elevator.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
public class Elevator {
/*
* Copyright 2013- Yan Bonnel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

private String commands[] = {
"OPEN","CLOSE","UP",
"OPEN","CLOSE","UP",
"OPEN","CLOSE","UP",
"OPEN","CLOSE","UP",
"OPEN","CLOSE","UP",
"OPEN","CLOSE","DOWN",
"OPEN","CLOSE","DOWN",
"OPEN","CLOSE","DOWN",
"OPEN","CLOSE","DOWN",
"OPEN","CLOSE","DOWN"
};
public interface Elevator {

private int count = 0;

public String nextCommand() {
return commands[(count++)%commands.length];
}
Command nextCommand();
void call(int floor, String to);
void go(int floorToGo);
void userHasEntered();
void userHasExited();
void reset(String cause);
}
61 changes: 61 additions & 0 deletions src/main/java/Omnibus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2013- Yan Bonnel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

public class Omnibus implements Elevator {

Command steps[] = {
Command.OPEN, Command.CLOSE, Command.UP,
Command.OPEN, Command.CLOSE, Command.UP,
Command.OPEN, Command.CLOSE, Command.UP,
Command.OPEN, Command.CLOSE, Command.UP,
Command.OPEN, Command.CLOSE, Command.UP,
Command.OPEN, Command.CLOSE, Command.DOWN,
Command.OPEN, Command.CLOSE, Command.DOWN,
Command.OPEN, Command.CLOSE, Command.DOWN,
Command.OPEN, Command.CLOSE, Command.DOWN,
Command.OPEN, Command.CLOSE, Command.DOWN
};

int currentIndex = 0;


@Override
public Command nextCommand() {
return steps[currentIndex++ % steps.length];
}

@Override
public void call(int floor, String to) {
}

@Override
public void go(int floorToGo) {
}

@Override
public void userHasEntered() {
}

@Override
public void userHasExited() {
}

@Override
public void reset(String cause) {
currentIndex = 0;
}
}
93 changes: 80 additions & 13 deletions src/main/java/Server.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,98 @@
import spark.Request;
import spark.Response;
import spark.Route;
import fr.ybonnel.simpleweb4j.SimpleWeb4j;
import fr.ybonnel.simpleweb4j.exception.HttpErrorException;
import fr.ybonnel.simpleweb4j.handlers.ContentType;
import fr.ybonnel.simpleweb4j.handlers.Response;
import fr.ybonnel.simpleweb4j.handlers.Route;
import fr.ybonnel.simpleweb4j.handlers.RouteParameters;

import static fr.ybonnel.simpleweb4j.SimpleWeb4j.get;
import static fr.ybonnel.simpleweb4j.SimpleWeb4j.setPort;

import static spark.Spark.get;

public class Server {
private final Elevator elevator;

public Server() {
elevator = new Elevator();
elevator = new Omnibus();
}

public void start() {
get(new Route("/:path") {
setPort(getPort());

get(new Route<Void, Command>("/nextCommand", Void.class, ContentType.PLAIN_TEXT) {
@Override
public Response<Command> handle(Void o, RouteParameters parameters) throws HttpErrorException {
return new Response<>(elevator.nextCommand());
}
});

get(new Route<Void, Void>("/reset", Void.class) {
@Override
public Object handle(Request request, Response response) {
String path = request.params(":path");
if (path.equals("nextCommand")) {
return elevator.nextCommand();
} else {
return "";
}
public Response<Void> handle(Void param, RouteParameters routeParams) throws HttpErrorException {
elevator.reset(routeParams.getParam("cause"));
return new Response<>(null);
}
});

get(new Route<Void, Void>("/call", Void.class) {
@Override
public Response<Void> handle(Void param, RouteParameters routeParams) throws HttpErrorException {
elevator.call(Integer.parseInt(routeParams.getParam("atFloor")),
routeParams.getParam("to"));
return new Response<>(null);
}
});

get(new Route<Void, Void>("/go", Void.class) {
@Override
public Response<Void> handle(Void param, RouteParameters routeParams) throws HttpErrorException {
elevator.go(Integer.parseInt(routeParams.getParam("floorToGo")));
return new Response<>(null);
}
});

get( new Route<Void, Void>("/userHasEntered", Void.class) {
@Override
public Response<Void> handle(Void param, RouteParameters routeParams) throws HttpErrorException {
elevator.userHasEntered();
return new Response<>(null);
}
});

get(new Route<Void, Void>("/userHasExited", Void.class) {
@Override
public Response<Void> handle(Void param, RouteParameters routeParams) throws HttpErrorException {
elevator.userHasExited();
return new Response<>(null);
}
});

SimpleWeb4j.start();
}



/**
* @return port to use
*/
private static int getPort() {
// Heroku
String herokuPort = System.getenv("PORT");
if (herokuPort != null) {
return Integer.parseInt(herokuPort);
}

// Cloudbees
String cloudbeesPort = System.getProperty("app.port");
if (cloudbeesPort != null) {
return Integer.parseInt(cloudbeesPort);
}

// Default port;
return 9999;
}


public static void main(String[] args) {
new Server().start();
}
Expand Down