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
355
356
357
358
package safeluck.drive.evaluation.fragment;
 
import android.content.Context;
import android.os.Bundle;
 
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
 
 
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;
 
import com.anyun.exam.lib.AYSdk;
import com.anyun.exam.lib.MyLog;
 
import org.json.JSONException;
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.Collections;
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.exam_status.ExamStatusInitWorker;
import safeluck.drive.evaluation.DB.exam_status.ExamStatusViewModel;
import safeluck.drive.evaluation.DB.failitems.FailedProjViewModel;
import safeluck.drive.evaluation.DB.failitems.FailedProj_select;
import safeluck.drive.evaluation.R;
import safeluck.drive.evaluation.adapter.ScoreAdapter;
import safeluck.drive.evaluation.DB.exam_status.ExamStatus;
import safeluck.drive.evaluation.bean.ScoreBean;
import safeluck.drive.evaluation.cEventCenter.CEventCenter;
import safeluck.drive.evaluation.cEventCenter.ICEventListener;
import safeluck.drive.evaluation.customview.ArrowView;
import safeluck.drive.evaluation.customview.HouseView;
 
/**
 * 联网训练UI
 * MyApplication2
 * Created by lzw on 2019/3/18. 13:33:14
 * 邮箱:632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public class NetWorkTrainFragment extends SupportFragment implements View.OnClickListener {
 
    private static final String TAG = NetWorkTrainFragment.class.getSimpleName();
    private ListView mListView;
    private Button btn_start_exam;
    private TextView tv_stop;//呼叫请求(结束考试)
    private ScoreAdapter mScoreAdapter;
    private ArrowView av_zhijiao, av_curve, av_park, av_podao;
    private HouseView houseView;
    private List<ExamStatus> examStatusList = new ArrayList<>();
    private List<String> item_conents = new ArrayList<>();//坡道定点停车和起步
 
    private int item_id;//扣分分数总和
    private ExamStatusViewModel examStatusViewModel;
    private static final int ADD_DATA = 1;
    private TextView tv_speed,tv_total_score,tv_sat_num;
    private double speed=0.0;
    private ImageView iv_rtk_status,iv_head;
 
    private int currTotalScore = 100;
    private List<ScoreBean> mArrayList = new ArrayList<>();
    private static final int SPEED_DATA = 2;
    private FailedProjViewModel failedProjViewModel;
 
    public static SupportFragment newInstance() {
        return new NetWorkTrainFragment();
    }
 
 
    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == ADD_DATA){
                tv_total_score.setText(getResources().getString(R.string.total_score,currTotalScore));
                houseView.setColor(whatPaintColor(examStatusList.get(0)));
                av_podao.setColor(whatPaintColor(examStatusList.get(1)));
                av_park.setColor(whatPaintColor(examStatusList.get(2)));
                av_curve.setColor(whatPaintColor(examStatusList.get(3)));
                av_zhijiao.setColor(whatPaintColor(examStatusList.get(4)));
            }
            if (msg.what == SPEED_DATA){
                tv_speed.setText(getResources().getString(R.string.speed,speed));
            }
            return false;
        }
    });
 
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_train_fragment, container, false);
        initView(view);
 
        failedProjViewModel = ViewModelProviders.of(this).get(FailedProjViewModel.class);
        failedProjViewModel.getFailedProjectsForI(Constant.TEST_STU_ID).observe(this, new Observer<List<FailedProj_select>>() {
            @Override
            public void onChanged(List<FailedProj_select> failedProj_selects) {
                if (failedProj_selects != null && failedProj_selects.size()>0){
                   
                    item_id = 0;
                    mArrayList.clear();
                    item_conents.clear();
                    for (FailedProj_select f :
                            failedProj_selects) {
                        item_id += f.getScore_deducting();
                        Log.i(TAG, "onChanged: " + f.toString());
                        mArrayList.add(new ScoreBean(f.getScore_deducting(), f.getItem_content(), f.getDeducting_reason()));
                        item_conents.add(f.getItem_content());
                        Collections.reverse(mArrayList);
                        mScoreAdapter.addDatas(mArrayList);
                    }
                    currTotalScore -=item_id;
                    if (currTotalScore < Constant.PASSING_SCORE){
                        MyLog.i(TAG,"低于80,不合格");
                        stopExam();
                    }
                    MyLog.i(TAG,"监测到有失败项目加入,通知UI更新颜色");
                    mHandler.obtainMessage(ADD_DATA).sendToTarget();
                }else{
                    MyLog.i(TAG,"失败项目数据表被清空更新UI");
                    item_id = 0;
                    mArrayList.clear();
                    item_conents.clear();
                    Collections.reverse(mArrayList);
                    mScoreAdapter.addDatas(mArrayList);
                    mHandler.obtainMessage(ADD_DATA).sendToTarget();
                }
            }
        });
 
 
        examStatusViewModel = ViewModelProviders.of(this).get(ExamStatusViewModel.class);
        examStatusViewModel.getExamStatus().observe(this, new Observer<List<ExamStatus>>() {
            @Override
            public void onChanged(List<ExamStatus> examStatus) {
 
 
 
                Log.i(TAG, "ExamStatus onChanged: ");
                if (examStatus!=null && examStatus.size()>0){
                    MyLog.i(TAG,"考试状态更新");
                    examStatusList.clear();
                    examStatusList.addAll(examStatus);
                    mHandler.obtainMessage(ADD_DATA).sendToTarget();
                    if (examStatus.get(0).getStartExam() == Constant.HAS_BEEN_START_EXAM){
                        btn_start_exam.setEnabled(false);
                        tv_stop.setEnabled(true);
                        sendExamJson(Constant.HAS_BEEN_START_EXAM);
                    }else{
                        btn_start_exam.setEnabled(true);
                        tv_stop.setEnabled(false);
                    }
                }
 
 
            }
        });
 
 
        AppStatusViewModel appStatusViewModel = ViewModelProviders.of(this).get(AppStatusViewModel.class);
        appStatusViewModel.getAppStatus().observe(this, new Observer<AppStatus>() {
            @Override
            public void onChanged(AppStatus appStatus) {
                if (appStatus != null){
                    Log.i(TAG, "onChanged: "+appStatus.toString());
                    iv_rtk_status.getDrawable().setLevel(appStatus.getRtk_connect_status());
                    tv_sat_num.setText(String.valueOf(appStatus.getDefault_observe1()).length()==1?"0"+appStatus.getDefault_observe1():String.valueOf(appStatus.getDefault_observe1()));
                }
            }
        });
 
        return view;
    }
 
    private int whatPaintColor(ExamStatus examStat) {
        int map_id = examStat.getMap_id();
        int status = examStat.getEnter();
        Log.i(TAG, "whatPaintColor: map_id="+map_id+" status="+status);
        switch (status) {
            case 0://考试完成
                switch (map_id) {
                    case 1://倒库
                        if (item_conents.contains("倒车入库")) {
                            return getResources().getColor(R.color.colorAccent);
                        }else{
                            return getResources().getColor(R.color.train_btn_return);
                        }
                    case 2:
                        if (item_conents.contains("坡道定点停车和起步")) {
                            return getResources().getColor(R.color.colorAccent);
                        }else{
                            return getResources().getColor(R.color.train_btn_return);
                        }
                    case 3:
                        if (item_conents.contains("侧方停车")) {
                            return getResources().getColor(R.color.colorAccent);
                        }else{
                            return getResources().getColor(R.color.train_btn_return);
                        }
                    case 4:
                        if (item_conents.contains("曲线行驶")) {
                            return getResources().getColor(R.color.colorAccent);
                        }else{
                            return getResources().getColor(R.color.train_btn_return);
                        }
                    case 5:
                        if (item_conents.contains("直角转弯")) {
                            return getResources().getColor(R.color.colorAccent);
                        }else{
                            return getResources().getColor(R.color.train_btn_return);
                        }
 
                }
            case 1://进入考场项目
                return getResources().getColor(R.color.pink);
            case 2://待考
                return getResources().getColor(R.color.examing);
        }
        return getResources().getColor(R.color.examing);
    }
 
    private void initView(View view) {
 
        av_curve = view.findViewById(R.id.av3);
        av_park = view.findViewById(R.id.av2);
        av_podao = view.findViewById(R.id.av1);
        av_zhijiao = view.findViewById(R.id.av4);
        houseView = view.findViewById(R.id.hv);
 
        iv_rtk_status = view.findViewById(R.id.iv_rtk_connect);
        iv_head = view.findViewById(R.id.iv_head);
        tv_sat_num = view.findViewById(R.id.tv_sat_num);
        tv_speed = view.findViewById(R.id.tv_speed);
        tv_total_score = view.findViewById(R.id.tv_total_score);
        tv_speed.setText(getResources().getString(R.string.speed,speed));
 
        view.findViewById(R.id.btn_return).setOnClickListener(this);
        mListView = view.findViewById(R.id.lv);
        mListView.setFocusable(false);
        mListView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent ev) {
                switch (ev.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        mListView.getParent().requestDisallowInterceptTouchEvent(true);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        mListView.getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                }
                return false;
            }
        });
 
        view.findViewById(R.id.view_map).setOnClickListener(this);
        mScoreAdapter = new ScoreAdapter(_mActivity);
        mListView.setAdapter(mScoreAdapter);
        btn_start_exam = view.findViewById(R.id.btn_start);
        btn_start_exam.setOnClickListener(this);
        tv_stop = view.findViewById(R.id.tv_stop);
        tv_stop.setOnClickListener(this);
        mListView.addHeaderView(LayoutInflater.from(_mActivity).inflate(R.layout.layout_score_item, null));
 
    }
 
 
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_start:
                examStatusViewModel.updateStartExam(Constant.HAS_BEEN_START_EXAM);
                failedProjViewModel.deleteAll();
                sendExamJson(1);
                iv_head.getDrawable().setLevel(1);
                break;
            case R.id.tv_stop:
                Toast.makeText(_mActivity, "结束考试", Toast.LENGTH_SHORT).show();
                stopExam();
                break;
            case R.id.view_map:
                MapFragment mapFragment = findFragment(MapFragment.class);
                MyLog.i(TAG,"查看地图");
                if (mapFragment == null) {
                    mapFragment = (MapFragment) MapFragment.newInstance();
                }
                start(mapFragment);
                break;
            case R.id.btn_return:
                _mActivity.onBackPressed();
                break;
        }
    }
 
    private void sendExamJson(int i) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("exam", i);
            String examJson = jsonObject.toString();
            Log.i(TAG, "onClick: " + examJson);
            AYSdk.getInstance().sendCmd(Constant.EXAM_STATUS, examJson);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
 
    private void stopExam() {
        MyLog.i(TAG,"结束考试");
        item_id = 0;
        currTotalScore = 100;
        tv_total_score.setText(String.valueOf(currTotalScore));
        iv_head.getDrawable().setLevel(0);
        OneTimeWorkRequest examStatusInitWork = OneTimeWorkRequest.from(ExamStatusInitWorker.class);
        WorkManager.getInstance(_mActivity).enqueue(examStatusInitWork);
        sendExamJson(0);
    }
 
    private ICEventListener icEventListener = new ICEventListener() {
        @Override
        public void onCEvent(String topic, int msgCode, int resultCode, Object obj) {
            speed  = (double)obj;
            mHandler.obtainMessage(SPEED_DATA).sendToTarget();
        }
    };
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        CEventCenter.onBindEvent(true,icEventListener,Constant.BIND_SPEED_TOPIC);
    }
 
    @Override
    public void onDetach() {
        super.onDetach();
        CEventCenter.onBindEvent(false,icEventListener,Constant.BIND_SPEED_TOPIC);
    }
}