Browse Source

Merge 46c2d65f75c339f2903476479fdb7e4c712a123f into 3c347c854c4c55456785ff026a703422d8f02f62

lmzdev 1 năm trước cách đây
mục cha
commit
f74c4244be

+ 3 - 3
client/src/components/Settings/WeatherSettings/WeatherSettings.tsx

@@ -110,10 +110,10 @@ export const WeatherSettings = (): JSX.Element => {
           onChange={(e) => inputChangeHandler(e)}
         />
         <span>
-          Using
-          <a href="https://www.weatherapi.com/pricing.aspx" target="blank">
+          Now using
+          <a href="https://openweathermap.org/api" target="blank">
             {' '}
-            Weather API
+            OpenWeatherMap
           </a>
           . Key is required for weather module to work.
         </span>

+ 3 - 338
client/src/components/UI/Icons/WeatherIcon/IconMapping.ts

@@ -13,345 +13,10 @@ export enum TimeOfDay {
   night
 }
 
+const mapFromJson = require('./WeatherMapping.json')
+
 export class IconMapping {
-  private conditions: WeatherCondition[] = [
-    {
-      code: 1000,
-      icon: {
-        day: 'clear-day',
-        night: 'clear-night'
-      }
-    },
-    {
-      code: 1003,
-      icon: {
-        day: 'partly-cloudy-day',
-        night: 'partly-cloudy-night'
-      }
-    },
-    {
-      code: 1006,
-      icon: {
-        day: 'cloudy',
-        night: 'cloudy'
-      }
-    },
-    {
-      code: 1009,
-      icon: {
-        day: 'cloudy',
-        night: 'cloudy'
-      }
-    },
-    {
-      code: 1030,
-      icon: {
-        day: 'fog',
-        night: 'fog'
-      }
-    },
-    {
-      code: 1063,
-      icon: {
-        day: 'rain-day',
-        night: 'rain-night'
-      }
-    },
-    {
-      code: 1066,
-      icon: {
-        day: 'snow-day',
-        night: 'snow-night'
-      }
-    },
-    {
-      code: 1069,
-      icon: {
-        day: 'rain-snow-day',
-        night: 'rain-snow-night'
-      }
-    },
-    {
-      code: 1072,
-      icon: {
-        day: 'sleet',
-        night: 'sleet'
-      }
-    },
-    {
-      code: 1087,
-      icon: {
-        day: 'thunder-day',
-        night: 'thunder-night'
-      }
-    },
-    {
-      code: 1114,
-      icon: {
-        day: 'snow',
-        night: 'snow'
-      }
-    },
-    {
-      code: 1117,
-      icon: {
-        day: 'snow',
-        night: 'snow'
-      }
-    },
-    {
-      code: 1135,
-      icon: {
-        day: 'fog',
-        night: 'fog'
-      }
-    },
-    {
-      code: 1147,
-      icon: {
-        day: 'fog',
-        night: 'fog'
-      }
-    },
-    {
-      code: 1150,
-      icon: {
-        day: 'rain',
-        night: 'rain'
-      }
-    },
-    {
-      code: 1153,
-      icon: {
-        day: 'rain',
-        night: 'rain'
-      }
-    },
-    {
-      code: 1168,
-      icon: {
-        day: 'sleet',
-        night: 'sleet'
-      }
-    },
-    {
-      code: 1171,
-      icon: {
-        day: 'sleet',
-        night: 'sleet'
-      }
-    },
-    {
-      code: 1180,
-      icon: {
-        day: 'rain-day',
-        night: 'rain-night'
-      }
-    },
-    {
-      code: 1183,
-      icon: {
-        day: 'rain',
-        night: 'rain'
-      }
-    },
-    {
-      code: 1186,
-      icon: {
-        day: 'rain-day',
-        night: 'rain-night'
-      }
-    },
-    {
-      code: 1189,
-      icon: {
-        day: 'rain',
-        night: 'rain'
-      }
-    },
-    {
-      code: 1192,
-      icon: {
-        day: 'rain-day',
-        night: 'rain-night'
-      }
-    },
-    {
-      code: 1195,
-      icon: {
-        day: 'rain',
-        night: 'rain'
-      }
-    },
-    {
-      code: 1198,
-      icon: {
-        day: 'sleet',
-        night: 'sleet'
-      }
-    },
-    {
-      code: 1201,
-      icon: {
-        day: 'sleet',
-        night: 'sleet'
-      }
-    },
-    {
-      code: 1204,
-      icon: {
-        day: 'rain-snow',
-        night: 'rain-snow'
-      }
-    },
-    {
-      code: 1207,
-      icon: {
-        day: 'rain-snow',
-        night: 'rain-snow'
-      }
-    },
-    {
-      code: 1210,
-      icon: {
-        day: 'snow-day',
-        night: 'snow-night'
-      }
-    },
-    {
-      code: 1213,
-      icon: {
-        day: 'snow',
-        night: 'snow'
-      }
-    },
-    {
-      code: 1216,
-      icon: {
-        day: 'snow-day',
-        night: 'snow-night'
-      }
-    },
-    {
-      code: 1219,
-      icon: {
-        day: 'snow',
-        night: 'snow'
-      }
-    },
-    {
-      code: 1222,
-      icon: {
-        day: 'snow-day',
-        night: 'snow-night'
-      }
-    },
-    {
-      code: 1225,
-      icon: {
-        day: 'snow',
-        night: 'snow'
-      }
-    },
-    {
-      code: 1237,
-      icon: {
-        day: 'hail',
-        night: 'hail'
-      }
-    },
-    {
-      code: 1240,
-      icon: {
-        day: 'rain-day',
-        night: 'rain-night'
-      }
-    },
-    {
-      code: 1243,
-      icon: {
-        day: 'rain-day',
-        night: 'rain-night'
-      }
-    },
-    {
-      code: 1246,
-      icon: {
-        day: 'rain-day',
-        night: 'rain-night'
-      }
-    },
-    {
-      code: 1249,
-      icon: {
-        day: 'rain-snow-day',
-        night: 'rain-snow-night'
-      }
-    },
-    {
-      code: 1252,
-      icon: {
-        day: 'rain-snow-day',
-        night: 'rain-snow-night'
-      }
-    },
-    {
-      code: 1255,
-      icon: {
-        day: 'snow-day',
-        night: 'snow-night'
-      }
-    },
-    {
-      code: 1258,
-      icon: {
-        day: 'snow-day',
-        night: 'snow-night'
-      }
-    },
-    {
-      code: 1261,
-      icon: {
-        day: 'hail',
-        night: 'hail'
-      }
-    },
-    {
-      code: 1264,
-      icon: {
-        day: 'hail',
-        night: 'hail'
-      }
-    },
-    {
-      code: 1273,
-      icon: {
-        day: 'thunder-rain-day',
-        night: 'thunder-rain-night'
-      }
-    },
-    {
-      code: 1276,
-      icon: {
-        day: 'thunder-rain',
-        night: 'thunder-rain'
-      }
-    },
-    {
-      code: 1279,
-      icon: {
-        day: 'thunder-day',
-        night: 'thunder-night'
-      }
-    },
-    {
-      code: 1282,
-      icon: {
-        day: 'thunder',
-        night: 'thunder'
-      }
-    }
-  ];
+  private conditions: WeatherCondition[] = mapFromJson.mapping
 
   mapIcon(weatherStatusCode: number, timeOfDay: TimeOfDay): IconKey {
     const mapping = this.conditions.find((condition: WeatherCondition) => condition.code === weatherStatusCode);

+ 160 - 118
client/src/components/UI/Icons/WeatherIcon/WeatherMapping.json

@@ -1,340 +1,382 @@
 {
   "mapping": [
     {
-      "code": 1000,
+      "code": 800,
       "icon": {
         "day": "clear-day",
         "night": "clear-night"
       }
     },
     {
-      "code": 1003,
+      "code": 801,
       "icon": {
         "day": "partly-cloudy-day",
         "night": "partly-cloudy-night"
       }
     },
     {
-      "code": 1006,
+      "code": 802,
+      "icon": {
+        "day": "partly-cloudy-day",
+        "night": "partly-cloudy-night"
+      }
+    },
+    {
+      "code": 803,
       "icon": {
         "day": "cloudy",
         "night": "cloudy"
       }
     },
     {
-      "code": 1009,
+      "code": 804,
       "icon": {
         "day": "cloudy",
         "night": "cloudy"
       }
     },
     {
-      "code": 1030,
+      "code": 701,
       "icon": {
         "day": "fog",
         "night": "fog"
       }
     },
     {
-      "code": 1063,
+      "code": 711,
       "icon": {
-        "day": "rain-day",
-        "night": "rain-night"
+        "day": "fog",
+        "night": "fog"
       }
     },
     {
-      "code": 1066,
+      "code": 721,
       "icon": {
-        "day": "snow-day",
-        "night": "snow-night"
+        "day": "fog",
+        "night": "fog"
       }
     },
     {
-      "code": 1069,
+      "code": 731,
       "icon": {
-        "day": "rain-snow-day",
-        "night": "rain-snow-night"
+        "day": "fog",
+        "night": "fog"
       }
     },
     {
-      "code": 1072,
+      "code": 741,
       "icon": {
-        "day": "sleet",
-        "night": "sleet"
+        "day": "fog",
+        "night": "fog"
       }
     },
     {
-      "code": 1087,
+      "code": 751,
       "icon": {
-        "day": "thunder-day",
-        "night": "thunder-night"
+        "day": "fog",
+        "night": "fog"
       }
     },
     {
-      "code": 1114,
+      "code": 761,
       "icon": {
-        "day": "snow",
-        "night": "snow"
+        "day": "fog",
+        "night": "fog"
       }
     },
     {
-      "code": 1117,
+      "code": 771,
       "icon": {
-        "day": "snow",
-        "night": "snow"
+        "day": "wind",
+        "night": "wind"
       }
     },
     {
-      "code": 1135,
+      "code": 781,
       "icon": {
-        "day": "fog",
-        "night": "fog"
+        "day": "wind",
+        "night": "wind"
       }
     },
     {
-      "code": 1147,
+      "code": 600,
       "icon": {
-        "day": "fog",
-        "night": "fog"
+        "day": "snow-day",
+        "night": "snow-night"
       }
     },
     {
-      "code": 1150,
+      "code": 601,
       "icon": {
-        "day": "rain",
-        "night": "rain"
+        "day": "snow-day",
+        "night": "snow-night"
       }
     },
     {
-      "code": 1153,
+      "code": 602,
       "icon": {
-        "day": "rain",
-        "night": "rain"
+        "day": "snow",
+        "night": "snow"
       }
     },
     {
-      "code": 1168,
+      "code": 611,
       "icon": {
         "day": "sleet",
         "night": "sleet"
       }
     },
     {
-      "code": 1171,
+      "code": 612,
       "icon": {
         "day": "sleet",
         "night": "sleet"
       }
     },
     {
-      "code": 1180,
+      "code": 613,
       "icon": {
-        "day": "rain-day",
-        "night": "rain-night"
+        "day": "sleet",
+        "night": "sleet"
       }
     },
     {
-      "code": 1183,
+      "code": 615,
       "icon": {
-        "day": "rain",
-        "night": "rain"
+        "day": "rain-snow-day",
+        "night": "rain-snow-night"
       }
     },
     {
-      "code": 1186,
+      "code": 616,
       "icon": {
-        "day": "rain-day",
-        "night": "rain-night"
+        "day": "rain-snow-day",
+        "night": "rain-snow-night"
       }
     },
     {
-      "code": 1189,
+      "code": 620,
       "icon": {
-        "day": "rain",
-        "night": "rain"
+        "day": "rain-snow",
+        "night": "rain-snow"
       }
     },
     {
-      "code": 1192,
+      "code": 621,
       "icon": {
-        "day": "rain-day",
-        "night": "rain-night"
+        "day": "rain-snow",
+        "night": "rain-snow"
       }
     },
     {
-      "code": 1195,
+      "code": 622,
       "icon": {
-        "day": "rain",
-        "night": "rain"
+        "day": "rain-snow",
+        "night": "rain-snow"
       }
     },
     {
-      "code": 1198,
+      "code": 500,
       "icon": {
-        "day": "sleet",
-        "night": "sleet"
+        "day": "rain-day",
+        "night": "rain-night"
       }
     },
     {
-      "code": 1201,
+      "code": 501,
       "icon": {
-        "day": "sleet",
-        "night": "sleet"
+        "day": "rain-day",
+        "night": "rain-night"
       }
     },
     {
-      "code": 1204,
+      "code": 502,
       "icon": {
-        "day": "rain-snow",
-        "night": "rain-snow"
+        "day": "rain-day",
+        "night": "rain-night"
       }
     },
     {
-      "code": 1207,
+      "code": 503,
       "icon": {
-        "day": "rain-snow",
-        "night": "rain-snow"
+        "day": "rain",
+        "night": "rain"
       }
     },
     {
-      "code": 1210,
+      "code": 504,
       "icon": {
-        "day": "snow-day",
-        "night": "snow-night"
+        "day": "rain",
+        "night": "rain"
       }
     },
     {
-      "code": 1213,
+      "code": 511,
       "icon": {
-        "day": "snow",
-        "night": "snow"
+        "day": "hail",
+        "night": "hail"
       }
     },
     {
-      "code": 1216,
+      "code": 520,
       "icon": {
-        "day": "snow-day",
-        "night": "snow-night"
+        "day": "rain-day",
+        "night": "rain-night"
       }
     },
     {
-      "code": 1219,
+      "code": 521,
       "icon": {
-        "day": "snow",
-        "night": "snow"
+        "day": "rain-day",
+        "night": "rain-night"
       }
     },
     {
-      "code": 1222,
+      "code": 522,
       "icon": {
-        "day": "snow-day",
-        "night": "snow-night"
+        "day": "rain",
+        "night": "rain"
       }
     },
     {
-      "code": 1225,
+      "code": 531,
       "icon": {
-        "day": "snow",
-        "night": "snow"
+        "day": "rain-day",
+        "night": "rain-night"
       }
     },
     {
-      "code": 1237,
+      "code": 300,
       "icon": {
-        "day": "hail",
-        "night": "hail"
+        "day": "rain-day",
+        "night": "rain-night"
       }
     },
     {
-      "code": 1240,
+      "code": 301,
       "icon": {
         "day": "rain-day",
         "night": "rain-night"
       }
     },
     {
-      "code": 1243,
+      "code": 302,
       "icon": {
         "day": "rain-day",
         "night": "rain-night"
       }
     },
     {
-      "code": 1246,
+      "code": 310,
       "icon": {
         "day": "rain-day",
         "night": "rain-night"
       }
     },
     {
-      "code": 1249,
+      "code": 311,
       "icon": {
-        "day": "rain-snow-day",
-        "night": "rain-snow-night"
+        "day": "rain-day",
+        "night": "rain-night"
       }
     },
     {
-      "code": 1252,
+      "code": 312,
       "icon": {
-        "day": "rain-snow-day",
-        "night": "rain-snow-night"
+        "day": "rain-day",
+        "night": "rain-night"
       }
     },
     {
-      "code": 1255,
+      "code": 313,
       "icon": {
-        "day": "snow-day",
-        "night": "snow-night"
+        "day": "rain-day",
+        "night": "rain-night"
       }
     },
     {
-      "code": 1258,
+      "code": 314,
       "icon": {
-        "day": "snow-day",
-        "night": "snow-night"
+        "day": "rain",
+        "night": "rain"
       }
     },
     {
-      "code": 1261,
+      "code": 321,
       "icon": {
-        "day": "hail",
-        "night": "hail"
+        "day": "rain",
+        "night": "rain"
       }
     },
     {
-      "code": 1264,
+      "code": 200,
       "icon": {
-        "day": "hail",
-        "night": "hail"
+        "day": "thunder-rain-day",
+        "night": "thunder-rain-night"
       }
     },
     {
-      "code": 1273,
+      "code": 201,
       "icon": {
         "day": "thunder-rain-day",
         "night": "thunder-rain-night"
       }
     },
     {
-      "code": 1276,
+      "code": 202,
       "icon": {
-        "day": "thunder-rain",
-        "night": "thunder-rain"
+        "day": "thunder-rain-day",
+        "night": "thunder-rain-night"
+      }
+    },
+    {
+      "code": 210,
+      "icon": {
+        "day": "thunder-day",
+        "night": "thunder-night"
       }
     },
     {
-      "code": 1279,
+      "code": 211,
       "icon": {
         "day": "thunder-day",
         "night": "thunder-night"
       }
     },
     {
-      "code": 1282,
+      "code": 212,
       "icon": {
         "day": "thunder",
         "night": "thunder"
       }
+    },
+    {
+      "code": 221,
+      "icon": {
+        "day": "thunder-day",
+        "night": "thunder-night"
+      }
+    },
+    {
+      "code": 230,
+      "icon": {
+        "day": "thunder-rain-day",
+        "night": "thunder-rain-night"
+      }
+    },
+    {
+      "code": 231,
+      "icon": {
+        "day": "thunder-rain-day",
+        "night": "thunder-rain-night"
+      }
+    },
+    {
+      "code": 232,
+      "icon": {
+        "day": "thunder-rain-day",
+        "night": "thunder-rain-night"
+      }
     }
   ]
 }

+ 17 - 13
utils/getExternalWeather.js

@@ -3,27 +3,31 @@ const axios = require('axios');
 const loadConfig = require('./loadConfig');
 
 const getExternalWeather = async () => {
-  const { WEATHER_API_KEY: secret, lat, long } = await loadConfig();
+  const { WEATHER_API_KEY: secret, lat, long, isCelsius } = await loadConfig();
+  
+  //units = standard, metric, imperial
+  const units = isCelsius?'metric':'imperial'
 
   // Fetch data from external API
   try {
     const res = await axios.get(
-      `http://api.weatherapi.com/v1/current.json?key=${secret}&q=${lat},${long}`
+      `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=${secret}&units=${units}`
     );
 
     // Save weather data
-    const cursor = res.data.current;
+    const cursor = res.data;
+    const isDay = (Math.floor(Date.now()/1000) < cursor.sys.sunset) | 0
     const weatherData = await Weather.create({
-      externalLastUpdate: cursor.last_updated,
-      tempC: cursor.temp_c,
-      tempF: cursor.temp_f,
-      isDay: cursor.is_day,
-      cloud: cursor.cloud,
-      conditionText: cursor.condition.text,
-      conditionCode: cursor.condition.code,
-      humidity: cursor.humidity,
-      windK: cursor.wind_kph,
-      windM: cursor.wind_mph,
+      externalLastUpdate: cursor.dt,
+      tempC: cursor.main.temp,
+      tempF: cursor.main.temp,
+      isDay: isDay,
+      cloud: cursor.clouds.all,
+      conditionText: cursor.weather[0].main,
+      conditionCode: cursor.weather[0].id,
+      humidity: cursor.main.humidity,
+      windK: cursor.wind.speed,
+      windM: 0,
     });
     return weatherData;
   } catch (err) {