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
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
145 additions
and
42 deletions
+145
-42
app.py
+145
-42
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,8 +38,8 @@ 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
)
...
...
@@ -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
({})
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
:
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
]))
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'
})
...
...
@@ -387,33 +413,49 @@ class MyNamespace(Namespace):
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
]
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
:
emit
(
'write_repair'
,
{
'data'
:
None
,
'error_code'
:
0
,
'error_message'
:
'success'
})
return
continue
else
:
emit
(
'write_repair'
,
{
'data'
:
None
,
'error_code'
:
1003
,
...
...
@@ -421,14 +463,16 @@ class MyNamespace(Namespace):
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
:
if
hcontext
:
hresult
=
self
.
scard
.
release_context
(
hcontext
)
# logger.info('Released context.')
def
on_read_sn
(
self
,
message
):
try
:
sn
=
""
...
...
@@ -515,8 +559,64 @@ class MyNamespace(Namespace):
# 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
):
try
:
hcontext
=
None
password
=
message
[
"password"
]
if
password
!=
"sswnb"
:
emit
(
'delete_all'
,
...
...
@@ -568,7 +668,10 @@ class MyNamespace(Namespace):
{
'data'
:
None
,
'error_code'
:
1009
,
'error_message'
:
'unkwon error, 删除数据,未知错误'
})
finally
:
if
hcontext
:
hresult
=
self
.
scard
.
release_context
(
hcontext
)
else
:
pass
# logger.info('Released context.')
...
...
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