-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjson_handler.py
60 lines (49 loc) · 1.98 KB
/
json_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import json
import base64
def create_paring_request_message(client_name) :
"""
This function will generate json string for ping request message
:param client_name: get name of the client
:return: json string of created message
"""
message = {"protocol_version":1,"payload":{"service_name":"androidtvremote","client_name":client_name},"type":10,"status":200}
json_dump = json.dumps(message)
return json_dump
def create_option_message() :
"""
This function will generate json string for option message
:return: json string of created message
"""
message = {"protocol_version":1,"payload":{"output_encodings":[{"symbol_length":4,"type":3}],
"input_encodings":[{"symbol_length":4,"type":3}],"preferred_role":1},"type":20,"status":200}
json_dump = json.dumps(message)
return json_dump
def create_configuration_mesaage() :
"""
This function will generate json string for configuration message
:return: json string of created message
"""
message = {"protocol_version":1,"payload":{"encoding":{"symbol_length":4,"type":3},"client_role":1},"type":30,"status":200}
json_dump = json.dumps(message)
return json_dump
def create_secret_message(secret_hash) :
"""
This function will generate json string for secret message
:param secret_hash: get string of clients secret hash
:return: json string of created message
"""
message = {"protocol_version":1,"payload":{"secret":base64.b64encode(secret_hash).decode()},"type":40,"status":200}
json_dump = json.dumps(message)
return json_dump
def parse_json_message(raw_data) :
"""
This function will parse the json message
:param raw_data: get bytes of raw json message
:return: extracted status and type of the message
"""
json_object = json.loads(raw_data)
message_status = json_object["status"]
message_type = 0
if message_status == 200 :
message_type = json_object["type"]
return message_status, message_type