File size: 6,474 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
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
"""
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 logging
import threading

import lib.common.utils as utils
from lib.db.db_scheduler import DBScheduler


class PluginInstanceObj:

    def __init__(self, _plugin_obj, _instance_key):
        self.logger = logging.getLogger(__name__)
        self.config_obj = _plugin_obj.config_obj
        self.plugin_obj = _plugin_obj
        self.instance_key = _instance_key
        self.scheduler_db = DBScheduler(self.config_obj.data)
        self.scheduler_tasks()
        self.enabled = True
        self.channels = None
        self.programs = None
        self.epg = None
        if not self.config_obj.data[self.config_section]['enabled']:
            self.enabled = False
        else:
            self.enabled = True

    def terminate(self):
        """
        Removes all has a object from the object and calls any subclasses to also terminate
        Not calling inherited class at this time
        """
        self.enabled = False
        if self.channels:
            self.channels.terminate()
        if self.epg:
            self.epg.terminate()
        if self.programs:
            self.programs.terminate()
        self.logger = None
        self.config_obj = None
        self.plugin_obj = None
        self.instance_key = None
        self.scheduler_db = None
        self.enabled = None
        self.channels = None
        self.programs = None
        self.epg = None


    ##############################
    # ## EXTERNAL STREAM METHODS
    ##############################

    def is_time_to_refresh(self, _last_refresh):
        """
        External request to determine if the m3u8 stream uri needs to 
        be refreshed.
        Called from stream object.
        """
        return False

    def get_channel_uri(self, sid):
        """
        External request to return the uri for a m3u8 stream.
        Called from stream object.
        """
        if self.enabled and self.config_obj.data[self.config_section]['enabled']:
            return self.channels.get_channel_uri(sid)
        else:
            self.logger.debug(
                '{}:{} Plugin instance disabled, not getting Channel uri'
                .format(self.plugin_obj.name, self.instance_key))
            return None

    ##############################
    # ## EXTERNAL EPG METHODS
    ##############################

    def get_channel_day(self, _zone, _uid, _day):
        """
        External request to return the program list for a channel
        based on the day requested day=0 means today
        """
        if self.enabled and self.config_obj.data[self.config_section]['enabled']:
            return self.epg.get_channel_day(_zone, _uid, _day)
        else:
            self.logger.debug(
                '{}:{} Plugin instance disabled, not getting EPG channel data'
                .format(self.plugin_obj.name, self.instance_key))
            return None

    def get_program_info(self, _prog_id):
        """
        External request to return the program details
        either from provider or from database
        includes updating database if needed.
        """
        if self.enabled and self.config_obj.data[self.config_section]['enabled']:
            return self.programs.get_program_info(_prog_id)
        else:
            self.logger.debug(
                '{}:{} Plugin instance disabled, not getting EPG program data'
                .format(self.plugin_obj.name, self.instance_key))
            return None

    def get_channel_list(self, _zone_id, _ch_ids=None):
        """
        External request to return the channel list for a zone.
        if ch_ids is None, then all channels are returned
        """
        if self.enabled and self.config_obj.data[self.config_section]['enabled']:
            return self.channels.get_channel_list(_zone_id, _ch_ids)
        else:
            self.logger.debug(
                '{}:{} Plugin instance disabled, not getting EPG zone data'
                .format(self.plugin_obj.name, self.instance_key))
            return None

    ##############################

    def scheduler_tasks(self):
        """
        dummy routine that will be overridden by subclass,
        if scheduler tasks are needed at the instance level
        """
        pass

    def refresh_channels(self):
        """
        Called from the scheduler
        """
        self.config_obj.refresh_config_data()
        if self.channels is not None and \
                self.config_obj.data[self.config_section]['enabled']:
            return self.channels.refresh_channels()
        else:
            self.logger.notice(
                '{}:{} Plugin instance disabled, not refreshing Channels'
                .format(self.plugin_obj.name, self.instance_key))
            return False

    def refresh_epg(self):
        """
        Called from the scheduler
        """
        self.config_obj.refresh_config_data()
        if self.epg is not None and \
                self.config_obj.data[self.config_section]['enabled']:
            return self.epg.refresh_epg()
        else:
            self.logger.info(
                '{}:{} Plugin instance disabled, not refreshing EPG'
                .format(self.plugin_obj.name, self.instance_key))
            return False

    def check_logger_refresh(self):
        if not self.logger.isEnabledFor(40):
            self.logger = logging.getLogger(__name__ + str(threading.get_ident()))
            if self.channels is not None:
                self.channels.check_logger_refresh()
            if self.epg is not None:
                self.epg.check_logger_refresh()
            if self.programs is not None:
                self.programs.check_logger_refresh()

    @property
    def config_section(self):
        return utils.instance_config_section(self.plugin_obj.name, self.instance_key)