bucket_manager.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "bucket_manager.h"
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5. void BucketManager::loadConfig(const json& config) {
  6. lock_guard<mutex> lk(mtx_);
  7. buckets_.clear();
  8. if (config.contains("buckets") && config["buckets"].is_array()) {
  9. for (const auto& bj : config["buckets"]) {
  10. BucketInfo b;
  11. b.fromJson(bj);
  12. buckets_.push_back(b);
  13. }
  14. }
  15. sort(buckets_.begin(), buckets_.end(),
  16. [](const BucketInfo& a, const BucketInfo& b) { return a.id < b.id; });
  17. cout << "[Bucket] 加载 " << buckets_.size() << " 个工位" << endl;
  18. }
  19. json BucketManager::saveConfig() const {
  20. lock_guard<mutex> lk(mtx_);
  21. json arr = json::array();
  22. for (const auto& b : buckets_) {
  23. arr.push_back(b.toJson());
  24. }
  25. return arr;
  26. }
  27. vector<BucketInfo> BucketManager::getBuckets() const {
  28. lock_guard<mutex> lk(mtx_);
  29. return buckets_;
  30. }
  31. BucketInfo BucketManager::getBucket(int id) const {
  32. lock_guard<mutex> lk(mtx_);
  33. for (const auto& b : buckets_) {
  34. if (b.id == id) return b;
  35. }
  36. return BucketInfo();
  37. }
  38. void BucketManager::addBucket(const BucketInfo& bucket) {
  39. lock_guard<mutex> lk(mtx_);
  40. for (const auto& b : buckets_) {
  41. if (b.id == bucket.id) {
  42. cout << "[Bucket] ID " << bucket.id << " 已存在" << endl;
  43. return;
  44. }
  45. }
  46. buckets_.push_back(bucket);
  47. cout << "[Bucket] 添加工位: " << bucket.description << endl;
  48. }
  49. void BucketManager::updateBucket(int id, const BucketInfo& bucket) {
  50. lock_guard<mutex> lk(mtx_);
  51. for (auto& b : buckets_) {
  52. if (b.id == id) {
  53. b.description = bucket.description;
  54. b.code_x = bucket.code_x;
  55. b.code_y = bucket.code_y;
  56. b.leftCamera = bucket.leftCamera;
  57. b.rightCamera = bucket.rightCamera;
  58. cout << "[Bucket] 更新工位: " << bucket.description << endl;
  59. return;
  60. }
  61. }
  62. cout << "[Bucket] 未找到工位 ID=" << id << endl;
  63. }
  64. void BucketManager::removeBucket(int id) {
  65. lock_guard<mutex> lk(mtx_);
  66. auto it = remove_if(buckets_.begin(), buckets_.end(),
  67. [id](const BucketInfo& b) { return b.id == id; });
  68. if (it != buckets_.end()) {
  69. buckets_.erase(it, buckets_.end());
  70. if (activeId_ == id) {
  71. activeId_ = -1;
  72. }
  73. cout << "[Bucket] 删除工位 ID=" << id << endl;
  74. }
  75. }
  76. void BucketManager::activateBucket(int id) {
  77. BucketInfo oldInfo, newInfo;
  78. bool hasOld = false, hasNew = false;
  79. {
  80. lock_guard<mutex> lk(mtx_);
  81. int oldId = activeId_;
  82. for (auto& b : buckets_) {
  83. if (b.id == oldId) {
  84. b.isActive = false;
  85. oldInfo = b;
  86. hasOld = true;
  87. }
  88. if (b.id == id) {
  89. b.isActive = true;
  90. newInfo = b;
  91. hasNew = true;
  92. }
  93. }
  94. activeId_ = id;
  95. }
  96. if (hasOld || hasNew) {
  97. cout << "[Bucket] 切换工位: "
  98. << (hasOld ? oldInfo.description : "无") << " -> "
  99. << (hasNew ? newInfo.description : "无") << endl;
  100. if (onActivate_) {
  101. onActivate_(hasOld ? &oldInfo : nullptr, hasNew ? &newInfo : nullptr);
  102. }
  103. }
  104. }
  105. void BucketManager::checkAutoSwitch(const string& tag) {
  106. int targetId = -1;
  107. {
  108. lock_guard<mutex> lk(mtx_);
  109. if (mode_ != SwitchMode::AUTO) return;
  110. for (const auto& b : buckets_) {
  111. if (b.code_x == tag && activeId_ != b.id) {
  112. targetId = b.id;
  113. break;
  114. }
  115. }
  116. }
  117. if (targetId >= 0) {
  118. activateBucket(targetId);
  119. }
  120. }
  121. void BucketManager::checkAutoSwitch(const vector<bool>& coils) {
  122. int targetId = -1;
  123. {
  124. lock_guard<mutex> lk(mtx_);
  125. if (mode_ != SwitchMode::AUTO) return;
  126. for (const auto& b : buckets_) {
  127. try {
  128. int addrX = stoi(b.code_x);
  129. if (addrX < 0 || addrX >= (int)coils.size()) continue;
  130. bool match = false;
  131. if (!b.code_y.empty()) {
  132. int addrY = stoi(b.code_y);
  133. if (addrY >= 0 && addrY < (int)coils.size()) {
  134. match = coils[addrX] && coils[addrY];
  135. }
  136. } else {
  137. match = coils[addrX];
  138. }
  139. if (match && activeId_ != b.id) {
  140. targetId = b.id;
  141. break;
  142. }
  143. } catch (...) {
  144. continue;
  145. }
  146. }
  147. }
  148. if (targetId >= 0) {
  149. activateBucket(targetId);
  150. }
  151. }
  152. int BucketManager::getActiveBucketId() const {
  153. lock_guard<mutex> lk(mtx_);
  154. return activeId_;
  155. }
  156. BucketInfo BucketManager::getActiveBucket() const {
  157. lock_guard<mutex> lk(mtx_);
  158. for (const auto& b : buckets_) {
  159. if (b.id == activeId_) return b;
  160. }
  161. return BucketInfo();
  162. }
  163. SwitchMode BucketManager::getSwitchMode() const {
  164. lock_guard<mutex> lk(mtx_);
  165. return mode_;
  166. }
  167. void BucketManager::setSwitchMode(SwitchMode mode) {
  168. {
  169. lock_guard<mutex> lk(mtx_);
  170. if (mode_ == mode) return;
  171. mode_ = mode;
  172. }
  173. cout << "[Bucket] 切换模式: " << (mode == SwitchMode::AUTO ? "自动" : "手动") << endl;
  174. if (onMode_) onMode_(mode);
  175. }