From cd450abd995d5648f57f40fe9dc2ad2cbd8570ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dhji=28=EC=A7=80=EB=8C=80=ED=95=9C=29?= Date: Tue, 27 Feb 2024 10:43:20 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20socket=20test=EC=9A=A9=20js=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- socket-test/pav-100-dron.js | 99 ++++++++++++++++++++ socket-test/pav-client.js | 47 ++++++++++ socket-test/pav-utils.js | 147 ++++++++++++++++++++++++++++++ socket-test/pav-warning copy 2.js | 89 ++++++++++++++++++ socket-test/pav-warning copy 3.js | 87 ++++++++++++++++++ socket-test/pav-warning copy 4.js | 87 ++++++++++++++++++ socket-test/pav-warning copy.js | 88 ++++++++++++++++++ socket-test/pav-warning.js | 87 ++++++++++++++++++ 8 files changed, 731 insertions(+) create mode 100644 socket-test/pav-100-dron.js create mode 100644 socket-test/pav-client.js create mode 100644 socket-test/pav-utils.js create mode 100644 socket-test/pav-warning copy 2.js create mode 100644 socket-test/pav-warning copy 3.js create mode 100644 socket-test/pav-warning copy 4.js create mode 100644 socket-test/pav-warning copy.js create mode 100644 socket-test/pav-warning.js diff --git a/socket-test/pav-100-dron.js b/socket-test/pav-100-dron.js new file mode 100644 index 0000000..a424ce3 --- /dev/null +++ b/socket-test/pav-100-dron.js @@ -0,0 +1,99 @@ +const { getConnection, writeData } = require('./pav-client'); +const { dumyData } = require('./pav-utils'); + +// const host = "192.168.0.24" +const host = "localhost" +// const host = '211.253.38.218'; +// const port = 8082; +const port = 8888; + +const prefix = 'TEST-DRON-'; +const severalDrones = cnt => { + // 클라이언트 정보 저장공간 + const clients = []; + + // 최초 좌표 + // y - y' : 37.57 -> 35.36 + // x - x' : 126.60 -> 128.94 + const firstCoord = [37.57, 126.6]; + const lastCoord = [35.36, 128.94]; + // 좌표 차이 계산 + const diffX = firstCoord[0] - lastCoord[0]; + const diffY = lastCoord[1] - firstCoord[1]; + // 구분점 + const divCnt = Math.round(Math.sqrt(cnt)); + const divDiifX = diffX / divCnt; + const divDiifY = diffY / divCnt; + + // 초기값 저장 + for (let i = 0; i < cnt; i++) { + const client = {}; + const suffix = i.toString().padStart(3, '0'); + const dronName = prefix + suffix; + // client + client.dronName = dronName; + client.socket = getConnection(dronName, host, port); + + const divX = i % divCnt; + const divY = Math.floor(i / divCnt); + const x = Number((firstCoord[0] - divX * divDiifX).toFixed(5)) || 0; + const y = Number((firstCoord[1] + divY * divDiifY).toFixed(5)) || 0; + + const initialData = { + ...dumyData, + terminalId: dronName, + body: [ + { + ...dumyData.body[0], + objectId: dronName, + lat: x, + lon: y + } + ] + }; + client.data = initialData; + + clients.push(client); + } + return clients; +}; +// +const clients = severalDrones(100); +const dist = 0.01; +const sendData = cnt => { + const direction = Math.floor(cnt / 10) % 4; + clients.forEach(item => { + switch (direction) { + case 1: + item.data.body[0].lat = Number( + (item.data.body[0].lat - dist).toFixed(5) + ); + break; + case 2: + item.data.body[0].lon = Number( + (item.data.body[0].lon - dist).toFixed(5) + ); + break; + case 3: + item.data.body[0].lat = Number( + (item.data.body[0].lat + dist).toFixed(5) + ); + break; + default: + item.data.body[0].lon = Number( + (item.data.body[0].lon + dist).toFixed(5) + ); + break; + } + + writeData(item.socket, JSON.stringify(item.data)); + }); + if (cnt >= 39) cnt = null; + iteratorSendData(++cnt); +}; + +const iteratorSendData = (cnt = 0) => { + setTimeout(() => sendData(cnt), 1000); +}; + +iteratorSendData(); diff --git a/socket-test/pav-client.js b/socket-test/pav-client.js new file mode 100644 index 0000000..9c93241 --- /dev/null +++ b/socket-test/pav-client.js @@ -0,0 +1,47 @@ +const net = require('net'); + + +const getConnection = (connName, host, port) => { + const client = net.connect({port: port, host: host}, function () { + console.log(connName + ' Connected: '); + console.log(' local = %s:%s', this.localAddress, this.localPort); + console.log(' remote = %s:%s', this.remoteAddress, this.remotePort); + this.setTimeout(1000); + this.setEncoding('utf8'); + this.on('data', function (data) { + console.log(connName + " From Server: " + data.toString()); + // this.end(); + }); + this.on('end', function () { + console.log(connName + ' Client disconnected'); + }); + this.on('error', function (err) { + console.log('Socket Error: ', err, JSON.stringify(err)); + }); + this.on('timeout', function () { + console.log('Socket Timed Out'); + }); + this.on('close', function (e) { + console.log('Socket Closed', e); + }); + }); + return client; +} + +const writeData = (socket, data) => { + const success = !!socket.write(data); + // if (success) { + // (function (socket, data) { + // socket.once('drain', function () { + // writeData(socket, data); + // }); + // })(socket, data); + // } +} + +// const Dwarves = getConnection("Dwarves"); +// writeData(Dwarves, JSON.stringify(sampleData)); + + + +module.exports = {getConnection, writeData} diff --git a/socket-test/pav-utils.js b/socket-test/pav-utils.js new file mode 100644 index 0000000..8112bef --- /dev/null +++ b/socket-test/pav-utils.js @@ -0,0 +1,147 @@ +// degree -> radian 변환 +const deg2rad = deg => { + return deg * (Math.PI / 180); +}; +const rad2deg = rad => { + return rad * (180 / Math.PI); +}; +// 좌표간 거리계산 +const getDistanceFromCoordByMeter = (coord1, coord2) => { + const R = 6371; // Radius of the earth in km + const dLat = deg2rad(coord2[0] - coord1[0]); // deg2rad below + const dLon = deg2rad(coord2[1] - coord1[1]); + const a = + Math.sin(dLat / 2) * Math.sin(dLat / 2) + + Math.cos(deg2rad(coord1[0])) * + Math.cos(deg2rad(coord2[0])) * + Math.sin(dLon / 2) * + Math.sin(dLon / 2); + const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); + // var d = R * c; // Distance in km + const d = R * c * 1000; // Distance in m + return d; +}; +// 두 좌표사이 특정 거리만큼의 좌표 추출 +const getCoordsFormBetweenCoord = (coord1, coord2, dist) => { + // console.log('init ===> ', {coord1, coord2, dist}) + const r = []; + // 처음 좌표 + r.push(coord1); + // 중간 좌표 + const totalDist = getDistanceFromCoordByMeter(coord1, coord2); + const dx = coord2[0] - coord1[0]; + const singX = dx < 0 ? -1 : 1; + const dy = coord2[1] - coord1[1]; + const singY = dy < 0 ? -1 : 1; + const m = dy / dx; + const rad = Math.atan(m); + + const currentTotalDist = Math.sqrt(dx ** 2 + dy ** 2); + const currentDivDist = (dist * currentTotalDist) / totalDist; + const div = Math.floor(currentTotalDist / currentDivDist); + + for (let i = 1; i <= div; i++) { + const currentDist = currentDivDist * i; + let x = coord1[0]; + let y = coord1[1]; + // var heading = Math.atan2(dy, dx) * 180 / Math.PI; + // if (heading < 0) { + // heading += 360; + // } + if (dx === 0) { + y += currentDist * singY; + } else if (dy === 0) { + x += currentDist * singX; + } else { + const angle = Math.atan2(dy, dx); + x += Math.cos(angle) * currentDist; + y += Math.sin(angle) * currentDist; + } + const heading = calculateHeading( + r[r.length - 1][0], + r[r.length - 1][1], + x, + y + ); + r.push([x, y, heading]); + } + // 마지막 좌표 + r.push(coord2); + return r; +}; + +function calculateHeading(x1, y1, x2, y2) { + var dx = x2 - x1; + var dy = y2 - y1; + var heading = (Math.atan2(dy, dx) * 180) / Math.PI; + + // Ensure heading is within the range [0, 360) + heading = (heading + 360) % 360; + + return heading; +} + +const dumyData = { + authKey: '1cc2e08e-0c5c-43b2-8d4d-cddd3de558e3', + terminalId: 'SANDBOX-01', + command: 'SANDBOX', + body: [ + { + objectId: 'JAL497', + lat: 37.52097, + lon: 126.6146, + elevType: 'M', + elev: 125.4, + speedType: 'm/s', + speed: 40.5, + heading: 0, + terminalRcvDt: '20210629173145', + betteryLevel: 91, + betteryVoltage: 50, + dronStatus: 'TAKEOFF', + moveDistance: 120, + moveDistanceType: 'M', + sensorCo: 100.11, + sensorSo2: 200.22, + sensorNo2: 300.33, + sensorO3: 400.44, + sensorDust: 500.55 + } + ] +}; + +module.exports = { + getDistanceFromCoordByMeter, + deg2rad, + getCoordsFormBetweenCoord, + dumyData +}; + +const test = () => { + const coord1 = [37.521, 126.6082]; + // const coord2 = [37.5210, 126.6057]; // x 절편 + const coord2 = [37.52, 126.6082]; // y 절편 + // const coord2 = [37.5220, 126.6057]; // 일반 + const r = getCoordsFormBetweenCoord(coord1, coord2, 10); + console.log(r); + for (let i = 1; i < r.length; i++) { + const dist = getDistanceFromCoordByMeter(r[i - 1], r[i]); + console.log(dist); + } +}; + +// test(); + +const test2 = () => { + let m = 10.5 / 14; + console.log(3 / 4, 13.5 / 18, (13.5 - 3) / (18 - 4)); + let rad = Math.atan(m); + let tan = Math.tan(rad); + let cos = Math.cos(rad); + let sin = Math.sin(rad); + console.log({ rad, tan, cos, sin }); + let x = Math.cos(rad) * 15; + let y = Math.sin(rad) * 15; + console.log({ x, y }); +}; +// test2(); diff --git a/socket-test/pav-warning copy 2.js b/socket-test/pav-warning copy 2.js new file mode 100644 index 0000000..6373665 --- /dev/null +++ b/socket-test/pav-warning copy 2.js @@ -0,0 +1,89 @@ +const { getConnection, writeData } = require('./pav-client'); +const { getCoordsFormBetweenCoord, dumyData } = require('./pav-utils'); + +// const host = '192.168.0.30'; +// const host = "localhost" +const host = '211.253.38.218'; +const port = 8082; + +// 기본정보 +const prefix = 'PALUTM-'; + +const pathSampleCoord = [ + [37.3450207, 126.6296542], + [37.2521693, 126.3218456], + [37.237675, 126.1280807], + [37.2521693, 126.3218456], + [37.3450207, 126.6296542] +]; +// const pathSampleCoord = [ +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655] +// ]; +const divDist = 10; // [m] + +// 특정거리에 따른 좌표 추출 +const getCoords = coords => { + if (!coords || coords.length < 1) return; + const totalCoords = []; + for (let j = 1; j < 500; j++) { + for (let i = 1; i < coords.length; i++) { + const divCoords = getCoordsFormBetweenCoord( + coords[i - 1], + coords[i], + divDist + ); + + totalCoords.push(...divCoords); + } + } + return totalCoords; +}; + +const getClient = () => { + const dronName = prefix + '002'; + + const client = {}; + client.dronName = dronName; + client.socket = getConnection(dronName, host, port); + client.data = getCoords(pathSampleCoord); + return client; +}; + +const sendData = (client, cnt) => { + const data = { + ...dumyData, + terminalId: client.dronName, + body: [ + { + ...dumyData.body[0], + objectId: client.dronName, + lat: client.data[cnt][0], + lon: client.data[cnt][1], + heading: client.data[cnt][2] + } + ] + }; + writeData(client.socket, JSON.stringify(data)); + iteratorSendData(++cnt); +}; + +const client = getClient(); + +const iteratorSendData = (cnt = 0) => { + if (cnt >= client.data.length) { + client.socket.destroy(); + return; + } + setTimeout(() => sendData(client, cnt), 1000); +}; + +iteratorSendData(); diff --git a/socket-test/pav-warning copy 3.js b/socket-test/pav-warning copy 3.js new file mode 100644 index 0000000..60ab342 --- /dev/null +++ b/socket-test/pav-warning copy 3.js @@ -0,0 +1,87 @@ +const { getConnection, writeData } = require('./pav-client'); +const { getCoordsFormBetweenCoord, dumyData } = require('./pav-utils'); + +// const host = '192.168.0.30'; +// const host = "localhost" +const host = '211.253.38.218'; +const port = 8082; + +// 기본정보 +const prefix = 'PALUTM-'; + +const pathSampleCoord = [ + [37.3450207, 126.6296542], + [37.521245, 126.6107763], + [37.4891845, 126.5513135], + [37.3450207, 126.6296542] +]; +// const pathSampleCoord = [ +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655] +// ]; +const divDist = 10; // [m] + +// 특정거리에 따른 좌표 추출 +const getCoords = coords => { + if (!coords || coords.length < 1) return; + const totalCoords = []; + for (let j = 1; j < 500; j++) { + for (let i = 1; i < coords.length; i++) { + const divCoords = getCoordsFormBetweenCoord( + coords[i - 1], + coords[i], + divDist + ); + totalCoords.push(...divCoords); + } + } + return totalCoords; +}; + +const getClient = () => { + const dronName = prefix + '003'; + + const client = {}; + client.dronName = dronName; + client.socket = getConnection(dronName, host, port); + client.data = getCoords(pathSampleCoord); + return client; +}; + +const sendData = (client, cnt) => { + const data = { + ...dumyData, + terminalId: client.dronName, + body: [ + { + ...dumyData.body[0], + objectId: client.dronName, + lat: client.data[cnt][0], + lon: client.data[cnt][1], + heading: client.data[cnt][2] + } + ] + }; + writeData(client.socket, JSON.stringify(data)); + iteratorSendData(++cnt); +}; + +const client = getClient(); + +const iteratorSendData = (cnt = 0) => { + if (cnt >= client.data.length) { + client.socket.destroy(); + return; + } + setTimeout(() => sendData(client, cnt), 1000); +}; + +iteratorSendData(); diff --git a/socket-test/pav-warning copy 4.js b/socket-test/pav-warning copy 4.js new file mode 100644 index 0000000..4e04354 --- /dev/null +++ b/socket-test/pav-warning copy 4.js @@ -0,0 +1,87 @@ +const { getConnection, writeData } = require('./pav-client'); +const { getCoordsFormBetweenCoord, dumyData } = require('./pav-utils'); + +// const host = '192.168.0.30'; +// const host = "localhost" +const host = '211.253.38.218'; +const port = 8082; + +// 기본정보 +const prefix = 'PALUTM-'; + +const pathSampleCoord = [ + [37.3450207, 126.6296542], + [37.521245, 126.6107763], + [37.4891845, 126.5513135], + [37.3450207, 126.6296542] +]; +// const pathSampleCoord = [ +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655] +// ]; +const divDist = 10; // [m] + +// 특정거리에 따른 좌표 추출 +const getCoords = coords => { + if (!coords || coords.length < 1) return; + const totalCoords = []; + for (let j = 1; j < 500; j++) { + for (let i = 1; i < coords.length; i++) { + const divCoords = getCoordsFormBetweenCoord( + coords[i - 1], + coords[i], + divDist + ); + totalCoords.push(...divCoords); + } + } + return totalCoords; +}; + +const getClient = () => { + const dronName = prefix + '004'; + + const client = {}; + client.dronName = dronName; + client.socket = getConnection(dronName, host, port); + client.data = getCoords(pathSampleCoord); + return client; +}; + +const sendData = (client, cnt) => { + const data = { + ...dumyData, + terminalId: client.dronName, + body: [ + { + ...dumyData.body[0], + objectId: client.dronName, + lat: client.data[cnt][0], + lon: client.data[cnt][1], + heading: client.data[cnt][2] + } + ] + }; + writeData(client.socket, JSON.stringify(data)); + iteratorSendData(++cnt); +}; + +const client = getClient(); + +const iteratorSendData = (cnt = 0) => { + if (cnt >= client.data.length) { + client.socket.destroy(); + return; + } + setTimeout(() => sendData(client, cnt), 1000); +}; + +iteratorSendData(); diff --git a/socket-test/pav-warning copy.js b/socket-test/pav-warning copy.js new file mode 100644 index 0000000..c3fd60f --- /dev/null +++ b/socket-test/pav-warning copy.js @@ -0,0 +1,88 @@ +const { getConnection, writeData } = require('./pav-client'); +const { getCoordsFormBetweenCoord, dumyData } = require('./pav-utils'); + +// const host = '192.168.0.30'; +// const host = "localhost" +const host = '211.253.38.218'; +const port = 8082; + +// 기본정보 +const prefix = 'PALUTM-'; + +const pathSampleCoord = [ + [37.237675, 126.1280807], + [37.2521693, 126.3218456], + [37.3450207, 126.6296542], + [37.2521693, 126.3218456], + [37.237675, 126.1280807] +]; +// const pathSampleCoord = [ +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655], +// [42.5515, 126.655] +// ]; +const divDist = 10; // [m] + +// 특정거리에 따른 좌표 추출 +const getCoords = coords => { + if (!coords || coords.length < 1) return; + const totalCoords = []; + for (let j = 1; j < 500; j++) { + for (let i = 1; i < coords.length; i++) { + const divCoords = getCoordsFormBetweenCoord( + coords[i - 1], + coords[i], + divDist + ); + totalCoords.push(...divCoords); + } + } + return totalCoords; +}; + +const getClient = () => { + const dronName = prefix + '001'; + + const client = {}; + client.dronName = dronName; + client.socket = getConnection(dronName, host, port); + client.data = getCoords(pathSampleCoord); + return client; +}; + +const sendData = (client, cnt) => { + const data = { + ...dumyData, + terminalId: client.dronName, + body: [ + { + ...dumyData.body[0], + objectId: client.dronName, + lat: client.data[cnt][0], + lon: client.data[cnt][1], + heading: client.data[cnt][2] + } + ] + }; + writeData(client.socket, JSON.stringify(data)); + iteratorSendData(++cnt); +}; + +const client = getClient(); + +const iteratorSendData = (cnt = 0) => { + if (cnt >= client.data.length) { + client.socket.destroy(); + return; + } + setTimeout(() => sendData(client, cnt), 1000); +}; + +iteratorSendData(); diff --git a/socket-test/pav-warning.js b/socket-test/pav-warning.js new file mode 100644 index 0000000..11a9bf5 --- /dev/null +++ b/socket-test/pav-warning.js @@ -0,0 +1,87 @@ +const {getConnection, writeData} = require('./pav-client'); +const {getCoordsFormBetweenCoord, dumyData} = require('./pav-utils'); + +// const host = "192.168.0.24" +const host = 'localhost'; +// const host = '192.168.0.34'; +const port = 8003; +// const port = 8082; +// const host = '121.190.193.50'; +// const port = 6083; + +// 기본정보 +const prefix = 'PA0001'; +const terminalId = ''; + +const pathSampleCoord = [ + [37.5215, 126.605], + // [37.5215, 126.61], //비정상, 날릴 것 + [37.5215, 126.6082], //계획서 + [37.521, 126.6082], + [37.521, 126.6057], + [37.5205, 126.6057], + [37.5205, 126.6082], + [37.52, 126.6082], + [37.52, 126.6057], + [37.5195, 126.6057], + [37.5195, 126.6082], + [37.519, 126.6082], + [37.519, 126.605] +]; + +const divDist = 10; // [m] + +// 특정거리에 따른 좌표 추출 +const getCoords = coords => { + if (!coords || coords.length < 1) return; + const totalCoords = []; + for (let i = 1; i < coords.length; i++) { + const divCoords = getCoordsFormBetweenCoord( + coords[i - 1], + coords[i], + divDist + ); + totalCoords.push(...divCoords); + } + return totalCoords; +}; + +const getClient = () => { + const dronName = prefix + 'trmnlId'; + + const client = {}; + client.dronName = dronName; + client.socket = getConnection(dronName, host, port); + client.data = getCoords(pathSampleCoord); + return client; +}; + +const sendData = (client, cnt) => { + const data = { + ...dumyData, + terminalId: terminalId, + command: 'SANDBOX', + body: [ + { + ...dumyData.body[0], + objectId: client.dronName, + lat: client.data[cnt][0], + lon: client.data[cnt][1] + } + ] + }; + writeData(client.socket, JSON.stringify(data)); + iteratorSendData(++cnt); +}; + +const client = getClient(); + +const iteratorSendData = (cnt = 0) => { + if (cnt >= client.data.length) { + client.socket.destroy(); + return; + } + setTimeout(() => sendData(client, cnt), 5 * 1000); +}; + +iteratorSendData();