public function readnwrite_ajax() { $ret_val = 0; $array_data = array(); $array_old = array(); $color = $this->input->get('c'); $temper = (int) $this->input->get('t'); $temper_f = (int) $this->input->get('tf'); $material = (int) $this->input->get('m'); $length = (int) $this->input->get('l') * 1000; $length_use = (int) $this->input->get('lu') * 1000; $abb_cartridge = $this->input->get('v'); $type = (int) $this->input->get('ct'); // change color from name to hex code $this->load->helper('printlist'); $ret_val = ModelList__changeColorName($color); if ($ret_val == ERROR_WRONG_PRM) { $this->output->set_status_header(404); $this->load->helper('printerlog'); PrinterLog_logMessage('unknown color name: ' . $color, __FILE__, __LINE__); return; } $color = str_replace('#', '', $color); // write RFID card $array_data = array(PRINTERSTATE_TITLE_COLOR => $color, PRINTERSTATE_TITLE_EXT_TEMPER => $temper, PRINTERSTATE_TITLE_EXT_TEMP_1 => $temper_f, PRINTERSTATE_TITLE_INITIAL => $length, PRINTERSTATE_TITLE_USED => $length_use, PRINTERSTATE_TITLE_MATERIAL => $material, PRINTERSTATE_TITLE_CARTRIDGE => $type); $ret_val = PrinterState_setCartridgeAsArray($abb_cartridge, $array_data); if ($ret_val != ERROR_OK) { $this->output->set_status_header(403); $this->load->helper('printerlog'); PrinterLog_logMessage('write rfid error: ' . $ret_val, __FILE__, __LINE__); } return; }
public function changecartridge_action($mode = '') { $abb_cartridge = $this->input->get('v'); if (!$abb_cartridge && !in_array($abb_cartridge, array('l', 'r'))) { $this->output->set_status_header(403); // invalid request return; } if ($mode == 'unload_r') { $mode = 'unload'; } else { if ($mode == 'load_r') { $mode = 'load'; } else { if ($mode != 'cancel_unload') { //block request when not in idle $this->load->helper('corestatus'); if (CoreStatus_checkInIdle() == FALSE) { $this->output->set_status_header(403); // bad request return; } } } } switch ($mode) { case 'unload': $ret_val = PrinterState_unloadFilament($abb_cartridge); if ($ret_val != ERROR_OK) { $this->output->set_status_header($ret_val, MyERRMSG($ret_val)); } break; case 'cancel_unload': $ret_val = 0; @unlink(PRINTERSTATE_FILE_UNLOAD_HEAT); $ret_val = CoreStatus_setInIdle(); if ($ret_val == FALSE) { $this->load->helper('printerlog'); PrinterLog_logError('can not set idle after cancelling unloading', __FILE__, __LINE__); $this->output->set_status_header(ERROR_INTERNAL); } break; case 'load': $ret_val = PrinterState_loadFilament($abb_cartridge); if ($ret_val != ERROR_OK) { $this->output->set_status_header($ret_val); } break; case 'detail': $id_model = $this->input->get('id'); $this->_display_changecartridge_cartridge_detail($abb_cartridge, $id_model); break; case 'write': $ret_val = 0; $array_data = array(); $array_old = array(); $color = $this->input->get('c'); $temper = (int) $this->input->get('t'); $material = (int) $this->input->get('m'); $length = (int) $this->input->get('l') * 1000; $abb_cartridge = $this->input->get('v'); // get cartridge type from old RFID $ret_val = PrinterState_getCartridgeAsArray($array_old, $abb_cartridge, FALSE); if ($ret_val != ERROR_OK) { $this->output->set_status_header(403); $this->load->helper('printerlog'); PrinterLog_logMessage('read rfid error: ' . $ret_val, __FILE__, __LINE__); break; } // change color from name to hex code $this->load->helper('printlist'); $ret_val = ModelList__changeColorName($color); if ($ret_val == ERROR_WRONG_PRM) { $this->output->set_status_header(404); $this->load->helper('printerlog'); PrinterLog_logMessage('unknown color name: ' . $color, __FILE__, __LINE__); break; } $color = str_replace('#', '', $color); // write RFID card $array_data = array(PRINTERSTATE_TITLE_COLOR => $color, PRINTERSTATE_TITLE_EXT_TEMPER => $temper, PRINTERSTATE_TITLE_INITIAL => $length, PRINTERSTATE_TITLE_MATERIAL => $material, PRINTERSTATE_TITLE_CARTRIDGE => $array_old[PRINTERSTATE_TITLE_CARTRIDGE]); $ret_val = PrinterState_setCartridgeAsArray($abb_cartridge, $array_data); if ($ret_val != ERROR_OK) { $this->output->set_status_header(403); $this->load->helper('printerlog'); PrinterLog_logMessage('write rfid error: ' . $ret_val, __FILE__, __LINE__); break; } break; default: $this->output->set_status_header(403); // unknown request break; } return; }
function ModelList_add($data_array) { global $CFG; $printlist_basepath = $CFG->config['printlist']; $model_path = ''; $model_pictures = array(); $model_names = array(); $model_desps = array(); $json_data = NULL; $command = ''; $output = array(); $ret_val = 0; $model_name = NULL; // string by $data_array['n'], string, 1th in the json structure // $model_desp = ''; // string by $data_array['d'], string $model_printtime = 0; // int by $data_array['t'], int $model_filament1 = 0; // int by $data_array['l1'], int $model_filament2 = 0; // int by $data_array['l2'], int $model_color1 = NULL; // string by $data_array['c1'], string $model_color2 = NULL; // string by $data_array['c2'], string $model_gcode = NULL; // file array by $data_array['f'], array //other parameters //$data_array['p1'] ~ $data_array['p5']: file array which contain images //========================================================== //check parameters //========================================================== if (is_array($data_array)) { if (!isset($data_array['n']) || !isset($data_array['f'])) { return ERROR_MISS_PRM; } else { $tmp_array = array(); //model name $tmp_array = json_decode($data_array['n']); if (is_null($tmp_array)) { return ERROR_WRONG_PRM; } $model_names = $tmp_array; foreach ($tmp_array as $lang => $name_lang) { if (is_null($model_name)) { $name_lang = str_replace(' ', '_', $name_lang); $model_name = ModelList__codeModelName($name_lang); } if (strlen($name_lang) > 50 || strlen($name_lang) == 0) { return ERROR_WRONG_PRM; } } //model gcode if (is_array($data_array['f'])) { $model_gcode = $data_array['f']; } else { return ERROR_INTERNAL; } if ($model_gcode['file_size'] > PRINTLIST_MAX_GCODE_SIZE) { return ERROR_TOOBIG_MODEL; } if ($model_gcode['file_type'] != 'application/octet-stream' && $model_gcode['file_type'] != 'text/plain' || $model_gcode['file_ext'] != '.gcode') { return ERROR_WRONG_FORMAT; } //model description $tmp_array = json_decode($data_array['d']); if (is_null($tmp_array)) { return ERROR_WRONG_PRM; } $model_desps = $tmp_array; // if (isset($data_array['d'])) { foreach ($tmp_array as $lang => $model_desp) { // $model_desp = $data_array['d']; if (strlen($model_desp) > 512) { // || strlen($model_name) == 0 //already check isset() return ERROR_WRONG_PRM; } } //model print time if (isset($data_array['t'])) { $model_printtime = (int) $data_array['t']; if ($model_printtime <= 0) { return ERROR_WRONG_PRM; } } //model filament1 length if (isset($data_array['l1'])) { $model_filament1 = (int) $data_array['l1']; if ($model_filament1 <= 0) { return ERROR_WRONG_PRM; } } //model filament2 length if (isset($data_array['l2'])) { $model_filament2 = (int) $data_array['l2']; if ($model_filament2 <= 0) { return ERROR_WRONG_PRM; } } //model filament1 color if (isset($data_array['c1'])) { $model_color1 = $data_array['c1']; if (ModelList__changeColorName($model_color1) != ERROR_OK) { return ERROR_WRONG_PRM; } } //model filament1 color if (isset($data_array['c2'])) { $model_color2 = $data_array['c2']; if (ModelList__changeColorName($model_color2) != ERROR_OK) { return ERROR_WRONG_PRM; } } //model picture 1 if (isset($data_array['p1'])) { if (is_array($data_array['p1'])) { $model_pictures[] = $data_array['p1']; } else { return ERROR_INTERNAL; } } //model picture 2 if (isset($data_array['p2'])) { if (is_array($data_array['p2'])) { $model_pictures[] = $data_array['p2']; } else { return ERROR_INTERNAL; } } //model picture 3 if (isset($data_array['p3'])) { if (is_array($data_array['p3'])) { $model_pictures[] = $data_array['p3']; } else { return ERROR_INTERNAL; } } //model picture 4 if (isset($data_array['p4'])) { if (is_array($data_array['p4'])) { $model_pictures[] = $data_array['p4']; } else { return ERROR_INTERNAL; } } //model picture 1 if (isset($data_array['p5'])) { if (is_array($data_array['p5'])) { $model_pictures[] = $data_array['p5']; } else { return ERROR_INTERNAL; } } //model pictures foreach ($model_pictures as $picture) { if ($picture['file_size'] > PRINTLIST_MAX_PIC_SIZE) { return ERROR_TOOBIG_FILE; } if ($picture['is_image'] != TRUE || $picture['image_type'] != 'jpeg' && $picture['image_type'] != 'png') { return ERROR_WRONG_FORMAT; } } } } else { return ERROR_INTERNAL; } //========================================================== //treat parameters //========================================================== //model name, description, duration, filament1+2 $json_data = array(PRINTLIST_TITLE_ID => ModelList_codeModelHash($model_name), PRINTLIST_TITLE_NAME => $model_names, PRINTLIST_TITLE_DESP => $model_desps, PRINTLIST_TITLE_TIME => $model_printtime, PRINTLIST_TITLE_LENG_F1 => $model_filament1, PRINTLIST_TITLE_LENG_F2 => $model_filament2, PRINTLIST_TITLE_COLOR_F1 => $model_color1, PRINTLIST_TITLE_COLOR_F2 => $model_color2, PRINTLIST_TITLE_PIC => array()); $model_path = $printlist_basepath . $model_name . '/'; // $model_path = utf8_decode($model_path); //decode path for accent and special character //always create a new folder to overwrite the old one if (file_exists($model_path)) { delete_files($model_path, TRUE); //there are no folders inside normally, but we delete all rmdir($model_path); usleep(3); //to make sure the folder is deleted } mkdir($model_path); //model gcode // rename($model_gcode['full_path'], $model_path . PRINTLIST_FILE_GCODE); $command = 'bzip2 -zcf "' . $model_gcode['full_path'] . '" > "' . $model_path . PRINTLIST_FILE_GCODE_BZ2 . '"'; exec($command, $output, $ret_val); if ($ret_val != ERROR_NORMAL_RC_OK) { return ERROR_INTERNAL; } // //if we don't want to fix the filename of gcode, and then store it in json info // $tmp_string = 'gcode' . time() . $model_gcode['file_ext']; //new gcode name // rename($model_gcode['full_path'], $model_path . $tmp_string); // $json_data[PRINTLIST_TITLE_GCODE] = $model_path . $tmp_string; //full path in json //model picture foreach ($model_pictures as $picture) { $i_tmp = isset($i_tmp) ? ++$i_tmp : 1; $tmp_string = 'img' . $i_tmp . '_' . time() . $picture['file_ext']; //new picture name rename($picture['full_path'], $model_path . $tmp_string); $json_data[PRINTLIST_TITLE_PIC][] = $tmp_string; } //write model json info try { $fp = fopen($model_path . PRINTLIST_FILE_JSON, 'w'); if ($fp) { fwrite($fp, json_encode_unicode($json_data)); // fwrite($fp, json_encode($json_data)); fclose($fp); } else { return ERROR_INTERNAL; } } catch (Exception $e) { return ERROR_INTERNAL; } return ERROR_OK; }