1 | # Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu> |
---|
2 | # |
---|
3 | # This program is free software: you can redistribute it and/or modify it under |
---|
4 | # the terms of the GNU Affero General Public License as published by the |
---|
5 | # Free Software Foundation; either version 3 of the License, or |
---|
6 | # (at your option) any later version. |
---|
7 | |
---|
8 | from cli.utils import * |
---|
9 | |
---|
10 | import argparse |
---|
11 | |
---|
12 | |
---|
13 | class OgWol(): |
---|
14 | |
---|
15 | @staticmethod |
---|
16 | def send_wol(rest, args): |
---|
17 | def scope_lookup(scope_id, scope_type, d): |
---|
18 | if scope_id == d.get('id') and \ |
---|
19 | scope_type == d.get('type'): |
---|
20 | return d |
---|
21 | for scope in d['scope']: |
---|
22 | lookup = scope_lookup(scope_id, |
---|
23 | scope_type, |
---|
24 | scope) |
---|
25 | if lookup is not None: |
---|
26 | return lookup |
---|
27 | return None |
---|
28 | |
---|
29 | parser = argparse.ArgumentParser() |
---|
30 | parser.add_argument('--type', |
---|
31 | nargs='?', |
---|
32 | choices=['broadcast', 'unicast'], |
---|
33 | default='broadcast', |
---|
34 | help='') |
---|
35 | group = parser.add_argument_group( |
---|
36 | 'clients', 'Client selection options') |
---|
37 | group.add_argument('--center-id', |
---|
38 | type=int, |
---|
39 | action='append', |
---|
40 | default=[], |
---|
41 | required=False, |
---|
42 | help='Clients from given center id') |
---|
43 | group.add_argument('--room-id', |
---|
44 | type=int, |
---|
45 | action='append', |
---|
46 | default=[], |
---|
47 | required=False, |
---|
48 | help='Clients from given room id') |
---|
49 | group.add_argument('--client-ip', |
---|
50 | action='append', |
---|
51 | default=[], |
---|
52 | required=False, |
---|
53 | help='Specific client IP') |
---|
54 | parsed_args = parser.parse_args(args) |
---|
55 | |
---|
56 | r = rest.get('/scopes') |
---|
57 | scopes = r.json() |
---|
58 | ips = set() |
---|
59 | |
---|
60 | for center in parsed_args.center_id: |
---|
61 | center_scope = scope_lookup(center, 'center', scopes) |
---|
62 | ips.update(ips_in_scope(center_scope)) |
---|
63 | for room in parsed_args.room_id: |
---|
64 | room_scope = scope_lookup(room, 'room', scopes) |
---|
65 | ips.update(ips_in_scope(room_scope)) |
---|
66 | for l in parsed_args.client_ip: |
---|
67 | ips.add(l) |
---|
68 | |
---|
69 | if not ips: |
---|
70 | print("No clients found") |
---|
71 | return None |
---|
72 | |
---|
73 | payload = {'type': parsed_args.type, 'clients': list(ips)} |
---|
74 | r = rest.post('/wol', payload=payload) |
---|