Skip to content

Commit

Permalink
Merge pull request #3 from Ramzay/configuration-command-file-execution
Browse files Browse the repository at this point in the history
Improve configuration and fix maven
  • Loading branch information
Ramzay authored Jul 8, 2020
2 parents b263d68 + dfc60e0 commit 3a6cc25
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 47 deletions.
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.smart.home</groupId>
<artifactId>pc.daemon</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>1.0-SNAPSHOT</version>
<name>pc.daemon</name>
<packaging>jar</packaging>
<description>Daemon that will be installed on computers and will handle simple daily tasks</description>

<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<build>
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/smart/home/pc/daemon/dto/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.smart.home.pc.daemon.dto;

public class Command {

private String fileName;

private String script;

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

public String getScript() {
return script;
}

public void setScript(String script) {
this.script = script;
}

}
22 changes: 12 additions & 10 deletions src/main/java/com/smart/home/pc/daemon/dto/Config.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.smart.home.pc.daemon.dto;

import java.util.List;

public class Config {

private String commandPath;

private String sleepBatch;

private String scriptDir;

private List<Command> commands;

public String getScriptDir() {
return scriptDir;
Expand All @@ -16,14 +18,6 @@ public void setScriptDir(String scriptDir) {
this.scriptDir = scriptDir;
}

public String getSleepBatch() {
return sleepBatch;
}

public void setSleepBatch(String sleepBatch) {
this.sleepBatch = sleepBatch;
}

public String getCommandPath() {
return commandPath;
}
Expand All @@ -32,4 +26,12 @@ public void setCommandPath(String commandPath) {
this.commandPath = commandPath;
}

public List<Command> getCommands() {
return commands;
}

public void setCommands(List<Command> commands) {
this.commands = commands;
}

}
46 changes: 16 additions & 30 deletions src/main/java/com/smart/home/pc/daemon/impl/Daemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,22 @@
import java.io.IOException;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.smart.home.pc.daemon.dto.Command;
import com.smart.home.pc.daemon.dto.FullConfiguration;
import com.smart.home.pc.daemon.service.SimpleCustomTasks;
import com.smart.home.pc.daemon.service.impl.SimpleCustomTasksImpl;

public class Daemon {

private static final String SHUTDOWN_FILE_COMMAND = "shutdown-bedroom"; // file name that triggers an action

private static final String SHUTDOWN_COMMAND = "SHUTDOWN"; // TODO: enum or configurable

private static final Date PROC_START_DATE = new Date();

// private String absHomeDir;

// private String scriptDirName;

private String commandDirAbsPath;

private SimpleCustomTasks simpleCustomTasks;

private FullConfiguration configuration;

public Daemon(FullConfiguration configuration) {
// absHomeDir = configuration.getAbsPathHomeFolder();
// scriptDirName = configuration.getUserConfiguration().getScriptDir();
commandDirAbsPath = configuration.getUserConfiguration().getCommandPath();
simpleCustomTasks = new SimpleCustomTasksImpl();
this.configuration = configuration;
Expand All @@ -41,15 +30,13 @@ public void run() throws RuntimeException, IOException {
initProc();

// Run proc
String command = getCommand();
if (!StringUtils.isEmpty(command)) {
// Check which command and perform action
if (SHUTDOWN_COMMAND.equals(command)) {
// Delete command
deleteAllFiles(SHUTDOWN_FILE_COMMAND);
Command command = getCommand();
if (command != null) {
// Delete files associated to this command
deleteAllFiles(command.getFileName());
// Execute shutdown action
simpleCustomTasks.shutdown(configuration);
}
simpleCustomTasks.executeCommand(configuration, command);

}

// Final proc
Expand All @@ -70,7 +57,6 @@ private void initProc() {
if (PROC_START_DATE.after(fileLastModified)) {
currentFile.delete();
}

}
}
}
Expand All @@ -81,26 +67,26 @@ private void initProc() {
* file name located in special directory if specific file is detected we return
* the underlying action to perform
*
* @return String : action to be performed
* @return Command : command to be performed
*/
private String getCommand() {
private Command getCommand() {

// Directory exists we check files inside
File[] listOfFiles = getFilesInCommandDirLocation();
if (listOfFiles != null && listOfFiles.length == 0) {
return null;
}

// Loop in file list and we will check if one of them is OK
// Check if there is a trigger file in the directory
for (int i = 0; i < listOfFiles.length; i++) {

if (listOfFiles[i].isFile()) {
if (listOfFiles[i].getName().contains(SHUTDOWN_FILE_COMMAND)) {
return SHUTDOWN_COMMAND;
}
if (listOfFiles[i].isFile()) { // Make sure it's a file and not a dir
for(Command currentCommand : configuration.getUserConfiguration().getCommands()) {
if (currentCommand.getFileName() != null && listOfFiles[i].getName().contains(currentCommand.getFileName())) {
return currentCommand; // TODO: list of commands ?
}
}
}
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import java.io.IOException;

import com.smart.home.pc.daemon.dto.Command;
import com.smart.home.pc.daemon.dto.FullConfiguration;

public interface SimpleCustomTasks {
public void shutdown(FullConfiguration configuration) throws RuntimeException, IOException;
public void executeCommand(FullConfiguration configuration, Command command) throws RuntimeException, IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@

import org.apache.commons.lang3.StringUtils;

import com.smart.home.pc.daemon.dto.Command;
import com.smart.home.pc.daemon.dto.FullConfiguration;
import com.smart.home.pc.daemon.impl.Daemon;
import com.smart.home.pc.daemon.service.SimpleCustomTasks;

public class SimpleCustomTasksImpl implements SimpleCustomTasks {


public void shutdown(FullConfiguration configuation) throws RuntimeException, IOException {
public void executeCommand(FullConfiguration configuation, Command command) throws RuntimeException, IOException {
String operatingSystem = System.getProperty("os.name");
if ("Linux".equals(operatingSystem) || "Mac OS X".equals(operatingSystem)) {
String shutdownCommand = "shutdown -h now";
Runtime.getRuntime().exec(shutdownCommand);
} else if (isWindowsOS(operatingSystem)) { // Windows
executWindowsProcedure(configuation);
executWindowsProcedure(configuation, command);
} else {
System.err.println("Unsupported operating system.");
}
Expand All @@ -45,12 +45,12 @@ private boolean isWindowsOS(String osName) {
* Will perform the windows tasks, and will attempt multiple times before
* stopping attempt
*/
private void executWindowsProcedure(FullConfiguration configuration) {
private void executWindowsProcedure(FullConfiguration configuration, Command command) {
int maxAttempts = 5;
int attempts = 0;
boolean processInSuccess = false;
while (!processInSuccess && attempts < maxAttempts) {
processInSuccess = executeBatchInBatchDir("sleep.bat", configuration);
processInSuccess = executeBatchInBatchDir(command.getScript(), configuration);
attempts++;
sleep(5000);
}
Expand Down

0 comments on commit 3a6cc25

Please sign in to comment.