InitActivity.java 20 KB

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