这幽灵车终于跑起来了

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 *HandleQuery();
bus_query_t *HandleBTWQuery();
};

View File

@ -13,7 +13,7 @@ class BusSCANStrategy : public BusStrategyBase
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 *HandleQuery();
bus_query_t *HandleBTWQuery();
};

View File

@ -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();
};

View File

@ -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;
}

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;

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_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;

View File

@ -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();

View File

@ -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控件

View File

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

View File

@ -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);
}