-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Milestone: Client can send hardware profile to the main hub
- Loading branch information
bghira
committed
Apr 2, 2023
1 parent
dc63215
commit 5f3eca3
Showing
6 changed files
with
219 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,38 @@ | ||
import asyncio | ||
from .ws_client import websocket_client | ||
import logging | ||
logging.basicConfig(level=logging.INFO) | ||
from discord_tron_client.classes.app_config import AppConfig | ||
config = AppConfig() | ||
|
||
|
||
async def main(): | ||
logging.info("Starting WebSocket client...") | ||
await websocket_client(config) | ||
|
||
if __name__ == '__main__': | ||
try: | ||
# Detect an expired token. | ||
logging.info("Inspecting auth ticket...") | ||
from discord_tron_client.classes.auth import Auth | ||
current_ticket = config.get_auth_ticket() | ||
auth = Auth(config, current_ticket["access_token"], current_ticket["refresh_token"], current_ticket["expires_in"], current_ticket["issued_at"]) | ||
try: | ||
is_expired = auth.is_token_expired() | ||
except Exception as e: | ||
logging.error(f"Error checking token expiration: {e}") | ||
is_expired = True | ||
if is_expired: | ||
logging.warning("Access token is expired. Attempting to refresh...") | ||
new_ticket = auth.refresh_client_token(current_ticket["refresh_token"]) | ||
import json | ||
print(f"New ticket: {json.dumps(new_ticket, indent=4)}") | ||
auth.get() | ||
|
||
# Start the WebSocket client in the background | ||
asyncio.get_event_loop().run_until_complete(websocket_client(config)) | ||
startup_sequence = [] | ||
from discord_tron_client.classes.message import WebsocketMessage | ||
# Add any startup sequence here | ||
from discord_tron_client.classes.hardware import HardwareInfo | ||
hardware_info = HardwareInfo() | ||
machine_info = hardware_info.get_machine_info() | ||
hardware_info_message = WebsocketMessage(message_type="hardware_info", module_name="system", module_command="update", data=machine_info) | ||
startup_sequence.append(hardware_info_message.to_json()) | ||
asyncio.get_event_loop().run_until_complete(websocket_client(config, startup_sequence)) | ||
|
||
# Start the Flask server | ||
from discord_tron_client.app_factory import create_app | ||
app = create_app() | ||
app.run() | ||
asyncio.run(main()) | ||
except KeyboardInterrupt: | ||
logging.info("Shutting down...") | ||
exit(0) | ||
except Exception as e: | ||
logging.error(f"An error occurred: {e}") | ||
import traceback | ||
logging.error(f"Stack trace: {traceback.format_exc()}") | ||
exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import subprocess | ||
import json, logging | ||
|
||
class HardwareInfo: | ||
def __init__(self): | ||
self.gpu_type = "Unknown type" | ||
self.cpu_type = "Unknown type" | ||
self.memory_amount = None | ||
self.video_memory_amount = None | ||
self.disk_space_total = None | ||
self.disk_space_used = None | ||
|
||
def get_gpu_info(self): | ||
try: | ||
output = subprocess.check_output(["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"]) | ||
self.gpu_type = output.decode().strip() | ||
except: | ||
try: | ||
output = subprocess.check_output(["rocm-smi", "--showproductname"]) | ||
self.gpu_type = output.decode().strip() | ||
except: | ||
self.gpu_type = "Unknown" | ||
|
||
def get_cpu_info(self): | ||
try: | ||
with open('/proc/cpuinfo') as f: | ||
for line in f: | ||
if line.strip() and line.startswith('model name'): | ||
self.cpu_type = line.strip().split(': ')[1] | ||
break | ||
except: | ||
self.cpu_type = "Unknown" | ||
|
||
def get_memory_info(self): | ||
try: | ||
with open('/proc/meminfo') as f: | ||
for line in f: | ||
if line.startswith('MemTotal:'): | ||
self.memory_amount = int(int(line.split()[1]) / 1024 / 1024) | ||
break | ||
except: | ||
self.memory_amount = "Unknown" | ||
|
||
def get_video_memory_info(self): | ||
try: | ||
output = subprocess.check_output(["nvidia-smi", "--query-gpu=memory.total", "--format=csv,noheader,nounits"]) | ||
self.video_memory_amount = int(output.decode().strip()) / 1024 | ||
except: | ||
self.video_memory_amount = "Unknown" | ||
|
||
def get_disk_space_info(self): | ||
try: | ||
output = subprocess.check_output(["df", "-h"]) | ||
for line in output.decode().split('\n'): | ||
if '/dev/' in line and not line.startswith('tmpfs'): | ||
line = line.split() | ||
self.disk_space_total = line[1] | ||
self.disk_space_used = line[2] | ||
break | ||
except: | ||
self.disk_space_total = "Unknown" | ||
self.disk_space_used = "Unknown" | ||
|
||
def get_machine_info(self): | ||
self.get_gpu_info() | ||
self.get_cpu_info() | ||
self.get_memory_info() | ||
self.get_video_memory_info() | ||
self.get_disk_space_info() | ||
|
||
return { | ||
'gpu_type': self.gpu_type, | ||
'cpu_type': self.cpu_type, | ||
'memory_amount': self.memory_amount, | ||
'video_memory_amount': self.video_memory_amount, | ||
'disk_space_total': self.disk_space_total, | ||
'disk_space_used': self.disk_space_used | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import time | ||
|
||
class WebsocketMessage: | ||
def __init__(self, message_type: str, module_name: str, module_command, data=None, arguments=None): | ||
self._message_type = message_type | ||
self._module_name = module_name | ||
self._module_command = module_command | ||
self._timestamp = time.time() | ||
self._data = data or {} | ||
self._arguments = arguments or {} | ||
|
||
@property | ||
def message_type(self): | ||
return self._message_type | ||
|
||
@message_type.setter | ||
def message_type(self, value: str): | ||
self._message_type = value | ||
|
||
@property | ||
def module_name(self): | ||
return self._module_name | ||
|
||
@module_name.setter | ||
def module_name(self, value): | ||
self._module_name = value | ||
|
||
@property | ||
def module_command(self): | ||
return self._module_command | ||
|
||
@module_command.setter | ||
def module_command(self, value): | ||
self._module_command = value | ||
|
||
@property | ||
def timestamp(self): | ||
return self._timestamp | ||
|
||
@property | ||
def data(self): | ||
return self._data | ||
|
||
@data.setter | ||
def data(self, value): | ||
self._data = value | ||
|
||
@property | ||
def arguments(self): | ||
return self._arguments | ||
|
||
@arguments.setter | ||
def arguments(self, value): | ||
self._arguments = value | ||
|
||
def to_dict(self): | ||
return { | ||
"message_type": self.message_type, | ||
"module_name": self.module_name, | ||
"module_command": self.module_command, | ||
"timestamp": self.timestamp, | ||
"data": self.data, | ||
"arguments": self.arguments | ||
} | ||
|
||
def to_json(self): | ||
return self.to_dict() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters