|
@@ -0,0 +1,401 @@
|
|
|
+package com.wdkl.ncs.s433.transbox;
|
|
|
+
|
|
|
+import android.app.smdt.SmdtManager;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.content.IntentFilter;
|
|
|
+import android.content.SharedPreferences;
|
|
|
+import android.net.ConnectivityManager;
|
|
|
+import android.net.Network;
|
|
|
+import android.net.NetworkRequest;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.util.Log;
|
|
|
+import android.view.View;
|
|
|
+import android.widget.Button;
|
|
|
+import android.widget.EditText;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import androidx.annotation.NonNull;
|
|
|
+import androidx.preference.PreferenceManager;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.wdkl.ncs.s433.transbox.common.Constants;
|
|
|
+import com.wdkl.ncs.s433.transbox.receiver.NetworkConnectChangedReceiver;
|
|
|
+import com.wdkl.ncs.s433.transbox.tcp.EventBusModel;
|
|
|
+import com.wdkl.ncs.s433.transbox.tcp.TcpAction;
|
|
|
+import com.wdkl.ncs.s433.transbox.tcp.TcpClient;
|
|
|
+import com.wdkl.ncs.s433.transbox.tcp.TcpModel;
|
|
|
+import com.wdkl.ncs.s433.transbox.tcp.TcpType;
|
|
|
+import com.wdkl.ncs.s433.transbox.utils.AlarmMessageUtil;
|
|
|
+import com.wdkl.ncs.s433.transbox.utils.AutoRebootUtil;
|
|
|
+import com.wdkl.ncs.s433.transbox.utils.NetUtil;
|
|
|
+import com.wdkl.ncs.s433.transbox.utils.SettingConfig;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.ArrayUtils;
|
|
|
+import org.greenrobot.eventbus.EventBus;
|
|
|
+import org.greenrobot.eventbus.Subscribe;
|
|
|
+import org.greenrobot.eventbus.ThreadMode;
|
|
|
+
|
|
|
+import java.io.DataOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+public class MainActivity extends SerialPortActivity {
|
|
|
+ String TAG = "MainActivity";
|
|
|
+ EditText mReception;
|
|
|
+ ConnectivityManager connectivityManager;
|
|
|
+ private SmdtManager smdtManager;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ setContentView(R.layout.activity_main);
|
|
|
+ //注册EventBus
|
|
|
+ EventBus.getDefault().register(this);
|
|
|
+ mReception = (EditText) findViewById(R.id.EditTextReception);
|
|
|
+ final Button buttonSetup = (Button) findViewById(R.id.ButtonSetup);
|
|
|
+ buttonSetup.setOnClickListener(new View.OnClickListener() {
|
|
|
+ public void onClick(View v) {
|
|
|
+// openNetworkDebug();
|
|
|
+ startActivity(new Intent(MainActivity.this, SettingsActivity.class));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ final Button rebootAppButton = (Button) findViewById(R.id.RebootApp);
|
|
|
+ rebootAppButton.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View view) {
|
|
|
+ AutoRebootUtil.restartApp();
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ smdtManager = SmdtManager.create(this);
|
|
|
+
|
|
|
+ this.init();
|
|
|
+// this.initReceive();
|
|
|
+ NetworkRequest.Builder builder = new NetworkRequest.Builder();
|
|
|
+ NetworkRequest request = builder.build();
|
|
|
+ connectivityManager =(ConnectivityManager) getApplication().getApplicationContext().getSystemService(CONNECTIVITY_SERVICE);
|
|
|
+ connectivityManager.registerNetworkCallback(request,callback);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void init() {
|
|
|
+ TextView settingInfo = (TextView) findViewById(R.id.setttingInfo);
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ Context context = getApplicationContext();
|
|
|
+ String path = SettingConfig.getSerialDevicePath(context);
|
|
|
+ Log.i("Application", "getSerialPort: " + path);
|
|
|
+ int baudrate = SettingConfig.getBaudRate(context);
|
|
|
+ int parity = SettingConfig.getParity(context);
|
|
|
+ int dataBits = SettingConfig.getDataBits(context);
|
|
|
+ int stopBits = SettingConfig.getStopBits(context);
|
|
|
+ String tcpServer = SettingConfig.getTcpServer(context);
|
|
|
+ Integer tcpServerPort = SettingConfig.getTcpServerPort(context);
|
|
|
+ Constants.MAC_ADDR = NetUtil.getInstance().getMacAddress();
|
|
|
+ String localIP = NetUtil.getInstance().getLocalIP();
|
|
|
+ sb.append("串口号:" + path + "\n");
|
|
|
+ sb.append("波特率:" + baudrate + "\n");
|
|
|
+ sb.append("校验位:" + parity + "\n");
|
|
|
+ sb.append("数据位:" + dataBits + "\n");
|
|
|
+ sb.append("停止位:" + stopBits + "\n");
|
|
|
+ sb.append("服务器地址:" + tcpServer + "\n");
|
|
|
+ sb.append("服务器端口:" + tcpServerPort + "\n");
|
|
|
+ sb.append("本机MAC地址:" + Constants.MAC_ADDR + "\n");
|
|
|
+ sb.append("本机IP地址:" + localIP + "\n");
|
|
|
+ settingInfo.setText(sb.toString());
|
|
|
+ Log.e(TAG, "init: "+ localIP);
|
|
|
+ TextView tcpstatus = (TextView) findViewById(R.id.serialStatus);
|
|
|
+ if (Constants.SERIAL_PORT_STATUS) {
|
|
|
+ tcpstatus.setText("正常");
|
|
|
+ tcpstatus.setTextColor(getResources().getColor(R.color.green));
|
|
|
+ } else {
|
|
|
+ tcpstatus.setText(Constants.SERIAL_PORT_FAIL_RESON);
|
|
|
+ tcpstatus.setTextColor(getResources().getColor(R.color.red));
|
|
|
+ }
|
|
|
+ //初始化tcp
|
|
|
+ if (Constants.NET_WORK_AVAILABLE) {
|
|
|
+ new Thread(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ if(!TcpClient.getInstance().isRunning()) {
|
|
|
+ TcpClient.getInstance().init(tcpServer, tcpServerPort, 60);
|
|
|
+ }else{
|
|
|
+ TcpClient.getInstance().doConnect();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }).start();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //默认打开网络调试
|
|
|
+ openNetworkDebug();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onResume() {
|
|
|
+ super.onResume();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onDestroy() {
|
|
|
+ super.onDestroy();
|
|
|
+ EventBus.getDefault().unregister(this);
|
|
|
+ connectivityManager.unregisterNetworkCallback(callback);
|
|
|
+// unregisterReceiver(networkConnectChangedReceiver);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onDataReceived(byte[] buffer, int size) {
|
|
|
+ runOnUiThread(new Runnable() {
|
|
|
+ public void run() {
|
|
|
+ if (mReception != null) {
|
|
|
+ byte[] bytes = Arrays.copyOfRange(buffer, 0, size);
|
|
|
+ String receiveData = bytesToHex(bytes);
|
|
|
+ String receiveString = new String(bytes, 0, size);
|
|
|
+ Log.e("Application", "receiveData: " + receiveData);
|
|
|
+ if (receiveString.contains("\r") || receiveString.contains("\n")) {
|
|
|
+ Log.e("Application", "有回车键: ");
|
|
|
+ }
|
|
|
+ /*if (receiveData.startsWith("fe")) {
|
|
|
+ byte[] cardCode = ArrayUtils.subarray(bytes, 5, 9);
|
|
|
+ //小端字节顺序,需要将数组反转
|
|
|
+ ArrayUtils.reverse(cardCode);
|
|
|
+ String hex = bytesToHex(cardCode);
|
|
|
+ String s = String.format("%010d", Integer.valueOf(hex, 16));
|
|
|
+ Log.e("Application", "s: " + s);
|
|
|
+ Log.e("Application", "receiveString: " + receiveString);
|
|
|
+ Log.e("Application", "receiveData: " + receiveData);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (receiveData.startsWith("fd") && receiveData.endsWith("df")) {
|
|
|
+ String mac = receiveData.substring(2, receiveData.indexOf("df"));
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ mReception.append(sdf.format(new Date()) + " 收到数据:" + mac + "\n");
|
|
|
+ //发送tcp消息
|
|
|
+ if (TcpClient.getInstance().isRunning()) {
|
|
|
+ TcpClient.getInstance().sendMsg(AlarmMessageUtil.getAlarmMessage(mac));
|
|
|
+ }
|
|
|
+
|
|
|
+ }*/
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void openNetworkDebug() {
|
|
|
+ try {
|
|
|
+ Process p = Runtime.getRuntime().exec("su");
|
|
|
+ OutputStream op = p.getOutputStream();
|
|
|
+ DataOutputStream os = new DataOutputStream(op);
|
|
|
+// os.writeBytes("/system/bin/sh\n");
|
|
|
+ os.writeBytes("setprop service.adb.tcp.port 5555\n");
|
|
|
+ os.writeBytes("stop adbd\n");
|
|
|
+ os.writeBytes("start adbd\n");
|
|
|
+ os.writeBytes("exit\n");
|
|
|
+ os.flush();
|
|
|
+ os.close();
|
|
|
+ op.close();
|
|
|
+ Log.i("openNetworkDebug", "打开调试:---- ");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String bytesToHex(byte[] bytes) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (byte b : bytes) {
|
|
|
+ sb.append(String.format("%02x", b));
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * NFC 读取卡号
|
|
|
+ *
|
|
|
+ * @param bytes
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String bytesToKeyCode(byte[] bytes) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (byte b : bytes) {
|
|
|
+ sb.append(String.format("%c", b));
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Subscribe(threadMode = ThreadMode.MAIN)
|
|
|
+ public void onMoonEvent(EventBusModel eventBusModel) {
|
|
|
+ switch (eventBusModel.getMsgType()) {
|
|
|
+ case Constants.EVENT_TCP_STATE: {
|
|
|
+ updateTcpStatus();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case Constants.NET_WORK_STATUS_CHANGE: {
|
|
|
+ if ((int) eventBusModel.getMsg() > -1) {
|
|
|
+ this.init();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case Constants.NET_WORK_AVAILABLE_CHANGE:{
|
|
|
+ Log.e("TAG", "onMoonEvent: "+(boolean) eventBusModel.getMsg());
|
|
|
+ if((boolean) eventBusModel.getMsg()){
|
|
|
+ this.init();
|
|
|
+ }else{
|
|
|
+ if (TcpClient.getInstance().isRunning()){
|
|
|
+ TcpClient.getInstance().closeClient();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ case Constants.EVENT_TCP_MSG:
|
|
|
+ //处理tcp消息
|
|
|
+ TcpModel tcpModel = (TcpModel) eventBusModel.getMsg();
|
|
|
+ handleTcpMsg(tcpModel);
|
|
|
+ break;
|
|
|
+ case Constants.EVENT_RESTART_APP:
|
|
|
+ //restart app
|
|
|
+ AutoRebootUtil.restartApp();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ NetworkConnectChangedReceiver networkConnectChangedReceiver;
|
|
|
+
|
|
|
+ private void initReceive() {
|
|
|
+ //网络状态广播
|
|
|
+ networkConnectChangedReceiver = new NetworkConnectChangedReceiver();
|
|
|
+ IntentFilter intentFilter = new IntentFilter();
|
|
|
+ intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
|
|
|
+ registerReceiver(networkConnectChangedReceiver, intentFilter);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void updateTcpStatus() {
|
|
|
+ TextView tcpstatus = (TextView) findViewById(R.id.tcpStatus);
|
|
|
+ if (Constants.TCP_CONNCETED) {
|
|
|
+ tcpstatus.setText("正常");
|
|
|
+ tcpstatus.setTextColor(getResources().getColor(R.color.green));
|
|
|
+ } else {
|
|
|
+ tcpstatus.setText("未连接");
|
|
|
+ tcpstatus.setTextColor(getResources().getColor(R.color.red));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private synchronized void handleTcpMsg(final TcpModel tcpModel) {
|
|
|
+ //返回ack
|
|
|
+ TcpModel respondModel = new TcpModel();
|
|
|
+ respondModel.setType(TcpType.S433);
|
|
|
+ respondModel.setAction(TcpAction.S433Action.ACK);
|
|
|
+ respondModel.setData(Constants.MAC_ADDR);
|
|
|
+ TcpClient.getInstance().sendMsg(respondModel.toJson());
|
|
|
+
|
|
|
+ switch (tcpModel.getType()) {
|
|
|
+ case TIME:
|
|
|
+ if (tcpModel.getAction() == TcpAction.TimeAction.SYNC) {
|
|
|
+ //同步时间
|
|
|
+ //action: SYNC_TIME, data: {"time_zone":"PRC","time":"1683626639"}
|
|
|
+ JSONObject jsonObject = JSON.parseObject(tcpModel.getData().toString());
|
|
|
+ String timezone = jsonObject.getString("time_zone");
|
|
|
+ long time = Long.parseLong(jsonObject.getString("time")) * 1000;
|
|
|
+
|
|
|
+ setSysTime(time, timezone);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case DEVICE:
|
|
|
+ if (tcpModel.getAction() == TcpAction.DeviceAction.APP_UPDATE) {
|
|
|
+ //
|
|
|
+ } else if (tcpModel.getAction() == TcpAction.DeviceAction.RESTART) {
|
|
|
+ AutoRebootUtil.restartApp();
|
|
|
+ } else if (tcpModel.getAction() == TcpAction.DeviceAction.REBOOT) {
|
|
|
+ AutoRebootUtil.reboot();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case SIDE:
|
|
|
+ //
|
|
|
+ break;
|
|
|
+ case S433:
|
|
|
+ if (tcpModel.getAction() == TcpAction.S433Action.SOS_CALL
|
|
|
+ || tcpModel.getAction() == TcpAction.S433Action.CALL
|
|
|
+ || tcpModel.getAction() == TcpAction.S433Action.CANCEL) {
|
|
|
+ //解析数据并通过433发射出去
|
|
|
+ } else if (tcpModel.getAction() == TcpAction.S433Action.DATA) {
|
|
|
+ //
|
|
|
+ } else if (tcpModel.getAction() == TcpAction.S433Action.SYNC_TIME) {
|
|
|
+ //
|
|
|
+ } else if (tcpModel.getAction() == TcpAction.S433Action.EXIST) {
|
|
|
+ //
|
|
|
+ }
|
|
|
+ //发送433数据
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private ConnectivityManager.NetworkCallback callback = new ConnectivityManager.NetworkCallback(){
|
|
|
+ @Override
|
|
|
+ public void onAvailable(@NonNull Network network) {
|
|
|
+ super.onAvailable(network);
|
|
|
+ Log.e(TAG, "onAvailable: ");
|
|
|
+ Constants.NET_WORK_AVAILABLE=true;
|
|
|
+ EventBusModel eventBusModel = new EventBusModel(Constants.NET_WORK_AVAILABLE_CHANGE,Constants.NET_WORK_AVAILABLE);
|
|
|
+ EventBus.getDefault().post(eventBusModel);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onLost(@NonNull Network network) {
|
|
|
+ Log.e(TAG, "onLost: "+NetUtil.getInstance().getNetAvailable());
|
|
|
+ super.onLost(network);
|
|
|
+ if(NetUtil.getInstance().getNetAvailable()){
|
|
|
+ Constants.NET_WORK_AVAILABLE=true;
|
|
|
+ }else{
|
|
|
+ Constants.NET_WORK_AVAILABLE=false;
|
|
|
+ }
|
|
|
+ EventBusModel eventBusModel = new EventBusModel(Constants.NET_WORK_AVAILABLE_CHANGE,Constants.NET_WORK_AVAILABLE);
|
|
|
+ EventBus.getDefault().post(eventBusModel);
|
|
|
+ }
|
|
|
+
|
|
|
+// @Override
|
|
|
+// public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
|
|
|
+// super.onCapabilitiesChanged(network, networkCapabilities);
|
|
|
+// Log.e(TAG, "onCapabilitiesChanged: "+networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED));
|
|
|
+//// if(networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)){
|
|
|
+//// Constants.NET_WORK_AVAILABLE=true;
|
|
|
+//// EventBusModel eventBusModel = new EventBusModel(Constants.NET_WORK_AVAILABLE_CHANGE,Constants.NET_WORK_AVAILABLE);
|
|
|
+//// EventBus.getDefault().post(eventBusModel);
|
|
|
+//// }
|
|
|
+// }
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置系统时间
|
|
|
+ *
|
|
|
+ * @param time
|
|
|
+ */
|
|
|
+ private void setSysTime(long time, String timeZone) {
|
|
|
+ try {
|
|
|
+ if (smdtManager != null) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(new Date(time));
|
|
|
+ smdtManager.setTime(getApplicationContext(),
|
|
|
+ calendar.get(Calendar.YEAR),
|
|
|
+ calendar.get(Calendar.MONTH)+1,
|
|
|
+ calendar.get(Calendar.DAY_OF_MONTH),
|
|
|
+ calendar.get(Calendar.HOUR_OF_DAY),
|
|
|
+ calendar.get(Calendar.MINUTE));
|
|
|
+
|
|
|
+ Log.e(TAG, "set sys time: " + time);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|