NEW: parse new protocols

Change-Id: Ie2b8963552e576260f461e33002ec6e8606e6fa9
Signed-off-by: Stone Li <stone.li@bambulab.com>
This commit is contained in:
Stone Li 2022-11-10 19:32:25 +08:00 committed by Lane.Wei
parent 72b4827f85
commit 4385fc9d94
2 changed files with 61 additions and 20 deletions

View file

@ -334,6 +334,7 @@ MachineObject::MachineObject(NetworkAgent* agent, std::string name, std::string
ams_insert_flag = false; ams_insert_flag = false;
ams_power_on_flag = false; ams_power_on_flag = false;
ams_support_use_ams = false; ams_support_use_ams = false;
ams_humidity = -1;
/* signals */ /* signals */
wifi_signal = ""; wifi_signal = "";
@ -1119,6 +1120,12 @@ void MachineObject::parse_state_changed_event()
last_mc_print_stage = mc_print_stage; last_mc_print_stage = mc_print_stage;
} }
void MachineObject::parse_status(int flag)
{
camera_recording = ((flag >> 5) & 0x1) != 0;
ams_calibrate_remain_flag = ((flag >> 7) & 0x1) != 0;
}
PrintingSpeedLevel MachineObject::_parse_printing_speed_lvl(int lvl) PrintingSpeedLevel MachineObject::_parse_printing_speed_lvl(int lvl)
{ {
if (lvl < (int)SPEED_LEVEL_COUNT) if (lvl < (int)SPEED_LEVEL_COUNT)
@ -1325,7 +1332,7 @@ int MachineObject::command_ams_change_filament(int tray_id, int old_temp, int ne
return this->publish_json(j.dump()); return this->publish_json(j.dump());
} }
int MachineObject::command_ams_user_settings(int ams_id, bool start_read_opt, bool tray_read_opt) int MachineObject::command_ams_user_settings(int ams_id, bool start_read_opt, bool tray_read_opt, bool remain_flag)
{ {
json j; json j;
j["print"]["command"] = "ams_user_setting"; j["print"]["command"] = "ams_user_setting";
@ -1333,14 +1340,38 @@ int MachineObject::command_ams_user_settings(int ams_id, bool start_read_opt, bo
j["print"]["ams_id"] = ams_id; j["print"]["ams_id"] = ams_id;
j["print"]["startup_read_option"] = start_read_opt; j["print"]["startup_read_option"] = start_read_opt;
j["print"]["tray_read_option"] = tray_read_opt; j["print"]["tray_read_option"] = tray_read_opt;
j["print"]["calibrate_remain_flag"] = remain_flag;
ams_insert_flag = tray_read_opt; ams_insert_flag = tray_read_opt;
ams_power_on_flag = start_read_opt; ams_power_on_flag = start_read_opt;
ams_calibrate_remain_flag = remain_flag;
ams_user_setting_hold_count = HOLD_COUNT_MAX; ams_user_setting_hold_count = HOLD_COUNT_MAX;
return this->publish_json(j.dump()); return this->publish_json(j.dump());
} }
int MachineObject::command_ams_user_settings(int ams_id, AmsOptionType op, bool value)
{
json j;
j["print"]["command"] = "ams_user_setting";
j["print"]["sequence_id"] = std::to_string(MachineObject::m_sequence_id++);
j["print"]["ams_id"] = ams_id;
if (op == AmsOptionType::AMS_OP_STARTUP_READ) {
j["print"]["startup_read_option"] = value;
ams_power_on_flag = value;
} else if (op == AmsOptionType::AMS_OP_TRAY_READ) {
j["print"]["tray_read_option"] = value;
ams_insert_flag = value;
} else if (op == AmsOptionType::AMS_OP_CALIBRATE_REMAIN) {
j["print"]["calibrate_remain_flag"] = value;
ams_calibrate_remain_flag = value;
} else {
return -1;
}
ams_user_setting_hold_count = HOLD_COUNT_MAX;
return this->publish_json(j.dump());
}
int MachineObject::command_ams_calibrate(int ams_id) int MachineObject::command_ams_calibrate(int ams_id)
{ {
std::string gcode_cmd = (boost::format("M620 C%1% \n") % ams_id).str(); std::string gcode_cmd = (boost::format("M620 C%1% \n") % ams_id).str();
@ -1584,6 +1615,7 @@ bool MachineObject::is_in_printing_status(std::string status)
{ {
if (status.compare("PAUSE") == 0 if (status.compare("PAUSE") == 0
|| status.compare("RUNNING") == 0 || status.compare("RUNNING") == 0
|| status.compare("SLICING") == 0
|| status.compare("PREPARE") == 0) { || status.compare("PREPARE") == 0) {
return true; return true;
} }
@ -1861,6 +1893,7 @@ int MachineObject::parse_json(std::string payload)
if (jj.contains("home_flag")) { if (jj.contains("home_flag")) {
home_flag = jj["home_flag"].get<int>(); home_flag = jj["home_flag"].get<int>();
parse_status(home_flag);
} }
if (jj.contains("hw_switch_state")) { if (jj.contains("hw_switch_state")) {
hw_switch_state = jj["hw_switch_state"].get<int>(); hw_switch_state = jj["hw_switch_state"].get<int>();
@ -2206,22 +2239,6 @@ int MachineObject::parse_json(std::string payload)
} }
#pragma endregion #pragma endregion
#pragma region options
try {
if (jj.contains("option")) {
if (jj["option"].is_number()) {
int option = jj["option"].get<int>();
if ((option & (1 << 0)) != 0) {
camera_recording = true;
} else {
camera_recording = false;
}
}
}
}
catch(...) {
}
#pragma endregion
#pragma region camera #pragma region camera
// parse camera info // parse camera info
try { try {
@ -2353,6 +2370,16 @@ int MachineObject::parse_json(std::string payload)
} }
if (jj["ams"].contains("ams_rfid_status")) if (jj["ams"].contains("ams_rfid_status"))
ams_rfid_status = jj["ams"]["ams_rfid_status"].get<int>(); ams_rfid_status = jj["ams"]["ams_rfid_status"].get<int>();
if (jj["ams"].contains("humidity")) {
if (jj["ams"]["humidity"].is_string()) {
std::string humidity_str = jj["ams"]["humidity"].get<std::string>();
try {
ams_humidity = atoi(humidity_str.c_str());
} catch (...) {
;
}
}
}
if (jj["ams"].contains("insert_flag") || jj["ams"].contains("power_on_flag")) { if (jj["ams"].contains("insert_flag") || jj["ams"].contains("power_on_flag")) {
if (ams_user_setting_hold_count > 0) { if (ams_user_setting_hold_count > 0) {
@ -2504,6 +2531,9 @@ int MachineObject::parse_json(std::string payload)
} else { } else {
curr_tray->color = ""; curr_tray->color = "";
} }
if (tray_it->contains("remain")) {
curr_tray->remain = (*tray_it)["remain"].get<int>();
}
try { try {
if (!ams_id.empty() && !curr_tray->id.empty()) { if (!ams_id.empty() && !curr_tray->id.empty()) {
int ams_id_int = atoi(ams_id.c_str()); int ams_id_int = atoi(ams_id.c_str());

View file

@ -127,6 +127,12 @@ enum AmsRfidStatus {
AMS_RFID_HAS_FILAMENT = 6 AMS_RFID_HAS_FILAMENT = 6
}; };
enum AmsOptionType {
AMS_OP_STARTUP_READ,
AMS_OP_TRAY_READ,
AMS_OP_CALIBRATE_REMAIN
};
class AmsTray { class AmsTray {
public: public:
AmsTray(std::string tray_id) { AmsTray(std::string tray_id) {
@ -178,6 +184,7 @@ public:
bool is_bbl; bool is_bbl;
bool is_exists = false; bool is_exists = false;
int hold_count = 0; int hold_count = 0;
int remain; // filament remain: 0 ~ 100
AmsRoadPosition road_position; AmsRoadPosition road_position;
AmsStep step_state; AmsStep step_state;
@ -376,7 +383,9 @@ public:
int ams_rfid_status = 0; int ams_rfid_status = 0;
bool ams_insert_flag { false }; bool ams_insert_flag { false };
bool ams_power_on_flag { false }; bool ams_power_on_flag { false };
bool ams_calibrate_remain_flag { false };
bool ams_support_use_ams { false }; bool ams_support_use_ams { false };
int ams_humidity;
int ams_user_setting_hold_count = 0; int ams_user_setting_hold_count = 0;
AmsStatusMain ams_status_main; AmsStatusMain ams_status_main;
int ams_status_sub; int ams_status_sub;
@ -495,6 +504,7 @@ public:
bool is_calibration_done(); bool is_calibration_done();
void parse_state_changed_event(); void parse_state_changed_event();
void parse_status(int flag);
/* printing status */ /* printing status */
std::string print_status; /* enum string: FINISH, RUNNING, PAUSE, INIT, FAILED */ std::string print_status; /* enum string: FINISH, RUNNING, PAUSE, INIT, FAILED */
@ -569,7 +579,8 @@ public:
// ams controls // ams controls
int command_ams_switch(int tray_index, int old_temp = 210, int new_temp = 210); int command_ams_switch(int tray_index, int old_temp = 210, int new_temp = 210);
int command_ams_change_filament(int tray_id, int old_temp = 210, int new_temp = 210); int command_ams_change_filament(int tray_id, int old_temp = 210, int new_temp = 210);
int command_ams_user_settings(int ams_id, bool start_read_opt, bool tray_read_opt); int command_ams_user_settings(int ams_id, bool start_read_opt, bool tray_read_opt, bool remain_flag = false);
int command_ams_user_settings(int ams_id, AmsOptionType op, bool value);
int command_ams_calibrate(int ams_id); int command_ams_calibrate(int ams_id);
int command_ams_filament_settings(int ams_id, int tray_id, std::string setting_id, std::string tray_color, std::string tray_type, int nozzle_temp_min, int nozzle_temp_max); int command_ams_filament_settings(int ams_id, int tray_id, std::string setting_id, std::string tray_color, std::string tray_type, int nozzle_temp_min, int nozzle_temp_max);
int command_ams_select_tray(std::string tray_id); int command_ams_select_tray(std::string tray_id);