Commit f18d4cee authored by martin hou's avatar martin hou

feat: 增加休眠检测

parent 3705029a
...@@ -142,6 +142,7 @@ declare class Jensen { ...@@ -142,6 +142,7 @@ declare class Jensen {
onconnect: () => void; onconnect: () => void;
// 事件:当连接异常时触发 // 事件:当连接异常时触发
onerror: (e: Error) => void; onerror: (e: Error) => void;
onwakeup: () => void;
// 主动断开连接(单连接时适用) // 主动断开连接(单连接时适用)
disconnect: () => void; disconnect: () => void;
// 事件:当连接断开时触发 // 事件:当连接断开时触发
......
...@@ -14,6 +14,9 @@ export function Home() { ...@@ -14,6 +14,9 @@ export function Home() {
jensen.connect(); jensen.connect();
jensen.onconnect = () => { jensen.onconnect = () => {
console.log('connect successfully'); console.log('connect successfully');
jensen.onwakeup = function() {
console.log('从休眠中恢复过来了?');
}
jensen.getDeviceInfo().then((info) => { jensen.getDeviceInfo().then((info) => {
console.log('getDeviceInfo', info); console.log('getDeviceInfo', info);
alert(info.sn + ' connected'); alert(info.sn + ' connected');
......
...@@ -102,6 +102,10 @@ function Jensen(log, conn) { ...@@ -102,6 +102,10 @@ function Jensen(log, conn) {
this.onconnect = null; this.onconnect = null;
this.onreceive = null; this.onreceive = null;
this.onerror = null; this.onerror = null;
this.onwakeup = null;
this.sleepChecker = null;
this.lastTimerInvoked = new Date().getTime();
const RECV_BUFF_SIZE = 512000; const RECV_BUFF_SIZE = 512000;
...@@ -168,6 +172,16 @@ function Jensen(log, conn) { ...@@ -168,6 +172,16 @@ function Jensen(log, conn) {
} catch (err) { } catch (err) {
Logger.error('jensen', 'setup', err); Logger.error('jensen', 'setup', err);
} }
if (self.sleepChecker) window.clearInterval(self.sleepChecker);
self.sleepChecker = setInterval(function() {
let now = new Date().getTime();
if (now - self.lastTimerInvoked > 6000)
{
if (typeof(self.onwakeup) == 'function') self.onwakeup();
}
self.lastTimerInvoked = now;
}, 5000);
console.log('休眠检测准备完毕');
}; };
this.initialize = async function() { this.initialize = async function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment