import prompt from '@ohos.prompt';
import wifi from '@ohos.wifi';
import socket from '@ohos.net.socket';
import display from '@ohos.display';
var promise ;
export default {
data: {
title: "",
x:150,
y:100,
forward_x:150,
msg:"forward",
forward_image:"Button_normal",
TGA:"YZJ",
command:"1",
local_ip:"",
remote_ip:"",
speed_mode:1,
speed:10,
tcp:socket.constructTCPSocketInstance(),
pre_cmd:'0',
cur_cmd:'0'
},
onInit() {
this.title = this.$t('strings.world');
this.getIpAddress();
this.creatScoket();
},
send_cmd(cmd){
if(cmd!=this.cur_cmd){
this.cur_cmd=cmd;
this.sendMessage(cmd);
}
},
onMoveStart(e){
console.info("开始移动"+JSON.stringify(e));
},
toSpeed_mode(){
if(this.speed_mode==0)
this.speed_mode=1;
else if(this.speed_mode==1)
this.speed_mode=0;
},
onMove(e){
//圆心是(100,250)
if(this.speed_mode==0){
console.info(JSON.stringify(e))
let nx=e.touches[0].globalY-50;
this.x=nx;
}
else if(this.speed_mode==1){
console.info(JSON.stringify(e))
let ny=e.touches[0].globalX-50;
this.y=ny;
if(ny>=110){
this.msg="trun_right"
console.info("YZJ:正在向右转")
this.command="4";
this.send_cmd(this.command);
}
else if(ny<=90){
this.msg="trun_left"
console.info("YZJ:正在向做左转")
this.command="3";
this.send_cmd(this.command);
}
}
},
onMoveEnd(){
this.x=150;
this.y=100;
this.msg="stop"
this.command='0';
this.send_cmd(this.command);
},
onForwardstart(e){
this.forward_image="Button_active";
this.forward_x=e.touches[0].globalY-50
},
onForward(e){
if( e.touches[0].globalY-50<=140){
console.info("正在前进")
this.msg="forward"
this.command="1"
this.send_cmd(this.command);
if(e.touches[0].globalY-50<100){
this.forward_x=100
}
else
this.forward_x=e.touches[0].globalY-50
}
else if(e.touches[0].globalY-50>165){
console.info("正在后退")
this.msg="backoff"
this.command="2"
this.send_cmd(this.command);
if(e.touches[0].globalY-50>200){
this.forward_x=200
}
else
this.forward_x=e.touches[0].globalY-50
}
},
onForwardend(){
this.forward_x=150;
console.info("停止前进")
this.msg="stop"
this.forward_image="Button_normal"
this.command="0"
this.send_cmd(this.command);
},
//创建udpSocket 默认端口10006
creatScoket: async function(){
this.tcp.bind({address: this.local_ip, port: 8888,family: 1}, err => {
if (err) {
console.log('YZJ---bind fail');
return;
}
console.log('YZJ---bind success');
})
//监听收到的信息 打印到屏幕上
this.tcp.on('message', value => {
let buffer = value.message;
let dataView = new DataView(buffer);
let str = "";
for (let i = 0;i < dataView.byteLength; ++i) {
str += String.fromCharCode(dataView.getUint8(i))
}
this.title =str;
});
},
sendMessage: async function(cmd){
//发送信息
// let promise1 = this.tcp.connect({ address: {address: this.remote_ip, port: 10006, family: 1} , timeout: 6000});
let promise2 = this.tcp.send({
data:cmd
});
promise2.then(() => {
console.log('YZJ---send success');
}).catch(err => {
console.log('YZJ---send fail');
});
},
onConnect: async function(){
promise = this.tcp.connect({ address: {address: "192.168.1.1", port: 8888, family: 1} , timeout: 6000});
promise.then(() => {
prompt.showToast({
message: "连接成功!"
})
console.log('YZJ---connect success');
this.tcp.setExtraOptions({
keepAlive: true,
OOBInline: true,
TCPNoDelay: true,
socketLinger: { on:true, linger:10 },
receiveBufferSize: 1000,
sendBufferSize: 1000,
reuseAddress: true,
socketTimeout: 3000,
},err => {
if (err) {
console.log('YZJ---setExtraOptions fail');
return;
}
console.log('YZJ---setExtraOptions success');
});
}).catch(err => {
console.log('YZJ---connect fail');
prompt.showToast({
message: "连接失败!"
})
});
},
onDisconnect(){
this.tcp.close()
prompt.showToast({
message: "断开链接!"
})
},
onDestroy(){
this.tcp.close()
prompt.showToast({
message: "断开链接!"
})
},
//获取本机ip地址
getIpAddress(){
let ip=wifi.getIpInfo().ipAddress;
this.local_ip = (ip >> 24 & 0xFF)+"."+ ((ip >> 16) & 0xFF)+"."+((ip >> 8) & 0xFF)+"."+(ip & 0xFF);
},
get_remote_ip(e){
this.remote_ip=e.value
}
}