function PrinterState__updateCartridge(&$code_cartridge, $abb_cartridge)
{
    $CI =& get_instance();
    $file_path = $CI->config->item('base_data') . PRINTERSTATE_FILE_UPDATE_RFID;
    if (file_exists($file_path)) {
        $data_json = array();
        $temp_code = NULL;
        try {
            $CI->load->helper('json');
            $tmp_array = @json_read($file_path, TRUE);
            if ($tmp_array['error']) {
                throw new Exception('read json error');
            } else {
                $data_json = $tmp_array['json'];
            }
        } catch (Exception $e) {
            $CI->load->helper('printerlog');
            PrinterLog_logError('read cartridge update data error', __FILE__, __LINE__);
            return;
            // log error and return
        }
        $temp_code = substr($code_cartridge, 0, 26);
        if (array_key_exists($temp_code, $data_json)) {
            $temp_hex = 0;
            $ret_val = 0;
            $CI->load->helper('printerlog');
            PrinterLog_logDebug('detected a cartridge to update', __FILE__, __LINE__);
            // add date in the end
            $temp_code = $data_json[$temp_code] . substr($code_cartridge, 26, 4);
            // calculate checksum
            for ($i = 0; $i <= 14; $i++) {
                $string_tmp = substr($temp_code, $i * 2, 2);
                $hex_tmp = hexdec($string_tmp);
                $temp_hex = $temp_hex ^ $hex_tmp;
            }
            $temp_hex = dechex($temp_hex);
            if (strlen($temp_hex) == 1) {
                $temp_hex = '0' . $temp_hex;
            }
            $temp_code .= strtoupper($temp_hex);
            $code_cartridge = $temp_code;
            $ret_val = PrinterState_setCartridgeCode($temp_code, $abb_cartridge);
            if ($ret_val != ERROR_OK) {
                // log error and return
                $CI->load->helper('printerlog');
                PrinterLog_logError('write cartridge error when in updating cartridge from database', __FILE__, __LINE__);
            }
        }
    }
    return;
}
Exemple #2
0
 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;
 }