· 4 years ago · Apr 20, 2021, 11:14 AM
1 def send_network_config(
2 self,
3 ssid: str,
4 passwd: str,
5 secret_key: str,
6 object_id: str = None,
7 security: str = 'wpa-psk',
8 https: bool = False,
9 custom_app_cert: bool = False,
10 ):
11 """
12 Send network config to device.
13 ssid is name AP device should connect to after setup
14 passwd is, you guessed it, password of AP device should connect after
15 setup. Can be None in case if SSID has no password.
16 security: set connection security type, e.g. 'wpa-eap', 'none', etc.
17 https: send config to device using HTTPS endpoint
18 custom_app_cert: try to send network config to device
19 using custom SSL certificates
20 """
21 def to_wait_send_network_config():
22 security_map = {
23 'wpa-none': 'wpa-personal',
24 'wpa-psk': 'wpa-personal',
25 'wpa-psk2': 'wpa2-personal',
26 'wpa-eap': 'wpa-personal',
27 'ieee8021x': 'wep',
28 'none': 'none',
29 }
30
31 config_security = security_map[security]
32 if passwd is None:
33 log.info(f'Password not provided - using "none" security type')
34 config_security = 'none'
35
36 log.info(f'secret key: {secret_key}.')
37
38 gateway = self.wifi.get_default_gateway()
39 domain_part = 'ring' if https else 'gainspan'
40 endpoint = f'{domain_part}/system/config/network'
41 url = construct_endpoint(gateway, endpoint, https=https)
42 config = network_config(
43 secret_key, ssid, passwd, object_id, config_security
44 )
45
46 log.info(f'Sending network config to "{url}": {config}')
47 r = requests.post(url, data=config, verify=custom_app_cert, timeout=10)
48
49 status_code = r.status_code
50 if not status_code == 200:
51 raise RuntimeError(f'http status code is not ok: ({status_code})')
52
53 wait_for(to_wait_send_network_config, timeout=60, interval=15,
54 raise_exc=False)