최현영

Highway congest function added

...@@ -54,7 +54,7 @@ router.route('/process/mapcctv').get(function(req, res){ ...@@ -54,7 +54,7 @@ router.route('/process/mapcctv').get(function(req, res){
54 54
55 55
56 56
57 -function jsonforecast(callback){ 57 +function jsonforecast(callback){ //교통예보
58 var url2 = 'http://data.ex.co.kr/openapi/safeDriving/forecast'; 58 var url2 = 'http://data.ex.co.kr/openapi/safeDriving/forecast';
59 var queryParams2 = '?' + encodeURIComponent('key') + '=4365330273'; /* Service Key*/ 59 var queryParams2 = '?' + encodeURIComponent('key') + '=4365330273'; /* Service Key*/
60 queryParams2 += '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json'); /* */ 60 queryParams2 += '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json'); /* */
...@@ -74,6 +74,25 @@ function jsonforecast(callback){ ...@@ -74,6 +74,25 @@ function jsonforecast(callback){
74 74
75 }; 75 };
76 76
77 +function jsoncongest(callback){ //교통정체
78 + var url1 = 'http://data.ex.co.kr/openapi/odtraffic/trafficAmountByCongest';
79 + var queryParams1 = '?' + encodeURIComponent('key') + '=4365330273'; /* Service Key*/
80 + queryParams1 += '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json'); /* */
81 +
82 + request({
83 + url: url1 + queryParams1,
84 + method: 'GET'
85 + }, function (error, response, body) {
86 + console.log('Status', response.statusCode);
87 + console.log('Headers', JSON.stringify(response.headers));
88 + console.log('Reponse received', body);
89 + var obj = JSON.parse(body);
90 + console.log(obj);
91 + callback(obj);
92 + });
93 +}
94 +
95 +
77 96
78 97
79 app.use('/', router); 98 app.use('/', router);
...@@ -94,7 +113,11 @@ function firstforecast(isforecast){ ...@@ -94,7 +113,11 @@ function firstforecast(isforecast){
94 } 113 }
95 else if(isforecast == '2'){ 114 else if(isforecast == '2'){
96 return true; 115 return true;
97 - }else { 116 + }
117 + else if(isforecast == '3'){
118 + return true;
119 + }
120 + else {
98 return false; 121 return false;
99 } 122 }
100 } 123 }
...@@ -136,7 +159,14 @@ function isroadnumber(typetext){ ...@@ -136,7 +159,14 @@ function isroadnumber(typetext){
136 } 159 }
137 } 160 }
138 161
139 - 162 +function inputroadnumber(typetext){
163 + if(typetext.indexOf("번") >= 0){
164 + return true;
165 + }
166 + else{
167 + return false;
168 + }
169 +}
140 170
141 171
142 app.post('/hook', function (req, res) { 172 app.post('/hook', function (req, res) {
...@@ -147,6 +177,7 @@ app.post('/hook', function (req, res) { ...@@ -147,6 +177,7 @@ app.post('/hook', function (req, res) {
147 var isselecttypeforecast; 177 var isselecttypeforecast;
148 var isfindroad; 178 var isfindroad;
149 var istypeforecast; 179 var istypeforecast;
180 + var isinputnumber;
150 var eventObj = req.body.events[0]; 181 var eventObj = req.body.events[0];
151 var source = eventObj.source; 182 var source = eventObj.source;
152 var message = eventObj.message; 183 var message = eventObj.message;
...@@ -183,7 +214,7 @@ app.post('/hook', function (req, res) { ...@@ -183,7 +214,7 @@ app.post('/hook', function (req, res) {
183 } 214 }
184 else if (message.text == '3'){ 215 else if (message.text == '3'){
185 console.log('[request Order] ', message); 216 console.log('[request Order] ', message);
186 - //tieupselect(eventObj); 217 + tieupselect(eventObj);
187 } 218 }
188 } 219 }
189 220
...@@ -219,6 +250,12 @@ app.post('/hook', function (req, res) { ...@@ -219,6 +250,12 @@ app.post('/hook', function (req, res) {
219 } 250 }
220 } 251 }
221 252
253 + isinputnumber = inputroadnumber(message.text);
254 + if(isinputnumber == true){
255 + roadcongest(eventObj, message.text);
256 + }
257 +
258 +
222 res.sendStatus(200); 259 res.sendStatus(200);
223 }); 260 });
224 261
...@@ -433,9 +470,6 @@ function busforecasttoS(eventObj){ ...@@ -433,9 +470,6 @@ function busforecasttoS(eventObj){
433 470
434 471
435 function busforecasttoG(eventObj){ 472 function busforecasttoG(eventObj){
436 -
437 -
438 -
439 jsonforecast(function(object){ 473 jsonforecast(function(object){
440 var obj = object; 474 var obj = object;
441 var jsonForforecast = obj; 475 var jsonForforecast = obj;
...@@ -607,22 +641,91 @@ function roadnumber(eventObj){ ...@@ -607,22 +641,91 @@ function roadnumber(eventObj){
607 }); 641 });
608 } 642 }
609 643
610 -// var url1 = 'http://data.ex.co.kr/openapi/odtraffic/trafficAmountByCongest'; 644 +function tieupselect(eventObj){
611 -// var queryParams1 = '?' + encodeURIComponent('key') + '=4365330273'; /* Service Key*/ 645 + request.post(
612 -// queryParams1 += '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json'); /* */ 646 + {
647 + url: TARGET_URL,
648 + headers: {
649 + 'Authorization': `Bearer ${TOKEN}`
650 + },
651 + json: {
652 + "replyToken":eventObj.replyToken,
653 + "messages":[
654 + {
655 + "type":"text",
656 + "text":"실시간 고속도로 정체상황 안내를 시작합니다."
657 + },
658 + {
659 + "type":"text",
660 + "text":"궁금한 노선번호를 입력해 주세요."
661 + },
662 + {
663 + "type":"text",
664 + "text":"예) 100번"
665 + },
666 + {
667 + "type":"text",
668 + "text":"반드시 노선번호(숫자)뒤에 '번'을 꼭 입력해주세요."
669 + }
670 + ]
671 + }
672 + },(error, response, body) => {
673 + console.log(body)
674 + });
675 +}
613 676
614 -// request({ 677 +function roadcongest(eventObj, msg ){
615 -// url: url1 + queryParams1, 678 + //jsoncongest의 routeNo는 앞의 세자리가 노선 번호, 마지막 숫자가 제1 중부내륙, 제2 중부내륙 처럼 노선번호는 같으나 노선의 경로가 다른 경우를 나타내며, 혹은 구간구간 개통된 고속도로를 구분지을 때도 구별용으로 사용됨으로 필요가 없음.
616 -// method: 'GET' 679 + jsoncongest(function(object){
617 -// }, function (error, response, body) { 680 + var obj = object;
618 -// console.log('Status', response.statusCode); 681 + var jsonForcongest = obj;
619 -// console.log('Headers', JSON.stringify(response.headers)); 682 + var number = msg.replace('번', '').trim();
620 -// console.log('Reponse received', body); 683 + console.log(number);
621 -// var obj = JSON.parse(body); 684 + var roadnumberstring = "";
622 -// console.log(obj); 685 + var count = 0;
623 -// console.log(obj.list[0]) 686 + if(number.length == 1){ //노선번호가 한자수일 경우, 노선을 찾을 때 두자리 혹은 세자리 번호와 겹칠 수 있으므로.
624 -// }); 687 + number = "00" + number;
688 + }
689 + else if (number.length == 2){
690 + number = "0" + number;
691 + }
625 692
693 + for(var i = 0; i < jsonForcongest.list.length; i++){
694 + if(jsonForcongest.list[i].routeNo.indexOf(number) >= 0){
695 + count += 1;
696 + roadnumberstring += ("- 노선이름: " + jsonForcongest.list[i].routeName + ", 정체구간: " + jsonForcongest.list[i].conzoneName + ', 기점종점방향: ' + jsonForcongest.list[i].updownTypeCode + ', 교통량: ' + jsonForcongest.list[i].trafficAmout + ', 평균속도: ' + jsonForcongest.list[i].speed + " ");
697 + }
698 + }
699 +
700 + if(count == 0){
701 + roadnumberstring = "검색하신 노선에서 발견된 정체구간은 없습니다.";
702 + }
703 + request.post(
704 + {
705 + url: TARGET_URL,
706 + headers: {
707 + 'Authorization': `Bearer ${TOKEN}`
708 + },
709 + json: {
710 + "replyToken":eventObj.replyToken,
711 + "messages":[
712 + {
713 + "type":"text",
714 + "text": msg + ' 정체상황은 다음과 같습니다.'
715 + },
716 + {
717 + "type":"text",
718 + "text":roadnumberstring
719 + }
720 +
721 + ]
722 + }
723 + },(error, response, body) => {
724 + console.log(body)
725 + });
726 +
727 + });
728 +}
626 729
627 730
628 731
......