获得方向与处理请求两个槽函数完成

This commit is contained in:
jackfiled 2022-06-26 18:10:47 +08:00
parent 04d6ab09ee
commit c9b7908e4c
2 changed files with 146 additions and 12 deletions

View File

@ -68,7 +68,16 @@ public:
*/ */
~BusControllerModel(); ~BusControllerModel();
/**
*
* @return
*/
std::string PrintState(); std::string PrintState();
/**
*
* @return
*/
bool JudgeOnStation(); bool JudgeOnStation();
public slots: public slots:
@ -85,6 +94,22 @@ public slots:
*/ */
void AddQuerySlot(int query_type, int node_id) const; void AddQuerySlot(int query_type, int node_id) const;
/**
*
* @param query
*/
void DeleteQuerySlot(bus_query_t *query) const;
/**
*
*/
void GetBusDirectionSlot();
/**
*
*/
void HandleQuerySlot();
signals: signals:
/** /**
* *
@ -93,9 +118,10 @@ signals:
void RailsCreated(int node_num); void RailsCreated(int node_num);
/** /**
* *
* @param query
*/ */
void QueryChangedSignal(); void DeleteQuerySignal(bus_query_t *query);
private: private:
@ -104,6 +130,10 @@ private:
*/ */
int total_distance = 10; int total_distance = 10;
bus_query_t *target_query;
void SetConnection() const;
/** /**
* *
* @return * @return
@ -145,10 +175,9 @@ private:
/** /**
* *
* SSTF策略中使用 * SSTF策略中使用
* @param query
* @return * @return
*/ */
int SSTFDirection(bus_query_t* query); int SSTFDirection();
/** /**
* 便 * 便
@ -168,7 +197,7 @@ private:
* @param query * @param query
* @return * @return
*/ */
int SCANDirection(bus_query_t *query); int SCANDirection();
/** /**
* 便 * 便

View File

@ -13,9 +13,12 @@ BusControllerModel::BusControllerModel()
rail_pos = rail_manager->rails; rail_pos = rail_manager->rails;
distance = 0; distance = 0;
direction = BUS_STOP; direction = BUS_STOP;
target_query = nullptr;
// 设置初始时间 // 设置初始时间
bus_time = 0; bus_time = 0;
SetConnection();
} }
BusControllerModel::~BusControllerModel() BusControllerModel::~BusControllerModel()
@ -24,6 +27,12 @@ BusControllerModel::~BusControllerModel()
delete(query_manager); delete(query_manager);
} }
void BusControllerModel::SetConnection() const
{
QObject::connect(this, &BusControllerModel::DeleteQuerySignal,
this, &BusControllerModel::DeleteQuerySlot);
}
void BusControllerModel::ReadConfigFileSlot(const QString& file_name) void BusControllerModel::ReadConfigFileSlot(const QString& file_name)
{ {
ReadConfigFile(file_name.toStdString()); ReadConfigFile(file_name.toStdString());
@ -35,6 +44,102 @@ void BusControllerModel::AddQuerySlot(int query_type, int node_id) const
query_manager->CreateQuery(query_type, node_pos); query_manager->CreateQuery(query_type, node_pos);
} }
void BusControllerModel::DeleteQuerySlot(bus_query_t *query) const
{
query_manager->DeleteQuery(query);
}
void BusControllerModel::GetBusDirectionSlot()
{
switch (chosen_strategy)
{
case BUS_FCFS:
direction = FCFSDirection();
break;
case BUS_SSTF:
if(target_query == nullptr)
{
target_query = SSTFGetQuery();
}
direction = SSTFDirection();
case BUS_SCAN:
if(target_query == nullptr)
{
target_query = SCANGetQuery();
}
direction = SCANDirection();
default:
break;
}
}
void BusControllerModel::HandleQuerySlot()
{
bus_query_t *finished_query;
switch (chosen_strategy)
{
case BUS_FCFS:
finished_query = FCFSQuery();
while (finished_query != nullptr)
{
emit DeleteQuerySignal(finished_query);
finished_query = FCFSQuery();
}
break;
case BUS_SSTF:
if(target_query == nullptr)
{
// 这里可能需要处理一下新生成的请求可以处理的情况
}
else if(target_query->node == rail_pos)
{
// 到达目标站点
while (target_query != nullptr and target_query->node == rail_pos)
{
emit DeleteQuerySignal(target_query);
target_query = SSTFGetQuery();
}
}
else
{
// 顺便处理请求
finished_query = SSTFBTWQuery();
while (finished_query != nullptr)
{
emit DeleteQuerySignal(finished_query);
finished_query = SSTFBTWQuery();
}
}
break;
case BUS_SCAN:
if(target_query == nullptr)
{
// 这里可能需要处理一下新生成的请求可以处理的情况
}
else if(target_query->node == rail_pos)
{
// 到达目标站点
while (target_query != nullptr and target_query->node == rail_pos)
{
emit DeleteQuerySignal(target_query);
target_query = SCANGetQuery();
}
}
else
{
// 顺便处理
finished_query = SCANBTWQuery();
while (finished_query != nullptr)
{
emit DeleteQuerySignal(finished_query);
finished_query = SCANBTWQuery();
}
}
}
}
/* /*
* auto_pilot_bus * auto_pilot_bus
* C写成 * C写成
@ -416,14 +521,14 @@ bus_query_t *BusControllerModel::SSTFGetQuery()
return query; return query;
} }
int BusControllerModel::SSTFDirection(bus_query_t *query) int BusControllerModel::SSTFDirection()
{ {
if (query == nullptr) if (target_query == nullptr)
{ {
return BUS_STOP; return BUS_STOP;
} }
int length = GetQueryDistance(query, BUS_CLOCK_WISE); int length = GetQueryDistance(target_query, BUS_CLOCK_WISE);
if(length > total_distance / 2) if(length > total_distance / 2)
{ {
return BUS_COUNTER_CLOCK_WISE; return BUS_COUNTER_CLOCK_WISE;
@ -530,16 +635,16 @@ bus_query_t *BusControllerModel::SCANGetQuery()
} }
} }
int BusControllerModel::SCANDirection(bus_query_t *query) int BusControllerModel::SCANDirection()
{ {
if(query == nullptr) if(target_query == nullptr)
{ {
return BUS_STOP; return BUS_STOP;
} }
if(direction == BUS_STOP) if(direction == BUS_STOP)
{ {
int length = GetQueryDistance(query, BUS_CLOCK_WISE); int length = GetQueryDistance(target_query, BUS_CLOCK_WISE);
if(length > total_distance / 2) if(length > total_distance / 2)
{ {
return BUS_COUNTER_CLOCK_WISE; return BUS_COUNTER_CLOCK_WISE;
@ -551,7 +656,7 @@ int BusControllerModel::SCANDirection(bus_query_t *query)
} }
else else
{ {
int length = GetQueryDistance(query, direction); int length = GetQueryDistance(target_query, direction);
if(length > total_distance / 2) if(length > total_distance / 2)
{ {
if(direction == BUS_CLOCK_WISE) if(direction == BUS_CLOCK_WISE)