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