박권수

feat. mqtt and medicine db module

1 const mqtt = require('mqtt'); 1 const mqtt = require('mqtt');
2 2
3 -exports.mqttOn = async(hosting, topic) => { 3 +exports.mqttOn = (hosting) => {
4 const client = mqtt.connect(hosting); 4 const client = mqtt.connect(hosting);
5 + client.on('connect', () => {
6 + console.log('Connected : ', client.connected)
7 + });
8 +
9 + return client;
10 +}
11 +
12 +exports.mqttSubscribe = (client, topic) => {
5 client.subscribe(topic); 13 client.subscribe(topic);
14 + client.on('message', (topic, message, packet) => {
15 + console.log('topic : ', topic);
16 + console.log('message : ', message.toString());
17 + });
6 } 18 }
...\ No newline at end of file ...\ No newline at end of file
......
1 const axios = require('axios'); 1 const axios = require('axios');
2 +const Medicine = require('../models/medicine');
2 3
3 exports.updateMedicineInfo = async() => { 4 exports.updateMedicineInfo = async() => {
4 - console.log('starting')
5 -
6 const itemArray = await getItemsList(getQueryURL); 5 const itemArray = await getItemsList(getQueryURL);
7 await exportJsonData(itemArray); 6 await exportJsonData(itemArray);
8 7
9 - console.log('data is saved'); 8 + console.log('All of data is updated!');
10 } 9 }
11 10
12 //queryUrl을 return하는 함수 : 한 페이지에 100개의 item씩 요청할 수 있다. 11 //queryUrl을 return하는 함수 : 한 페이지에 100개의 item씩 요청할 수 있다.
...@@ -29,7 +28,7 @@ const getItemsList = async(queryUrl) => { ...@@ -29,7 +28,7 @@ const getItemsList = async(queryUrl) => {
29 getItem = await axios.get(queryUrl(i)); 28 getItem = await axios.get(queryUrl(i));
30 items = getItem.data.body.items; 29 items = getItem.data.body.items;
31 30
32 - if(items === undefined) 31 + if(items === undefined)
33 return result; 32 return result;
34 33
35 result.push(...items); 34 result.push(...items);
...@@ -38,9 +37,23 @@ const getItemsList = async(queryUrl) => { ...@@ -38,9 +37,23 @@ const getItemsList = async(queryUrl) => {
38 } 37 }
39 } 38 }
40 39
41 -//itemArray에 있는 모든 data를 json으로 만들어서 json파일로 저장 40 +//itemArray에 있는 모든 data를 MongoDB의 SMB collections에 저장함
42 const exportJsonData = async(itemList) => { 41 const exportJsonData = async(itemList) => {
43 itemList.forEach(item => { 42 itemList.forEach(item => {
44 - 43 + const medicine = new Medicine({
44 + medicineId : item.itemSeq,
45 + name : item.itemName,
46 + company : item.entpName,
47 + target : item.efcyQesitm,
48 + dosage : item.useMethodQesitm,
49 + warn : item.atpnWarnQesitm + '\n\n' + item.atpnQesitm,
50 + antiEffect : item.seQesitm
51 + })
52 +
53 + Medicine.findOneAndUpdate({
54 + medicineId : medicine.medicineId
55 + }, medicine, {
56 + upsert : true
57 + })
45 }) 58 })
46 } 59 }
...\ No newline at end of file ...\ No newline at end of file
......