lizhanwei
2020-02-27 c2da1feec3a992ead1723e577a8654012be0f140
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package safeluck.drive.evaluation.fragment;
 
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
 
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
import com.anyun.exam.lib.AYSdk;
import com.anyun.exam.lib.MyLog;
import com.anyun.exam.lib.util.ByteUtil;
 
import java.io.IOException;
import java.nio.charset.Charset;
 
import me.yokeyword.fragmentation.SupportFragment;
import safeluck.drive.evaluation.Constant;
import safeluck.drive.evaluation.MainActivity;
import safeluck.drive.evaluation.R;
import safeluck.drive.evaluation.bean.ExamPlatformData;
import safeluck.drive.evaluation.util.CThreadPoolExecutor;
import safeluck.drive.evaluation.util.FileUtil;
 
/**
 * 基础数据设置
 * 通过设置IP和port,与驾考辅助软件通过tcp传输GPS报文给评判软件
 * MyApplication2
 * Created by lzw on 2019/3/20. 11:22:39
 * 邮箱:632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public class BaseDatasFragment extends SupportFragment implements View.OnClickListener {
 
    private static final String TAG = BaseDatasFragment.class.getSimpleName();
    private static final int REQUEST_CODE_MAP = 100;
    private static final int REQUEST_CODE_CAR = 101;
    private int request_code = REQUEST_CODE_MAP;
    private Button btn_inspect_signal;
    private Button btn_config_signal;
    private Button btn_mcu_upgrade,btn_map_select;
    private EditText et_ip,et_port;
    public static SupportFragment newInstance() {
        return new BaseDatasFragment();
    }
 
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
 
 
        View view = inflater.inflate(R.layout.layout_base_datas, container, false);
        initView(view);
        return view;
    }
 
    private void initView(View view) {
        view.findViewById(R.id.btn_save_platform).setOnClickListener(this);
        et_ip = view.findViewById(R.id.et_platform_ip);
        et_port = view.findViewById(R.id.et_platform_port);
        et_ip.setText(ExamPlatformData.getInstance().getPlatformIP());
        et_port.setText(ExamPlatformData.getInstance().getPlatformPort()+"");
        btn_inspect_signal = view.findViewById(R.id.btn_inpsect_signal);
        btn_mcu_upgrade = view.findViewById(R.id.btn_mcu_upgrade);
        btn_map_select = view.findViewById(R.id.btn_map_select);
        view.findViewById(R.id.btn_car_select).setOnClickListener(this);
        btn_mcu_upgrade.setOnClickListener(this);
        btn_map_select.setOnClickListener(this);
        btn_inspect_signal.setOnClickListener(this);
        btn_config_signal = view.findViewById(R.id.btn_signal_conf);
        btn_config_signal.setOnClickListener(this);
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_inpsect_signal:
                ((BaseSettingFragment)getParentFragment()).startBrotherFragment(InspectSignalFragment.newInstance());
                break;
            case R.id.btn_signal_conf:
                ((BaseSettingFragment)getParentFragment()).startBrotherFragment(SignalConfigFragment.newInstance());
                break;
            case R.id.btn_mcu_upgrade:
 
 
 
                try {
                    byte[] datas =FileUtil.readLocalFile(getActivity(),"dfu.bin");
                    if (datas != null){
                        Log.i(TAG,ByteUtil.byte2hex(datas));
 
                        String strs = new String(datas, Charset.forName("ISO-8859-1"));
                        Log.i(TAG, "onClick: datas.legnth=="+strs.getBytes("ISO-8859-1").length);
                        AYSdk.getInstance().sendCmd(Constant.UPGRADE_MCU_CONTENT_FILE, strs);
                    }else{
                        MyLog.i(TAG,"mcu升级文件不存在");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_save_platform:
                if(ExamPlatformData.getInstance().compareIPandPort(et_ip.getText().toString().trim(),Integer.parseInt(et_port.getText().toString().trim()))){
                    ((MainActivity)getActivity()).examPlatformModel.getDataChange().postValue(1);
                    ExamPlatformData.getInstance().insertPlatformIp(et_ip.getText().toString().trim());
                    ExamPlatformData.getInstance().insertPlatformPort(Integer.parseInt(et_port.getText().toString().trim()));
                }
                break;
            case R.id.btn_car_select://车辆模型和地图公用一套代码 区别在于REQUEST_CODE
                request_code = REQUEST_CODE_CAR;
                openFileMgr();
                break;
            case R.id.btn_map_select:
                request_code = REQUEST_CODE_MAP;
                openFileMgr();
                break;
            default:
                break;
        }
    }
 
    private void openFileMgr() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        try {
            startActivityForResult(intent, request_code);
        } catch (android.content.ActivityNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(_mActivity, "请安装文件管理器", Toast.LENGTH_SHORT).show();
        }
    }
 
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (REQUEST_CODE_MAP == requestCode){
           final Uri uri= data.getData();
            Log.i(TAG, "onActivityResult: "+uri.getPath());
            ExamPlatformData.getInstance().setNewMapPath(uri.getPath());
            CThreadPoolExecutor.runInBackground(new Runnable() {
                @Override
                public void run() {
                    byte[] fileContent = FileUtil.readFile(uri.getPath());
                    if (fileContent != null){
                        String str = new String(fileContent);
                        Log.i(TAG, "文件内容:"+str);
                        AYSdk.getInstance().sendCmd(Constant.PUSH_MAP_INFO,str);
                    }
                }
            });
 
        }else if (requestCode == REQUEST_CODE_CAR){
            final Uri uri= data.getData();
            Log.i(TAG, "onActivityResult: "+uri.getPath());
            ExamPlatformData.getInstance().setCarModelPath(uri.getPath());
            CThreadPoolExecutor.runInBackground(new Runnable() {
                @Override
                public void run() {
                    byte[] fileContent = FileUtil.readFile(uri.getPath());
                    if (fileContent != null){
                        String str = new String(fileContent);
                        Log.i(TAG, "文件内容:"+str);
                        AYSdk.getInstance().sendCmd(Constant.PUSH_VECHILE_PROFILE,str);
                    }
                }
            });
 
        }
    }
}