Skip to content
This repository has been archived by the owner on Jan 30, 2019. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahJelen committed Jan 23, 2018
0 parents commit 2e57856
Show file tree
Hide file tree
Showing 16 changed files with 887 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="lib" path="/home/nethernoah/Desktop/Sleep Timer/Sleep Timer/json-simple-1.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
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
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Sleep Timer</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

Binary file added bin/sleepTimer/CFGWindow.class
Binary file not shown.
Binary file added bin/sleepTimer/ConfigLoad.class
Binary file not shown.
Binary file added bin/sleepTimer/Init.class
Binary file not shown.
Binary file added bin/sleepTimer/PCQuit.class
Binary file not shown.
Binary file added bin/sleepTimer/shutDown.class
Binary file not shown.
Binary file added json-simple-1.1.jar
Binary file not shown.
48 changes: 48 additions & 0 deletions src/sleepTimer/CFGWindow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package sleepTimer;

import java.io.FileWriter;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.json.simple.JSONObject;

public class CFGWindow {
@SuppressWarnings({ "unchecked", "resource" })
public static void Config() {
System.out.println("You are running "+System.getProperty("os.name"));
JTextField shdHour = new JTextField();
JTextField shdMinute = new JTextField();
JTextField rmdHour = new JTextField();
JTextField rmdMinute = new JTextField();
final JComponent[] inputs = new JComponent[] {
new JLabel("Shut Down Hour"),
shdHour,
new JLabel("Shut Down Minute"),
shdMinute,
new JLabel("Reminder Hour"),
rmdHour,
new JLabel("Reminder Minute"),
rmdMinute,
new JLabel("Version 1.0")
};
int result = JOptionPane.showConfirmDialog(null, inputs, "Configuration", JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("shutdownhr", Long.parseLong(shdHour.getText()));
jsonObject.put("shutdownmin", Long.parseLong(shdMinute.getText()));
jsonObject.put("remindhr", Long.parseLong(rmdHour.getText()));
jsonObject.put("remindmin", Long.parseLong(rmdMinute.getText()));
try {
FileWriter fileWriter = new FileWriter("config.json");
fileWriter.write(jsonObject.toJSONString());
fileWriter.flush();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
50 changes: 50 additions & 0 deletions src/sleepTimer/ConfigLoad.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package sleepTimer;
import java.io.FileReader;
import java.io.FileWriter;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class ConfigLoad {
@SuppressWarnings("unchecked")
public static long[] readJson(String file) {
JSONParser parser = new JSONParser();
long[] options = {0,0,0,0};

try {
FileReader fileReader = new FileReader(file);
JSONObject json = (JSONObject) parser.parse(fileReader);

long shutdownhr = (long) json.get("shutdownhr");
long shutdownmin = (long) json.get("shutdownmin");
long remindhr = (long) json.get("remindhr");
long remindmin = (long) json.get("remindmin");

options[0] =shutdownhr;
options[1]=shutdownmin;
options[2]=remindhr;
options[3]=remindmin;
return options;
}
catch (Exception ex) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("shutdownhr", 23);
jsonObject.put("shutdownmin", 0);
jsonObject.put("remindhr", 22);
jsonObject.put("remindmin", 50);
options[0] =23;
options[1]=0;
options[2]=22;
options[3]=50;
try {
@SuppressWarnings("resource")
FileWriter fileWriter = new FileWriter("config.json");
fileWriter.write(jsonObject.toJSONString());
fileWriter.flush();
}
catch (Exception e) {
e.printStackTrace();
}
}
return options;
}
}
14 changes: 14 additions & 0 deletions src/sleepTimer/Init.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sleepTimer;

public class Init {
public static void main(String[] args) {
try {
if (args[0].equals("config")||args[0].equals("c")||args[0].equals("cfg")){
CFGWindow.Config();
return;
}
}
catch (Exception e){}
PCQuit.run();
}
}
39 changes: 39 additions & 0 deletions src/sleepTimer/PCQuit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package sleepTimer;

import java.io.IOException;
import java.util.Date;

import javax.swing.JOptionPane;

public class PCQuit{
@SuppressWarnings("deprecation")
public static void run() {
Date today= new Date();
long[] options=ConfigLoad.readJson("resources/config.json");
String quitHr=Long.toString(options[0]);
String quitMin=Long.toString(options[1]);

boolean sawWarning=false;

if (options[0]<10)
quitHr="0"+quitHr;

if (options[1]<10)
quitMin="0"+quitMin;
while (true) {
if (today.getMinutes()==options[3]&&today.getHours()==options[2]&&!sawWarning) {
JOptionPane.showMessageDialog(null,"You need to go to bed!\nThis machine will automatically shut down at "+quitHr+":"+quitMin,"Bed Time!",JOptionPane.WARNING_MESSAGE);
System.exit(0);
sawWarning=true;
}
if (today.getMinutes()==options[1]&&today.getMinutes()==options[0]) {
try {
shutDown.shutdown();
}
catch (RuntimeException|IOException e) {
e.printStackTrace();
}
}
}
}
}
25 changes: 25 additions & 0 deletions src/sleepTimer/shutDown.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sleepTimer;

import java.io.IOException;

public class shutDown {
public static void shutdown() throws RuntimeException, IOException {
String shutdownCommand;
String operatingSystem = System.getProperty("os.name");

if ("Linux".equals(operatingSystem) || "Mac OS X".equals(operatingSystem)) {
shutdownCommand = "shutdown -h now";
}

else if ("Windows".equals(operatingSystem)||"ReactOS".equals(operatingSystem)) {
shutdownCommand = "shutdown.exe -s -t 0";
}

else {
throw new RuntimeException("Unsupported operating system.");
}

Runtime.getRuntime().exec(shutdownCommand);
System.exit(0);
}
}

0 comments on commit 2e57856

Please sign in to comment.