|
3 | 3 | import requests |
4 | 4 |
|
5 | 5 | from modules.module import MtcModule |
6 | | -from mypylib.mypylib import get_timestamp |
| 6 | +from mypylib.mypylib import get_timestamp, print_table, color_print |
7 | 7 | from mytoncore import get_hostname |
8 | 8 | from mytonctrl.utils import timestamp2utcdatetime |
9 | 9 |
|
@@ -99,6 +99,8 @@ def send_message(self, text: str): |
99 | 99 | raise Exception(f"send_message error: {response}") |
100 | 100 |
|
101 | 101 | def send_alert(self, alert_name: str, *args, **kwargs): |
| 102 | + if not self.alert_is_enabled(alert_name): |
| 103 | + return |
102 | 104 | last_sent = self.get_alert_sent(alert_name) |
103 | 105 | time_ = timestamp2utcdatetime(int(time.time())) |
104 | 106 | alert = ALERTS.get(alert_name) |
@@ -136,15 +138,50 @@ def init(self): |
136 | 138 | self.set_global_vars() |
137 | 139 | self.inited = True |
138 | 140 |
|
139 | | - def set_alert_sent(self, alert_name: str): |
| 141 | + def get_alert_from_db(self, alert_name: str): |
140 | 142 | if 'alerts' not in self.ton.local.db: |
141 | 143 | self.ton.local.db['alerts'] = {} |
142 | | - self.ton.local.db['alerts'][alert_name] = int(time.time()) |
| 144 | + if alert_name not in self.ton.local.db['alerts']: |
| 145 | + self.ton.local.db['alerts'][alert_name] = {'sent': 0, 'enabled': True} |
| 146 | + return self.ton.local.db['alerts'][alert_name] |
| 147 | + |
| 148 | + def set_alert_sent(self, alert_name: str): |
| 149 | + alert = self.get_alert_from_db(alert_name) |
| 150 | + alert['sent'] = int(time.time()) |
143 | 151 |
|
144 | 152 | def get_alert_sent(self, alert_name: str): |
145 | | - if 'alerts' not in self.ton.local.db: |
146 | | - return 0 |
147 | | - return self.ton.local.db['alerts'].get(alert_name, 0) |
| 153 | + alert = self.get_alert_from_db(alert_name) |
| 154 | + return alert.get('sent', 0) |
| 155 | + |
| 156 | + def alert_is_enabled(self, alert_name: str): |
| 157 | + alert = self.get_alert_from_db(alert_name) |
| 158 | + return alert.get('enabled', True) # default is True |
| 159 | + |
| 160 | + def set_alert_enabled(self, alert_name: str, enabled: bool): |
| 161 | + alert = self.get_alert_from_db(alert_name) |
| 162 | + alert['enabled'] = enabled |
| 163 | + self.ton.local.save() |
| 164 | + |
| 165 | + def enable_alert(self, args): |
| 166 | + if len(args) != 1: |
| 167 | + raise Exception("Usage: enable_alert <alert_name>") |
| 168 | + alert_name = args[0] |
| 169 | + self.set_alert_enabled(alert_name, True) |
| 170 | + color_print("enable_alert - {green}OK{endc}") |
| 171 | + |
| 172 | + def disable_alert(self, args): |
| 173 | + if len(args) != 1: |
| 174 | + raise Exception("Usage: disable_alert <alert_name>") |
| 175 | + alert_name = args[0] |
| 176 | + self.set_alert_enabled(alert_name, False) |
| 177 | + color_print("disable_alert - {green}OK{endc}") |
| 178 | + |
| 179 | + def print_alerts(self, args): |
| 180 | + table = [['Name', 'Enabled', 'Last sent']] |
| 181 | + for alert_name in ALERTS: |
| 182 | + alert = self.get_alert_from_db(alert_name) |
| 183 | + table.append([alert_name, alert['enabled'], alert['sent']]) |
| 184 | + print_table(table) |
148 | 185 |
|
149 | 186 | def check_db_usage(self): |
150 | 187 | usage = self.ton.GetDbUsage() |
@@ -226,4 +263,6 @@ def check_status(self): |
226 | 263 | self.local.try_function(self.check_adnl_connection_failed) |
227 | 264 |
|
228 | 265 | def add_console_commands(self, console): |
229 | | - ... |
| 266 | + console.AddItem("enable_alert", self.enable_alert, self.local.translate("enable_alert_cmd")) |
| 267 | + console.AddItem("disable_alert", self.disable_alert, self.local.translate("disable_alert_cmd")) |
| 268 | + console.AddItem("list_alerts", self.print_alerts, self.local.translate("list_alerts_cmd")) |
0 commit comments