Merge branch 'master' of https://github.uci.edu/rtrimana/smart_home_traffic
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / io / TriggerTimesFileReader.java
1 package edu.uci.iotproject.io;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.time.Instant;
8 import java.time.LocalDateTime;
9 import java.time.ZoneId;
10 import java.time.ZonedDateTime;
11 import java.time.format.DateTimeFormatter;
12 import java.util.*;
13
14 /**
15  * Parses a file to obtain the timestamps at which the smart plug was toggled on/off.
16  *
17  * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
18  * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
19  */
20 public class TriggerTimesFileReader {
21
22     public static final ZoneId ZONE_ID_LOS_ANGELES = ZoneId.of("America/Los_Angeles");
23     public static final ZoneId ZONE_ID_BUDAPEST = ZoneId.of("Europe/Budapest");
24
25     /**
26      * Reads a file with trigger timestamps and parses the timestamps into {@link Instant}s using the rules specified
27      * by {@link #parseTriggerTimestamp(String, boolean)}.
28      * @param fileName The absolute path to the file with trigger timestamps.
29      * @param _24hFormat {@code true} if the timestamps in the file are in 24 hour format, {@code false} if they are in
30      *                               AM/PM format.
31      * @return A containing the trigger timestamps represented as {@code Instant}s.
32      */
33     public List<Instant> readTriggerTimes(String fileName, boolean _24hFormat) {
34         List<Instant> listTriggerTimes = new ArrayList<>();
35         File file = new File(fileName);
36         try (BufferedReader br = new BufferedReader(new FileReader(file))) {
37             String s;
38             while ((s = br.readLine()) != null) {
39                 listTriggerTimes.add(parseTriggerTimestamp(s, _24hFormat));
40             }
41         } catch (IOException e) {
42             e.printStackTrace();
43         }
44         System.out.println("List has: " + listTriggerTimes.size());
45         return listTriggerTimes;
46     }
47
48     /**
49      * Parses a timestamp string to an {@link Instant} (UTC). Assumes timestamps are LA time.
50      * Format is expected to be either "MM/dd/uuuu HH:mm:ss" or "MM/dd/uuuu h:mm:ss a".
51      *
52      * @param timestampStr The string containing a date-time timestamp for LA's timezone.
53      * @param _24hFormat {@code true} if the time in {@code timestampStr} is given in 24 hour format, {@code false} if
54      *                               it is given in AM/PM format.
55      * @return An {@code Instant} representation of the parsed timestamp. Note that the {@code Instant} marks a point on
56      *         the timeline in UTC. Use {@link Instant#atZone(ZoneId)} to convert to the corresponding time in a given
57      *         timezone.
58      */
59     public Instant parseTriggerTimestamp(String timestampStr, boolean _24hFormat) {
60         // Note: only one 'h' when not prefixed with leading 0 for 1-9; and only one 'a' for AM/PM marker in Java 8 time
61         String format = _24hFormat ? "MM/dd/uuuu HH:mm:ss" : "MM/dd/uuuu h:mm:ss a";
62         LocalDateTime localDateTime = LocalDateTime.parse(timestampStr, DateTimeFormatter.ofPattern(format, Locale.US));
63         ZonedDateTime laZonedDateTime = localDateTime.atZone(ZONE_ID_LOS_ANGELES);
64         return laZonedDateTime.toInstant();
65     }
66
67 }