#include "bucket_manager.h" #include #include using namespace std; void BucketManager::loadConfig(const json& config) { lock_guard lk(mtx_); buckets_.clear(); if (config.contains("buckets") && config["buckets"].is_array()) { for (const auto& bj : config["buckets"]) { BucketInfo b; b.fromJson(bj); buckets_.push_back(b); } } sort(buckets_.begin(), buckets_.end(), [](const BucketInfo& a, const BucketInfo& b) { return a.id < b.id; }); cout << "[Bucket] 加载 " << buckets_.size() << " 个工位" << endl; } json BucketManager::saveConfig() const { lock_guard lk(mtx_); json arr = json::array(); for (const auto& b : buckets_) { arr.push_back(b.toJson()); } return arr; } vector BucketManager::getBuckets() const { lock_guard lk(mtx_); return buckets_; } BucketInfo BucketManager::getBucket(int id) const { lock_guard lk(mtx_); for (const auto& b : buckets_) { if (b.id == id) return b; } return BucketInfo(); } void BucketManager::addBucket(const BucketInfo& bucket) { lock_guard lk(mtx_); for (const auto& b : buckets_) { if (b.id == bucket.id) { cout << "[Bucket] ID " << bucket.id << " 已存在" << endl; return; } } buckets_.push_back(bucket); cout << "[Bucket] 添加工位: " << bucket.description << endl; } void BucketManager::updateBucket(int id, const BucketInfo& bucket) { lock_guard lk(mtx_); for (auto& b : buckets_) { if (b.id == id) { b.description = bucket.description; b.code_x = bucket.code_x; b.code_y = bucket.code_y; b.leftCamera = bucket.leftCamera; b.rightCamera = bucket.rightCamera; cout << "[Bucket] 更新工位: " << bucket.description << endl; return; } } cout << "[Bucket] 未找到工位 ID=" << id << endl; } void BucketManager::removeBucket(int id) { lock_guard lk(mtx_); auto it = remove_if(buckets_.begin(), buckets_.end(), [id](const BucketInfo& b) { return b.id == id; }); if (it != buckets_.end()) { buckets_.erase(it, buckets_.end()); if (activeId_ == id) { activeId_ = -1; } cout << "[Bucket] 删除工位 ID=" << id << endl; } } void BucketManager::activateBucket(int id) { BucketInfo oldInfo, newInfo; bool hasOld = false, hasNew = false; { lock_guard lk(mtx_); int oldId = activeId_; for (auto& b : buckets_) { if (b.id == oldId) { b.isActive = false; oldInfo = b; hasOld = true; } if (b.id == id) { b.isActive = true; newInfo = b; hasNew = true; } } activeId_ = id; } if (hasOld || hasNew) { cout << "[Bucket] 切换工位: " << (hasOld ? oldInfo.description : "无") << " -> " << (hasNew ? newInfo.description : "无") << endl; if (onActivate_) { onActivate_(hasOld ? &oldInfo : nullptr, hasNew ? &newInfo : nullptr); } } } void BucketManager::checkAutoSwitch(const string& tag) { int targetId = -1; { lock_guard lk(mtx_); if (mode_ != SwitchMode::AUTO) return; for (const auto& b : buckets_) { if (b.code_x == tag && activeId_ != b.id) { targetId = b.id; break; } } } if (targetId >= 0) { activateBucket(targetId); } } void BucketManager::checkAutoSwitch(const vector& coils) { int targetId = -1; { lock_guard lk(mtx_); if (mode_ != SwitchMode::AUTO) return; for (const auto& b : buckets_) { try { int addrX = stoi(b.code_x); if (addrX < 0 || addrX >= (int)coils.size()) continue; bool match = false; if (!b.code_y.empty()) { int addrY = stoi(b.code_y); if (addrY >= 0 && addrY < (int)coils.size()) { match = coils[addrX] && coils[addrY]; } } else { match = coils[addrX]; } if (match && activeId_ != b.id) { targetId = b.id; break; } } catch (...) { continue; } } } if (targetId >= 0) { activateBucket(targetId); } } int BucketManager::getActiveBucketId() const { lock_guard lk(mtx_); return activeId_; } BucketInfo BucketManager::getActiveBucket() const { lock_guard lk(mtx_); for (const auto& b : buckets_) { if (b.id == activeId_) return b; } return BucketInfo(); } SwitchMode BucketManager::getSwitchMode() const { lock_guard lk(mtx_); return mode_; } void BucketManager::setSwitchMode(SwitchMode mode) { { lock_guard lk(mtx_); if (mode_ == mode) return; mode_ = mode; } cout << "[Bucket] 切换模式: " << (mode == SwitchMode::AUTO ? "自动" : "手动") << endl; if (onMode_) onMode_(mode); }