这幽灵车终于跑起来了
This commit is contained in:
parent
ab830fcd9e
commit
ccf82dd0f5
|
@ -12,7 +12,7 @@ class BusFCFSStrategy : public BusStrategyBase
|
|||
|
||||
bus_query_t *GetTargetQuery();
|
||||
|
||||
bus_query_t *HandleQuery();
|
||||
bus_query_t *HandleBTWQuery();
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ class BusSCANStrategy : public BusStrategyBase
|
|||
|
||||
bus_query_t *GetTargetQuery();
|
||||
|
||||
bus_query_t *HandleQuery();
|
||||
bus_query_t *HandleBTWQuery();
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ class BusSSTFStrategy : public BusStrategyBase
|
|||
|
||||
bus_query_t *GetTargetQuery();
|
||||
|
||||
bus_query_t *HandleQuery();
|
||||
bus_query_t *HandleBTWQuery();
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -40,17 +40,17 @@ public:
|
|||
virtual bus_query_t *GetTargetQuery() = 0;
|
||||
|
||||
/**
|
||||
* 获取公交车现在可以处理的请求
|
||||
* 获取公交车现在可以顺便处理的请求
|
||||
* @return 请求指针
|
||||
*/
|
||||
virtual bus_query_t *HandleQuery() = 0;
|
||||
virtual bus_query_t *HandleBTWQuery() = 0;
|
||||
|
||||
signals:
|
||||
/**
|
||||
* 删除请求信号
|
||||
* @param query 需要删除请求的指针
|
||||
*/
|
||||
void DeleteQuerySignal(bus_query_t *query);
|
||||
void DeleteQuerySignal(int query_type, int node_id);
|
||||
|
||||
/**
|
||||
* 打印状态信号
|
||||
|
@ -58,8 +58,25 @@ public:
|
|||
*/
|
||||
void PrintStateSignal(QString string);
|
||||
|
||||
/**
|
||||
* 获得公交车前进方向的信号
|
||||
*/
|
||||
void GetBusDirectionSignal();
|
||||
|
||||
/**
|
||||
* 运行公交车的信号
|
||||
* @param direction 公交车前进的方向
|
||||
* @param duration 前进需要的时间
|
||||
*/
|
||||
void BusRunningSignal(int direction, int duration);
|
||||
|
||||
public slots:
|
||||
void AppendQuerySlot(int query_type, int node_id) const;
|
||||
/**
|
||||
* 添加请求的槽函数
|
||||
* @param query_type 请求的类别
|
||||
* @param node_id 请求的站点ID
|
||||
*/
|
||||
void AppendQuerySlot(int query_type, int node_id);
|
||||
|
||||
/**
|
||||
* 处理开始事件的槽函数
|
||||
|
@ -76,6 +93,10 @@ public slots:
|
|||
*/
|
||||
void OneTickSlot(int remaining_time);
|
||||
|
||||
void GetBusDirectionSlot();
|
||||
|
||||
void OnStopSlot();
|
||||
|
||||
private:
|
||||
/**
|
||||
* 储存当前的状态
|
||||
|
@ -91,6 +112,7 @@ private:
|
|||
|
||||
void SetConnection() const;
|
||||
|
||||
void HandleQuery();
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -55,21 +55,12 @@ int BusFCFSStrategy::GetBusDirection()
|
|||
|
||||
bus_query_t *BusFCFSStrategy::GetTargetQuery()
|
||||
{
|
||||
// FCFS只处理第一个请求
|
||||
return query_model->queries;
|
||||
}
|
||||
|
||||
bus_query_t *BusFCFSStrategy::HandleQuery()
|
||||
bus_query_t *BusFCFSStrategy::HandleBTWQuery()
|
||||
{
|
||||
bus_query_t *result = NULL;
|
||||
bus_query_t *query = query_model->queries;
|
||||
|
||||
if(query != nullptr)
|
||||
{
|
||||
if(bus_model->rail_pos == query->node)
|
||||
{
|
||||
result = query;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
// FCFS不进行顺便处理
|
||||
return nullptr;
|
||||
}
|
||||
|
|
|
@ -116,7 +116,7 @@ bus_query_t *BusSCANStrategy::GetTargetQuery()
|
|||
}
|
||||
}
|
||||
|
||||
bus_query_t *BusSCANStrategy::HandleQuery()
|
||||
bus_query_t *BusSCANStrategy::HandleBTWQuery()
|
||||
{
|
||||
//获取公交车当前所在站点
|
||||
rail_node_t *now_position = bus_model->rail_pos;
|
||||
|
|
|
@ -28,7 +28,7 @@ int BusSSTFStrategy::GetBusDirection()
|
|||
}
|
||||
}
|
||||
|
||||
bus_query_t *BusSSTFStrategy::HandleQuery()
|
||||
bus_query_t *BusSSTFStrategy::HandleBTWQuery()
|
||||
{
|
||||
// 这里只是处理顺便服务的代码
|
||||
|
||||
|
|
|
@ -11,6 +11,8 @@ BusStrategyBase::BusStrategyBase()
|
|||
bus_model = new BusModel;
|
||||
|
||||
bus_tick = 0;
|
||||
|
||||
SetConnection();
|
||||
}
|
||||
|
||||
BusStrategyBase::~BusStrategyBase()
|
||||
|
@ -22,13 +24,20 @@ BusStrategyBase::~BusStrategyBase()
|
|||
|
||||
void BusStrategyBase::SetConnection() const
|
||||
{
|
||||
|
||||
QObject::connect(this, &BusStrategyBase::GetBusDirectionSignal,
|
||||
this, &BusStrategyBase::GetBusDirectionSlot);
|
||||
}
|
||||
|
||||
void BusStrategyBase::AppendQuerySlot(int query_type, int node_id) const
|
||||
void BusStrategyBase::AppendQuerySlot(int query_type, int node_id)
|
||||
{
|
||||
rail_node_t *node = rails_model->FindNode(node_id);
|
||||
query_model->CreateQuery(query_type, node);
|
||||
|
||||
// 如果公交车停车且在系统在运行
|
||||
if(bus_model->direction == BUS_STOP && status == BUS_RUNNING)
|
||||
{
|
||||
emit GetBusDirectionSignal();
|
||||
}
|
||||
}
|
||||
|
||||
void BusStrategyBase::OneTickSlot(int remaining_time)
|
||||
|
@ -48,6 +57,8 @@ void BusStrategyBase::BusBeginSlot()
|
|||
// 在一开始先打印一下状态
|
||||
QString str = PrintState(0);
|
||||
emit PrintStateSignal(str);
|
||||
|
||||
emit GetBusDirectionSignal();
|
||||
}
|
||||
|
||||
void BusStrategyBase::BusEndSlot()
|
||||
|
@ -58,6 +69,80 @@ void BusStrategyBase::BusEndSlot()
|
|||
bus_model->ResetBus(rails_model->rails);
|
||||
}
|
||||
|
||||
void BusStrategyBase::GetBusDirectionSlot()
|
||||
{
|
||||
bus_model->target_query = GetTargetQuery();
|
||||
bus_model->direction = GetBusDirection();
|
||||
|
||||
// 如果没有目标请求,跳出循环
|
||||
if(bus_model->target_query == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int duration = 0;
|
||||
|
||||
switch (bus_model->direction)
|
||||
{
|
||||
case BUS_CLOCK_WISE:
|
||||
duration = bus_model->rail_pos->next_node_distance / bus_model->velocity * 1000;
|
||||
break;
|
||||
case BUS_COUNTER_CLOCK_WISE:
|
||||
duration = bus_model->rail_pos->last_node_distance / bus_model->velocity * 1000;
|
||||
break;
|
||||
case BUS_STOP:
|
||||
duration = 1000;
|
||||
break;
|
||||
}
|
||||
|
||||
emit BusRunningSignal(bus_model->direction, duration);
|
||||
}
|
||||
|
||||
void BusStrategyBase::HandleQuery()
|
||||
{
|
||||
if(bus_model->target_query != nullptr)
|
||||
{
|
||||
if(bus_model->target_query->node == bus_model->rail_pos)
|
||||
{
|
||||
// 如果已经到站
|
||||
while (bus_model->target_query != nullptr and
|
||||
bus_model->target_query->node == bus_model->rail_pos)
|
||||
{
|
||||
emit DeleteQuerySignal(bus_model->target_query->type, bus_model->target_query->node->id);
|
||||
query_model->DeleteQuery(bus_model->target_query);
|
||||
bus_model->target_query = GetTargetQuery();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 没有到站就进行顺便处理
|
||||
bus_query_t *query = HandleBTWQuery();
|
||||
|
||||
while(query != nullptr)
|
||||
{
|
||||
emit DeleteQuerySignal(query->type, query->node->id);
|
||||
query_model->DeleteQuery(query);
|
||||
query = HandleBTWQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BusStrategyBase::OnStopSlot()
|
||||
{
|
||||
if(bus_model->direction == BUS_CLOCK_WISE)
|
||||
{
|
||||
bus_model->rail_pos = bus_model->rail_pos->next_node;
|
||||
}
|
||||
else if(bus_model->direction == BUS_COUNTER_CLOCK_WISE)
|
||||
{
|
||||
bus_model->rail_pos = bus_model->rail_pos->last_node;
|
||||
}
|
||||
HandleQuery();
|
||||
|
||||
emit GetBusDirectionSignal();
|
||||
}
|
||||
|
||||
QString BusStrategyBase::PrintState(int remaining_time) const
|
||||
{
|
||||
int count = 0;
|
||||
|
|
|
@ -44,8 +44,13 @@ void CentralWidget::SetControlConnection()
|
|||
QObject::connect(controller, &BusStrategyBase::DeleteQuerySignal,
|
||||
this, &CentralWidget::DeleteQueryItemSlot);
|
||||
|
||||
// 设置打印状态事件的连接
|
||||
QObject::connect(controller, &BusStrategyBase::PrintStateSignal,
|
||||
this, &CentralWidget::PrintStateSlot);
|
||||
|
||||
QObject::connect(controller, &BusStrategyBase::BusRunningSignal,
|
||||
this, &CentralWidget::BeginBusAnimationSlot);
|
||||
|
||||
}
|
||||
|
||||
void CentralWidget::SetWidgetConnection()
|
||||
|
@ -94,14 +99,18 @@ void CentralWidget::AppendQueryItemSlot(int query_type, int node_id)
|
|||
ui->query_list->setItemWidget(widget_item, item);
|
||||
}
|
||||
|
||||
void CentralWidget::DeleteQueryItemSlot(bus_query_t *query)
|
||||
void CentralWidget::DeleteQueryItemSlot(int query_type, int node_id)
|
||||
{
|
||||
// 由于请求列表头的存在,编号从1开始
|
||||
int query_id = 1;
|
||||
|
||||
for(auto itor = query_items.begin(); itor != query_items.end(); ++itor)
|
||||
// 先排除表头的影响
|
||||
auto first_item = query_items.begin();
|
||||
first_item++;
|
||||
|
||||
for(auto itor = first_item; itor != query_items.end(); ++itor)
|
||||
{
|
||||
if((*itor)->query_type == query->type and (*itor)->target_node_id == query->node->id)
|
||||
if((*itor)->query_type == query_type and (*itor)->target_node_id == node_id)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
@ -118,8 +127,8 @@ void CentralWidget::DeleteQueryItemSlot(bus_query_t *query)
|
|||
}
|
||||
|
||||
delete deleted_item;
|
||||
query_items.erase(pos);
|
||||
delete *pos;
|
||||
query_items.erase(pos);
|
||||
}
|
||||
|
||||
void CentralWidget::PrintStateSlot(const QString& string)
|
||||
|
@ -132,6 +141,11 @@ void CentralWidget::PrintStateSlot(const QString& string)
|
|||
ui->text_output->setTextCursor(cursor);
|
||||
}
|
||||
|
||||
void CentralWidget::BeginBusAnimationSlot(int direction, int duration)
|
||||
{
|
||||
scene_manager->BeginBusAnimation(direction, duration);
|
||||
}
|
||||
|
||||
void CentralWidget::AddQueryButtonClicked()
|
||||
{
|
||||
int query_type = ui->query_type_combo->currentIndex();
|
||||
|
|
|
@ -56,7 +56,7 @@ public slots:
|
|||
* 处理删除一个请求的槽函数
|
||||
* @param query 请求指针
|
||||
*/
|
||||
void DeleteQueryItemSlot(bus_query_t *query);
|
||||
void DeleteQueryItemSlot(int query_type, int node_id);
|
||||
|
||||
/**
|
||||
* 处理点击添加请求按钮的槽函数
|
||||
|
@ -69,6 +69,8 @@ public slots:
|
|||
*/
|
||||
void PrintStateSlot(const QString& string);
|
||||
|
||||
void BeginBusAnimationSlot(int direction, int duration);
|
||||
|
||||
private:
|
||||
/**
|
||||
* UI控件
|
||||
|
|
|
@ -79,6 +79,8 @@ public slots:
|
|||
*/
|
||||
void OneTickSlot();
|
||||
|
||||
void BeginBusTimerSlot(int direction, int duration);
|
||||
|
||||
private:
|
||||
/**
|
||||
* 设置一时刻的长度,单位毫秒
|
||||
|
|
|
@ -25,6 +25,9 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
|
|||
|
||||
//开始多线程事件循环
|
||||
worker_thread->start();
|
||||
|
||||
// 设置计时器只工作一次
|
||||
bus_timer->setSingleShot(true);
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
|
@ -80,6 +83,12 @@ void MainWindow::SetControlConnection()
|
|||
// 每一tick连接
|
||||
QObject::connect(this, &MainWindow::BusTickSignal,
|
||||
controller, &BusStrategyBase::OneTickSlot);
|
||||
|
||||
QObject::connect(bus_timer, &QTimer::timeout,
|
||||
controller, &BusStrategyBase::OnStopSlot);
|
||||
|
||||
QObject::connect(controller, &BusStrategyBase::BusRunningSignal,
|
||||
this, &MainWindow::BeginBusTimerSlot);
|
||||
}
|
||||
|
||||
void MainWindow::ReadConfigFileButtonClicked()
|
||||
|
@ -150,3 +159,8 @@ void MainWindow::EndTickTimerSlot()
|
|||
{
|
||||
tick_timer->stop();
|
||||
}
|
||||
|
||||
void MainWindow::BeginBusTimerSlot(int direction, int duration)
|
||||
{
|
||||
bus_timer->start(duration);
|
||||
}
|
Loading…
Reference in New Issue
Block a user