subject-two/entry/src/main/ets/utils/business/CentralHeartbeat.ets

45 lines
1.1 KiB
Plaintext
Raw Normal View History

// 中心心跳/发送消息
import { EnvironmentConfigurationType } from '../../model';
import UdpClient from '../UdpUtils';
class centralHeartbeat {
private centralHeartbeatUdp: UdpClient;
private timer: number = -1
constructor() {
}
// 初始化
init() {
let config: EnvironmentConfigurationType =
AppStorage.get<EnvironmentConfigurationType>("EnvironmentConfiguration")
this.centralHeartbeatUdp = new UdpClient();
this.centralHeartbeatUdp.create(config.udplocalIp, config.udplocalIpPort, config.udpOppositeIp,
config.udpOppositeIpPort);
}
// 接受中心远程指令
getData(callback: (data: ArrayBuffer) => void) {
this.centralHeartbeatUdp.onMessage((data: ArrayBuffer) => {
callback(data);
});
}
// 发送消息
sendData() {
// 组装消息,一秒发送一次
let data = "1";
this.timer = setInterval(() => {
this.centralHeartbeatUdp.sendMsg(data);
}, 1000);
}
// 关闭所有动作
close() {
clearInterval(this.timer);
this.centralHeartbeatUdp.close()
}
}
export const CentralHeartbeat = new centralHeartbeat();