Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
new-socketemulator
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
李远洪
new-socketemulator
Commits
84b6d267
Commit
84b6d267
authored
Feb 04, 2020
by
李远洪
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完成平台通用应答消息解析类
parent
2b2ab5b7
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
176 additions
and
5 deletions
+176
-5
lib/protocol/message/MessageBase.py
lib/protocol/message/MessageBase.py
+3
-3
lib/protocol/message/TerminalHeartbeat_msg.py
lib/protocol/message/TerminalHeartbeat_msg.py
+1
-1
lib/protocol/messageRes/PlatformCommon_res.py
lib/protocol/messageRes/PlatformCommon_res.py
+115
-0
lib/protocol/messageRes/ResponseBase.py
lib/protocol/messageRes/ResponseBase.py
+50
-0
lib/protocol/messageRes/__init__.py
lib/protocol/messageRes/__init__.py
+0
-0
lib/socket/messageTest.py
lib/socket/messageTest.py
+7
-1
No files found.
lib/protocol/message/MessageBase.py
View file @
84b6d267
...
...
@@ -33,8 +33,8 @@ class MessageBase(Base):
msgID
=
self
.
int2hexStringByBytes
(
102
,
2
)
#消息id
msgBodyProperty
=
self
.
getMsgBodyProperty
()
#消息体属性
phoneNum
=
self
.
int2BCD
(
13146201118
)
#终端手机号
msgWaterCode
=
self
.
int2
BCD
(
1
)
#消息流水号
subPkgContent
=
""
#消息包封装项
msgWaterCode
=
self
.
int2
hexStringByBytes
(
1
)
#消息流水号
subPkgContent
=
""
#消息包封装项
s
data
=
msgID
+
msgBodyProperty
+
phoneNum
+
msgWaterCode
+
subPkgContent
return
data
...
...
@@ -130,6 +130,6 @@ if __name__ == "__main__":
# print(MessageBase().int2hexStringByBytes(22,2))
# print(MessageBase().getMsgBodyProperty())
# print(MessageBase().int2BCD(13146201117))
print
(
MessageBase
()
.
getCheckCode
(
"
abcde
0"
))
print
(
MessageBase
()
.
getCheckCode
(
"
800100050131462011180000000000020
0"
))
print
(
MessageBase
()
.
getMsgHeader
())
print
(
MessageBase
()
.
generateMsg
())
\ No newline at end of file
lib/protocol/message/TerminalHeartbeat_msg.py
View file @
84b6d267
...
...
@@ -40,7 +40,7 @@ class TerminalHeartbeat_msg(MessageBase):
# msgID = self.int2hexStringByBytes(102,2) #消息id
msgID
=
"0002"
msgBodyProperty
=
self
.
getMsgBodyProperty
(
len
(
self
.
getMsgBody
()))
#消息体属性
phoneNum
=
self
.
int2BCD
(
1314620111
8
)
#终端手机号
phoneNum
=
self
.
int2BCD
(
1314620111
9
)
#终端手机号
msgWaterCode
=
self
.
int2BCD
(
1
)
#消息流水号
subPkgContent
=
""
#消息包封装项
data
=
msgID
+
msgBodyProperty
+
phoneNum
+
msgWaterCode
+
subPkgContent
...
...
lib/protocol/messageRes/PlatformCommon_res.py
0 → 100644
View file @
84b6d267
#encoding:utf-8
'''
定义平台通用应答消息解码类
'''
from
lib.protocol.message.MessageBase
import
MessageBase
from
lib.protocol.messageRes.ResponseBase
import
ResponseBase
class
PlatformCommon_res
(
ResponseBase
):
def
__init__
(
self
,
msg
):
super
()
.
__init__
()
if
type
(
msg
)
==
bytes
:
self
.
msg
=
self
.
binary2ascii
(
msg
)
else
:
self
.
msg
=
msg
pass
#######################################################
# 获取消息
#######################################################
def
getMsg
(
self
):
json_msg
=
{}
json_msg
[
"header"
]
=
self
.
getMsgHeader
()
json_msg
[
"body"
]
=
self
.
getMsgBody
()
json_msg
[
"checkCode"
]
=
self
.
getCheckCode
()
json_msg
[
"calculateCheckCode"
]
=
self
.
getCalculateCheckCode
()
#自己计算消息后得到的校验码
return
json_msg
#######################################################
# 获取消息头
#######################################################
def
getMsgHeader
(
self
):
json_header
=
{}
data
=
self
.
removeIdentify
(
self
.
msg
)
data
=
self
.
restore_7e7d
(
data
)
header
=
data
[:
24
]
msgId
=
header
[:
4
]
#消息id
msgBodyProperty
=
header
[
4
:
8
]
#消息体属性
phoneNum
=
header
[
8
:
20
]
#终端手机号
msgWaterCode
=
header
[
20
:
24
]
#消息流水号
json_header
[
"msgId"
]
=
int
(
msgId
,
16
)
json_header
[
"msgBodyProperty"
]
=
self
.
getMsgBodyProperty
(
msgBodyProperty
)
json_header
[
"phoneNum"
]
=
phoneNum
[
1
:]
json_header
[
"msgWaterCode"
]
=
int
(
msgWaterCode
,
16
)
return
json_header
#获取消息体属性
def
getMsgBodyProperty
(
self
,
data
):
data
=
self
.
int2binStr
(
int
(
data
),
2
)
data
=
self
.
restore_7e7d
(
data
)
json_data
=
{}
subPkg
=
data
[
2
:
3
]
#分包
encryptionType
=
data
[
3
:
6
]
#加密方式
msgBodyLen
=
data
[
6
:]
#消息体长度
json_data
[
"subPkg"
]
=
int
(
subPkg
,
2
)
json_data
[
"encryptionType"
]
=
int
(
encryptionType
,
2
)
json_data
[
"msgBodyLen"
]
=
int
(
msgBodyLen
,
2
)
return
json_data
#######################################################
# 获取消息体
#######################################################
def
getMsgBody
(
self
):
json_body
=
{}
data
=
self
.
removeIdentify
(
self
.
msg
)
data
=
self
.
restore_7e7d
(
data
)
dataLen
=
len
(
data
)
body
=
data
[
24
:
dataLen
-
2
]
resWaterCode
=
body
[:
4
]
#应答流水号
resId
=
body
[
4
:
8
]
#应答id
result
=
body
[
8
:]
#结果
json_body
[
"resWaterCode"
]
=
int
(
resWaterCode
,
16
)
json_body
[
"resId"
]
=
int
(
resId
,
16
)
json_body
[
"result"
]
=
int
(
result
,
16
)
return
json_body
#######################################################
# 获取校验码
#######################################################
def
getCheckCode
(
self
):
data
=
self
.
removeIdentify
(
self
.
msg
)
data
=
self
.
restore_7e7d
(
data
)
dataLen
=
len
(
data
)
checkCode
=
data
[
dataLen
-
2
:]
return
checkCode
#######################################################
# 计算消息得到校验码
#######################################################
def
getCalculateCheckCode
(
self
):
data
=
self
.
removeIdentify
(
self
.
msg
)
data
=
self
.
restore_7e7d
(
data
)
dataLen
=
len
(
data
)
data
=
data
[:
dataLen
-
2
]
calculateCheckCode
=
MessageBase
()
.
getCheckCode
(
data
)
return
calculateCheckCode
#######################################################
# 获取最原始的消息数据(没有替换7e,7d之前的状态)
#######################################################
def
getOriginalMsg
(
self
):
data
=
self
.
removeIdentify
(
self
.
msg
)
data
=
self
.
restore_7e7d
(
data
)
data
=
"7e"
+
data
+
"7e"
return
data
lib/protocol/messageRes/ResponseBase.py
0 → 100644
View file @
84b6d267
#encoding:utf-8
import
binascii
from
lib.protocol.Base
import
Base
class
ResponseBase
(
Base
):
def
__init__
(
self
):
pass
#######################################################
# 二进制转换为ascii码
#######################################################
def
binary2ascii
(
self
,
binData
):
strs
=
binascii
.
b2a_hex
(
binData
)
strsLen
=
len
(
str
(
strs
))
data
=
str
(
strs
)[
2
:
strsLen
-
1
]
return
data
#######################################################
# 还原消息中被转换过的7e和7d
#######################################################
def
restore_7e7d
(
self
,
data
):
data
=
data
.
replace
(
"7d02"
,
"7e"
)
data
=
data
.
replace
(
"7d01"
,
"7d"
)
return
data
#######################################################
# 去除标识位
#######################################################
def
removeIdentify
(
self
,
data
):
dataLen
=
len
(
data
)
data
=
data
[
2
:
dataLen
-
2
]
return
data
#######################################################
# 10进制转换为2进制字符串
#######################################################
def
int2binStr
(
self
,
data
,
bytescount
=
1
):
binStr
=
bin
(
data
)
binStr
=
binStr
[
2
:]
bytesLen
=
bytescount
*
8
while
len
(
binStr
)
<
bytesLen
:
binStr
=
"0"
+
binStr
return
binStr
if
__name__
==
"__main__"
:
print
(
ResponseBase
()
.
int2binStr
(
7
,
2
))
\ No newline at end of file
lib/protocol/messageRes/__init__.py
0 → 100644
View file @
84b6d267
lib/socket/messageTest.py
View file @
84b6d267
...
...
@@ -3,6 +3,7 @@ import binascii
import
socket
from
lib.protocol.message.MessageBase
import
MessageBase
from
lib.protocol.message.TerminalHeartbeat_msg
import
TerminalHeartbeat_msg
from
lib.protocol.messageRes.PlatformCommon_res
import
PlatformCommon_res
def
back7e7d
(
data
):
...
...
@@ -31,8 +32,13 @@ client.connect((host, port))
client
.
send
(
binascii
.
a2b_hex
(
msg
))
# client.send(bytes.fromhex(msg))
data
=
client
.
recv
(
BUF_SIZE
)
print
(
data
)
print
(
binascii
.
b2a_hex
(
data
))
print
(
binary2ascii
(
data
))
print
(
PlatformCommon_res
(
data
)
.
binary2ascii
(
data
))
print
(
PlatformCommon_res
(
data
)
.
getOriginalMsg
())
print
(
PlatformCommon_res
(
data
)
.
getMsg
())
client
.
close
()
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