[87c2a6a] | 1 | #!/usr/bin/python3 |
---|
[99f2951] | 2 | |
---|
[834f5cd] | 3 | # |
---|
[cb9edc8] | 4 | # Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu> |
---|
[834f5cd] | 5 | # |
---|
| 6 | # This program is free software: you can redistribute it and/or modify it under |
---|
| 7 | # the terms of the GNU Affero General Public License as published by the |
---|
[cb9edc8] | 8 | # Free Software Foundation; either version 3 of the License, or |
---|
| 9 | # (at your option) any later version. |
---|
[834f5cd] | 10 | |
---|
[38b6d77] | 11 | import json |
---|
[fd1f01d] | 12 | import logging |
---|
[05f2fd4] | 13 | import argparse |
---|
[1601ad9] | 14 | import platform |
---|
[bf69d20] | 15 | import subprocess |
---|
[fd1f01d] | 16 | try: |
---|
| 17 | from signal import SIG_DFL, SIGPIPE |
---|
| 18 | except ImportError: |
---|
| 19 | from signal import SIG_DFL |
---|
| 20 | |
---|
| 21 | |
---|
[834f5cd] | 22 | from src.ogClient import * |
---|
[3dfe549] | 23 | from src.log import configure_logging |
---|
[fd1f01d] | 24 | |
---|
[834f5cd] | 25 | |
---|
[05f2fd4] | 26 | def send_event_dgram(msg, ip='127.0.0.1', port=55885): |
---|
| 27 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
---|
| 28 | sock.sendto(bytes(msg, "utf-8"), (ip, port)) |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | def create_parser(): |
---|
| 32 | events = ['login', 'logout'] |
---|
| 33 | parser = argparse.ArgumentParser() |
---|
| 34 | parser.set_defaults(func=None) |
---|
| 35 | subparsers = parser.add_subparsers() |
---|
| 36 | |
---|
| 37 | parser_event = subparsers.add_parser('event') |
---|
| 38 | |
---|
| 39 | subparsers_event = parser_event.add_subparsers() |
---|
| 40 | parser_event_login = subparsers_event.add_parser('login') |
---|
| 41 | parser_event_login.set_defaults(func=lambda x: send_event_dgram(f'session start {x.user}')) |
---|
| 42 | parser_event_login.add_argument('user', type=str) |
---|
| 43 | parser_event_logout = subparsers_event.add_parser('logout') |
---|
| 44 | parser_event_logout.set_defaults(func=lambda x: send_event_dgram(f'session stop {x.user}')) |
---|
| 45 | parser_event_logout.add_argument('user', type=str) |
---|
| 46 | |
---|
[ecd735c] | 47 | parser.add_argument('-c', '--config', default="", |
---|
| 48 | help='ogClient JSON config file path') |
---|
| 49 | |
---|
[05f2fd4] | 50 | return parser |
---|
| 51 | |
---|
| 52 | |
---|
[834f5cd] | 53 | def main(): |
---|
[05f2fd4] | 54 | parser = create_parser() |
---|
| 55 | args = parser.parse_args(sys.argv[1:]) |
---|
| 56 | if args.func: |
---|
| 57 | args.func(args) |
---|
| 58 | return |
---|
[1601ad9] | 59 | |
---|
[ecd735c] | 60 | if args.config: |
---|
| 61 | config_path = args.config |
---|
| 62 | elif platform.system().lower() == 'linux': |
---|
[1601ad9] | 63 | config_path = f'{ogClient.OG_PATH}ogclient/cfg/ogclient.json' |
---|
| 64 | else: |
---|
| 65 | config_path = './cfg/ogclient.json' |
---|
| 66 | |
---|
[38b6d77] | 67 | try: |
---|
| 68 | with open(config_path, 'r') as f: |
---|
| 69 | CONFIG = json.load(f) |
---|
| 70 | except: |
---|
| 71 | print('Error: Parsing configuration file') |
---|
[834f5cd] | 72 | return 0 |
---|
| 73 | |
---|
[38b6d77] | 74 | MODE = CONFIG['opengnsys']['mode'] |
---|
| 75 | URL = CONFIG['opengnsys']['url'] |
---|
[1377ace] | 76 | if MODE == 'live': |
---|
[38b6d77] | 77 | proc = subprocess.Popen(["browser", "-qws", URL]) |
---|
[fd1f01d] | 78 | if MODE != 'windows': |
---|
| 79 | signal.signal(SIGPIPE, SIG_DFL) |
---|
[bf69d20] | 80 | |
---|
[3dfe549] | 81 | configure_logging(MODE) |
---|
| 82 | |
---|
[38b6d77] | 83 | client = ogClient(config=CONFIG) |
---|
[834f5cd] | 84 | client.connect() |
---|
| 85 | client.run() |
---|
| 86 | |
---|
| 87 | if __name__ == "__main__": |
---|
| 88 | main() |
---|