-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathglass_server.py
More file actions
71 lines (49 loc) · 1.95 KB
/
Copy pathglass_server.py
File metadata and controls
71 lines (49 loc) · 1.95 KB
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
61
62
63
64
65
66
67
68
69
70
71
# coding=utf-8
# socket server to communicate with smart glass app
import json
import socket_server
import time
TYPE_SEPARATOR_CHAR = '|'
DATA_TYPE_DUMMY = 'D'
DATA_TYPE_USER_CONVERSATION = 'U'
DATA_TYPE_CONVERSATION = 'C'
DATA_TYPE_MESSAGE = 'M'
DATA_TYPE_INSTRUCTION_MESSAGE = 'I'
DUMMY_DATA = DATA_TYPE_DUMMY + TYPE_SEPARATOR_CHAR
def start_chat_server():
socket_server.start_server_threaded()
def stop_chat_server():
socket_server.stop_server_threaded()
def is_connected():
return socket_server.is_client_connected()
def send_user_conversation(user_conversation):
tx_data = json.dumps(user_conversation)
socket_server.send_data(f'{DATA_TYPE_USER_CONVERSATION}{TYPE_SEPARATOR_CHAR}{tx_data}')
def send_conversation(conversation):
tx_data = json.dumps(conversation)
socket_server.send_data(f'{DATA_TYPE_CONVERSATION}{TYPE_SEPARATOR_CHAR}{tx_data}')
def reset_conversation(conversation):
pass
def send_message(message):
tx_data = json.dumps(message)
socket_server.send_data(f'{DATA_TYPE_MESSAGE}{TYPE_SEPARATOR_CHAR}{tx_data}')
def send_instruction_message(message):
tx_data = json.dumps(message)
socket_server.send_data(f'{DATA_TYPE_INSTRUCTION_MESSAGE}{TYPE_SEPARATOR_CHAR}{tx_data}')
# return <TYPE, JSON_OBJECT> if available, else send <None, None>
def get_chat_data():
rx_data = socket_server.receive_data()
if rx_data is None:
return None, None
# <TYPE><TYPE_SEPARATOR><JSON_STRING>
type_data = rx_data.split(TYPE_SEPARATOR_CHAR)
return type_data[0], _decode_data(type_data[0], type_data[1])
def _decode_data(type, json_string):
if type == DATA_TYPE_DUMMY:
return json_string
if type == DATA_TYPE_USER_CONVERSATION or type == DATA_TYPE_CONVERSATION or type == DATA_TYPE_MESSAGE:
object = json.loads(json_string)
object['server_time'] = round(time.time() * 1000)
return object
print(f'Unknown data type: {type}-{json_string}')
return json_string