Skip to content

Commit

Permalink
link to flutter successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
womendoushihaoyin committed Jan 2, 2025
1 parent 98780c7 commit d7dbdf3
Show file tree
Hide file tree
Showing 65 changed files with 351,245 additions and 36,460 deletions.
2 changes: 1 addition & 1 deletion resources/web/flutter/flutter_bootstrap.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions resources/web/flutter/flutter_service_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const MANIFEST = 'flutter-app-manifest';
const TEMP = 'flutter-temp-cache';
const CACHE_NAME = 'flutter-app-cache';

const RESOURCES = {"flutter_bootstrap.js": "998855bfaa183a7addca2d581dfea973",
"version.json": "e1c4f22753735d91caf5a154114c1370",
"index.html": "1ad14fded796c3d5935f7323dd2d9976",
"/": "1ad14fded796c3d5935f7323dd2d9976",
"main.dart.js": "ef34130afee7ec66a7fde97543a045bd",
const RESOURCES = {"flutter_bootstrap.js": "9661b7d163f75f259274f23d209a1f8c",
"version.json": "01826e76d41ce77a190fd78b947729b7",
"index.html": "11e4d9885287432e3f92e1c88b0530b4",
"/": "11e4d9885287432e3f92e1c88b0530b4",
"main.dart.js": "afcd416e109487d890ac490ce41bdc01",
"flutter.js": "383e55f7f3cce5be08fcf1f3881f585c",
"favicon.png": "5dcef449791fa27946b3d35ad8803796",
"icons/Icon-192.png": "ac9a721a12bbc803b44f645561ecb1e1",
Expand Down
170 changes: 57 additions & 113 deletions resources/web/flutter/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,67 +37,6 @@

<script>

function connect(){
var header = {
seqid:10003,
};

var tSend = {
"header": header,
"payload": {
"cmd": "sw_Connect",
"params": {
"ip": "172.18.1.47",
"protocol": "moonraker",
},

},
};

window.wx.postMessage(JSON.stringify(tSend));
}

function test_connect(){
var header = {
seqid: 10001,
};

var tSend = {
"header": header,
"payload": {
"cmd": "sw_Test_connect",
"params": {
"ip": "172.18.1.90",
"protocol": "moonraker",
},

},
};

window.wx.postMessage(JSON.stringify(tSend));
}

function get_machine_state(){
var header = {
seqid: 10000,
};

var tSend = {
"header": header,
"payload": {
'cmd': 'sw_GetMachineState',
'params': {
items:[
"extruder",
],
}
},
};

window.wx.postMessage(JSON.stringify(tSend));

}

function getDeviceData() {
var devices = {
'90F3A2F30063C233': {
Expand Down Expand Up @@ -140,70 +79,75 @@
return devices;
}

var j_header;

// 接收来自于 Flutter 的消息
function onMessageFromFlutter(headerJson, payloadJson) {

function simulateResponse(jsonMessage) {
try {
// 反序列化 header 和 payload
const header = JSON.parse(headerJson);
const payload = JSON.parse(payloadJson);

j_header = header;

// console.log("Deserialized Header:", header);
// console.log("Deserialized Payload:", payload);
// console.log("Web receiveFromFlutter header: " + header.map + ", payload: ", payload.toString());
if (payload['cmd'] == 'sw_StartMachineFind') {
// 开始机器发现
var devices = getDeviceData(header, payload);
var tSend = {
"header": header,
"payload": {
'cmd': payload['cmd'],
'data': payload['data'],
},
};

var mokeData={
"header": header,
"payload": {
'code': 200,
'data': devices,
'msg': 'ok',
}
};
// console.log("Web sendToFlutter header: " + JSON.stringify(mokeData) );
// 延迟 2 秒后调用回调函数
setTimeout(() => {
// callback(JSON.stringify(mokeData));
// window.postMessage(JSON.stringify(mokeData), '*'); // 调用 window.postMessage 传递消息给 flutter
}, 2000);
}

window.wx.postMessage(JSON.stringify(tSend));
const data = JSON.parse(jsonMessage);
var header = data.header;
var payload = data.payload;

// 反序列化 header 和 payload
header['event_id'] = payload['event_id'];
header['cmd'] = payload['cmd']
// 开始机器发现
var devices = getDeviceData();
var mokeData={
"header": header,
"payload": {
'code': 200,
'data': devices,
'msg': 'ok',
}
};
console.log("simulateResponse message: " + JSON.stringify(mokeData) );
window.postMessage(JSON.stringify(mokeData), '*'); // 调用 window.postMessage 传递消息给 flutter
} catch (error) {
console.error("Error parsing JSON:", error);
}
}

function test(res) {
console.log(res);
window.postMessage(JSON.stringify(res), '*'); // 调用 window.postMessage 传递消息给 flutter
}
function ackResponse(jsonMessage) {
try {
// 反序列化 header 和 payload
const data = JSON.parse(jsonMessage);
var header = data.header;
var payload = data.payload;

// 发送消息给Flutter
window.sendMessageToFlutter = function (message) {
// console.log("window.sendMessageToFlutter: " + message);
window.postMessage(message, '*'); // 调用 window.postMessage 传递消息给 flutter
// 反序列化 header 和 payload
header['event_id'] = payload['event_id'];
header['cmd'] = payload['cmd']
// 开始机器发现
var devices = getDeviceData();
var mokeData={
"header": header,
"payload": {
'code': 200,
'data': null,
'msg': 'ok',
}
};
console.log("ackResponse message: " + JSON.stringify(mokeData) );
window.postMessage(JSON.stringify(mokeData), '*'); // 调用 window.postMessage 传递消息给 flutter
} catch (error) {
console.error("Error parsing JSON:", error);
}
}

window.sendMessage = function (message) {
console.log("I am proxy in html to send message to container: " + message);
//收到请求后,模拟收到响应结果
// ackResponse(message);
// 调用 window.postMessage 传递消息给 flutter
window.wx && window.wx.postMessage(message); // 调用 window.postMessage 传递消息给 原生系统

///模拟收到响应结果5000
if(!window.wx) {
console.log('i cant find window.wx, so i simulate a return by postMessage')
// setTimeout(simulateResponse(message), 5000)
}
}

window.addEventListener('message', function (event) {
console.log("testtesttesttesttesttesttesttesttesttesttesttesttesttesttest");
console.log(event.data);
});
</script>

</body>
Expand Down
Loading

0 comments on commit d7dbdf3

Please sign in to comment.