Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nfc_storage
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
冯佳佳
nfc_storage
Commits
7ca9c59b
Commit
7ca9c59b
authored
Oct 19, 2021
by
冯佳佳
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
528c7072
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
212 additions
and
109 deletions
+212
-109
app.py
+212
-109
No files found.
app.py
View file @
7ca9c59b
...
...
@@ -13,25 +13,22 @@ from flask_socketio import SocketIO, Namespace, emit, join_room, leave_room, \
from
smartcard.scard
import
*
import
smartcard.util
import
engineio.async_drivers.gevent
#
import engineio.async_drivers.gevent
import
engineio.async_drivers.threading
# import engineio.async_drivers.eventlet
# logging.basicConfig(level=logging.DEBUG, # 控制台打印的日志级别
# filename='nfc.log',
# filemode='a', ##模式,有w和a,w就是写模式,每次都会重新写日志,覆盖之前的日志
# # a是追加模式,默认如果不写的话,就是追加模式
# format=
# '%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
# # 日志格式
# )
logging
.
basicConfig
(
level
=
logging
.
DEBUG
,
# 控制台打印的日志级别
filename
=
'nfc.log'
,
filemode
=
'a'
,
##模式,有w和a,w就是写模式,每次都会重新写日志,覆盖之前的日志
# a是追加模式,默认如果不写的话,就是追加模式
format
=
'
%(asctime)
s -
%(pathname)
s[line:
%(lineno)
d] -
%(levelname)
s:
%(message)
s'
# 日志格式
)
logger
=
logging
.
getLogger
(
__name__
)
# async_mode = 'eventlet'
async_mode
=
'gevent'
# async_mode = 'threading'
# async_mode = 'gevent'
async_mode
=
'threading'
# if getattr(sys, 'frozen', False):
# template_folder = os.path.join(sys._MEIPASS, 'templates')
...
...
@@ -41,19 +38,19 @@ async_mode = 'gevent'
app
=
Flask
(
__name__
)
socketio
=
SocketIO
(
app
,
async_mode
=
async_mode
,
cors_allowed_origins
=
"*"
,
#
logger=True,
#
engineio_logger=True
logger
=
True
,
engineio_logger
=
True
)
class
ScardService
(
object
):
def
__init__
(
self
,
reader
=
None
):
if
reader
is
None
:
self
.
reader
=
"ACS ACR1281 1S Dual Reader PICC 0"
else
:
self
.
reader
=
reader
def
get_reader_state
(
self
,
state
):
reader
,
eventstate
,
atr
=
state
# logger.info(reader + " " + smartcard.util.toHexString(atr, smartcard.util.HEX))
...
...
@@ -102,23 +99,23 @@ class ScardService(object):
# logger.info(message)
return
10
,
message
return
False
def
establish_context
(
self
):
hresult
,
hcontext
=
SCardEstablishContext
(
SCARD_SCOPE_USER
)
if
hresult
!=
SCARD_S_SUCCESS
:
raise
error
(
'Failed to establish context: '
+
SCardGetErrorMessage
(
hresult
))
# logger.info('Context established!')
return
hcontext
def
release_context
(
self
,
hcontext
):
hresult
=
SCardReleaseContext
(
hcontext
)
if
hresult
!=
SCARD_S_SUCCESS
:
raise
error
(
'Failed to release context: '
+
SCardGetErrorMessage
(
hresult
))
# logger.info('Released context.')
def
find_self_reader
(
self
,
hcontext
):
hresult
,
readers
=
SCardListReaders
(
hcontext
,
[])
if
hresult
!=
SCARD_S_SUCCESS
:
...
...
@@ -127,7 +124,7 @@ class ScardService(object):
if
self
.
reader
not
in
readers
:
raise
error
(
'can not find reader ==> '
+
self
.
reader
)
return
True
def
handle_status_change
(
self
,
hcontext
,
readerstates
):
try
:
hresult
,
readerstates
=
SCardGetStatusChange
(
hcontext
,
INFINITE
,
readerstates
)
...
...
@@ -148,7 +145,7 @@ class ScardService(object):
raise
error
(
'Failed to release context: '
+
SCardGetErrorMessage
(
hresult
))
# print('Released context.')
def
my_scard_handler
(
self
):
try
:
hresult
,
hcontext
=
SCardEstablishContext
(
SCARD_SCOPE_USER
)
...
...
@@ -156,7 +153,7 @@ class ScardService(object):
raise
error
(
'Failed to establish context: '
+
SCardGetErrorMessage
(
hresult
))
# print('Context established!')
try
:
# 获取读卡器列表,筛选PICC类型是否存在
hresult
,
readers
=
SCardListReaders
(
hcontext
,
[])
...
...
@@ -165,9 +162,9 @@ class ScardService(object):
'Failed to list readers: '
+
SCardGetErrorMessage
(
hresult
))
if
self
.
reader
not
in
readers
:
raise
error
(
'can not find reader ==> '
+
self
.
reader
)
readerstates
=
[(
self
.
reader
,
SCARD_STATE_UNAWARE
)]
while
True
:
hresult
,
readerstates
=
SCardGetStatusChange
(
hcontext
,
INFINITE
,
readerstates
)
rs_code
,
message
=
self
.
get_reader_state
(
readerstates
[
0
])
...
...
@@ -179,8 +176,8 @@ class ScardService(object):
else
:
# logger.info("未知错位,请重试")
exit
(
1
)
finally
:
hresult
=
SCardReleaseContext
(
hcontext
)
if
hresult
!=
SCARD_S_SUCCESS
:
...
...
@@ -188,14 +185,14 @@ class ScardService(object):
'Failed to release context: '
+
\
SCardGetErrorMessage
(
hresult
))
# print('Released context.')
except
error
as
e
:
pass
def
card_transmit
(
self
,
hcontext
,
zreader
,
command
):
GET_RESPONSE
=
[
0xA0
,
0xC0
,
0x00
,
0x00
]
# print('Trying to select DF_TELECOM of card in', zreader)
try
:
hresult
,
hcard
,
dwActiveProtocol
=
SCardConnect
(
hcontext
,
...
...
@@ -206,7 +203,7 @@ class ScardService(object):
'Unable to connect: '
+
SCardGetErrorMessage
(
hresult
))
# print('Connected with active protocol', dwActiveProtocol)
try
:
hresult
,
response
=
SCardTransmit
(
hcard
,
dwActiveProtocol
,
command
)
...
...
@@ -226,7 +223,7 @@ class ScardService(object):
'Failed to disconnect: '
+
SCardGetErrorMessage
(
hresult
))
# print('Disconnected')
except
error
as
message
:
pass
# print(error, message)
...
...
@@ -241,13 +238,13 @@ def index():
def
send_code
():
json_data
=
request
.
get_json
()
phone
=
json_data
[
'phone'
]
url
=
"https://guide.ssw-htzn.com/business_web/account/sendCode"
headers
=
{
"platform"
:
"business_web"
}
data
=
{
"phone"
:
phone
}
# json_data = json.dumps(data, ensure_ascii=False)
# result = requests.post(url=url, data=json_data.encode('utf-8'), headers=headers, verify=None)
result
=
requests
.
post
(
url
=
url
,
json
=
data
,
headers
=
headers
)
...
...
@@ -261,12 +258,12 @@ def send_code():
def
run_login
():
token
=
request
.
headers
.
get
(
'Authorization'
)
json_data
=
request
.
get_json
()
data
=
{}
phone
=
json_data
[
'phone'
]
if
'phone'
in
json_data
else
None
code
=
json_data
[
'code'
]
if
'code'
in
json_data
else
None
login_type
=
json_data
[
'type'
]
if
'type'
in
json_data
else
1
# 1.验证码登录,2.密码登录
url
=
"https://guide.ssw-htzn.com/business_web/account/login"
headers
=
{
"platform"
:
"business_web"
...
...
@@ -274,13 +271,13 @@ def run_login():
if
token
:
headers
.
update
({
"Authorization"
:
token
})
data
=
{
"code"
:
code
,
"phone"
:
phone
,
"type"
:
login_type
}
result
=
requests
.
post
(
url
=
url
,
json
=
data
,
headers
=
headers
)
result
=
json
.
loads
(
result
.
text
)
if
int
(
result
[
"error_code"
])
!=
0
:
return
jsonify
({
"error_code"
:
-
1
,
"error_message"
:
"login error"
})
data
=
result
[
"data"
]
return
jsonify
({
"data"
:
data
,
"error_code"
:
0
,
"error_message"
:
"send message success"
})
...
...
@@ -288,17 +285,17 @@ class MyNamespace(Namespace):
def
__init__
(
self
,
namespace
=
None
):
super
(
Namespace
,
self
)
.
__init__
(
namespace
)
self
.
scard
=
ScardService
()
def
on_send_code
(
self
,
message
):
json_data
=
message
phone
=
json_data
[
'phone'
]
url
=
"https://guide.ssw-htzn.com/business_web/account/sendCode"
headers
=
{
"platform"
:
"business_web"
}
data
=
{
"phone"
:
phone
}
# json_data = json.dumps(data, ensure_ascii=False)
# result = requests.post(url=url, data=json_data.encode('utf-8'), headers=headers, verify=None)
result
=
requests
.
post
(
url
=
url
,
json
=
data
,
headers
=
headers
)
...
...
@@ -306,15 +303,15 @@ class MyNamespace(Namespace):
if
int
(
result
[
"error_code"
])
!=
0
:
emit
(
'send_code'
,
{
"error_code"
:
-
1
,
"error_message"
:
"send message error"
})
emit
(
'send_code'
,
{
"error_code"
:
0
,
"error_message"
:
"send message success"
})
def
on_login
(
self
,
message
):
json_data
=
message
token
=
json_data
.
get
(
'Authorization'
)
phone
=
json_data
[
'phone'
]
if
'phone'
in
json_data
else
None
code
=
json_data
[
'code'
]
if
'code'
in
json_data
else
None
login_type
=
json_data
[
'type'
]
if
'type'
in
json_data
else
1
# 1.验证码登录,2.密码登录
url
=
"https://guide.ssw-htzn.com/business_web/account/login"
headers
=
{
"platform"
:
"business_web"
...
...
@@ -322,16 +319,16 @@ class MyNamespace(Namespace):
if
token
:
headers
.
update
({
"Authorization"
:
token
})
data
=
{
"code"
:
code
,
"phone"
:
phone
,
"type"
:
login_type
}
result
=
requests
.
post
(
url
=
url
,
json
=
data
,
headers
=
headers
)
result
=
json
.
loads
(
result
.
text
)
if
int
(
result
[
"error_code"
])
!=
0
:
emit
(
'login'
,
{
"error_code"
:
-
1
,
"error_message"
:
"login error"
})
else
:
data
=
result
[
"data"
]
emit
(
'login'
,
{
"data"
:
data
,
"error_code"
:
0
,
"error_message"
:
"send message success"
})
def
on_reader_connect
(
self
,
message
):
try
:
hcontext
=
self
.
scard
.
establish_context
()
...
...
@@ -347,7 +344,7 @@ class MyNamespace(Namespace):
finally
:
hresult
=
self
.
scard
.
release_context
(
hcontext
)
# logger.info('Released context.')
def
on_read_history
(
self
,
message
):
try
:
hcontext
=
self
.
scard
.
establish_context
()
...
...
@@ -356,22 +353,51 @@ class MyNamespace(Namespace):
emit
(
'read_history'
,
{
'data'
:
None
,
'error_code'
:
1001
,
'error_message'
:
'读卡器连接失败, reader connect error'
})
history_data
=
[]
start_block
=
24
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
)
if
result
:
data
=
result
[:
16
]
data
=
result
[:
4
]
return_code
=
result
[
-
2
:]
if
data
[
0
]
==
0
:
history_data
.
append
({})
else
:
data
=
[
str
(
i
)
for
i
in
data
]
repair_date
=
''
.
join
(
data
[:
8
])
repair_content
=
int
(
''
.
join
(
data
[
8
:
10
]))
repair_man
=
int
(
''
.
join
(
data
[
10
:
12
]))
if
data
[
0
]
==
66
and
data
[
1
]
==
88
:
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
:
break
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
:
emit
(
'read_history'
,
{
'data'
:
None
,
'error_code'
:
1002
,
'error_message'
:
'读取历史数据失败连接错误, reader connect error'
})
...
...
@@ -382,53 +408,71 @@ class MyNamespace(Namespace):
pass
finally
:
hresult
=
self
.
scard
.
release_context
(
hcontext
)
# logger.info('Released context.')
def
on_write_repair
(
self
,
message
):
try
:
repair_date
=
message
[
"repair_date"
]
repair_content
=
str
(
message
[
"repair_content"
])
.
zfill
(
2
)
repair_man
=
str
(
message
[
"repair_man"
])
.
zfill
(
2
)
hcontext
=
None
repair_date
=
message
[
"repair_date"
][:
8
]
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
()
result
=
self
.
scard
.
find_self_reader
(
hcontext
)
if
not
result
:
emit
(
'write_repair'
,
{
'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
):
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
)
if
result
:
data
=
result
[:
16
]
return_code
=
result
[
-
2
:]
if
data
[
0
]
==
0
:
write_command
=
[
0xFF
,
0xD6
,
0x00
,
10
+
i
,
0x10
]
write_command
+=
[
int
(
x
)
for
x
in
repair_date
]
write_command
+=
[
int
(
x
)
for
x
in
repair_content
]
write_command
+=
[
int
(
x
)
for
x
in
repair_man
]
write_command
+=
[
0
,
0
,
0
,
0
]
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
:
emit
(
'write_repair'
,
{
'data'
:
None
,
'error_code'
:
0
,
'error_message'
:
'success'
})
return
else
:
emit
(
'write_repair'
,
{
'data'
:
None
,
'error_code'
:
1003
,
'error_message'
:
'写入维修数据失败,请重试, write repair data error'
})
break
if
data
[
0
]
==
66
and
data
[
1
]
==
88
:
start_block
+=
4
continue
else
:
# 数据 66 88两位头部标记 20211018 4位日期 010203 6位维修内容 JJJJ 4位字母
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
)
return_code
=
result
[
-
2
:]
if
return_code
[
0
]
==
144
and
return_code
[
1
]
==
0
:
continue
else
:
emit
(
'write_repair'
,
{
'data'
:
None
,
'error_code'
:
1003
,
'error_message'
:
'写入维修数据失败,请重试, write repair data error'
})
break
emit
(
'write_repair'
,
{
'data'
:
None
,
'error_code'
:
0
,
'error_message'
:
'success'
})
return
emit
(
'write_repair'
,
{
'data'
:
None
,
'error_code'
:
0
,
'error_message'
:
'success'
})
except
error
as
e
:
# logger.info(e)
pass
finally
:
hresult
=
self
.
scard
.
release_context
(
hcontext
)
# logger.info('Released context.')
if
hcontext
:
hresult
=
self
.
scard
.
release_context
(
hcontext
)
def
on_read_sn
(
self
,
message
):
try
:
sn
=
""
...
...
@@ -441,7 +485,7 @@ class MyNamespace(Namespace):
result
=
self
.
scard
.
card_transmit
(
hcontext
,
self
.
scard
.
reader
,
read_command
)
if
result
:
data
=
result
[:
16
]
return_code
=
result
[
-
2
:]
if
return_code
[
0
]
==
144
and
return_code
[
1
]
==
0
:
sn
=
[
hex
(
i
)
.
replace
(
'0x'
,
''
)
.
upper
()
for
i
in
data
]
...
...
@@ -458,16 +502,16 @@ class MyNamespace(Namespace):
pass
finally
:
hresult
=
self
.
scard
.
release_context
(
hcontext
)
# logger.info('Released context.')
def
on_write_sn
(
self
,
message
):
try
:
sn
=
message
[
"sn"
]
token
=
message
[
"Authorization"
]
last_four
=
hex
(
int
(
sn
[
-
4
:]))
.
replace
(
'0x'
,
''
)
.
zfill
(
4
)
sn
=
sn
[:
-
4
]
+
last_four
# TODO 校验sn是否重复
sn
=
sn
[
2
:]
power_no
=
sn
[
4
:]
...
...
@@ -477,7 +521,7 @@ class MyNamespace(Namespace):
"platform"
:
"business_web"
}
data
=
{
"power_no"
:
sn
}
# json_data = json.dumps(data, ensure_ascii=False)
# result = requests.post(url=url, data=json_data.encode('utf-8'), headers=headers, verify=None)
result
=
requests
.
post
(
url
=
url
,
json
=
data
,
headers
=
headers
)
...
...
@@ -493,7 +537,7 @@ class MyNamespace(Namespace):
if
not
result
:
emit
(
'write_sn'
,
{
'data'
:
None
,
'error_code'
:
1001
,
'error_message'
:
'读卡器连接失败, reader connect error'
})
write_command
=
[
0xFF
,
0xD6
,
0x00
,
17
,
0x10
]
write_command
+=
[
int
(
i
,
16
)
for
i
in
sn
]
result
=
self
.
scard
.
card_transmit
(
hcontext
,
self
.
scard
.
reader
,
write_command
)
...
...
@@ -512,10 +556,10 @@ class MyNamespace(Namespace):
pass
finally
:
hresult
=
self
.
scard
.
release_context
(
hcontext
)
# logger.info('Released context.')
def
on_delete_
all
(
self
,
message
):
def
on_delete_
sn
(
self
,
message
):
try
:
password
=
message
[
"password"
]
if
password
!=
"sswnb"
:
...
...
@@ -523,14 +567,14 @@ class MyNamespace(Namespace):
{
'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
)]
...
...
@@ -543,12 +587,12 @@ class MyNamespace(Namespace):
{
'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
:
...
...
@@ -558,10 +602,10 @@ class MyNamespace(Namespace):
{
'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'
,
...
...
@@ -569,22 +613,81 @@ class MyNamespace(Namespace):
'error_message'
:
'unkwon error, 删除数据,未知错误'
})
finally
:
hresult
=
self
.
scard
.
release_context
(
hcontext
)
def
on_delete_all
(
self
,
message
):
try
:
hcontext
=
None
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
:
if
hcontext
:
hresult
=
self
.
scard
.
release_context
(
hcontext
)
else
:
pass
# logger.info('Released context.')
# 广播事件,接收消息后广播
def
on_my_broadcast_event
(
self
,
message
):
session
[
'receive_count'
]
=
session
.
get
(
'receive_count'
,
0
)
+
1
emit
(
'my_response'
,
{
'data'
:
message
[
'data'
],
'count'
:
session
[
'receive_count'
]},
broadcast
=
True
)
def
on_ping
(
self
):
emit
(
'my_pong'
)
def
on_connect
(
self
):
emit
(
'my_response'
,
{
'data'
:
'Connected'
})
def
on_disconnect
(
self
):
emit
(
'my_response'
,
{
'data'
:
'Connected'
})
# print('Client disconnected', request.sid)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment