123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- import bus from "./bus";
- import { Message } from "element-ui";
- export default class wsService {
- constructor() {
- // const wsUrl = ref("ws://172.16.50.3/websocket");
- // this.address = "ws://172.16.50.3/websocket";
- this.address = "ws://106.14.237.165/websocket";
- this.promisePool = {};
- this._websocket = undefined;
- this.wsClosed = false;
- this.latestLog = "";
- this.latestMessage = "";
- }
- open() {
- return new Promise((resolve, reject) => {
- console.log(this.address);
- if (typeof this._websocket === "undefined") {
- this._websocket = new WebSocket(this.address);
- this._websocket.onopen = e => {
- this.wsClosed = false;
- resolve({ e, ws: this });
- };
- this._websocket.onerror = e => {
- reject(e);
- };
- }
- this._websocket.onclose = e => {
- reject(e);
- this.wsClosed = true;
- // bus.emit("websocket-onclose", e);
- };
- this._websocket.onmessage = res => {
- if (res.data) {
- let msg = JSON.parse(res.data);
- let [token, action, status, bench_id, data] = [
- msg.msg_id,
- msg.method,
- msg.status,
- msg.bench_id,
- msg.data
- ];
- // console.log(action != "play_audio");
- // if (action != "get_image" && action != "play_audio" && action != "collect_audio") {
- // console.log("onmessage", action);
- // }
- // handle message subscribtion
- if (action == "notify_im") {
- if (status === "WS_OK") {
- bus.emit("newMessage", data);
- }
- } else if (action == "notify_bench_status") {
- // console.log('notify_bench_status', data);
- // handle log subscribtion
- if (status === "WS_OK") {
- bus.emit("newLog", JSON.parse(data));
- }
- } else {
- const req = this.promisePool[token];
- if (status === "WS_OK") {
- if (action == "collect_audio") {
- bus.emit("sendAudio" + bench_id, { data, bench_id });
- // bus.emit("sendAudio", { data, bench_id });
- }
- if (this.promisePool[token]) {
- let benchId = this.promisePool[token][
- "bench_id"
- ];
- req.resolve({ data: data, benchId });
- delete this.promisePool[token];
- }
- } else {
- let err = { action, data: JSON.stringify(data) };
- console.log(err);
- console.log(err.data);
- Message({
- message: err.data,
- // message: "",
- type: "error",
- });
- // req.reject(err);
- }
- }
- }
- };
- });
- }
- close() {
- this._websocket.close();
- }
- send(method, params) {
- if (!method) {
- console.error("no method assigned");
- } else {
- let bench_id = params.bench_id;
- let msg_id = this.genMsgId();
- let msgObj = {
- msg_id: msg_id,
- method: method,
- ...params
- };
- let msg = JSON.stringify(msgObj);
- // if (!(method == "get_image" || method == "play_audio")) {
- // console.log(`< ${method} > ${msg}`);
- // }
- return new Promise((resolve, reject) => {
- this.promisePool[msg_id] = {
- bench_id,
- resolve,
- reject
- };
- if (this._websocket.readyState == 1) {
- // if (!this.wsClosed) {
- this._websocket.send(msg);
- }
- // setTimeout(() => {
- // console.log('this._websocket.readyState2', this._websocket.readyState);
- // }, 100)
- });
- }
- }
- genMsgId() {
- let rand = Math.random()
- .toString(36)
- .substr(2);
- return "request_" + rand;
- }
- connectBench(connectParam) {
- let res = this.send("connect_bench", connectParam);
- return res;
- }
- disconnectBench(connectParam) {
- let res = this.send("disconnect_bench", connectParam);
- return res;
- }
- getImage(id, screenType) {
- return this.send("get_image", {
- bench_id: id,
- screen_type: screenType
- });
- }
- singleFingerTouch(id, x, y, screenType = 1, debug = false) {
- return this.send("single_finger_touch", {
- bench_id:id,
- x: x,
- y: y,
- screen_type: screenType,
- debug: debug
- });
- }
- singleFingerDrag(id, x1, y1, x2, y2, screenType = 1, debug = false) {
- return this.send("single_finger_drag", {
- bench_id: id,
- x1: x1,
- y1: y1,
- x2: x2,
- y2: y2,
- screen_type: screenType,
- debug: debug
- });
- }
- singleFingerLongTouch(id, x, y, duration, screenType = 1, debug = false) {
- return this.send("single_finger_long_touch", {
- bench_id: id,
- x: x,
- y: y,
- duration: duration,
- screen_type: screenType,
- debug: debug
- });
- }
- subscribeBenchStatus(id) {
- return this.send("subscribe_bench_status", id);
- }
- unsubscribeBenchStatus(id) {
- return this.send("unsubscribe_bench_status", id);
- }
- planExecute(id) {
- return this.send("plan-execute", {
- bench_id: id,
- });
- }
- play_audio(data) {
- return this.send("play_audio", {
- bench_id: "",
- bench_ids: data.benchIds,
- data: data.data
- });
- }
- stop_play_audio(data) {
- return this.send("stop_play_audio", {
- bench_id: data.benchId,
- });
- }
- collect_audio(data) {
- return this.send("collect_audio", {
- bench_id: data.benchId,
- });
- }
- stop_collect_audio(data) {
- return this.send("stop_collect_audio", {
- bench_id: data.benchId,
- });
- }
- press_ptt(data) {
- return this.send("press_ptt", {
- bench_id: data.benchId,
- });
- }
- connect_audio_device(data) {
- return this.send("connect_audio_device", {
- bench_id: data.benchId,
- });
- }
- }
|