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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package safeluck.drive.evaluation.fragment;
 
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
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.ListView;
import android.widget.Toast;
 
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.work.Data;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;
 
import com.anyun.exam.lib.AYSdk;
import com.anyun.basecommonlib.MyLog;
import com.anyun.exam.lib.util.ByteUtil;
import com.google.gson.Gson;
import com.safeluck.aykj.utils.BytesUtils;
 
import org.json.JSONException;
import org.json.JSONObject;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
 
import me.yokeyword.fragmentation.SupportFragment;
import safeluck.drive.evaluation.Constant;
import safeluck.drive.evaluation.DB.appstatusdb.AppStatus;
import safeluck.drive.evaluation.DB.appstatusdb.AppStatusViewModel;
import safeluck.drive.evaluation.DB.rtktb.RTKConfig;
import safeluck.drive.evaluation.DB.rtktb.RTKConfigViewModel;
import safeluck.drive.evaluation.MainActivity;
import safeluck.drive.evaluation.R;
import safeluck.drive.evaluation.adapter.GpsInfoAdapter;
import safeluck.drive.evaluation.adapter.QuickAdapter;
import safeluck.drive.evaluation.adapter.WrapContentLinearLayoutManager;
import safeluck.drive.evaluation.bean.ExamPlatformData;
import safeluck.drive.evaluation.bean.MCUInfo;
import safeluck.drive.evaluation.cEventCenter.CEventCenter;
import safeluck.drive.evaluation.cEventCenter.ICEventListener;
import safeluck.drive.evaluation.customview.MyArgEditText;
import safeluck.drive.evaluation.customview.SelectDialog;
import safeluck.drive.evaluation.util.CThreadPoolExecutor;
import safeluck.drive.evaluation.util.FileUtil;
import safeluck.drive.evaluation.util.SPUtils;
import safeluck.drive.evaluation.worker.MCUUpgradeWorker;
 
/**
 * 设置所有参数
 * 通过设置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 SetArgumentsFragment extends SupportFragment implements View.OnClickListener {
 
    private static final String TAG = SetArgumentsFragment.class.getSimpleName();
    private static final int REQUEST_CODE_MAP = 100;
    private static final int REQUEST_CODE_ROADMAP = 102;
    private static final int REQUEST_CODE_CAR = 101;
    private int request_code = REQUEST_CODE_MAP;
    private Button btn_config_signal;
    private Button btn_mcu_upgrade,btn_map_select;
    private Gson gson = new Gson();
 
 
    private MyArgEditText platform_ip,platform_port,rtkPlatform_ip,rtkPlatform_port,city,province;
 
    private RTKConfigViewModel rtkConfigViewModel;
    private RTKConfig mRtkConfig;
    private boolean needSendRtk = false;
 
    public static SetArgumentsFragment newInstance() {
        return new SetArgumentsFragment();
    }
    
    @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);
        rtkConfigViewModel= ViewModelProviders.of(this).get(RTKConfigViewModel.class);
        rtkConfigViewModel.getRTKConfig().observe(this, rtkConfig -> {
            if (rtkConfig != null){
                Log.i(TAG, "RTKConfig Changed: "+rtkConfig.toString());
                mRtkConfig = rtkConfig;
 
                rtkPlatform_ip.updateStr(rtkConfig.getIp());
                rtkPlatform_port.updateStr(String.valueOf(rtkConfig.getPort()));
                city.updateStr(String.valueOf(rtkConfig.getCity()));
                province.updateStr(String.valueOf(rtkConfig.getProvince()));
                ExamPlatformData.getInstance().setRTKPort(rtkConfig.getPort());
                ExamPlatformData.getInstance().setRTKIP(rtkConfig.getIp());
 
            }
 
 
        });
 
 
 
        return view;
    }
 
        private void sendRtkConfigInfo(RTKConfig mRTKConfig) {
        if (mRTKConfig != null) {
            String rtkjson = gson.toJson(mRTKConfig);
 
            //去除id字段
            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(rtkjson);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            jsonObject.remove("_id");
            rtkjson = jsonObject.toString();
            MyLog.i( "need resend RTK配置信息:" + rtkjson+"标志位:"+needSendRtk);
            AYSdk.getInstance().sendCmd(Constant.PUSH_RTK_PLATFORM_INFO, rtkjson);
        } else {
            MyLog.i("RTKConfig未取到数据");
        }
        needSendRtk = false;
 
    }
    private void initView(View view) {
        if (HomeFragment.class.getSimpleName().equals(getTopFragment().getClass().getSimpleName())){
            ((MainActivity)_mActivity).viewtitle.setVisibility(View.GONE);
            ((MainActivity)_mActivity).btn_return.setVisibility(View.GONE);
        }else{
            ((MainActivity)_mActivity).viewtitle.setVisibility(View.VISIBLE);
            ((MainActivity)_mActivity).btn_return.setVisibility(View.VISIBLE);
        }
        view.findViewById(R.id.btn_save_platform).setOnClickListener(this);
        platform_ip = view.findViewById(R.id.platform_ip);
        platform_port = view.findViewById(R.id.platform_port);
 
        platform_ip.updateStr(ExamPlatformData.getInstance().getPlatformIP());
 
        platform_port.updateStr(ExamPlatformData.getInstance().getPlatformPort()+"");
        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_config_signal = view.findViewById(R.id.btn_signal_conf);
        btn_config_signal.setOnClickListener(this);
 
 
        rtkPlatform_ip = view.findViewById(R.id.rtk_addr_ip);
        rtkPlatform_port = view.findViewById(R.id.rtk_addr_port);
        city = view.findViewById(R.id.city_id);
        province = view.findViewById(R.id.province_id);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
 
            case R.id.btn_signal_conf:
                SupportFragment signalFragment = findChildFragment(SignalConfigFragment.class);
                if (signalFragment == null){
                    signalFragment= SignalConfigFragment.newInstance();
                }
                start(signalFragment);
                break;
            case R.id.btn_mcu_upgrade:
 
 
                OneTimeWorkRequest mcuUpgradeWorkReq = OneTimeWorkRequest.from(MCUUpgradeWorker.class);
                WorkManager.getInstance(_mActivity.getApplicationContext()).enqueue(mcuUpgradeWorkReq);
                WorkManager.getInstance(_mActivity.getApplicationContext()).getWorkInfoByIdLiveData(mcuUpgradeWorkReq.getId())
                        .observe(this, new Observer<WorkInfo>() {
                            @Override
                            public void onChanged(WorkInfo workInfo) {
                                if (workInfo.getState()== WorkInfo.State.FAILED){
                                    Data data = workInfo.getOutputData();
                                    String str = data.getString(Constant.MCU_UPGRADE_FAIL_RESASON);
                                    Toast.makeText(_mActivity, str, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                break;
            case R.id.btn_save_platform:
                Toast.makeText(_mActivity, "保存成功", Toast.LENGTH_SHORT).show();
                String tempPlatformIp = platform_ip.getInputStr().trim();
                String tempRTKPlatformIp = rtkPlatform_ip.getInputStr().trim();
 
 
                int city_id_ = Integer.parseInt(city.getInputStr().trim());
 
                int province_id_ = Integer.parseInt(province.getInputStr().trim());
 
                int tempPlatformPort = Integer.parseInt(platform_port.getInputStr().trim());
                int tempRTKPlatformPort = Integer.parseInt(rtkPlatform_port.getInputStr().trim());
 
                if(ExamPlatformData.getInstance().compareIPandPort(tempPlatformIp,tempPlatformPort)){
                    SPUtils.put(getActivity(), SPUtils.DES_HEX_PWD,"");
                    ((MainActivity)getActivity()).examPlatformModel.getDataChange().postValue(1);
                    ExamPlatformData.getInstance().insertPlatformIp(tempPlatformIp);
                    ExamPlatformData.getInstance().insertPlatformPort(tempPlatformPort);
                }
 
                if (mRtkConfig != null){
 
                    mRtkConfig.setPort(tempRTKPlatformPort);
                    mRtkConfig.setCity(city_id_);
                    mRtkConfig.setProvince(province_id_);
                    mRtkConfig.setIp(tempRTKPlatformIp);
 
                    if (ExamPlatformData.getInstance().compareRTKIPandPort(tempRTKPlatformIp,tempRTKPlatformPort)){
                        mRtkConfig.setRegistered(0);
                        MyLog.i("rtk ip和地址有变化");
                        needSendRtk = true;
                        sendRtkConfigInfo(mRtkConfig);
 
                    }
                    rtkConfigViewModel.insertRTKConfig(mRtkConfig);
 
                }
                _mActivity.onBackPressed();
                break;
            case R.id.btn_car_select://车辆模型和地图公用一套代码 区别在于REQUEST_CODE
                request_code = REQUEST_CODE_CAR;
                openFileMgr();
                break;
            case R.id.btn_map_select:
                SelectDialog selectDialog = SelectDialog.newInstance();
                selectDialog.show(getFragmentManager(),"selectdialog");
                selectDialog.setSelectedListener((int res) -> {
                    if (res != SelectDialog.SELECT_NONE){
                        if (res == SelectDialog.FIRST){
                            request_code = REQUEST_CODE_MAP;
                            Toast.makeText(_mActivity, "场考地图选择!", Toast.LENGTH_SHORT).show();
                        }else{
                            request_code = REQUEST_CODE_ROADMAP;
                            Toast.makeText(_mActivity, "路考地图选择!", Toast.LENGTH_SHORT).show();
                        }
                        openFileMgr();
                    }else{
                        Toast.makeText(_mActivity, "没有任何选择!", Toast.LENGTH_SHORT).show();
                    }
                });
                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){
            if (data != null){
                final Uri uri= data.getData();
                Log.i(TAG, "onActivityResult: "+uri.getPath());
                ExamPlatformData.getInstance().setNewMapPath(uri.getPath());
                CThreadPoolExecutor.runInBackground(()-> {
 
                        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){
            if (data != null){
                final Uri uri= data.getData();
                Log.i(TAG, "onActivityResult: "+uri.getPath());
                ExamPlatformData.getInstance().setCarModelPath(uri.getPath());
                CThreadPoolExecutor.runInBackground(()-> {
 
                        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);
                        }
 
                });
            }
 
 
        }else  if (REQUEST_CODE_ROADMAP == requestCode){
            if (data != null){
                final Uri uri= data.getData();
                Log.i(TAG, "onActivityResult: "+uri.getPath());
                ExamPlatformData.getInstance().setNewRoadMapPath(uri.getPath());
                CThreadPoolExecutor.runInBackground(() -> {
                    byte[] fileContent = FileUtil.readFile(uri.getPath());
                    if (fileContent != null){
                        String str = new String(fileContent);
                        Log.i(TAG, "文件内容:"+str);
                        AYSdk.getInstance().sendCmd(Constant.PUSH_ROAD_MAP_INFO,str);
                    }
                });
            }
 
 
        }
    }
 
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }
 
    @Override
    public void onDetach() {
        super.onDetach();
    }
 
    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}