public function readnwrite($side = 'r') { $cr = 0; $cartridge_code = NULL; $err_code = NULL; $error = NULL; $cartridge_data = array(); $initial_length_value = 0; $used_length_value = 0; $temper_value = 0; $temper_first = 0; $material_value = 0; $cartridge_value = 0; $color_value = '#ffffff'; $option_selected = 'selected="selected"'; $packdate_val = 'N/A'; // right as default if (in_array($side, array('l', 'left'))) { $side = 'l'; } else { $side = 'r'; } $this->load->library('parser'); $this->lang->load('cartridge', $this->config->item('language')); if (PrinterState_getCartridgeCode($cartridge_code, $side) != ERROR_OK) { $cartridge_code = t('error retrieving cartridge code'); } $cr = PrinterState_getCartridgeAsArray($cartridge_data, $side, TRUE, $err_code); if ($cr != ERROR_OK) { $initial_length_value = 20; $used_length_value = 10; $temper_value = 200; $temper_first = 210; if ($cr == ERROR_INTERNAL) { switch ($err_code) { case PRINTERSTATE_CARTRIDGE_ERR_MAGIC: $error = t('magic_number_error'); break; case PRINTERSTATE_CARTRIDGE_ERR_CRC: $error = t('crc_error'); break; case PRINTERSTATE_CARTRIDGE_ERR_CART: $error = t('cartridge_type_error'); break; case PRINTERSTATE_CARTRIDGE_ERR_FILA: $error = t('filament_type_error'); break; default: $error = t('internal_error'); break; } } else { if (in_array($cr, array(ERROR_MISS_LEFT_CART, ERROR_MISS_RIGT_CART))) { $error = 'No cartridge'; } } } else { $initial_length_value = $cartridge_data[PRINTERSTATE_TITLE_INITIAL] / 1000; $used_length_value = $cartridge_data[PRINTERSTATE_TITLE_USED] / 1000; // if ($length_value < 10) { // $length_value = 10; // } $temper_value = $cartridge_data[PRINTERSTATE_TITLE_EXT_TEMPER]; $temper_first = $cartridge_data[PRINTERSTATE_TITLE_EXT_TEMP_1]; $color_value = $cartridge_data[PRINTERSTATE_TITLE_COLOR]; if ($temper_value < 165) { $temper_value = 165; } $packdate_val = $cartridge_data[PRINTERSTATE_TITLE_SETUP_DATE]; } $template_data = array('material_array' => array(array('name' => t('PLA'), 'value' => PRINTERSTATE_VALUE_MATERIAL_PLA, 'on' => NULL), array('name' => t('ABS'), 'value' => PRINTERSTATE_VALUE_MATERIAL_ABS, 'on' => NULL), array('name' => t('PVA'), 'value' => PRINTERSTATE_VALUE_MATERIAL_PVA, 'on' => NULL)), 'cartridge_array' => array(array('name' => t('Normal'), 'value' => PRINTERSTATE_VALUE_CARTRIDGE_NORMAL, 'on' => NULL), array('name' => t('Refill'), 'value' => PRINTERSTATE_VALUE_CARTRIDGE_REFILL, 'on' => NULL)), 'cartridge_code' => $cartridge_code, 'initial_length_value' => $initial_length_value, 'used_length_value' => $used_length_value, 'temper_value' => $temper_value, 'temper_f_value' => $temper_first, 'rfid_color' => $color_value, 'abb_cartridge' => $side, 'side_cartridge' => $side == 'l' ? t('left') : t('right'), 'error' => $error, 'color' => t('color'), 'cartridge_type' => t('cartridge_type'), 'material_type' => t('material_type'), 'temperature' => t('temperature'), 'temperature_first' => t('temperature_first'), 'initial_length' => t('initial_length'), 'used_length' => t('used_length'), 'initial_length_mm' => t('initial_length_mm'), 'used_length_mm' => t('used_length_mm'), 'cartridge_code_hint' => t('cartridge_code_hint'), 'magic_number' => t('magic_number'), 'red_label' => t('red_label'), 'green_label' => t('green_label'), 'blue_label' => t('blue_label'), 'pack_label' => t('pack_label'), 'checksum_label' => t('checksum_label'), 'write_button' => t('write_button'), 'pack_date_val' => $packdate_val, 'info_not_changed' => t('information_not_changed'), 'writing_successed' => t('writing_successed'), 'error_writing' => t('error_writing'), 'bicolor' => $this->config->item('nb_extruder') >= 2 ? 'true' : 'false'); if ($cr == ERROR_OK) { switch ($cartridge_data[PRINTERSTATE_TITLE_MATERIAL]) { case PRINTERSTATE_DESP_MATERIAL_PLA: $material_value = PRINTERSTATE_VALUE_MATERIAL_PLA; break; case PRINTERSTATE_DESP_MATERIAL_ABS: $material_value = PRINTERSTATE_VALUE_MATERIAL_ABS; break; case PRINTERSTATE_DESP_MATERIAL_PVA: $material_value = PRINTERSTATE_VALUE_MATERIAL_PVA; break; default: $material_value = PRINTERSTATE_VALUE_MATERIAL_PLA; break; } $template_data['material_array'][$material_value]['on'] = $option_selected; switch ($cartridge_data[PRINTERSTATE_TITLE_CARTRIDGE]) { case PRINTERSTATE_DESP_CARTRIDGE_NORMAL: $cartridge_value = PRINTERSTATE_VALUE_CARTRIDGE_NORMAL; break; case PRINTERSTATE_DESP_CARTRIDGE_REFILL: $cartridge_value = PRINTERSTATE_VALUE_CARTRIDGE_REFILL; break; default: $cartridge_value = PRINTERSTATE_VALUE_CARTRIDGE_NORMAL; break; } $template_data['cartridge_array'][$cartridge_value]['on'] = $option_selected; } // parse all page $this->_parseBaseTemplate(t('readnwrite_pagetitle'), $this->parser->parse('cartridge', $template_data, TRUE)); return; }
function PrinterState_getCartridgeTemper(&$temper_cartridge, $abb_cartridge, $power_off = TRUE) { $last_output = NULL; $ret_val = 0; $ret_val = PrinterState_getCartridgeCode($last_output, $abb_cartridge, $power_off); if ($ret_val != ERROR_OK) { return $ret_val; } // check and treat output data if ($last_output) { $version_rfid = 0; $string_tmp = NULL; $hex_checksum = 0; $hex_cal = 0; $hex_tmp = 0; $offset_temp = 0; $length_temp = 0; $data_json = array(); // checksum 0 to 14 for ($i = 0; $i <= 14; $i++) { $string_tmp = substr($last_output, $i * 2, 2); $hex_tmp = hexdec($string_tmp); $hex_cal = $hex_cal ^ $hex_tmp; } $hex_checksum = hexdec(substr($last_output, 30, 2)); if ($hex_cal != $hex_checksum) { PrinterLog_logError('checksum error, $hex_cal: ' . $hex_cal . ', $hex_data: ' . $hex_checksum, __FILE__, __LINE__); return ERROR_INTERNAL; // checksum failed } // magic number $string_tmp = substr($last_output, 0, 4); switch (hexdec($string_tmp)) { case PRINTERSTATE_MAGIC_NUMBER_V1: $version_rfid = 1; break; case PRINTERSTATE_MAGIC_NUMBER_V2: $version_rfid = 2; break; case PRINTERSTATE_MAGIC_NUMBER_V3: $version_rfid = 3; break; case PRINTERSTATE_MAGIC_NUMBER_V4: $version_rfid = 4; break; default: PrinterLog_logError('magic number error', __FILE__, __LINE__); return ERROR_INTERNAL; break; } if ($version_rfid == 1) { $offset_temp = 22; $length_temp = 2; } else { if ($version_rfid == 2) { //$version_rfid == 2 $offset_temp = 24; $length_temp = 2; } else { if ($version_rfid == 3) { //$version_rfid == 3 $offset_temp = 24; $length_temp = 1; } else { //$version_rfid == 4 $offset_temp = 22; $length_temp = 2; } } } // normal extrusion temperature $string_tmp = substr($last_output, $offset_temp, $length_temp); if ($version_rfid == 3) { $temper_cartridge = hexdec($string_tmp) + PRINTERSTATE_OFFSET_TEMPER_V2; } else { $temper_cartridge = hexdec($string_tmp) + PRINTERSTATE_OFFSET_TEMPER; } } else { PrinterLog_logMessage('missing cartridge', __FILE__, __LINE__); $temp_cartridge = NULL; if ($abb_cartridge == 'l') { return ERROR_MISS_LEFT_CART; } else { return ERROR_MISS_RIGT_CART; } } return ERROR_OK; }
public function rfid_ajax() { $array_rfid = array(); $array_cmd = array(); $this->load->helper(array('printerstate', 'errorcode')); if ($this->config->item('nb_extruder') >= 2) { $array_cmd['left'] = 'l'; } $array_cmd['right'] = 'r'; foreach ($array_cmd as $key => $value) { $tmp_rfid = NULL; $cr = PrinterState_getCartridgeCode($tmp_rfid, $value); if ($cr != ERROR_OK) { $this->output->set_status_header(403); return; } else { if (is_null($tmp_rfid)) { $tmp_rfid = 'EMPTY'; } } $array_rfid[$key] = $tmp_rfid; } $this->output->set_status_header(200); print json_encode($array_rfid); return; }
function PrinterState_getCartridgeAsArray(&$json_cartridge, $abb_cartridge, $power_off = TRUE, $error_type = NULL) { $last_output = NULL; $ret_val = 0; $ret_val = PrinterState_getCartridgeCode($last_output, $abb_cartridge, $power_off); if ($ret_val != ERROR_OK) { return $ret_val; } // check and treat output data if ($last_output && strlen($last_output) == 32) { $version_rfid = 0; $string_tmp = NULL; $hex_checksum = 0; $hex_cal = 0; $hex_tmp = 0; $time_start = 0; $time_pack = 0; $data_json = array(); // checksum 0 to 14 for ($i = 0; $i <= 14; $i++) { $string_tmp = substr($last_output, $i * 2, 2); $hex_tmp = hexdec($string_tmp); $hex_cal = $hex_cal ^ $hex_tmp; } $hex_checksum = hexdec(substr($last_output, 30, 2)); if ($hex_cal != $hex_checksum) { PrinterLog_logError('checksum error, $hex_cal: ' . $hex_cal . ', $hex_data: ' . $hex_checksum, __FILE__, __LINE__); $error_type = PRINTERSTATE_CARTRIDGE_ERR_CRC; return ERROR_INTERNAL; // checksum failed } // magic number $string_tmp = substr($last_output, 0, 4); switch (hexdec($string_tmp)) { case PRINTERSTATE_MAGIC_NUMBER_V1: $version_rfid = 1; break; case PRINTERSTATE_MAGIC_NUMBER_V2: $version_rfid = 2; break; case PRINTERSTATE_MAGIC_NUMBER_V3: $version_rfid = 3; break; case PRINTERSTATE_MAGIC_NUMBER_V4: $version_rfid = 4; break; default: PrinterLog_logError('magic number error', __FILE__, __LINE__); $error_type = PRINTERSTATE_CARTRIDGE_ERR_MAGIC; return ERROR_INTERNAL; break; } if ($version_rfid <= 3) { $length_cartridge = 2; $offset_material = 6; $length_material = 2; $offset_color = 8; $length_color = 6; $offset_init = 14; if ($version_rfid == 1) { $length_init = 4; $offset_used = 18; $length_used = 4; $offset_temp = 22; $length_temp = 2; $offset_pack = 24; $length_pack = 4; } else { if ($version_rfid == 2) { //$version_rfid == 2 $length_init = 5; $offset_used = 19; $length_used = 5; $offset_temp = 24; $length_temp = 2; $offset_pack = 26; $length_pack = 4; } else { if ($version_rfid == 3) { //$version_rfid == 3 $length_init = 5; $offset_used = 19; $length_used = 5; $offset_temp = 24; $length_temp = 1; $offset_temp_f = 25; $length_temp_f = 1; $offset_pack = 26; $length_pack = 4; } } } } else { //$version_rfid == 4 $length_cartridge = 1; $offset_material = 5; $length_material = 1; $offset_color = 6; $length_color = 6; $offset_init = 12; $length_init = 5; $offset_used = 17; $length_used = 5; $offset_temp = 22; $length_temp = 2; $offset_temp_f = 24; $length_temp_f = 2; $offset_pack = 26; $length_pack = 4; } // type of cartridge $string_tmp = substr($last_output, 4, $length_cartridge); $hex_tmp = hexdec($string_tmp); switch ($hex_tmp) { case PRINTERSTATE_VALUE_CARTRIDGE_NORMAL: $data_json[PRINTERSTATE_TITLE_CARTRIDGE] = PRINTERSTATE_DESP_CARTRIDGE_NORMAL; break; case PRINTERSTATE_VALUE_CARTRIDGE_REFILL: $data_json[PRINTERSTATE_TITLE_CARTRIDGE] = PRINTERSTATE_DESP_CARTRIDGE_REFILL; break; default: PrinterLog_logError('cartridge type error', __FILE__, __LINE__); $error_type = PRINTERSTATE_CARTRIDGE_ERR_CART; return ERROR_INTERNAL; } // type of material $string_tmp = substr($last_output, $offset_material, $length_material); $hex_tmp = hexdec($string_tmp); switch ($hex_tmp) { case PRINTERSTATE_VALUE_MATERIAL_PLA: $data_json[PRINTERSTATE_TITLE_MATERIAL] = PRINTERSTATE_DESP_MATERIAL_PLA; break; case PRINTERSTATE_VALUE_MATERIAL_ABS: $data_json[PRINTERSTATE_TITLE_MATERIAL] = PRINTERSTATE_DESP_MATERIAL_ABS; break; case PRINTERSTATE_VALUE_MATERIAL_PVA: $data_json[PRINTERSTATE_TITLE_MATERIAL] = PRINTERSTATE_DESP_MATERIAL_PVA; break; default: PrinterLog_logError('filament type error', __FILE__, __LINE__); $error_type = PRINTERSTATE_CARTRIDGE_ERR_FILA; return ERROR_INTERNAL; } // color $string_tmp = substr($last_output, $offset_color, $length_color); $data_json[PRINTERSTATE_TITLE_COLOR] = '#' . $string_tmp; // initial quantity $string_tmp = substr($last_output, $offset_init, $length_init); $hex_tmp = hexdec($string_tmp); $data_json[PRINTERSTATE_TITLE_INITIAL] = $hex_tmp; // used quantity $string_tmp = substr($last_output, $offset_used, $length_used); $hex_tmp = hexdec($string_tmp); $data_json[PRINTERSTATE_TITLE_USED] = $hex_tmp; // normal extrusion temperature $string_tmp = substr($last_output, $offset_temp, $length_temp); if ($version_rfid == 3) { $hex_tmp = hexdec($string_tmp) + PRINTERSTATE_OFFSET_TEMPER_V2; } else { $hex_tmp = hexdec($string_tmp) + PRINTERSTATE_OFFSET_TEMPER; } $data_json[PRINTERSTATE_TITLE_EXT_TEMPER] = $hex_tmp; // first layer extrusion temperature if ($version_rfid > 2) { $string_tmp = substr($last_output, $offset_temp_f, $length_temp_f); if ($version_rfid == 3) { $hex_tmp = hexdec($string_tmp) + PRINTERSTATE_OFFSET_TEMPER_V2; } else { $hex_tmp = hexdec($string_tmp) + PRINTERSTATE_OFFSET_TEMPER; } $data_json[PRINTERSTATE_TITLE_EXT_TEMP_1] = $hex_tmp; } else { $data_json[PRINTERSTATE_TITLE_EXT_TEMP_1] = $data_json[PRINTERSTATE_TITLE_EXT_TEMPER] + 10; } // packing date //FIXME argument max acceptable date (Unix timestamp, 2038-01-19) $string_tmp = substr($last_output, $offset_pack, $length_pack); $hex_tmp = hexdec($string_tmp); $time_start = gmmktime(0, 0, 0, 1, 1, PRINTERSTATE_OFFSET_YEAR_SETUP_DATE); $time_pack = $time_start + $hex_tmp * 60 * 60 * 24; // $data_json[PRINTERSTATE_TITLE_SETUP_DATE] = date("Y-m-d\TH:i:sO", $time_pack); $data_json[PRINTERSTATE_TITLE_SETUP_DATE] = date("Y-m-d\\TH:i:s\\Z", $time_pack); // // change temperature values to user settings if cartridge is refillable // if ($data_json[PRINTERSTATE_TITLE_TYPE] == PRINTERSTATE_DESP_CARTRIDGE_REFILL) { // $CI = &get_instance(); // $tmp_array = array(); // $temper_filepath = $CI->config->item('conf') . PRINTERSTATE_JSON_REFILL_TEMPER; // $CI->load->helper('json'); // try { // $tmp_array = json_read($temper_filepath, TRUE); // if ($tmp_array['error']) { // throw new Exception('read json error'); // } // } catch (Exception $e) { // $CI->load->helper('printerlog'); // PrinterLog_logMessage('read refillable cartridge user temperature json error', __FILE__, __LINE__); // // log error message and use the default temperature in RFID // $json_cartridge = $data_json; // return ERROR_OK; // } // $data_json[PRINTERSTATE_TITLE_EXT_TEMPER] = $tmp_array['json'][$abb_cartridge][PRINTERSTATE_TITLE_EXT_TEMPER]; // $data_json[PRINTERSTATE_TITLE_EXT_TEMP_1] = $tmp_array['json'][$abb_cartridge][PRINTERSTATE_TITLE_EXT_TEMP_1]; // } $json_cartridge = $data_json; } else { PrinterLog_logMessage('missing cartridge', __FILE__, __LINE__); $json_cartridge = array(); if ($abb_cartridge == 'l') { return ERROR_MISS_LEFT_CART; } else { return ERROR_MISS_RIGT_CART; } } return ERROR_OK; }
public function write() { $cr = 0; $code_write = NULL; $code_read = NULL; $template_data = array(); $body_page = NULL; $button_html = '<input type="submit" value="ok 确认">'; $type = $this->input->post('type'); $year = (int) $this->input->post('year'); $month = (int) $this->input->post('month'); $day = (int) $this->input->post('day'); $times = (int) $this->input->post('times'); $side = $this->input->post('side'); if ($side != 'l') { $side = 'r'; } if ($times > 0 && $this->_get_final_code($code_write, $year, $month, $day, $type)) { $cr = PrinterState_setCartridgeCode($code_write, $side, FALSE); if ($cr == ERROR_OK) { $power_off = FALSE; if ($times == 1) { $power_off = TRUE; } $cr = PrinterState_getCartridgeCode($code_read, $side, $power_off); if ($cr == ERROR_OK) { if ($code_write != $code_read) { $cr = ERROR_WRONG_FORMAT; $this->load->helper('printerlog'); PrinterLog_logError('write tag not correspond', __FILE__, __LINE__); } else { --$times; } } } } else { $cr = ERROR_INTERNAL; $this->load->helper('printerlog'); PrinterLog_logError('user input error', __FILE__, __LINE__); } if (in_array($cr, array(ERROR_OK, ERROR_WRONG_FORMAT))) { $this->load->library('parser'); $template_data = array('type' => $type, 'year' => $year, 'month' => $month, 'day' => $day, 'times' => $times, 'side' => $side, 'ok' => NULL, 'ko' => NULL); $template_data['image'] = '/images/' . ($cr == ERROR_OK ? 'right.png' : 'wrong.png'); if ($cr == ERROR_OK) { $template_data['hint'] = $times == 0 ? 'All tags written 全部标签已写完' : 'Tag written successfully 标签已成功写入'; $template_data['ok'] = $button_html; } else { $template_data['hint'] = 'Error 标签写入错误'; $template_data['ko'] = $button_html; } $body_page = $this->parser->parse('setupcartridge/write', $template_data, TRUE); // parse all page $template_data = array('lang' => 'en', 'headers' => '<title>SetupCartridge 设置标签</title>', 'contents' => $body_page); $this->parser->parse('basetemplate', $template_data); } else { $this->output->set_header('Location: /setupcartridge/input?v=' . $side); } return; }