From: snickersun Date: Mon, 20 Jan 2020 09:58:09 +0000 (+0800) Subject: 中间版本 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=9a2c991e2743d890859577a98e69bcb8ccae7e48;p=icsupport.git 中间版本 --- diff --git a/app/src/main/java/com/ic/protocol/service/HeartRateManager.java b/app/src/main/java/com/ic/protocol/service/HeartRateManager.java new file mode 100644 index 00000000..62bb1250 --- /dev/null +++ b/app/src/main/java/com/ic/protocol/service/HeartRateManager.java @@ -0,0 +1,206 @@ +package com.ic.protocol.service; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.SystemClock; + +import com.eiot.watch_lib.LauncherCtrl; +import com.ic.protocol.adapter.protocolAPIHelper; +import com.ic.protocol.bean.Runtime; +import com.ic.protocol.bean.StringMessage; +import com.ic.protocol.service.base.AbstractConnectionService; +import com.ic.protocol.utils.AlarmWakeUp; +import com.ic.protocol.utils.Connection; +import com.ic.protocol.utils.Logger; +import com.ic.protocol.utils.NettyConnection; +import com.ic.protocol.utils.NetworkObservable; + +import rx.Subscription; +import rx.functions.Action1; + +import static com.eiot.watch_lib.LauncherCtrl.STATUS_BAR_STATE_SERVER_CONNECTED_OFF; +import static com.eiot.watch_lib.LauncherCtrl.STATUS_BAR_STATE_SERVER_CONNECTED_ON; + +public class HeartRateManager extends AbstractConnectionService { + private static final long HEARTBEAT_TIMEOUT = 660000L; + private static String TAG = HeartRateManager.class.getSimpleName(); + public static boolean connectionError = false; + + public static boolean isConnected = false; + + private static boolean isInSleepMode = false; + + private static int mFlag = 30; + + Connection connection; + + private int currentReConnectInterval = 11; + + private boolean firstReConnected = true; + + private long idleTimeMillis; + + private int isFirstAsjust; + + private int lastReConnectInterval = -1; + + private int lastReConnectionNumber = -1; + + Context mContext; + + NetworkObservable mNetworkObservable = new NetworkObservable(); + + Runtime mRuntime; + + private ShutDownReceiver mShutDownReceiver; + + private Subscription mSubscription; + + private int reConnectionNumber; + + private int reCycleTimes; + + private int timeout = 660; + + private int totalAdjustTimes; + + private int whichTimeAdjust; + + static { + + } + + + public void onAlarmClock(Connection paramConnection, Runtime paramRuntime) { + + Logger.i("onAlarmClock"); + + long l = SystemClock.elapsedRealtime(); + + //如果 没有连接成功 或者 时间超过了11分钟 ,则进行重新连接服务 + if (!isConnected || l - this.idleTimeMillis > 660000L) { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("重连"); + stringBuilder.append(isConnected); + Logger.i(stringBuilder.toString()); + if (!isConnected && this.firstReConnected) { + this.reConnectionNumber++; + this.firstReConnected = false; + } +// if (paramRuntime.getLockSimInfo()) { +// paramConnection.disconnect(); +// return; +// } + + } + } + + public void onAttach(final Connection connection, final Runtime runtime) { + this.mRuntime = runtime; + this.mContext = (runtime.getContextProxy()).context; + this.mNetworkObservable.register(this.mContext); + this.connection = connection; + this.mSubscription = this.mNetworkObservable.onChange().subscribe(new Action1() { + private boolean first; + + public void call(Integer param1Integer) { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("yls aBoolean="); + stringBuilder.append(param1Integer); + stringBuilder.append(",,first="); + stringBuilder.append(this.first); + Logger.i(stringBuilder.toString()); + if (param1Integer.intValue() > 0) { + if (this.first) { + this.first = false; + return; + } + connection.connect(runtime.getSrvIP(), runtime.getPort()); + } + } + }); + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction("android.intent.action.ACTION_SHUTDOWN_MOREINFO"); + this.mShutDownReceiver = new ShutDownReceiver(); + this.mContext.registerReceiver(this.mShutDownReceiver, intentFilter); + } + + public boolean onConnectionClose(Connection paramConnection, Runtime paramRuntime) { + if (isConnected) protocolMainServer.getInstance().startConnectionRetryTimer(); + connectionError = true; + isConnected = false; + paramRuntime.getContextProxy().sendBroadcast(isConnected); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("onConnectionClose="); + stringBuilder.append(",set KeepAlive isConnected = false"); + Logger.i(stringBuilder.toString()); + LauncherCtrl.getInstance(this.mContext).setStatusBarIcon("server_connected", STATUS_BAR_STATE_SERVER_CONNECTED_OFF); + return false; + } + + public boolean onConnectionOpen(Connection paramConnection, Runtime paramRuntime) { + + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("onConnectionOpen connectionError="); + stringBuilder.append(connectionError); + stringBuilder.append(",sInSleepMode ="); + stringBuilder.append(isInSleepMode); + stringBuilder.append(",set KeepAlive isConnected = true"); + Logger.i(stringBuilder.toString()); +// if (!connectionError) { + connectionError = false; +// connection.send(protocolAPIHelper.genStringMessage("AP00",protocolAPIHelper.AP00(paramRuntime))); + connection.send(protocolAPIHelper.genStringMessage("heart",protocolAPIHelper.AP03(paramRuntime))); +// if (Deliver.haveSendH42) { +// ServiceUtil.offerQueue(new StringMessage(protocolAPIHelper.AP03(paramRuntime))); +// Deliver.haveSendH42 = false; +// } +// } + + isConnected = true; + this.firstReConnected = true; + this.idleTimeMillis = SystemClock.elapsedRealtime(); + paramRuntime.getContextProxy().sendBroadcast(isConnected); + + LauncherCtrl.getInstance(this.mContext).setStatusBarIcon("server_connected", STATUS_BAR_STATE_SERVER_CONNECTED_ON); + return false; + } + + public void onDetach(Connection paramConnection, Runtime paramRuntime) { + if (this.mSubscription != null && !this.mSubscription.isUnsubscribed()) + this.mSubscription.unsubscribe(); + this.mNetworkObservable.unregister(this.mContext); + this.mContext.unregisterReceiver(this.mShutDownReceiver); + } + + public void onHeartAlarm(NettyConnection paramNettyConnection, Runtime paramRuntime) { + Logger.i("onHeartAlarm"); +// UpdateHeartBeatTimeout(); +// if (this.reCycleTimes == 0 && this.currentReConnectInterval != 11){ +// paramNettyConnection.send(protocolAPIHelper.genStringMessage("AP00",protocolAPIHelper.AP00(paramRuntime))); + paramNettyConnection.send(protocolAPIHelper.genStringMessage("heart",protocolAPIHelper.AP03(paramRuntime))); + // } + + } + + public boolean onReceive(StringMessage paramStringMessage, Connection paramConnection, Runtime paramRuntime) { + this.idleTimeMillis = SystemClock.elapsedRealtime(); + Logger.i(TAG+paramStringMessage.cmd()); + + + return false; + } + + + class ShutDownReceiver extends BroadcastReceiver { + public void onReceive(Context param1Context, Intent param1Intent) { + boolean bool = param1Intent.getBooleanExtra("reboot", false); + if (HeartRateManager.this.mRuntime.getBatteryLow() && !HeartRateManager.this.mRuntime.getBatterychanging() && !bool && !HeartRateManager.this.mRuntime.isShutdown()){ +// KeepAlive.this.connection.send(new StringMessage(protocolAPIHelper.AP00(KeepAlive.this.mRuntime))); + } + + } + } +}