yy1717
2021-01-19 87156fa3adfa2e3232a6f6e612584aa8a4ebaea1
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
//
// Created by YY on 2020/3/9.
//
 
#include <cstdlib>
#include "../common/apptimer.h"
#include "dummy_light.h"
#include "../native-lib.h"
#include "../jni_log.h"
#include "../test_common/car_sensor.h"
 
#define DEBUG(fmt, args...)     LOGD("<road_exam dummy_light> <%s>: " fmt, __func__, ##args)
 
static struct RtkTime currRtkTime;
static struct dummy_light_exam *content;
static int contentNum;
 
static int question;
 
static vector<int> process;
 
static bool testing;
 
static void CheckSolution(union sigval sig);
static void ExamDummyLight(void);
 
void StartDummyLightExam(struct dummy_light_exam *ptr, int num, const struct RtkTime* rtkTime)
{
    DEBUG("StartDummyLightExam");
    content = ptr;
    contentNum = num;
    question = 0;
 
    if (content != NULL && num > 0) {
        DEBUG("启动灯光");
        currRtkTime = *rtkTime;
 
        for (int i = 0; i < contentNum; ++i) {
            DEBUG("灯光项目 <%d> item %d, TTS %s", i, content[i].item, content[i].tts.c_str());
        }
        testing = true;
 
        AppTimer_delete(CheckSolution);
        ExamDummyLight();
    } else {
        testing = false;
    }
}
 
bool ExecuteDummyLightExam(const struct RtkTime* rtkTime)
{
    currRtkTime = *rtkTime;
    return testing;
}
 
void DummyLightTTSDone(int id)
{
    // 等语音播报完毕后计时
    if (testing) {
        vector<int>().swap(process);
 
        // 预读取有中间操作步骤的灯光
        for (int i = 0; i < content[question].process.size(); ++i) {
            if (ReadCarStatus((content[question].process[i]>>8) & 0xFF) == content[question].process[i] & 0xFF) {
                process.push_back(content[question].process[i]);
            }
        }
 
        AppTimer_delete(CheckSolution);
        AppTimer_add(CheckSolution, D_SEC(5));
    }
}
 
void TerminateDummyLightExam(void)
{
    testing = false;
    AppTimer_delete(CheckSolution);
}
 
// 记录中间过程
void handleLigthExam(uint16_t id, int value)
{
    if (testing) {
        for (int i = 0; i < content[question].process.size(); ++i) {
            if (id == ((content[question].process[i] >> 8) & 0xFF) && value == (content[question].process[i] & 0xFF)) {
                if (process.size() == 0 || process.back() != content[question].process[i]) {
                    process.push_back(content[question].process[i]);
                }
            }
        }
    }
}
 
// 检查最终状态
static void CheckSolution(union sigval sig)
{
    AppTimer_delete(CheckSolution);
 
    if (content[question].process.size() > 0) {
        if (content[question].process.size() != process.size()) {
            AddExamFault(content[question].wrongCode, &currRtkTime);
            goto NEXT_LIGHT;
        } else {
            // 操作顺序也要满足
            for (int i = 0; i < content[question].process.size(); ++i) {
                if (process[i] != content[question].process[i]) {
                    AddExamFault(content[question].wrongCode, &currRtkTime);
                    goto NEXT_LIGHT;
                }
            }
        }
    }
 
    for (int i = 0; i < content[question].solution.size(); ++i) {
        if (ReadCarStatus((content[question].solution[i]>>8)&0xFF) != (content[question].solution[i] & 0xFF)) {
            AddExamFault(content[question].wrongCode, &currRtkTime);
            break;
        }
    }
 
NEXT_LIGHT:
    question++;
    ExamDummyLight();
}
 
static void ExamDummyLight(void)
{
    if (testing && question < contentNum) {
        DEBUG("灯光题目: %s", content[question].tts.c_str());
        PlayTTS(content[question].tts, DummyLightTTSDone);
    } else {
        testing = false;
    }
}