这幽灵车终于跑起来了

This commit is contained in:
jackfiled 2022-06-28 21:15:50 +08:00
parent ab830fcd9e
commit ccf82dd0f5
12 changed files with 160 additions and 30 deletions

View File

@ -12,7 +12,7 @@ class BusFCFSStrategy : public BusStrategyBase
bus_query_t *GetTargetQuery(); bus_query_t *GetTargetQuery();
bus_query_t *HandleQuery(); bus_query_t *HandleBTWQuery();
}; };

View File

@ -13,7 +13,7 @@ class BusSCANStrategy : public BusStrategyBase
bus_query_t *GetTargetQuery(); bus_query_t *GetTargetQuery();
bus_query_t *HandleQuery(); bus_query_t *HandleBTWQuery();
}; };

View File

@ -12,7 +12,7 @@ class BusSSTFStrategy : public BusStrategyBase
bus_query_t *GetTargetQuery(); bus_query_t *GetTargetQuery();
bus_query_t *HandleQuery(); bus_query_t *HandleBTWQuery();
}; };

View File

@ -40,17 +40,17 @@ public:
virtual bus_query_t *GetTargetQuery() = 0; virtual bus_query_t *GetTargetQuery() = 0;
/** /**
* * 便
* @return * @return
*/ */
virtual bus_query_t *HandleQuery() = 0; virtual bus_query_t *HandleBTWQuery() = 0;
signals: signals:
/** /**
* *
* @param query * @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 PrintStateSignal(QString string);
/**
*
*/
void GetBusDirectionSignal();
/**
*
* @param direction
* @param duration
*/
void BusRunningSignal(int direction, int duration);
public slots: 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 OneTickSlot(int remaining_time);
void GetBusDirectionSlot();
void OnStopSlot();
private: private:
/** /**
* *
@ -91,6 +112,7 @@ private:
void SetConnection() const; void SetConnection() const;
void HandleQuery();
}; };

View File

@ -55,21 +55,12 @@ int BusFCFSStrategy::GetBusDirection()
bus_query_t *BusFCFSStrategy::GetTargetQuery() bus_query_t *BusFCFSStrategy::GetTargetQuery()
{ {
// FCFS只处理第一个请求
return query_model->queries; return query_model->queries;
} }
bus_query_t *BusFCFSStrategy::HandleQuery() bus_query_t *BusFCFSStrategy::HandleBTWQuery()
{ {
bus_query_t *result = NULL; // FCFS不进行顺便处理
bus_query_t *query = query_model->queries; return nullptr;
if(query != nullptr)
{
if(bus_model->rail_pos == query->node)
{
result = query;
}
}
return result;
} }

View File

@ -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; rail_node_t *now_position = bus_model->rail_pos;

View File

@ -28,7 +28,7 @@ int BusSSTFStrategy::GetBusDirection()
} }
} }
bus_query_t *BusSSTFStrategy::HandleQuery() bus_query_t *BusSSTFStrategy::HandleBTWQuery()
{ {
// 这里只是处理顺便服务的代码 // 这里只是处理顺便服务的代码

View File

@ -11,6 +11,8 @@ BusStrategyBase::BusStrategyBase()
bus_model = new BusModel; bus_model = new BusModel;
bus_tick = 0; bus_tick = 0;
SetConnection();
} }
BusStrategyBase::~BusStrategyBase() BusStrategyBase::~BusStrategyBase()
@ -22,13 +24,20 @@ BusStrategyBase::~BusStrategyBase()
void BusStrategyBase::SetConnection() const 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); rail_node_t *node = rails_model->FindNode(node_id);
query_model->CreateQuery(query_type, node); query_model->CreateQuery(query_type, node);
// 如果公交车停车且在系统在运行
if(bus_model->direction == BUS_STOP && status == BUS_RUNNING)
{
emit GetBusDirectionSignal();
}
} }
void BusStrategyBase::OneTickSlot(int remaining_time) void BusStrategyBase::OneTickSlot(int remaining_time)
@ -48,6 +57,8 @@ void BusStrategyBase::BusBeginSlot()
// 在一开始先打印一下状态 // 在一开始先打印一下状态
QString str = PrintState(0); QString str = PrintState(0);
emit PrintStateSignal(str); emit PrintStateSignal(str);
emit GetBusDirectionSignal();
} }
void BusStrategyBase::BusEndSlot() void BusStrategyBase::BusEndSlot()
@ -58,6 +69,80 @@ void BusStrategyBase::BusEndSlot()
bus_model->ResetBus(rails_model->rails); 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 QString BusStrategyBase::PrintState(int remaining_time) const
{ {
int count = 0; int count = 0;

View File

@ -44,8 +44,13 @@ void CentralWidget::SetControlConnection()
QObject::connect(controller, &BusStrategyBase::DeleteQuerySignal, QObject::connect(controller, &BusStrategyBase::DeleteQuerySignal,
this, &CentralWidget::DeleteQueryItemSlot); this, &CentralWidget::DeleteQueryItemSlot);
// 设置打印状态事件的连接
QObject::connect(controller, &BusStrategyBase::PrintStateSignal, QObject::connect(controller, &BusStrategyBase::PrintStateSignal,
this, &CentralWidget::PrintStateSlot); this, &CentralWidget::PrintStateSlot);
QObject::connect(controller, &BusStrategyBase::BusRunningSignal,
this, &CentralWidget::BeginBusAnimationSlot);
} }
void CentralWidget::SetWidgetConnection() void CentralWidget::SetWidgetConnection()
@ -94,21 +99,25 @@ void CentralWidget::AppendQueryItemSlot(int query_type, int node_id)
ui->query_list->setItemWidget(widget_item, item); ui->query_list->setItemWidget(widget_item, item);
} }
void CentralWidget::DeleteQueryItemSlot(bus_query_t *query) void CentralWidget::DeleteQueryItemSlot(int query_type, int node_id)
{ {
// 由于请求列表头的存在编号从1开始 // 由于请求列表头的存在编号从1开始
int query_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; break;
} }
query_id++; query_id++;
} }
QListWidgetItem *deleted_item = ui->query_list->takeItem(query_id); QListWidgetItem *deleted_item = ui->query_list->takeItem(query_id);
auto pos = query_items.begin(); auto pos = query_items.begin();
@ -118,8 +127,8 @@ void CentralWidget::DeleteQueryItemSlot(bus_query_t *query)
} }
delete deleted_item; delete deleted_item;
query_items.erase(pos);
delete *pos; delete *pos;
query_items.erase(pos);
} }
void CentralWidget::PrintStateSlot(const QString& string) void CentralWidget::PrintStateSlot(const QString& string)
@ -132,6 +141,11 @@ void CentralWidget::PrintStateSlot(const QString& string)
ui->text_output->setTextCursor(cursor); ui->text_output->setTextCursor(cursor);
} }
void CentralWidget::BeginBusAnimationSlot(int direction, int duration)
{
scene_manager->BeginBusAnimation(direction, duration);
}
void CentralWidget::AddQueryButtonClicked() void CentralWidget::AddQueryButtonClicked()
{ {
int query_type = ui->query_type_combo->currentIndex(); int query_type = ui->query_type_combo->currentIndex();

View File

@ -56,7 +56,7 @@ public slots:
* *
* @param query * @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 PrintStateSlot(const QString& string);
void BeginBusAnimationSlot(int direction, int duration);
private: private:
/** /**
* UI控件 * UI控件

View File

@ -79,6 +79,8 @@ public slots:
*/ */
void OneTickSlot(); void OneTickSlot();
void BeginBusTimerSlot(int direction, int duration);
private: private:
/** /**
* *

View File

@ -25,6 +25,9 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
//开始多线程事件循环 //开始多线程事件循环
worker_thread->start(); worker_thread->start();
// 设置计时器只工作一次
bus_timer->setSingleShot(true);
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@ -80,6 +83,12 @@ void MainWindow::SetControlConnection()
// 每一tick连接 // 每一tick连接
QObject::connect(this, &MainWindow::BusTickSignal, QObject::connect(this, &MainWindow::BusTickSignal,
controller, &BusStrategyBase::OneTickSlot); controller, &BusStrategyBase::OneTickSlot);
QObject::connect(bus_timer, &QTimer::timeout,
controller, &BusStrategyBase::OnStopSlot);
QObject::connect(controller, &BusStrategyBase::BusRunningSignal,
this, &MainWindow::BeginBusTimerSlot);
} }
void MainWindow::ReadConfigFileButtonClicked() void MainWindow::ReadConfigFileButtonClicked()
@ -150,3 +159,8 @@ void MainWindow::EndTickTimerSlot()
{ {
tick_timer->stop(); tick_timer->stop();
} }
void MainWindow::BeginBusTimerSlot(int direction, int duration)
{
bus_timer->start(duration);
}