You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.5 KiB
71 lines
2.5 KiB
11 months ago
|
from dotenv import load_dotenv
|
||
|
load_dotenv() # take environment variables from .env.
|
||
|
|
||
11 months ago
|
import asyncio
|
||
11 months ago
|
import subprocess
|
||
|
import platform
|
||
11 months ago
|
|
||
11 months ago
|
from .logs import setup_logging
|
||
|
from .logs import logger
|
||
11 months ago
|
setup_logging()
|
||
11 months ago
|
|
||
11 months ago
|
def get_kernel_messages():
|
||
11 months ago
|
"""
|
||
|
Is this the way to do this?
|
||
|
"""
|
||
11 months ago
|
current_platform = platform.system()
|
||
|
|
||
|
if current_platform == "Darwin":
|
||
|
process = subprocess.Popen(['syslog'], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
|
||
|
output, _ = process.communicate()
|
||
|
return output.decode('utf-8')
|
||
|
elif current_platform == "Linux":
|
||
|
with open('/var/log/dmesg', 'r') as file:
|
||
|
return file.read()
|
||
|
else:
|
||
11 months ago
|
logger.info("Unsupported platform.")
|
||
11 months ago
|
|
||
|
def custom_filter(message):
|
||
|
# Check for {TO_INTERPRETER{ message here }TO_INTERPRETER} pattern
|
||
|
if '{TO_INTERPRETER{' in message and '}TO_INTERPRETER}' in message:
|
||
|
start = message.find('{TO_INTERPRETER{') + len('{TO_INTERPRETER{')
|
||
|
end = message.find('}TO_INTERPRETER}', start)
|
||
|
return message[start:end]
|
||
|
# Check for USB mention
|
||
11 months ago
|
# elif 'USB' in message:
|
||
|
# return message
|
||
|
# # Check for network related keywords
|
||
|
# elif any(keyword in message for keyword in ['network', 'IP', 'internet', 'LAN', 'WAN', 'router', 'switch']) and "networkStatusForFlags" not in message:
|
||
|
|
||
|
# return message
|
||
11 months ago
|
else:
|
||
|
return None
|
||
11 months ago
|
|
||
11 months ago
|
last_messages = ""
|
||
11 months ago
|
|
||
11 months ago
|
def check_filtered_kernel():
|
||
11 months ago
|
messages = get_kernel_messages()
|
||
|
messages.replace(last_messages, "")
|
||
|
messages = messages.split("\n")
|
||
|
|
||
|
filtered_messages = []
|
||
|
for message in messages:
|
||
|
if custom_filter(message):
|
||
|
filtered_messages.append(message)
|
||
|
|
||
|
return "\n".join(filtered_messages)
|
||
|
|
||
|
async def put_kernel_messages_into_queue(queue):
|
||
11 months ago
|
while True:
|
||
11 months ago
|
text = check_filtered_kernel()
|
||
|
if text:
|
||
|
if isinstance(queue, asyncio.Queue):
|
||
|
await queue.put({"role": "computer", "type": "console", "start": True})
|
||
|
await queue.put({"role": "computer", "type": "console", "format": "output", "content": text})
|
||
|
await queue.put({"role": "computer", "type": "console", "end": True})
|
||
|
else:
|
||
|
queue.put({"role": "computer", "type": "console", "start": True})
|
||
|
queue.put({"role": "computer", "type": "console", "format": "output", "content": text})
|
||
|
queue.put({"role": "computer", "type": "console", "end": True})
|
||
11 months ago
|
|
||
11 months ago
|
await asyncio.sleep(5)
|