Commit 7ca9c59b by 冯佳佳

update

parent 528c7072
...@@ -13,25 +13,22 @@ from flask_socketio import SocketIO, Namespace, emit, join_room, leave_room, \ ...@@ -13,25 +13,22 @@ from flask_socketio import SocketIO, Namespace, emit, join_room, leave_room, \
from smartcard.scard import * from smartcard.scard import *
import smartcard.util import smartcard.util
import engineio.async_drivers.gevent # import engineio.async_drivers.gevent
import engineio.async_drivers.threading import engineio.async_drivers.threading
# import engineio.async_drivers.eventlet logging.basicConfig(level=logging.DEBUG, # 控制台打印的日志级别
filename='nfc.log',
# logging.basicConfig(level=logging.DEBUG, # 控制台打印的日志级别 filemode='a', ##模式,有w和a,w就是写模式,每次都会重新写日志,覆盖之前的日志
# filename='nfc.log', # a是追加模式,默认如果不写的话,就是追加模式
# filemode='a', ##模式,有w和a,w就是写模式,每次都会重新写日志,覆盖之前的日志 format=
# # a是追加模式,默认如果不写的话,就是追加模式 '%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
# format= # 日志格式
# '%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s' )
# # 日志格式
# )
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# async_mode = 'eventlet' # async_mode = 'eventlet'
async_mode = 'gevent' # async_mode = 'gevent'
# async_mode = 'threading' async_mode = 'threading'
# if getattr(sys, 'frozen', False): # if getattr(sys, 'frozen', False):
# template_folder = os.path.join(sys._MEIPASS, 'templates') # template_folder = os.path.join(sys._MEIPASS, 'templates')
...@@ -41,8 +38,8 @@ async_mode = 'gevent' ...@@ -41,8 +38,8 @@ async_mode = 'gevent'
app = Flask(__name__) app = Flask(__name__)
socketio = SocketIO(app, async_mode=async_mode, cors_allowed_origins="*", socketio = SocketIO(app, async_mode=async_mode, cors_allowed_origins="*",
# logger=True, logger=True,
# engineio_logger=True engineio_logger=True
) )
...@@ -356,22 +353,51 @@ class MyNamespace(Namespace): ...@@ -356,22 +353,51 @@ class MyNamespace(Namespace):
emit('read_history', emit('read_history',
{'data': None, 'error_code': 1001, 'error_message': '读卡器连接失败, reader connect error'}) {'data': None, 'error_code': 1001, 'error_message': '读卡器连接失败, reader connect error'})
history_data = [] history_data = []
start_block = 24
for i in range(5): for i in range(5):
command = [0xFF, 0xB0, 0x00, 10 + i, 0x10] tmp_data = {'repair_date': None, 'repair_content': None,
'repair_man': None}
command = [0xFF, 0xB0, 0x00, start_block, 0x10]
result = self.scard.card_transmit(hcontext, self.scard.reader, command) result = self.scard.card_transmit(hcontext, self.scard.reader, command)
if result: if result:
data = result[:16] data = result[:4]
return_code = result[-2:] return_code = result[-2:]
if data[0] == 0: if data[0] == 66 and data[1] == 88:
history_data.append({}) data = [str(i) for i in data[:4]]
repair_date = ''.join(data[2:4])
repair_content = ""
repair_man = ""
start_block += 1
command = [0xFF, 0xB0, 0x00, start_block, 0x10]
result = self.scard.card_transmit(hcontext, self.scard.reader, command)
data = [str(i) for i in result[:4]]
repair_date += ''.join(data[:2])
repair_content += ''.join(data[2:4])
start_block += 1
command = [0xFF, 0xB0, 0x00, start_block, 0x10]
result = self.scard.card_transmit(hcontext, self.scard.reader, command)
data = [str(i) for i in result[:4]]
repair_content += ''.join(data[:4])
repair_content = repair_content.rstrip('0')
start_block += 1
command = [0xFF, 0xB0, 0x00, start_block, 0x10]
result = self.scard.card_transmit(hcontext, self.scard.reader, command)
start_block += 1
data = result[:4]
for x in data[:4]:
if x not in (0, "0"):
repair_man += chr(int(x))
else: else:
data = [str(i) for i in data] break
repair_date = ''.join(data[:8])
repair_content = int(''.join(data[8:10]))
repair_man = int(''.join(data[10:12]))
history_data.append( history_data.append(
{'repair_date': repair_date, 'repair_content': repair_content, 'repair_man': repair_man}) {'repair_date': repair_date, 'repair_content': repair_content,
'repair_man': repair_man})
else:
history_data.append({})
start_block += 4
else: else:
emit('read_history', emit('read_history',
{'data': None, 'error_code': 1002, 'error_message': '读取历史数据失败连接错误, reader connect error'}) {'data': None, 'error_code': 1002, 'error_message': '读取历史数据失败连接错误, reader connect error'})
...@@ -387,33 +413,49 @@ class MyNamespace(Namespace): ...@@ -387,33 +413,49 @@ class MyNamespace(Namespace):
def on_write_repair(self, message): def on_write_repair(self, message):
try: try:
repair_date = message["repair_date"] hcontext = None
repair_content = str(message["repair_content"]).zfill(2) repair_date = message["repair_date"][:8]
repair_man = str(message["repair_man"]).zfill(2) repair_content = str(message["repair_content"])[:6]
if len(repair_content) < 6:
repair_content += "0".zfill(6 - len(repair_content))
repair_man = str(message["repair_man"])[:4]
hcontext = self.scard.establish_context() hcontext = self.scard.establish_context()
result = self.scard.find_self_reader(hcontext) result = self.scard.find_self_reader(hcontext)
if not result: if not result:
emit('write_repair', emit('write_repair',
{'data': None, 'error_code': 1001, 'error_message': '读卡器连接失败, reader connect error'}) {'data': None, 'error_code': 1001, 'error_message': '读卡器连接失败, reader connect error'})
# write_command = [0xFF, 0xD6, 0x00, 10 + i, 0x10]
# write_command += [66, 88]
# write_command += [int(repair_date[x * 2:x * 2 + 2]) for x in
# range(int(len(repair_date) / 2))]
# write_command += [int(x) for x in repair_content]
# write_command += [ord(x) for x in repair_man]
start_block = 24
for i in range(5): for i in range(5):
read_command = [0xFF, 0xB0, 0x00, 10 + i, 0x10] read_command = [0xFF, 0xB0, 0x00, start_block, 0x10]
result = self.scard.card_transmit(hcontext, self.scard.reader, read_command) result = self.scard.card_transmit(hcontext, self.scard.reader, read_command)
if result: if result:
data = result[:16] data = result[:16]
return_code = result[-2:] return_code = result[-2:]
if data[0] == 0: if data[0] == 66 and data[1] == 88:
write_command = [0xFF, 0xD6, 0x00, 10 + i, 0x10] start_block += 4
write_command += [int(x) for x in repair_date] continue
write_command += [int(x) for x in repair_content] else:
write_command += [int(x) for x in repair_man] # 数据 66 88两位头部标记 20211018 4位日期 010203 6位维修内容 JJJJ 4位字母
write_command += [0, 0, 0, 0] data_command = [66, 88]
data_command += [int(repair_date[x * 2:x * 2 + 2]) for x in
range(int(len(repair_date) / 2))]
data_command += [int(x) for x in repair_content]
data_command += [ord(x) for x in repair_man]
for j in range(4):
write_command = [0xFF, 0xD6, 0x00, start_block + j, 0x04]
write_command += data_command[(4 * j):(4 * j) + 4]
result = self.scard.card_transmit(hcontext, self.scard.reader, write_command) result = self.scard.card_transmit(hcontext, self.scard.reader, write_command)
return_code = result[-2:] return_code = result[-2:]
if return_code[0] == 144 and return_code[1] == 0: if return_code[0] == 144 and return_code[1] == 0:
emit('write_repair', continue
{'data': None, 'error_code': 0, 'error_message': 'success'})
return
else: else:
emit('write_repair', emit('write_repair',
{'data': None, 'error_code': 1003, {'data': None, 'error_code': 1003,
...@@ -421,14 +463,16 @@ class MyNamespace(Namespace): ...@@ -421,14 +463,16 @@ class MyNamespace(Namespace):
break break
emit('write_repair', emit('write_repair',
{'data': None, 'error_code': 0, 'error_message': 'success'}) {'data': None, 'error_code': 0, 'error_message': 'success'})
return
emit('write_repair',
{'data': None, 'error_code': 0, 'error_message': 'success'})
except error as e: except error as e:
# logger.info(e) # logger.info(e)
pass pass
finally: finally:
if hcontext:
hresult = self.scard.release_context(hcontext) hresult = self.scard.release_context(hcontext)
# logger.info('Released context.')
def on_read_sn(self, message): def on_read_sn(self, message):
try: try:
sn = "" sn = ""
...@@ -515,8 +559,64 @@ class MyNamespace(Namespace): ...@@ -515,8 +559,64 @@ class MyNamespace(Namespace):
# logger.info('Released context.') # logger.info('Released context.')
def on_delete_sn(self, message):
try:
password = message["password"]
if password != "sswnb":
emit('delete_all',
{'data': None, 'error_code': 1008,
'error_message': 'password error ,密码错误,请重试'})
return
hcontext = self.scard.establish_context()
result = self.scard.find_self_reader(hcontext)
if not result:
emit('delete_all',
{'data': None, 'error_code': 1001, 'error_message': '读卡器连接失败, reader connect error'})
return
# 写入全为0的sn
write_command = [0xFF, 0xD6, 0x00, 17, 0x10]
write_command += [0 for i in range(16)]
result = self.scard.card_transmit(hcontext, self.scard.reader, write_command)
return_code = result[-2:]
if return_code[0] == 144 and return_code[1] == 0:
pass
else:
emit('delete_all',
{'data': None, 'error_code': 1007,
'error_message': '删除sn数据失败,请重试, write repair data error'})
return
# 写入全为0的维修数据
for i in range(5):
write_command = [0xFF, 0xD6, 0x00, 10 + i, 0x10]
write_command += [0 for x in range(16)]
result = self.scard.card_transmit(hcontext, self.scard.reader, write_command)
return_code = result[-2:]
if return_code[0] == 144 and return_code[1] == 0:
pass
else:
emit('delete_all',
{'data': None, 'error_code': 1006,
'error_message': '删除维修数据失败,请重试, write repair data error'})
return
emit('delete_all',
{'data': None, 'error_code': 0, 'error_message': 'success'})
except error as e:
# logger.info(e)
emit('delete_all',
{'data': None, 'error_code': 1009,
'error_message': 'unkwon error, 删除数据,未知错误'})
finally:
hresult = self.scard.release_context(hcontext)
def on_delete_all(self, message): def on_delete_all(self, message):
try: try:
hcontext = None
password = message["password"] password = message["password"]
if password != "sswnb": if password != "sswnb":
emit('delete_all', emit('delete_all',
...@@ -568,7 +668,10 @@ class MyNamespace(Namespace): ...@@ -568,7 +668,10 @@ class MyNamespace(Namespace):
{'data': None, 'error_code': 1009, {'data': None, 'error_code': 1009,
'error_message': 'unkwon error, 删除数据,未知错误'}) 'error_message': 'unkwon error, 删除数据,未知错误'})
finally: finally:
if hcontext:
hresult = self.scard.release_context(hcontext) hresult = self.scard.release_context(hcontext)
else:
pass
# logger.info('Released context.') # logger.info('Released context.')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment