Commit 25adfe3f authored by KangMin An's avatar KangMin An
Browse files

Update : 통합 데이터 경로 생성, 데이터 헤더 추가.

parent 09a1f37a
......@@ -12,6 +12,9 @@ const IN = "In";
const OUTSIDE = "Outside";
const USERS = "Users";
const OUT_DATA_HEADER = "date,temp,humi,press,wind_speed\n";
const IN_DATA_HEADER = "date,temp,humi,lights\n";
// 데이터 수집 기로 부터 받아온 지역 코드 세분화
const locCodeSep = (code = "") => {
const DO = code.slice(0, 2);
......@@ -39,10 +42,10 @@ const getTimeInfo = () => {
const time = {
year: year,
month: month,
date: date,
hour: hour,
minute: minute,
month: month < 10 ? `0${month}` : month,
date: date < 10 ? `0${date}` : date,
hour: hour < 10 ? `0${hour}` : hour,
minute: minute < 10 ? `0${minute}` : minute,
};
return time;
......@@ -51,8 +54,8 @@ const getTimeInfo = () => {
// Data 접근 경로 반환, DB에 존재 하지 않을 시 Update 동작
const getDataDIR = async (loc, time, type, id) => {
const year = time.year;
const month = time.month < 10 ? `0${time.month}` : time.month;
const date = time.date < 10 ? `0${time.date}` : time.date;
const month = time.month;
const date = time.date;
const select_query =
"SELECT DATALINK" +
......@@ -88,41 +91,62 @@ const getDataDIR = async (loc, time, type, id) => {
const timeDIR = `/${year}` + `/${year}${month}` + `/${year}${month}${date}`;
// 최종 Data 저장소 경로
const repoDIR = process.env.LOCAL_SERVER_DIR + baseDIR + timeDIR;
// 지역별 통합 데이터와 지역+날짜별 Data 저장소 경로
const dirs = {
base: process.env.LOCAL_SERVER_DIR + baseDIR,
time: timeDIR,
};
return repoDIR;
return dirs;
};
// 데이터를 파일 형식으로 저장
const storeData = (type, time, loc, fdir, data) => {
const storeData = (type, time, loc, dir, data) => {
const fileName = "weather.csv";
fs.open(fdir + `/${fileName}`, "a", (err, fd) => {
fs.open(dir + `/${fileName}`, "a", (err, fd) => {
if (err) {
// Directory가 존재하지 않는 경우, 생성
if (err.code === "ENOENT") {
console.log("There is no directory.");
fs.mkdirSync(fdir, { recursive: true }, (err) => {
fs.mkdirSync(dir, { recursive: true }, (err) => {
if (err) console.log(err);
});
// 디렉토리를 새로 생성하는 경우 header 추가.
const header = type == OUT ? OUT_DATA_HEADER : IN_DATA_HEADER;
fs.appendFile(dir + `/${fileName}`, header, (err) => {
if (err) console.log(err);
else console.log("Create directory and record header.");
});
console.log("Create directory.");
// 헤더 추가 후 데이터 추가
fs.appendFile(dir + `/${fileName}`, data, (err) => {
if (err) console.log(err);
else
console.log(
`${time.year}/${time.month}/${time.date} ${time.hour}:${
time.minute
} - ${loc.EMD} ${type === OUT ? OUTSIDE : USERS} data append.`
);
});
}
// 그 외의 에러는 출력
else console.log(err);
}
fs.appendFile(fdir + `/${fileName}`, data, (err) => {
if (err) console.log(err);
else
console.log(
`${time.year}/${time.month}/${time.date} ${time.hour}:${
time.minute
} - ${loc.EMD} ${type === OUT ? OUTSIDE : USERS} data append.`
);
});
// 디렉토리가 존재하는 경우 실행
else {
fs.appendFile(dir + `/${fileName}`, data, (err) => {
if (err) console.log(err);
else
console.log(
`${time.year}/${time.month}/${time.date} ${time.hour}:${
time.minute
} - ${loc.EMD} ${type === OUT ? OUTSIDE : USERS} data append.`
);
});
}
});
};
......@@ -142,11 +166,13 @@ const handleOutData = async (locCode, lat, lng) => {
const loc = locCodeSep(locCode);
const time = getTimeInfo();
const fdir = await getDataDIR(loc, time, OUT, OUTSIDE);
const dirs = await getDataDIR(loc, time, OUT, OUTSIDE);
const data = `${time.month},${time.date},${time.hour},${time.minute},${temp},${humi},${press},${wind_speed}\n`;
// 데이터 형식 - [ 연 월 일 시 분 | 온도 | 습도 | 기압 | 풍속 ]
const data = `${time.year}${time.month}${time.date}${time.hour}${time.minute},${temp},${humi},${press},${wind_speed}\n`;
storeData(OUT, time, loc, fdir, data);
storeData(OUT, time, loc, dirs.base, data);
storeData(OUT, time, loc, dirs.base + dirs.time, data);
};
// 내부 수집기로 부터 들어온 정보 처리
......@@ -154,11 +180,13 @@ const handleInData = async (id, locCode, temp, humi, lights) => {
const loc = locCodeSep(locCode);
const time = getTimeInfo();
const fdir = await getDataDIR(loc, time, IN, id);
// 데이터 형식 - [ 월 | 일 | 시 | 분 | 온도 | 습도 | 광도 ]
const data = `${time.month},${time.date},${time.hour},${time.minute},${temp},${humi},${lights}\n`;
const dirs = await getDataDIR(loc, time, IN, id);
// 데이터 형식 - [ 연 월 일 시 분 | 온도 | 습도 | 광도 ]
const data = `${time.year}${time.month}${time.date}${time.hour}${time.minute},${temp},${humi},${lights}\n`;
storeData(IN, time, loc, fdir, data);
storeData(IN, time, loc, dirs.base, data);
storeData(IN, time, loc, dirs.base + dirs.time, data);
};
// 데이터 수신 처리
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment