최현준

개인별 기상 정보 저장 및 기상 정보 번역 제공

Showing 1 changed file with 76 additions and 22 deletions
......@@ -14,20 +14,26 @@ var async = require('async');
const { resolve } = require('path');
var Language_Detect_Array = new Array();
var User_Info_Array = new Array();
var Weahter_Info_Array = new Array();
var Weahter_Detect_Array = new Array();
Weahter_Info_Array[0] =
Weahter_Detect_Array[0] =
{
"Location" : '수원',
"Region" : '수원',
"nx" : 61,
"ny" : 120
}
Weahter_Info_Array[1] =
Weahter_Detect_Array[1] =
{
"Location" : '서울',
"Region" : '서울',
"nx" : 60,
"ny" : 127
}
Weahter_Detect_Array[2] =
{
"Region" : '밀양',
"nx" : 92,
"ny" : 83
}
Language_Detect_Array[0] =
{
......@@ -53,7 +59,8 @@ Language_Detect_Array[3] =
User_Info_Array[0] =
{
UserId : null,
UserPapagoLanguage : ''
UserPapagoLanguage : '',
UserRegion : '수원',
}
app.use(bodyParser.json());
......@@ -64,7 +71,7 @@ app.post('/hook', function (req, res) {
var replyToken = eventObj.replyToken;
var userid = eventObj.source.userId;
if(message == '날씨') weathercheck(replyToken); else AsyncFunction(message, replyToken, userid);
AsyncFunction(message, replyToken, userid);
res.sendStatus(200);
});
......@@ -74,18 +81,29 @@ async function AsyncFunction(message, replyToken, userid)
var User_Location = search_user_info(userid);
var DetectLanguage = await detector(message);
if(DetectLanguage != 'ko') var TransToKoreanMessage = await trans(DetectLanguage, message, 'ko'); else var TransToKoreanMessage = message;
var DetectLanguageChange = Detect_Papago_Language_Change(TransToKoreanMessage, User_Location);
console.log(TransToKoreanMessage);
var DetectChange = Detect_Papago_Language_Change(TransToKoreanMessage, User_Location);
if (DetectChange) DetectChange = Detect_Weather_Region_Change(TransToKoreanMessage, User_Location);
if(TransToKoreanMessage != '날씨')
{
if(DetectLanguage != User_Info_Array[User_Location].UserPapagoLanguage) var TransToUserSettingMessage = await trans(DetectLanguage, message, User_Info_Array[User_Location].UserPapagoLanguage);
var Messaging = await messaging(TransToUserSettingMessage, replyToken, DetectLanguageChange);
var Messaging = await messaging(TransToUserSettingMessage, replyToken, DetectChange);
console.log("log-Message: ", Messaging);
}
else
weathercheck(replyToken, userid, DetectLanguage);
}
async function weathercheck(replyToken)
async function weathercheck(replyToken, userid, DetectLanguage)
{
var User_Location = search_user_info(userid);
var location = user_location(User_Location);
var timevalue = make_time()
var url = make_url(timevalue)
var persentofweawther = await weather_get(url);
var Messaging = await messaging(persentofweawther, replyToken, 1);
var url = make_url(timevalue, location);
var message = await weather_get(url, User_Location);
if(DetectLanguage != 'ko')
message = await trans ('ko', message, DetectLanguage)
var Messaging = await messaging(message, replyToken, 1);
console.log("log-Message: ", Messaging);
}
......@@ -122,12 +140,12 @@ function detector (message)
})
}
function messaging(message, replyToken, DetectLanguageChange)
function messaging(message, replyToken, DetectChange)
//메시지 보내기
{
return new Promise(function(resolve, reject)
{
if(DetectLanguageChange)
if(DetectChange)
request.post
(
{
......@@ -203,7 +221,8 @@ function search_user_info (userid)
User_Info_Array[User_Info_Array.length] =
{
UserId : userid,
UserPapagoLanguage : 'en'
UserPapagoLanguage : 'en',
UserRegion : '수원'
}
return User_Info_Array.length-1;
}
......@@ -213,7 +232,6 @@ function search_user_info (userid)
function Detect_Papago_Language_Change (TransToKoreanMessage, User_Location)
//번역 언어 변경 감지
{
console.log('user의 정보는',User_Info_Array[User_Location]);
for(var i in Language_Detect_Array) // Detect Language change
{
if (TransToKoreanMessage == Language_Detect_Array[i].Message)
......@@ -221,6 +239,7 @@ function Detect_Papago_Language_Change (TransToKoreanMessage, User_Location)
//언어 변경 감지o
User_Info_Array[User_Location].UserPapagoLanguage = Language_Detect_Array[i].PapagoLanguage;
return 0;
break;
}
else if ( i == Language_Detect_Array.length-1)
......@@ -231,6 +250,26 @@ function Detect_Papago_Language_Change (TransToKoreanMessage, User_Location)
}
}
function Detect_Weather_Region_Change (TransToKoreanMessage, User_Location)
//지역 변경 감지
{
for(var i in Weahter_Detect_Array)
{
if (TransToKoreanMessage == Weahter_Detect_Array[i].Region)
{
//지역 변경 감지o
User_Info_Array[User_Location].UserRegion = Weahter_Detect_Array[i].Region
return 0;
break;
}
else if ( i == Weahter_Detect_Array.length-1)
{
//지역 변경 감지x
return 1;
}
}
}
function make_time()
{
var today = new Date();
......@@ -282,7 +321,7 @@ function make_time()
return value;
}
function make_url(timevalue)
function make_url(timevalue, location)
{
var serviceKey = weather_key;
var pageNo = '1';
......@@ -290,8 +329,8 @@ function make_url(timevalue)
var dataType = 'JSON';
var base_date = timevalue.today;
var base_time = timevalue.hours;
var nx = '61';
var ny = '120';
var nx = location.nx;
var ny = location.ny;
var url = `${WEATHER_TARGET_URL}`;
url += '?serviceKey='+serviceKey;
......@@ -305,8 +344,23 @@ function make_url(timevalue)
return url;
}
function user_location(User_Location)
{
var location_info = new Object();
for(var i in Weahter_Detect_Array)
{
if (User_Info_Array[User_Location].UserRegion == Weahter_Detect_Array[i].Region)
{
location_info.nx = Weahter_Detect_Array[i].nx;
location_info.ny = Weahter_Detect_Array[i].ny;
return location_info;
break;
}
}
}
function weather_get (url)
function weather_get (url, User_Location)
{
return new Promise(function(resolve, reject)
{
......@@ -318,7 +372,7 @@ function weather_get (url)
if(!error && response.statusCode == 200)
{
var weatherObj = JSON.parse(response.body);
resolve('4시간 안에 비가 올 확률은'+weatherObj.response.body.items.item[0].fcstValue+'%입니다.');
resolve(User_Info_Array[User_Location].UserRegion+'에 4시간 안에 비가 올 확률은 '+weatherObj.response.body.items.item[0].fcstValue+'% 입니다.');
}
else{
console.log('Error', error);
......