]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/weather/SingleForecast.java
Version 16, February 2014
[GpsPrune.git] / tim / prune / function / weather / SingleForecast.java
1 package tim.prune.function.weather;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Calendar;
6
7 /**
8  * Class to represent a weather forecast
9  * for a single day or a 3-hour period
10  */
11 public class SingleForecast
12 {
13         private String _date = null;
14         private String _dayDescKey = null;
15         private String _timeFrom = null, _timeTo = null;
16         private String _imageName = null;
17         private String _desc = null;
18         private String _tempString = null;
19         private String _humidity = null;
20         private String _windDesc = null;
21
22         /** For getting today's and tomorrow's dates */
23         private static SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
24
25         /** Set the time of the forecast */
26         public void setTime(String inDate, String inTimeFrom, String inTimeTo)
27         {
28                 _date = inDate;
29                 if (inTimeFrom != null && inTimeFrom.length() > 10
30                         && inTimeTo != null && inTimeTo.length() > 10)
31                 {
32                         _date = inTimeFrom.substring(0, 10);
33                         _timeFrom = inTimeFrom.substring(11, 16);
34                         _timeTo   = inTimeTo.substring(11, 16);
35                 }
36                 _dayDescKey = getDayDescriptionKey(_date);
37                 // System.out.println(_date + " is " + _dayDescKey);
38         }
39
40         /**
41          * Set the symbol details
42          */
43         public void setSymbol(String inNumber, String inImageCode, String inDesc)
44         {
45                 _imageName = getIconName(inNumber, inImageCode);
46                 // System.out.println("For number " + inNumber + "(" + inDesc + ") and code " + inImageCode + ", the symbol is " + _imageName);
47                 _desc = inDesc;
48         }
49
50         /**
51          * Set the minimum and maximum temperatures (will be rounded to nearest int)
52          */
53         public void setTemps(String inMin, String inMax)
54         {
55                 String tempMin = null, tempMax = null;
56                 try {
57                         tempMin = "" + Math.round(Double.parseDouble(inMin));
58                 } catch (Exception e) {}; // tempMin stays null if temp can't be parsed
59                 try {
60                         tempMax = "" + Math.round(Double.parseDouble(inMax));
61                 } catch (Exception e) {}; // tempMax stays null if temp can't be parsed
62
63                 _tempString = tempMin;
64                 if (tempMin != null && tempMax != null) {
65                         if (!tempMin.equals(tempMax))
66                         {
67                                 if (tempMin.charAt(0) == '-' && tempMax.charAt(0) != '-' && tempMax.charAt(0) != '0') {
68                                         // min temp is negative, max is positive, so add a + to the max
69                                         tempMax = "+" + tempMax;
70                                 }
71                                 _tempString = tempMin  + " — " + tempMax;
72                         }
73                 }
74                 else if (tempMax != null) {
75                         _tempString = tempMax;
76                 }
77         }
78
79         /** Set humidity */
80         public void setHumidity(String inHumidity) {
81                 _humidity = inHumidity;
82         }
83
84         /** Set description of wind */
85         public void setWindDesc(String inDesc) {
86                 _windDesc = inDesc;
87         }
88
89         /**
90          * Get the name of the image file for the given weather report
91          * @param inCode numeric three-digit code, as string
92          * @param inImage filename as given by openweather (just used for day/night)
93          * @return image file using GpsPrune's icons
94          */
95         public static String getIconName(String inCode, String inImage)
96         {
97                 final boolean daytime = inImage == null || inImage.length() != 3 || inImage.charAt(2) != 'n';
98                 final char leadDigit = (inCode == null || inCode.equals("")) ? '0' : inCode.charAt(0);
99                 String iconName = null;
100                 switch (leadDigit)
101                 {
102                         case '2':       return "storm.png";
103                         case '3':       return "lightrain.png";
104                         case '5':
105                                 iconName = "rain.png";
106                                 if (inCode.equals("500")) {iconName = "lightrain.png";}
107                                 else if (inCode.equals("511")) {iconName = "hail.png";}
108                                 break;
109                         case '6':       return "snow.png";
110                         case '7':       return "fog.png";
111                         case '8':
112                                 iconName = daytime ? "clouds-day.png" : "clouds-night.png";
113                                 if (inCode.equals("800")) {iconName = daytime ? "clear-day.png" : "clear-night.png";}
114                                 else if (inCode.equals("804")) {iconName = "clouds.png";}
115                                 break;
116                         case '9':
117                                 iconName = "extreme.png";
118                                 if (inCode.equals("906")) {iconName = "hail.png";}
119                                 break;
120                 }
121                 return iconName;
122         }
123
124         /**
125          * MAYBE: Maybe split off into separate DateFunctions class?
126          * @param inDate date
127          * @return day description, such as "today" or "saturday"
128          */
129         private static String getDayDescriptionKey(String inDate)
130         {
131                 if (inDate == null || inDate.length() != 10) {return null;}
132                 Calendar cal = Calendar.getInstance();
133                 String todaysDate = DATE_FORMATTER.format(cal.getTime());
134                 if (inDate.equals(todaysDate)) {return "today";}
135                 cal.add(Calendar.DATE, 1);
136                 String tomorrowsDate = DATE_FORMATTER.format(cal.getTime());
137                 if (inDate.equals(tomorrowsDate)) {return "tomorrow";}
138                 // Construct a date with this string and find out its day
139                 try
140                 {
141                         cal.setTime(DATE_FORMATTER.parse(inDate));
142                         switch (cal.get(Calendar.DAY_OF_WEEK))
143                         {
144                                 case Calendar.MONDAY   : return "monday";
145                                 case Calendar.TUESDAY  : return "tuesday";
146                                 case Calendar.WEDNESDAY : return "wednesday";
147                                 case Calendar.THURSDAY : return "thursday";
148                                 case Calendar.FRIDAY   : return "friday";
149                                 case Calendar.SATURDAY : return "saturday";
150                                 case Calendar.SUNDAY   : return "sunday";
151                         }
152                 }
153                 catch (ParseException pe) {}
154
155                 return "other";
156         }
157
158         /** @return true if there are times present, not just a date */
159         public boolean hasTimes() {
160                 return _timeFrom != null && _timeTo != null;
161         }
162         /** @return temperature range */
163         public String getTemps() {
164                 return _tempString;
165         }
166
167         /** @return date */
168         public String getDate() {return _date;}
169         /** @return time from */
170         public String getTimeFrom() {return _timeFrom;}
171         /** @return time to */
172         public String getTimeTo() {return _timeTo;}
173         /** @return day description */
174         public String getDayDesc() {return _dayDescKey;}
175
176         /** @return image name */
177         public String getImageName() {return _imageName;}
178         /** @return description */
179         public String getDescription() {return _desc;}
180
181         /** @return humidity */
182         public String getHumidity() {return _humidity;}
183         /** @return wind description */
184         public String getWindDescription() {return _windDesc;}
185 }