~aleteoryx/muditaos

ref: eda92600a7df852e18bdb44388966248a4af3c77 muditaos/test/custom_quotes.py -rw-r--r-- 8.1 KiB
eda92600 — Maciej Gibowicz [BH-2095] Add quote interval setting 10 months ago
                                                                                
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import base64
import os.path
import serial
import random
import json
import zlib
import argparse
import time
from tqdm import tqdm

http_methods = {
    'GET': 1,
    'POST': 2,
    'PUT': 3,
    'DELETE': 4
}

endpoint_types = {
    'Invalid': 0,
    'DeviceInfo': 1,
    'Update': 2,
    'FilesystemUpload': 3,
    'Backup': 4,
    'Restore': 5,
    'Factory': 6,
    'Contacts': 7,
    'Messages': 8,
    'Calllog': 9,
    'CalendarEventsPlaceholder': 10,
    'DeveloperMode': 11,
    'Bluetooth': 12,
    'UsbSecurity': 13,
    'Outbox': 14,
    'Reboot': 15,
    'TimeSync': 16,
    'Quotes': 17
}

payload_marker = '#'
payload_size_len = 9
port_timeout_short_s = 1
port_timeout_long_s = 5
port_baudrate = 115200


def get_new_uuid() -> int:
    return random.randint(1, 10000)

def add_quote(port_path: str, quote_str: str, author_str: str) -> bool:
    uuid = get_new_uuid()

    payload = {
        'endpoint': endpoint_types['Quotes'],
        'method': http_methods['POST'],
        'uuid': uuid,
        'body': {
            'quote': quote_str,
            'author': author_str
        }
    }

    payload_str = json.dumps(payload)
    payload_len_str = str(len(payload_str)).rjust(payload_size_len, '0')
    request_str = payload_marker + payload_len_str + payload_str

    with serial.Serial(port_path, port_baudrate, timeout=port_timeout_short_s) as port:
        port.write(request_str.encode('ascii'))
        response = port.read(2048)
        resp_json = json.loads(response[10:])
        status = resp_json['status']
        if status == 200:
            print(f'Request success {resp_json["body"]}')
            return True
        print(f'Request failed, status {status}')
        return False
    
def edit_quote(port_path: str, quote_id: int,  quote_str: str, author_str: str) -> bool:
    uuid = get_new_uuid()

    payload = {
        'endpoint': endpoint_types['Quotes'],
        'method': http_methods['PUT'],
        'uuid': uuid,
        'body': {
            'quoteID': quote_id,
            'quote': quote_str,
            'author': author_str
        }
    }

    payload_str = json.dumps(payload)
    payload_len_str = str(len(payload_str)).rjust(payload_size_len, '0')
    request_str = payload_marker + payload_len_str + payload_str

    with serial.Serial(port_path, port_baudrate, timeout=port_timeout_short_s) as port:
        port.write(request_str.encode('ascii'))
        response = port.read(2048)
        resp_json = json.loads(response[10:])
        status = resp_json['status']
        if status == 200:
            print(f'Request success')
            return True
        print(f'Request failed, status {status}')
        return False

def delete_quote(port_path: str, quote_id: int) -> bool:
    uuid = get_new_uuid()

    payload = {
        'endpoint': endpoint_types['Quotes'],
        'method': http_methods['DELETE'],
        'uuid': uuid,
        'body': {
            'quoteID': quote_id
        }
    }

    payload_str = json.dumps(payload)
    payload_len_str = str(len(payload_str)).rjust(payload_size_len, '0')
    request_str = payload_marker + payload_len_str + payload_str

    with serial.Serial(port_path, port_baudrate, timeout=port_timeout_short_s) as port:
        port.write(request_str.encode('ascii'))
        response = port.read(2048)
        resp_json = json.loads(response[10:])
        status = resp_json['status']
        if status == 204:
            print(f'Request success')
            return True
        print(f'Request failed, status {status}')
        return False

def change_group(port_path: str, quote_group: str) -> bool:
    uuid = get_new_uuid()

    payload = {
        'endpoint': endpoint_types['Quotes'],
        'method': http_methods['PUT'],
        'uuid': uuid,
        'body': {
            'group': quote_group
        }
    }

    payload_str = json.dumps(payload)
    payload_len_str = str(len(payload_str)).rjust(payload_size_len, '0')
    request_str = payload_marker + payload_len_str + payload_str

    with serial.Serial(port_path, port_baudrate, timeout=port_timeout_short_s) as port:
        port.write(request_str.encode('ascii'))
        response = port.read(2048)
        resp_json = json.loads(response[10:])
        status = resp_json['status']
        if status == 200:
            print(f'Request success')
            return True
        print(f'Request failed, status {status}')
        return False

def change_interval(port_path: str, quote_interval: str) -> bool:
    uuid = get_new_uuid()

    payload = {
        'endpoint': endpoint_types['Quotes'],
        'method': http_methods['PUT'],
        'uuid': uuid,
        'body': {
            'interval': quote_interval
        }
    }

    payload_str = json.dumps(payload)
    payload_len_str = str(len(payload_str)).rjust(payload_size_len, '0')
    request_str = payload_marker + payload_len_str + payload_str

    with serial.Serial(port_path, port_baudrate, timeout=port_timeout_short_s) as port:
        port.write(request_str.encode('ascii'))
        response = port.read(2048)
        resp_json = json.loads(response[10:])
        status = resp_json['status']
        if status == 200:
            print(f'Request success')
            return True
        print(f'Request failed, status {status}')
        return False

def main():
    parser = argparse.ArgumentParser(
        prog='custom_quotes',
        description='Script used to test functionality of custom quotes endpoint in Harmony project'
    )
    parser.add_argument('-p', '--port',
                        metavar='path_to_com_port',
                        help='path to COM port of the device (e.g. /dev/ttyACM0)')
    parser.add_argument('-a', '--add',
                        action='store_true',
                        help='add new custom quote')
    parser.add_argument('-d', '--delete',
                        metavar='quote_id_to_delete',
                        type=int,
                        help='delete custom quote')
    parser.add_argument('-e', '--edit',
                        metavar='quote_id_to_edit',
                        type=int,
                        help='edit custom quote')
    parser.add_argument('-q', '--quote',
                        metavar='string_with_quote',
                        help='string with new quote')
    parser.add_argument('-t', '--author',
                        metavar='string_with_author',
                        help='string with author')
    parser.add_argument('-g', '--group',
                        metavar='string_with_group_type',
                        help='string with type of group of displayed quotes [Predefined or Custom]')
    parser.add_argument('-i', '--interval',
                        metavar='quotes_display_interval',
                        help='quotes display interval [\'x\' minutes or \'AtMidnight\']')

    args = parser.parse_args()
    if not args.port:
        print('Invalid usage: please specify device port')
        print('Run with -h to see help')
        return
    if not args.add and not args.delete and not args.edit and not args.group and not args.interval:
        print('Invalid usage: please specify add, delete, edit, group or interval argument')
        print('Run with -h to see help')
        return
    if args.add:
        if not args.quote:
            print('Invalid usage: please add quote')
            print('Run with -h to see help')
            return
        else:
            print("adding new quotes: " + args.quote)
            status = add_quote(args.port, args.quote, args.author)
    elif args.delete:
        print("deleting quotes nr: " + str(args.delete))
        status = delete_quote(args.port, args.delete)
    elif args.edit:
        if not args.quote:
            print('Invalid usage: please add quote')
            print('Run with -h to see help')
            return
        else:
            print("editing quotes nr: " + str(args.edit))
            status = edit_quote(args.port, args.edit, args.quote, args.author)
    elif args.group:
        print("quotes group: " + args.group)
        status = change_group(args.port, args.group)
    elif args.interval:
        print("quotes interval: " + args.interval)
        status = change_interval(args.port, args.interval)

if __name__ == '__main__':
    main()