import javax.sound.sampled.*; import java.util.Calendar; import java.io.File; import java.io.IOException; import java.math.*; import java.io.IOException; import java.util.Scanner; class FilePlayer { private static File theTimeIsFile, timeForFile, repeatFile, dexFile, hydFile, gabFile, andFile; private static AudioInputStream theTimeIsStream, timeForStream, repeatStream, dexStream, hydStream, gabStream, andStream; private static Clip theTimeIsClip, timeForClip, repeatClip, dexClip, hydClip, gabClip, andClip; private static AudioFormat theTimeIsFormat, timeForFormat, repeatFormat, dexFormat, hydFormat, gabFormat, andFormat; private static DataLine.Info theTimeIsInfo, timeForInfo, repeatInfo, dexInfo, hydInfo, gabInfo, andInfo; private int time; static { theTimeIsFile = new File("C:/PillMonitor/ProgramData/Audio/TheTimeIs.wav"); timeForFile = new File("C:/PillMonitor/ProgramData/Audio/TimeFor.wav"); repeatFile = new File("C:/PillMonitor/ProgramData/Audio/Repeat.wav"); dexFile = new File("C:/PillMonitor/ProgramData/Audio/dex.wav"); hydFile = new File("C:/PillMonitor/ProgramData/Audio/hyd.wav"); gabFile = new File("C:/PillMonitor/ProgramData/Audio/gab.wav"); andFile = new File("C:/PillMonitor/ProgramData/Audio/and.wav"); try { theTimeIsStream = AudioSystem.getAudioInputStream(theTimeIsFile); timeForStream = AudioSystem.getAudioInputStream(timeForFile); repeatStream = AudioSystem.getAudioInputStream(repeatFile); dexStream = AudioSystem.getAudioInputStream(dexFile); hydStream = AudioSystem.getAudioInputStream(hydFile); gabStream = AudioSystem.getAudioInputStream(gabFile); andStream = AudioSystem.getAudioInputStream(andFile); } catch(UnsupportedAudioFileException ex) { System.out.println("Audio files are unsupported."); System.exit(1); } catch(IOException ex) { System.out.println("IO Exception in FilePlayer SIB, AudioStream initialization."); System.exit(1); } theTimeIsFormat = theTimeIsStream.getFormat(); timeForFormat = timeForStream.getFormat(); repeatFormat = repeatStream.getFormat(); dexFormat = dexStream.getFormat(); hydFormat = hydStream.getFormat(); gabFormat = gabStream.getFormat(); andFormat = andStream.getFormat(); theTimeIsInfo = new DataLine.Info(Clip.class, theTimeIsFormat); timeForInfo = new DataLine.Info(Clip.class, timeForFormat); repeatInfo = new DataLine.Info(Clip.class, repeatFormat); dexInfo = new DataLine.Info(Clip.class, dexFormat); hydInfo = new DataLine.Info(Clip.class, hydFormat); gabInfo = new DataLine.Info(Clip.class, gabFormat); andInfo = new DataLine.Info(Clip.class, andFormat); try { theTimeIsClip = (Clip) AudioSystem.getLine(theTimeIsInfo); timeForClip = (Clip) AudioSystem.getLine(timeForInfo); repeatClip = (Clip) AudioSystem.getLine(repeatInfo); dexClip = (Clip) AudioSystem.getLine(dexInfo); hydClip = (Clip) AudioSystem.getLine(hydInfo); gabClip = (Clip) AudioSystem.getLine(gabInfo); andClip = (Clip) AudioSystem.getLine(andInfo); } catch(LineUnavailableException ex) { System.out.println("Audio line unavailable exception in FilePlayer static initialization block."); System.exit(1); } try { theTimeIsClip.open(theTimeIsStream); timeForClip.open(timeForStream); repeatClip.open(repeatStream); dexClip.open(dexStream); hydClip.open(hydStream); gabClip.open(gabStream); andClip.open(andStream); } catch(LineUnavailableException ex) { System.out.println("LU-Ex in FilePlayer static-init-block opening methods."); System.exit(1); } catch(IOException ex) { System.out.println("IOEx in FilePlayer static-init-block opening methods."); System.exit(1); } } private static double theTimeIsTimeVar, timeForTimeVar, repeatTimeVar, dexTimeVar, hydTimeVar, gabTimeVar, andTimeVar; private static long theTimeIsTime, timeForTime, repeatTime, dexTime, hydTime, gabTime, andTime; static { theTimeIsTimeVar = theTimeIsClip.getMicrosecondLength() * 0.001; timeForTimeVar = timeForClip.getMicrosecondLength() * 0.001; repeatTimeVar = repeatClip.getMicrosecondLength() * 0.001; dexTimeVar = dexClip.getMicrosecondLength() * 0.001; hydTimeVar = hydClip.getMicrosecondLength() * 0.001; gabTimeVar = gabClip.getMicrosecondLength() * 0.001; andTimeVar = andClip.getMicrosecondLength() * 0.001; BigDecimal theTimeIsBD = new BigDecimal(theTimeIsTimeVar); BigDecimal timeForBD = new BigDecimal(timeForTimeVar); BigDecimal repeatBD = new BigDecimal(repeatTimeVar); BigDecimal dexBD = new BigDecimal(dexTimeVar); BigDecimal hydBD = new BigDecimal(hydTimeVar); BigDecimal gabBD = new BigDecimal(gabTimeVar); BigDecimal andBD = new BigDecimal(andTimeVar); theTimeIsTime = theTimeIsBD.longValue(); timeForTime = timeForBD.longValue(); repeatTime = repeatBD.longValue(); dexTime = dexBD.longValue(); hydTime = hydBD.longValue(); gabTime = gabBD.longValue(); andTime = andBD.longValue(); } public FilePlayer(int time) { this.time = time; } public void playAudio() { switch(time) { case 7: try {play07();} catch(IOException ex) {System.out.println("IOEx in FP switchblock (method play07())."); System.exit(1);} catch(UnsupportedAudioFileException ex) {System.out.println("USAF-Ex in FP switch method 07."); System.exit(1);} catch(LineUnavailableException ex) {System.out.println("LU-Ex FP SB m-7."); System.exit(1);} catch(InterruptedException ex) {System.out.println("INT-Ex FP SB m-7."); System.exit(1);} break; case 11: try {play11();} catch(IOException ex) {System.out.println("IOEx in FP switchblock (method play11())."); System.exit(1);} catch(UnsupportedAudioFileException ex) {System.out.println("USAF-Ex in FP switch method 11."); System.exit(1);} catch(LineUnavailableException ex) {System.out.println("LU-Ex FP SB m-11."); System.exit(1);} catch(InterruptedException ex) {System.out.println("INT-Ex FP SB m-11."); System.exit(1);} break; case 15: try {play15();} catch(IOException ex) {System.out.println("IOEx in FP switchblock (method play15())."); System.exit(1);} catch(UnsupportedAudioFileException ex) {System.out.println("USAF-Ex in FP switch method 15."); System.exit(1);} catch(LineUnavailableException ex) {System.out.println("LU-Ex FP SB m-15."); System.exit(1);} catch(InterruptedException ex) {System.out.println("INT-Ex FP SB m-15."); System.exit(1);} break; case 19: try {play19();} catch(IOException ex) {System.out.println("IOEx in FP switchblock (method play19())."); System.exit(1);} catch(UnsupportedAudioFileException ex) {System.out.println("USAF-Ex in FP switch method 19."); System.exit(1);} catch(LineUnavailableException ex) {System.out.println("LU-Ex FP SB m-19."); System.exit(1);} catch(InterruptedException ex) {System.out.println("INT-Ex FP SB m-19."); System.exit(1);} break; } } private void play07() throws UnsupportedAudioFileException, LineUnavailableException, InterruptedException, IOException { for(int i = 0; i < 2; i++) { theTimeIsClip.start(); Thread.currentThread().sleep(theTimeIsTime); theTimeIsClip.stop(); theTimeIsClip.setFramePosition(0); Clip timeClip; File timeFile = new File("C:/PillMonitor/ProgramData/Audio/07.wav"); AudioInputStream stream = AudioSystem.getAudioInputStream(timeFile); AudioFormat format = stream.getFormat(); DataLine.Info infos = new DataLine.Info(Clip.class, format); timeClip = (Clip) AudioSystem.getLine(infos); timeClip.open(stream); double timeTimeVar = timeClip.getMicrosecondLength() * 0.001; BigDecimal timeTimeBD = new BigDecimal(timeTimeVar); long timeTime = timeTimeBD.longValue(); timeClip.start(); Thread.currentThread().sleep(timeTime); timeClip.stop(); timeClip.setFramePosition(0); timeForClip.start(); Thread.currentThread().sleep(timeForTime); timeForClip.stop(); timeForClip.setFramePosition(0); dexClip.start(); Thread.currentThread().sleep(dexTime); dexClip.stop(); dexClip.setFramePosition(0); andClip.start(); Thread.currentThread().sleep(dexTime); andClip.stop(); andClip.setFramePosition(0); hydClip.start(); Thread.currentThread().sleep(dexTime); hydClip.stop(); hydClip.setFramePosition(0); if(i != 0) { break; } repeatClip.start(); Thread.currentThread().sleep(repeatTime); repeatClip.stop(); repeatClip.setFramePosition(0); } } private void play11() throws UnsupportedAudioFileException, LineUnavailableException, InterruptedException, IOException { for(int i = 0; i < 2; i++) { theTimeIsClip.start(); Thread.currentThread().sleep(theTimeIsTime); theTimeIsClip.stop(); theTimeIsClip.setFramePosition(0); Clip timeClip; File timeFile = new File("C:/PillMonitor/ProgramData/Audio/11.wav"); AudioInputStream stream = AudioSystem.getAudioInputStream(timeFile); AudioFormat format = stream.getFormat(); DataLine.Info infos = new DataLine.Info(Clip.class, format); timeClip = (Clip) AudioSystem.getLine(infos); timeClip.open(stream); double timeTimeVar = timeClip.getMicrosecondLength() * 0.001; BigDecimal timeTimeBD = new BigDecimal(timeTimeVar); long timeTime = timeTimeBD.longValue(); timeClip.start(); Thread.currentThread().sleep(timeTime); timeClip.stop(); timeClip.setFramePosition(0); timeForClip.start(); Thread.currentThread().sleep(timeForTime); timeForClip.stop(); timeForClip.setFramePosition(0); hydClip.start(); Thread.currentThread().sleep(hydTime); hydClip.stop(); hydClip.setFramePosition(0); if(i != 0) break; repeatClip.start(); Thread.currentThread().sleep(repeatTime); repeatClip.stop(); repeatClip.setFramePosition(0); } } private void play15() throws UnsupportedAudioFileException, LineUnavailableException, InterruptedException, IOException { for(int i = 0; i < 2; i++) { theTimeIsClip.start(); Thread.currentThread().sleep(theTimeIsTime); theTimeIsClip.stop(); theTimeIsClip.setFramePosition(0); Clip timeClip; File timeFile = new File("C:/PillMonitor/ProgramData/Audio/15.wav"); AudioInputStream stream = AudioSystem.getAudioInputStream(timeFile); AudioFormat format = stream.getFormat(); DataLine.Info infos = new DataLine.Info(Clip.class, format); timeClip = (Clip) AudioSystem.getLine(infos); timeClip.open(stream); double timeTimeVar = timeClip.getMicrosecondLength() * 0.001; BigDecimal timeTimeBD = new BigDecimal(timeTimeVar); long timeTime = timeTimeBD.longValue(); timeClip.start(); Thread.currentThread().sleep(timeTime); timeClip.stop(); timeClip.setFramePosition(0); timeForClip.start(); Thread.currentThread().sleep(timeForTime); timeForClip.stop(); timeForClip.setFramePosition(0); hydClip.start(); Thread.currentThread().sleep(hydTime); hydClip.stop(); hydClip.setFramePosition(0); if(i != 0) break; repeatClip.start(); Thread.currentThread().sleep(repeatTime); repeatClip.stop(); repeatClip.setFramePosition(0); } } private void play19() throws UnsupportedAudioFileException, LineUnavailableException, InterruptedException, IOException { for(int i = 0; i < 2; i++) { theTimeIsClip.start(); Thread.currentThread().sleep(theTimeIsTime); theTimeIsClip.stop(); theTimeIsClip.setFramePosition(0); Clip timeClip; File timeFile = new File("C:/PillMonitor/ProgramData/Audio/19.wav"); AudioInputStream stream = AudioSystem.getAudioInputStream(timeFile); AudioFormat format = stream.getFormat(); DataLine.Info infos = new DataLine.Info(Clip.class, format); timeClip = (Clip) AudioSystem.getLine(infos); timeClip.open(stream); double timeTimeVar = timeClip.getMicrosecondLength() * 0.001; BigDecimal timeTimeBD = new BigDecimal(timeTimeVar); long timeTime = timeTimeBD.longValue(); timeClip.start(); Thread.currentThread().sleep(timeTime); timeClip.stop(); timeClip.setFramePosition(0); timeForClip.start(); Thread.currentThread().sleep(timeForTime); timeForClip.stop(); timeForClip.setFramePosition(0); hydClip.start(); Thread.currentThread().sleep(hydTime); hydClip.stop(); hydClip.setFramePosition(0); andClip.start(); Thread.currentThread().sleep(andTime); andClip.stop(); andClip.setFramePosition(0); gabClip.start(); Thread.currentThread().sleep(gabTime); gabClip.stop(); gabClip.setFramePosition(0); if(i != 0) break; repeatClip.start(); Thread.currentThread().sleep(repeatTime); repeatClip.stop(); repeatClip.setFramePosition(0); } } } class WaitingMachine { public static int waitFor() { while(Calendar.getInstance().get(Calendar.HOUR_OF_DAY) != 7 && Calendar.getInstance().get(Calendar.HOUR_OF_DAY) != 11 && Calendar.getInstance().get(Calendar.HOUR_OF_DAY) != 15 && Calendar.getInstance().get(Calendar.HOUR_OF_DAY) != 19 || Calendar.getInstance().get(Calendar.SECOND) != 0 || Calendar.getInstance().get(Calendar.MINUTE) != 0) { continue; } Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.HOUR_OF_DAY); } } public class PillMonitor { public static void main(String[] args) { System.out.println("Pill Monitor v1.1. Do not close this window."); for(;;) { int pillTimer = WaitingMachine.waitFor(); new Watcher(pillTimer).start(); FilePlayer player = new FilePlayer(pillTimer); player.playAudio(); } } } class Watcher extends Thread{ private int hour; private static boolean isWritten = false; private static Scanner scanner = new Scanner(System.in); public Watcher(int hour) { this.hour = hour; } @Override public void run() { int localHour = hour; if(hour == 15) localHour = 3; else if(hour == 19) localHour = 7; if(isWritten) { System.out.println("\n\t-ALERT: Pill not taken or registered."); } System.out.printf("\nLast pill: %s Time: %d", getPill(hour), localHour); System.out.print(":00"); if(hour == 7 || hour == 11 ) System.out.print(" AM"); else System.out.print(" PM"); if(!isWritten) { isWritten = true; long skip = 0; try { skip = System.in.available(); } catch(IOException ex) { System.out.println("\nIOException in Watcher thread, System.in.available() method."); System.exit(1); } if(skip != 0) { try { System.in.skip(skip); } catch(IOException ex) { System.out.println("\nIOException in Watcher thread, System.in.skip() method."); System.exit(1); } } scanner.nextLine(); System.out.print("\t-Pill taken\n"); isWritten = false; } } private static String getPill(int h) { String s = ""; switch(h) { case 7: s = "Dexa and Hydro"; break; case 11: case 15: s = "Hydro"; break; case 19: s = "Hydro and Gaba"; break; } return s; } }