diff --git a/entry/src/main/ets/config/global.ets b/entry/src/main/ets/config/global.ets index 1202dfa..23ad160 100644 --- a/entry/src/main/ets/config/global.ets +++ b/entry/src/main/ets/config/global.ets @@ -12,7 +12,7 @@ export const GlobalConfig: GlobalConfigType = { commonFileWriteAddress: '/mnt/hmdfs/100/account/device_view/local/files/duolun', picSavePath: '/storage/cloud/100/files/Photo/', videoSavePath: '/storage/cloud/100/files/Videos/', - host: 'http://172.37.55.192:8082', + host: 'http://192.168.32.105:8089', modelNo: "3", version: { //杭州 diff --git a/entry/src/main/ets/config/judge.ets b/entry/src/main/ets/config/judge.ets index 907424a..96aeea8 100644 --- a/entry/src/main/ets/config/judge.ets +++ b/entry/src/main/ets/config/judge.ets @@ -10,9 +10,9 @@ import { JudgeConfigType } from '../model' //考试回放开关 -export const JudgeConfig:JudgeConfigType = { +export const JudgeConfig: JudgeConfigType = { //本地目录开关 - isTrajectoryOpen: true, + isTrajectoryOpen: false, //是否开启拍照 isPhotoOpen: true, //扣分语音是否强制开启 diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 3c55ff2..71cf1d0 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -20,6 +20,7 @@ import CardComponent from './Index/Card'; import BottomMessageComponent from './Index/BottomMessage'; import LoadingComponent from './Index/Loading'; import Prompt from '@system.prompt'; +import { DifferentialSignal } from '../utils/business/DifferentialSignal'; @Entry @@ -64,7 +65,9 @@ struct Index { this.ratio = AppStorage.get('baseInfo')?.ratio || 1.4 this.angle = 0 AppStorage.set('lsh', '1111111111111') + JudgeEmitterInstance.init() + GetSyncData("MA_SYSSET").then((res: MASYSSETTableType[]) => { res.forEach((element) => { if (element.v_no === "305") { @@ -83,6 +86,12 @@ struct Index { AppStorage.setOrCreate('singlePlay', false) this.num = 0 AppStorage.setOrCreate('lsh', '1111111111111') + DifferentialSignal.init(); + DifferentialSignal.sendData() + DifferentialSignal.getData((data: ArrayBuffer) => { + console.log("Received differential signal data:", data.byteLength, "bytes") + + }) } // 联网考试逻辑处理 diff --git a/entry/src/main/ets/pages/TerminalInfos.ets b/entry/src/main/ets/pages/TerminalInfos.ets index 9ae562f..3f843cb 100644 --- a/entry/src/main/ets/pages/TerminalInfos.ets +++ b/entry/src/main/ets/pages/TerminalInfos.ets @@ -96,7 +96,7 @@ struct Index { this.fileUtil.addFile(`${folderPath}/ipConfig.txt`, JSON.stringify(param)) AppStorage.setOrCreate("EnvironmentConfiguration", param) const host = `http://${param.centerIp}:${param.centerPort}` - console.log("中心host",host) + console.log("中心host", host) AppStorage.setOrCreate("host", host) ethernet.setIfaceConfig("eth0", { mode: ethernet.IPSetMode.STATIC, diff --git a/entry/src/main/ets/utils/TcpUtils.ets b/entry/src/main/ets/utils/TcpUtils.ets index ba4dc98..a1e091e 100644 --- a/entry/src/main/ets/utils/TcpUtils.ets +++ b/entry/src/main/ets/utils/TcpUtils.ets @@ -10,7 +10,7 @@ export default class TcpClient { private oppositeIp: string = '' private oppositeIpPort: string = '' private tcpSendNum: number = 0 - private tcp: socket.TCPSocket = null + private tcp: socket.TCPSocket = socket.constructTCPSocketInstance() private events: Array = [] constructor() { @@ -20,59 +20,102 @@ export default class TcpClient { return TcpClient.instance } - init(tcpLocalIp: string, tcpLocalIpPort: string, tcpOppositeIp: string, tcpOppositePort: string) { + // 初始化tcp连接 + async init(tcpLocalIp: string, tcpLocalIpPort: string, tcpOppositeIp: string, tcpOppositePort: string) { this.localIp = tcpLocalIp this.oppositeIp = tcpOppositeIp this.localIpPort = tcpLocalIpPort this.oppositeIpPort = tcpOppositePort console.log(TCPTag, 'new Tcp', this.localIp, this.localIpPort, this.oppositeIp, this.oppositeIpPort) this.tcp = socket.constructTCPSocketInstance(); - this.bindTcp() + await this.bindTcp() + await this.connectTcp() } - bindTcp(): Promise { - return this.tcp.bind({ - address: this.localIp, port: parseInt(this.localIpPort), family: 1 - }).then(() => { - return this.tcp.connect({ - address: { - address: this.oppositeIp, port: parseInt(this.oppositeIpPort) - }, - timeout: 6000 + // 绑定tcp + bindTcp(): Promise { + return new Promise((resolve, reject) => { + this.tcp.bind({ + address: this.localIp, + port: Number(this.localIpPort), + family: 1 + }).then(() => { + console.log(TCPTag, 'bindTcp success:', this.localIp, this.localIpPort, this.oppositeIp, this.oppositeIpPort) + resolve(true) + }).catch((err: BusinessError) => { + console.log(TCPTag, 'bindTcp error:', JSON.stringify(err), this.localIp, this.localIpPort, this.oppositeIp, this.oppositeIpPort) + reject(err) }) - }).then(() => { - try { - this.tcp.on("message", value => { - let data = new DataView(value.message) - this.events.forEach(cb => { - cb(value.message.slice(5, data.byteLength)) - }) - }) - return Promise.resolve() - } catch (e) { - return Promise.reject(e) - } }) } + // 连接tcp + connectTcp(): Promise { + return new Promise((resolve, reject) => { + this.tcp.connect({ + address: { + address: this.oppositeIp, port: Number(this.oppositeIpPort), family: 1 + }, timeout: 1000 * 15 + }) + .then(() => { + this.getMessage() + console.log(TCPTag, "tcp connect success") + return this.tcp.setExtraOptions({ + keepAlive: true + }) + }) + .then(() => { + resolve(true) + }) + .catch((err: BusinessError) => { + console.log(TCPTag, "tcp connect or keepAlive error: ", JSON.stringify(err)) + console.log(TCPTag, "tcp 重启服务") + reject(err) + }) + }) + } + + getMessage() { + this.tcp.on("message", value => { + let data = new DataView(value.message) + this.events.forEach(cb => { + cb(value.message.slice(5, data.byteLength)) + }) + }) + } + + // 重新绑定tcp async reBind() { await this.close() this.tcp = socket.constructTCPSocketInstance(); await this.bindTcp() + await this.connectTcp() } + // 监听tcp错误 + onError(callback: Function) { + this.tcp.on('error', err => { + console.log(TCPTag, 'tcp on error: ', JSON.stringify(err)) + callback?.() + }); + } + + // 关闭tcp连接 close(): Promise { return this.tcp?.close() } + // 监听tcp消息 onMsg(callback: Function) { this.events.push(callback) } + // 接收tcp消息 sendMsg(data: string): Promise { return this.tcp?.send({ data }).catch(async (err: BusinessError) => { + console.log(TCPTag, 'sendMsg error:', JSON.stringify(err)) this.tcpSendNum++ if (this.tcpSendNum > 10) { this.tcpSendNum = 0 @@ -82,6 +125,7 @@ export default class TcpClient { }) } + // 取消监听tcp消息 offMsg(callback: Function) { this.events = this.events.filter(cb => cb !== callback) } diff --git a/entry/src/main/ets/utils/business/DifferentialSignal.ets b/entry/src/main/ets/utils/business/DifferentialSignal.ets index 64cfedd..965e73e 100644 --- a/entry/src/main/ets/utils/business/DifferentialSignal.ets +++ b/entry/src/main/ets/utils/business/DifferentialSignal.ets @@ -1,9 +1,10 @@ //差分信号 +import { TCPTag } from '../../config'; import { EnvironmentConfigurationType } from '../../model'; import TcpClient from '../TcpUtils'; class differentialSignal { - private differentialSignalTcp: TcpClient; + private differentialSignalTcp: TcpClient = new TcpClient() private timer: number = -1 constructor() { @@ -12,9 +13,19 @@ class differentialSignal { init() { this.differentialSignalTcp = new TcpClient(); let config: EnvironmentConfigurationType = - AppStorage.get("EnvironmentConfiguration") - this.differentialSignalTcp.init(config.tcplocalIp, config.tcplocalIpPort, config.tcpOppositeIp, - config.tcpOppositePort); + AppStorage.get("EnvironmentConfiguration") || { + tcplocalIp: "", + tcplocalIpPort: "", + tcpOppositeIp: "", + tcpOppositePort: "" + } + console.log(TCPTag, "初始化", JSON.stringify(config)) + if (config.tcplocalIp || config.tcplocalIpPort || config.tcpOppositeIp || config.tcpOppositePort) { + this.differentialSignalTcp.init(config.tcplocalIp || "", config.tcplocalIpPort || "", config.tcpOppositeIp || "", + config.tcpOppositePort || ""); + } else { + console.log(TCPTag, "未配置差分信号TCP信息,请在环境配置中设置") + } } // 发送消息 @@ -30,6 +41,7 @@ class differentialSignal { // 获取消息 getData(callback: (data: ArrayBuffer) => void) { this.differentialSignalTcp.onMsg((data: ArrayBuffer) => { + console.log(TCPTag, "获取", data); callback(data); }); }