InitActivity.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. package com.wdkl.callingbed.ui;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.graphics.drawable.AnimationDrawable;
  7. import android.net.ConnectivityManager;
  8. import android.net.NetworkInfo;
  9. import android.os.Handler;
  10. import android.os.Message;
  11. import android.util.Log;
  12. import android.view.View;
  13. import android.widget.ImageView;
  14. import com.google.gson.Gson;
  15. import com.wdkl.callingbed.MyApplication;
  16. import com.wdkl.callingbed.R;
  17. import com.wdkl.callingbed.base.BaseActivity;
  18. import com.wdkl.callingbed.common.Constants;
  19. import com.wdkl.callingbed.entity.InitDataEntity;
  20. import com.wdkl.callingbed.entity.MessageEvent;
  21. import com.wdkl.callingbed.util.AutoRebootUtil;
  22. import com.wdkl.callingbed.util.CutSerialPortUtil;
  23. import com.wdkl.callingbed.util.DateUtil;
  24. import com.wdkl.callingbed.util.DownloadUtil;
  25. import com.wdkl.callingbed.util.LogUtil;
  26. import com.wdkl.callingbed.util.NetUtil;
  27. import com.wdkl.callingbed.util.ScreenManagerUtil;
  28. import com.wdkl.callingbed.util.StringUtils;
  29. import com.wdkl.callingbed.util.ToastUtil;
  30. import com.wdkl.callingbed.util.UdpSendUtil;
  31. import com.wdkl.callingbed.util.sendcommand.CallingBedSendCommand;
  32. import com.zhy.http.okhttp.OkHttpUtils;
  33. import com.zhy.http.okhttp.callback.StringCallback;
  34. import org.greenrobot.eventbus.EventBus;
  35. import org.greenrobot.eventbus.Subscribe;
  36. import org.greenrobot.eventbus.ThreadMode;
  37. import org.json.JSONObject;
  38. import butterknife.Bind;
  39. import butterknife.OnClick;
  40. import okhttp3.Call;
  41. import serialporttest.utils.SerialPortUtil;
  42. import static com.wdkl.callingbed.util.AutoRebootUtil.ethreboot;
  43. import static com.wdkl.callingbed.util.sendcommand.CallingBedSendCommand.closeHeart;
  44. import static com.wdkl.callingbed.util.sendcommand.CallingBedSendCommand.setNurseBrightness;
  45. /**
  46. * 类名称:InitActivity <br>
  47. * 类描述:APP初始化页面<br>
  48. * 创建人:Waderson <br>
  49. * 创建时间:2018-02-01 <br>
  50. *
  51. * @version V1.0
  52. */
  53. public class InitActivity extends BaseActivity implements SerialPortUtil.ISerialPortBedOnclickEvent, SerialPortUtil.ISerialPortBedOnclickString {
  54. private String TAG = InitActivity.class.getSimpleName();
  55. @Bind(R.id.activity_init_layout_rl)
  56. View initView;
  57. @Bind(R.id.activity_init_layout_iv_loading)
  58. ImageView ivLoading;
  59. AnimationDrawable animationDrawable;
  60. ConnectivityManager cm;
  61. /**
  62. * mac地址
  63. */
  64. String macAddress = "";
  65. SerialPortUtil serialPortUtil;
  66. private int countsReboot;//重启计数
  67. @Override
  68. public int getLayoutId() {
  69. return R.layout.activity_init_layout;
  70. }
  71. @Override
  72. protected void initView() {
  73. ivLoading.setVisibility(View.VISIBLE);
  74. animationDrawable = (AnimationDrawable) ivLoading.getBackground();
  75. animationDrawable.start();
  76. }
  77. @Override
  78. protected void initUtil() {
  79. serialPortUtil = MyApplication.serialPortUtil;
  80. serialPortUtil.setOnDataReceiveListener(this);
  81. serialPortUtil.setOnDataReceiveStringListener(this);
  82. }
  83. @Override
  84. protected void initData() {
  85. //刚进来的时候将所有的护理灯全部灭掉
  86. dismissNurseBrightness();
  87. //20190415 waderson
  88. ScreenManagerUtil.setScreenBrightness(context, 210);
  89. dateRefresh();
  90. }
  91. @Override
  92. public View getLoadingTargetView() {
  93. return initView;
  94. }
  95. @OnClick(R.id.activity_init_layout_rl)
  96. public void init(View view) {
  97. isNewWork();
  98. }
  99. /**
  100. * 获取网络请求地址头
  101. */
  102. private void getLocalWayAddress() {
  103. try {
  104. //Constants.URL = "http://" + NetUtil.getLocalElement(NetUtil.getLocalInetAddress().toString());
  105. Constants.URL = "http://" + NetUtil.getLocalElement(3);
  106. } catch (Exception e) {
  107. LogUtil.e("getLocalWayAddress", "Exception==" + e.toString());
  108. CallingBedSendCommand.setSipStatus(serialPortUtil, "0");
  109. showNetErrorView("MAC地址: " + macAddress + "\nError:请检查网络连接是否正常"
  110. + "\n" + AutoRebootUtil.getTextTip());
  111. e.printStackTrace();
  112. }
  113. }
  114. /**
  115. * 获取MAC地址
  116. */
  117. private void getMacAddress() {
  118. if (NetUtil.isethernetConnected(cm)) {
  119. macAddress = NetUtil.getMacAddress();
  120. if (null != macAddress) {
  121. SharedPreferences sharedPreferences = getSharedPreferences(Constants.MSG_SP, Context.MODE_PRIVATE); //私有数据
  122. SharedPreferences.Editor editor = sharedPreferences.edit();
  123. editor.putString("macAddress", macAddress);
  124. editor.commit();//提交修改
  125. }
  126. } else {
  127. SharedPreferences sharedPreferences = getSharedPreferences(Constants.MSG_SP, Context.MODE_PRIVATE); //私有数据
  128. macAddress = sharedPreferences.getString("macAddress", null);
  129. }
  130. LogUtil.d("getMacAddress", "macAddress==" + macAddress);
  131. Constants.MAC_ADDRESS = macAddress;
  132. }
  133. /**
  134. * 判断网络连接
  135. * 无网络显示错误页面
  136. */
  137. public void isNewWork() {
  138. if (cm != null) {
  139. //ethreboot();//wuyq add
  140. NetworkInfo networkInfo = cm.getActiveNetworkInfo();
  141. if (networkInfo != null && networkInfo.isConnected()) { //wuyq modify
  142. if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI || networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
  143. CallingBedSendCommand.setSipStatus(serialPortUtil, "2");
  144. getInitData();
  145. countsReboot = 0; //wuyq add
  146. } else {
  147. updateHandler.sendEmptyMessageDelayed(404, DATEREFRESH_DELAYMILLIS);
  148. showNetErrorView("MAC地址: " + macAddress + "\nError:本地网络或wifi连接错误");
  149. ToastUtil.showToast("本地网络或wifi连接错误");
  150. CallingBedSendCommand.setSipStatus(serialPortUtil, "0");
  151. }
  152. } else {
  153. //updateHandler.sendEmptyMessageDelayed(404, DATEREFRESH_DELAYMILLIS);//wuyq remove
  154. updateHandler.sendEmptyMessageDelayed(4444, DATEREFRESH_DELAYMILLIS);//wuyq add
  155. CallingBedSendCommand.setSipStatus(serialPortUtil, "0");
  156. showNetErrorView("MAC地址: " + macAddress + "\nError:无网络连接;以太网是否连接正常?");//网络信息初始化错误
  157. ToastUtil.showToast("无网络连接;以太网是否连接正常?");//网络信息初始化错误
  158. //autoReboot();//wuyq remove
  159. }
  160. } else {
  161. updateHandler.sendEmptyMessageDelayed(404, DATEREFRESH_DELAYMILLIS);
  162. showNetErrorView("MAC地址: " + macAddress + "\nError:ConnectivityManager初始化失败");
  163. CallingBedSendCommand.setSipStatus(serialPortUtil, "0");
  164. ToastUtil.showToast("ConnectivityManager初始化失败");
  165. }
  166. }
  167. /**
  168. * =================================================(网络不通)15次以上自动重启系统=====================================================
  169. */
  170. private void autoReboot() {
  171. if (countsReboot == 16) {
  172. AutoRebootUtil.calculate(this);
  173. countsReboot = 0;
  174. }
  175. countsReboot++;
  176. }
  177. /**
  178. * 检查APP更新版本
  179. *
  180. */
  181. private void appCheckUpdate() {
  182. if (!StringUtils.notEmpty(Constants.URL)) return;
  183. if (!StringUtils.notEmpty(Constants.PartID)) return;
  184. try {
  185. OkHttpUtils.post().url(Constants.URL + Constants.URL_END + Constants.APP_CHECK_UPDATE)
  186. .addParams("PartID", Constants.PartID)
  187. .build()
  188. .execute(new StringCallback() {
  189. @Override
  190. public void onError(Call call, Exception e, int id) {
  191. Constants.UPDATE_APP_FLAG = false;
  192. }
  193. @Override
  194. public void onResponse(String response, int id) {
  195. String data = response.substring(0, response.length() - 4);
  196. LogUtil.d("appCheckUpdate", "appCheckUpdate==" + data);
  197. try {
  198. JSONObject object = new JSONObject(data);
  199. if (object.getString("Code").equals("OK!")) {
  200. float APPVersion = StringUtils.parseFloat(StringUtils.deleteCharAt(object.getString("APPVersion"), 0));
  201. float APPVersion_Now = StringUtils.parseFloat(StringUtils.getAppVersionName(InitActivity.this));
  202. Log.e(TAG,"appCheckUpdate() 服务器返回版本 "+APPVersion+" 设备当前版本 "+APPVersion_Now);
  203. String downloadURL = object.getString("downloadURL");
  204. if (APPVersion_Now != APPVersion) {//本来是“<”的;但有个别机器老是不升级成功
  205. if (StringUtils.notEmpty(downloadURL)) {
  206. closeHeart();//关闭心跳
  207. DownloadUtil.APP_VERSION = APPVersion;
  208. Intent intent = new Intent(InitActivity.this, APPUpdateActivity.class);
  209. intent.putExtra("downLoadURL", Constants.URL + Constants.URL_END + "/" + downloadURL);
  210. startActivity(intent);
  211. }
  212. }
  213. }
  214. } catch (Exception e) {
  215. e.printStackTrace();
  216. Constants.UPDATE_APP_FLAG = false;
  217. }
  218. }
  219. });
  220. } catch (Exception e) {
  221. Constants.UPDATE_APP_FLAG = false;
  222. e.printStackTrace();
  223. }
  224. }
  225. /**
  226. * 获取初始化信息
  227. */
  228. private void getInitData() {
  229. LogUtil.d("getInitData", "URL==" + Constants.URL + Constants.URL_END + Constants.CALLINGBED_INIT);
  230. if (Constants.URL.length() > 7) {
  231. OkHttpUtils
  232. .post()
  233. .url(Constants.URL + Constants.URL_END + Constants.CALLINGBED_INIT)
  234. .addParams("deviceMAC", macAddress)
  235. .build()
  236. .execute(new StringCallback() {
  237. @Override
  238. public void onError(Call call, Exception e, int id) {
  239. ivLoading.setVisibility(View.INVISIBLE);
  240. dismissNurseBrightness();
  241. showNetErrorView("MAC地址: " + macAddress + "\nError:" + "初始化数据请求错误[MAC地址是否注册?]");
  242. //updateHandler.sendEmptyMessageDelayed(404, DATEREFRESH_DELAYMILLIS);//wuyq remove
  243. updateHandler.sendEmptyMessageDelayed(4444, DATEREFRESH_DELAYMILLIS);//wuyq add
  244. ToastUtil.showToast("初始化数据请求错误");
  245. }
  246. @Override
  247. public void onResponse(String response, int id) {
  248. String data = response.substring(0, response.length() - 4);
  249. ivLoading.setVisibility(View.INVISIBLE);
  250. LogUtil.d("getInitData", "getInitData==" + data);
  251. try {
  252. showContent();
  253. JSONObject object = new JSONObject(data);
  254. if (object.getString("Code").equals("ERROR!")) {
  255. dismissNurseBrightness();
  256. showNetErrorView("MAC地址: " + macAddress + "\nError:" + "数据错误异常[ERROR!]");
  257. updateHandler.sendEmptyMessageDelayed(404, DATEREFRESH_DELAYMILLIS);
  258. ToastUtil.showToast("数据请求失败");
  259. } else {
  260. if (object.getString("deviceStatus").equals("1")) {
  261. Gson gson = new Gson();
  262. InitDataEntity initDataEntity = gson.fromJson(data, InitDataEntity.class);
  263. saveData(initDataEntity);
  264. Intent intent = new Intent();
  265. intent.putExtra(Constants.INITENTITY, initDataEntity);
  266. intent.setClass(InitActivity.this, CallingBedActivity.class);
  267. startActivity(intent);
  268. InitActivity.this.finish();
  269. } else {
  270. dismissNurseBrightness();
  271. showNetErrorView("MAC地址: " + macAddress + "\nError: " + "设备未启用");
  272. updateHandler.sendEmptyMessageDelayed(404, DATEREFRESH_DELAYMILLIS);
  273. ToastUtil.showToast("设备未启用");
  274. }
  275. }
  276. } catch (Exception e) {
  277. LogUtil.d("getInitData", "getInitData==Exception");
  278. showNetErrorView("MAC地址: " + macAddress + "\nError:" + "Response was Exception");
  279. e.printStackTrace();
  280. }
  281. }
  282. });
  283. }
  284. }
  285. private void saveData(InitDataEntity initDataEntity) {
  286. if (null != initDataEntity) {
  287. Constants.SIP_ID = initDataEntity.getDeviceSipId();
  288. Constants.SIP_PASS_WORD = initDataEntity.getDeviceSipPassWord();
  289. Constants.SIP_IP = initDataEntity.getDeviceSipIp();
  290. Constants.DEVICE_WIFI_HOST_NAME = initDataEntity.getDeviceWifiHostName();
  291. Constants.BED_ID = initDataEntity.getId();
  292. Constants.CALLMAIN_ID = initDataEntity.getDeviceHostingID();
  293. Constants.MYSELF_ID = initDataEntity.getId();
  294. Constants.ROOM_ID = initDataEntity.getDeviceRoomId();
  295. Constants.DEVICE_SCREEN_SLEEP = initDataEntity.getDeviceScreamSleep();
  296. Constants.DEVICE_HUMAN_ID = initDataEntity.getDeviceHumanId();
  297. Constants.PartID = initDataEntity.getPartId();
  298. LogUtil.d("saveData", "deviceHostingID==" + initDataEntity.getDeviceHostingID());
  299. if (StringUtils.notEmpty(initDataEntity.getDeviceHostingID())) {
  300. String n = StringUtils.substringByLengh(initDataEntity.getDeviceHostingID(), 0, 1);
  301. if ("#".equals(n)) {//服务器托管状态:处于托管中
  302. String nDis = StringUtils.deleteCharAt(initDataEntity.getDeviceHostingID(), 0);
  303. Constants.DEVICE_HOSTING_ID = nDis.split(",")[0];
  304. Constants.TRUST_NEW_MAIN_ID = nDis.split(",")[1];
  305. } else {//服务器托管状态:处于未托管
  306. Constants.DEVICE_HOSTING_ID = initDataEntity.getDeviceHostingID();
  307. Constants.TRUST_NEW_MAIN_ID = "";
  308. }
  309. }
  310. setSystemTime(initDataEntity);
  311. }
  312. }
  313. /**
  314. * 设置系统时间 (邓喆)
  315. *
  316. * @param initDataEntity
  317. */
  318. private void setSystemTime(InitDataEntity initDataEntity) {
  319. LogUtil.d("setSystemTime", initDataEntity.getCurTime());
  320. String[] time = initDataEntity.getCurTime().trim().split(" ");
  321. String[] day = time[0].split("/");
  322. String[] hour = time[1].split(":");
  323. //=================设置时间(可用)
  324. DateUtil.setSystemTime(this, Integer.valueOf(day[0]), Integer.valueOf(day[1]) - 1, Integer.valueOf(day[2])
  325. , Integer.valueOf(hour[0]), Integer.valueOf(hour[1]), Integer.valueOf(hour[2]));
  326. // if (!TimeUtil.isRoot()) {
  327. // String t = initDataEntity.getCurTime().trim().replaceAll("/", "").replace(":", "").replace(" ", ".");
  328. // if (t != null && t != "") {
  329. // TimeUtil.setSysDate(t);
  330. // }
  331. // }
  332. }
  333. @Override
  334. public void onReload() {
  335. super.onReload();
  336. dateRefresh();
  337. }
  338. public void dateRefresh() {
  339. cm = (ConnectivityManager) MyApplication.getAppContext().getSystemService(Context.CONNECTIVITY_SERVICE);
  340. if (null != cm) getMacAddress();
  341. getLocalWayAddress();
  342. isNewWork();
  343. }
  344. @Override
  345. protected void onDestroy() {
  346. super.onDestroy();
  347. animationDrawable.stop();
  348. updateHandler = null;
  349. serialPortUtil = null;//2018-01-10 add by Waderson
  350. }
  351. /**
  352. * 灭掉护理灯
  353. */
  354. public void dismissNurseBrightness() {
  355. new Thread(new Runnable() {
  356. @Override
  357. public void run() {
  358. try {
  359. setNurseBrightness(serialPortUtil, 1, "000000", "000000", "000000", "000000", "000000");
  360. } catch (InterruptedException e) {
  361. e.printStackTrace();
  362. }
  363. }
  364. }).start();
  365. }
  366. /**
  367. * 监听呼叫按钮 buffer[5] 如果按下则 放松注册信息给后台
  368. */
  369. @Override
  370. public void serialPortBedOnclick(byte[] buffer) {
  371. // mScreenExtinguishUtil.touchScreen();//===============================息屏
  372. //呼叫护士键短按/长按松开
  373. if (buffer[5] == 1 || buffer[5] == 2) {
  374. if (null != Constants.MAC_ADDRESS) {
  375. //有线mac地址,设备出场信息,无线mac地址
  376. final String str = "MGR_REG_A" + Constants.DELIMITER + Constants.MAC_ADDRESS + Constants.DELIMITER + "4" + Constants.DELIMITER + android.os.Build.DISPLAY +"_"+Constants.MCU_VERSION_NUMBER +
  377. Constants.DELIMITER + Constants.MAC_ADDRESS + Constants.DELIMITER + "FF:FF:FF:FF:FF:FF" + Constants.DELIMITER + SerialPortUtil.KEY_ID;
  378. UdpSendUtil.sendManualReboot(str);
  379. runOnUiThread(new Runnable() {
  380. @Override
  381. public void run() {
  382. ToastUtil.showToast("收到按键消息,发送注册信息:" + str);
  383. }
  384. });
  385. }
  386. }
  387. }
  388. @Override
  389. public void serialPortBedOnclickString(String str) {
  390. try {
  391. String newStr = CutSerialPortUtil.delHeadAndEnd(str, "$", "#");
  392. String content[] = newStr.split(",");
  393. LogUtil.d("serialPortBedOnclickString", "InitActivity newStr==" + newStr);
  394. switch (content[0]) {
  395. case "V":
  396. Constants.MCU_VERSION_NUMBER = content[1].substring(0, 14);
  397. LogUtil.d("serialPortBedOnclick", "Constants.MCU_VERSION_NUMBER==" + Constants.MCU_VERSION_NUMBER);
  398. break;
  399. }
  400. } catch (Exception e) {
  401. LogUtil.d("serialPortBedOnclickString", "Exception==");
  402. }
  403. }
  404. @SuppressLint("HandlerLeak")
  405. Handler updateHandler = new Handler() {
  406. public void handleMessage(Message msg) {
  407. switch (msg.what) {
  408. case 404:
  409. dateRefresh();
  410. break;
  411. case 4444: // wuyq add
  412. ethreboot();
  413. dateRefresh();
  414. break;
  415. }
  416. }
  417. };
  418. public static final long DATEREFRESH_DELAYMILLIS = 3000;
  419. @Subscribe(threadMode = ThreadMode.MAIN)
  420. public void onMoonEvent(MessageEvent messageEvent) {
  421. switch (messageEvent.getType()) {
  422. case Constants.EVENT_MGR_APP_UPDATE://APP更新
  423. appCheckUpdate();
  424. break;
  425. }
  426. }
  427. }