File size: 5,059 Bytes
27867f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
MIT License

Copyright (C) 2023 ROCKY4546
https://github.com/rocky4546

This file is part of Cabernet

Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the “Software”), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
"""

import lib.common.utils as utils
import lib.clients.hdhr.hdhr_server as hdhr_server

from lib.web.pages.templates import web_templates
from lib.common.decorators import getrequest
from lib.common.decorators import postrequest
from .templates import hdhr_templates


@getrequest.route('/discover.json')
def discover_json(_webserver):
    ns_inst_path = _webserver.get_ns_inst_path(_webserver.query_data)
    name = _webserver.query_data['name']
    if name is None:
        name = ''
        hdhr_id =  _webserver.config['hdhomerun']['hdhr_id']
    else:
        namespace = name
        name = namespace + ' '
        hdhr_id = _webserver.config[namespace.lower()].get('hdhr_id')
        if not hdhr_id:
            hdhr_id = _webserver.config['hdhomerun']['hdhr_id']
        
    namespace = None
    for area, area_data in _webserver.config.items():
        if 'player-tuner_count' in area_data.keys():
            namespace = area
    _webserver.do_mime_response(200,
                                'application/json',
                                hdhr_templates['jsonDiscover'].format(
                                    name + _webserver.config['hdhomerun']['reporting_friendly_name'],
                                    _webserver.config['hdhomerun']['reporting_model'],
                                    _webserver.config['hdhomerun']['reporting_firmware_name'],
                                    _webserver.config['main']['version'],
                                    hdhr_id,
                                    _webserver.config[namespace]['player-tuner_count'],
                                    _webserver.web_admin_url, ns_inst_path))


@getrequest.route('/device.xml')
def device_xml(_webserver):
    name = _webserver.query_data['name']
    if name is None:
        name = ''
        hdhr_id =  _webserver.config['hdhomerun']['hdhr_id']
    else:
        namespace = name
        name = namespace + ' '
        hdhr_id = _webserver.config[namespace.lower()].get('hdhr_id')
        if not hdhr_id:
            hdhr_id = _webserver.config['hdhomerun']['hdhr_id']

    _webserver.do_mime_response(200,
                                'application/xml',
                                hdhr_templates['xmlDevice'].format(
                                    name + _webserver.config['hdhomerun']['reporting_friendly_name'],
                                    _webserver.config['hdhomerun']['reporting_model'],
                                    hdhr_id,
                                    _webserver.config['main']['uuid'],
                                    utils.CABERNET_URL
                                ))


@getrequest.route('/lineup_status.json')
def lineup_status_json(_webserver):
    # Assumes only one scan can be active at a time.
    if _webserver.scan_state < 0:
        return_json = hdhr_templates['jsonLineupStatusIdle'] \
            .replace("Antenna", _webserver.config['hdhomerun']['tuner_type'])
    else:
        _webserver.scan_state += 20
        if _webserver.scan_state > 100:
            _webserver.scan_state = 100
        num_of_channels = len(_webserver.channels_db.get_channels(_webserver.query_data['name'], None, True))
        return_json = hdhr_templates['jsonLineupStatusScanning'].format(
            _webserver.scan_state,
            int(num_of_channels * _webserver.scan_state / 100))

        if _webserver.scan_state == 100:
            _webserver.scan_state = -1
            _webserver.update_scan_status(_webserver.query_data['name'], 'Idle')
    _webserver.do_mime_response(200, 'application/json', return_json)


@postrequest.route('/lineup.post')
def lineup_post(_webserver):
    if _webserver.query_data['scan'] == 'start':
        _webserver.scan_state = 0
        _webserver.update_scan_status(_webserver.query_data['name'], 'Scan')
        _webserver.do_mime_response(200, 'text/html')
    elif _webserver.query_data['scan'] == 'abort':
        _webserver.do_mime_response(200, 'text/html')
        _webserver.scan_state = -1
        _webserver.update_scan_status(_webserver.query_data['name'], 'Idle')
    else:
        _webserver.logger.warning("Unknown scan command " + _webserver.query_data['scan'])
        _webserver.do_mime_response(
            400, 'text/html',
            web_templates['htmlError'].format(
                _webserver.query_data['scan'] + ' is not a valid scan command'))