Browse Source

init pro
Signed-off-by: Caner

Caner 3 years ago
parent
commit
8b6103c40a
6 changed files with 249 additions and 1 deletions
  1. 7 1
      README.md
  2. 43 0
      contrl.js
  3. 71 0
      index.js
  4. 19 0
      package.json
  5. 104 0
      web/index.html
  6. 5 0
      web/socket.io.min.js

+ 7 - 1
README.md

@@ -1,3 +1,9 @@
 # Contrl_For_Server
 
-控制端
+### 不在使用 python
+
+### 同样摇杆控制
+
+### 视频采用 webrtc 传输
+
+### 通信服务 express,视频服务 socket,建立在控制端以减少车端视频解析压力

+ 43 - 0
contrl.js

@@ -0,0 +1,43 @@
+// PCF8591 数模转换芯片
+const i2cBus = require("i2c-bus")
+const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
+class ContrlService {
+    constructor() {
+        this.BUS = i2cBus.openSync(1)
+        // 寄存器地址
+        this.ADR = 0x48
+        // 电位器地址->对应模块in1,in2
+        this.P1 = 0x41
+        this.P2 = 0x42
+        this.P3 = 0x43
+        this.P4 = 0x44
+    }
+
+    async run() {
+        // 数模初始值
+        const data = this.Orientation()
+        process.send(data);
+        await sleep(0.03)
+        this.run()
+    }
+
+    // 获取值
+    async Orientation() {
+        this.BUS.write_byte(this.ADR, this.P1)
+        const v0 = this.BUS.read_byte(this.ADR)
+        await sleep(0.02)
+        this.BUS.write_byte(this.ADR, this.P2)
+        const v1 = this.BUS.read_byte(this.ADR)
+        await sleep(0.02)
+        this.BUS.write_byte(this.ADR, this.P3)
+        const v2 = this.BUS.read_byte(this.ADR)
+        await sleep(0.02)
+        this.BUS.write_byte(this.ADR, this.P4)
+        const v3 = this.BUS.read_byte(this.ADR)
+        // 接入socket 发送数据
+        db = `{"type":"conctrl","conctrl":{"v0":${v0},"v1":${v1},"v2":${v2},"v3":${v3}}}`
+        return db
+    }
+
+}
+new ContrlService()

+ 71 - 0
index.js

@@ -0,0 +1,71 @@
+// socket + http
+const { createServer } = require("http");
+const express = require("express");
+const { Server } = require("socket.io");
+const { fork } = require('child_process');
+class VideoServer {
+  constructor() {
+    this.Contrl = null
+    this.socket = null
+    this.initSoket();
+  }
+  // socket 服务
+  initSoket() {
+    const app = express();
+    app.use(express.static("./web"));
+    const httpServer = createServer(app);
+    const io = new Server(httpServer, {
+      cors: {
+        origin: ["*"],
+      },
+    });
+    // 中间件
+    io.use((socket, next) => {
+      const { roomID, name } = socket.handshake.auth;
+      if (roomID !== "feiCar" && (name !== "car" || name !== "ctrl"))
+        return next(new Error("invalid token"));
+      socket.join(roomID);
+      socket.broadcast.to(roomID).emit('joined', { name: name, roomID: roomID })
+      next();
+    });
+    io.on("connection", (socket) => {
+      const { roomID, name } = socket.handshake.auth;
+      console.log(name, "连接");
+
+      // 监听其它信息
+      socket.on("msg", (data) => {
+        // 转发消息
+        socket.broadcast.to(roomID).emit("msg", data);
+      });
+
+      // 监听离开
+      socket.on("disconnect", () => {
+        // 转发离开
+        socket.broadcast.to(roomID).emit("leaved", {
+          name: name,
+          roomID: roomID
+        });
+        socket.leave(roomID)
+        this.socket = null
+        console.log(name, '断开连接');
+      })
+
+      // 控制服务
+      this.socket = socket
+    });
+    httpServer.listen(7896);
+    console.log("服务开启成功7896");
+    this.initContrl()
+  }
+  // 控制服务
+  initContrl() {
+    this.Contrl = fork('./contrl.js')
+    this.Contrl.on('message', (msg) => {
+      if (this.socket && this.socket.connected) {
+        console.log('用户连接可发送', msg);
+      }
+      console.log('parent get message: ' + msg);
+    })
+  }
+}
+new VideoServer();

+ 19 - 0
package.json

@@ -0,0 +1,19 @@
+{
+  "name": "car",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "directories": {
+    "lib": "lib"
+  },
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "",
+  "license": "ISC",
+  "dependencies": {
+    "express": "^4.17.3",
+    "socket.io": "^4.4.1",
+    "i2c-bus": "^5.2.2"
+  }
+}

+ 104 - 0
web/index.html

@@ -0,0 +1,104 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+    <meta charset="UTF-8" />
+    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
+    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+    <title>测试</title>
+    <script src="./socket.io.min.js"></script>
+    <style>
+        video {
+            width: 500px;
+            height: 400px;
+            background: none;
+            object-fit: fill;
+            border: solid 1px red;
+        }
+    </style>
+</head>
+
+<body>
+    <div>
+        <video id="v2" autoplay playsinline></video>
+    </div>
+    <!-- webrtc -->
+    <script>
+        let RTC = null
+        const socket = io({
+            auth: {
+                roomID: "feiCar",
+                name: "ctrl",
+            },
+            transports: ["websocket"],
+        });
+        socket.on("connect", async () => {
+            const PeerConnection =
+                window.RTCPeerConnection ||
+                window.mozRTCPeerConnection ||
+                window.webkitRTCPeerConnection;
+            RTC = new PeerConnection();
+
+            RTC.ontrack = (event) => {
+                const remoteVideo = document.getElementById("v2");
+                remoteVideo.srcObject = event.streams[0];
+                console.log("远程流|dom", event, remoteVideo);
+
+            };
+            // 监听ICE|发送
+            RTC.onicecandidate = (event) => {
+                if (event.candidate) {
+                    socket.emit("msg", {
+                        type: "candidate",
+                        candidate: event.candidate,
+                    });
+                    console.log("己方ICE", event.candidate);
+                }
+            };
+            // 监听ICE状态
+            RTC.oniceconnectionstatechange = () => {
+                console.log("ICE状态", RTC.iceConnectionState);
+            };
+            console.log("连接成功监听webrtc");
+        });
+        //   发送offer及anwser
+        socket.on("msg", async (data) => {
+            console.log("用户信息", data);
+            if (data.type === 'offer') {
+                // 设置本地应答
+                RTC.setRemoteDescription(data.offer);
+                // 返回应答
+                const answer = await RTC.createAnswer();
+                RTC.setLocalDescription(answer);
+                // 发送anwser
+                socket.emit("msg", {
+                    type: "answer",
+                    answer: answer,
+                });
+            } else if (data.type === 'candidate') {
+                // 添加ICE
+                RTC.addIceCandidate(data.candidate);
+            }
+        });
+        //   监听加入
+        socket.on('joined', user => {
+            console.log(`${user.name}加入${user.roomID}房间`)
+            // 向其它用户发送开始webrtc
+            socket.emit('msg', {
+                type: 'startRTC'
+            })
+        })
+        // 监听离开
+        socket.on('leaved', user => {
+            console.log(`${user.name}离开${user.roomID}房间`)
+            RTC.close()
+        })
+        //   断开连接
+        socket.on("connect_error", (err) => {
+            console.log("连接错误", err);
+            if (RTC) RTC.close()
+        });
+    </script>
+</body>
+
+</html>

File diff suppressed because it is too large
+ 5 - 0
web/socket.io.min.js


Some files were not shown because too many files changed in this diff