const https = require('https');
function main() {
const accessToken = 'Bearer <접근토큰>';
// 현재 시간을 yyyyMMddHHmmss 형식으로 생성
const now = new Date();
const currentDttm = now.getFullYear().toString() +
(now.getMonth() + 1).toString().padStart(2, '0') +
now.getDate().toString().padStart(2, '0') +
now.getHours().toString().padStart(2, '0') +
now.getMinutes().toString().padStart(2, '0') +
now.getSeconds().toString().padStart(2, '0');
const data = {
"mecId": "RSU00001",
"dttm": currentDttm,
"roadWeather": {
"weatherData": {
"ta": -3.1,
"hm": 17.1,
"pa": 1025.7,
"ws": 10.1,
"wd": 0.1,
"cpsd": 0,
"rn": 0.1,
"statCd": "24"
},
"visibilityData": {
"vs": 11,
"at": -3,
"statCd": "00"
},
"roadSurfaceData": {
"ts": -3.1,
"ft": -24.6,
"wfh": 0.1,
"ilt": 0.1,
"sd": 0.3,
"ip": 17.1,
"st": 3,
"f": 0.5,
"statCd": "00"
}
}
};
const apiURL = 'https://service.mqnicrnd5.com/api/v1/rw/collect/prw';
const headers = {
'Authorization': accessToken,
'Content-Type': 'application/json'
};
post(apiURL, headers, data)
.then(responseBody => {
console.log(`[currentDttm: ${currentDttm}] ${responseBody}`);
})
.catch(error => {
console.error(`[currentDttm: ${currentDttm}] Error:`, error);
});
}
function post(apiURL, headers, data) {
return new Promise((resolve, reject) => {
const jsonData = JSON.stringify(data);
const url = new URL(apiURL);
const options = {
hostname: url.hostname,
port: url.port || 443,
path: url.pathname,
method: 'POST',
headers: {
...headers,
'Content-Length': Buffer.byteLength(jsonData)
}
};
const req = https.request(options, response => {
let responseData = '';
response.on('data', chunk => {
responseData += chunk;
});
response.on('end', () => {
resolve(responseData);
});
});
req.on('error', error => {
reject(error);
});
req.write(jsonData);
req.end();
});
}
main();