Skip to content

Commit

Permalink
Move Ping sensor to stats.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mathoudebine committed Dec 21, 2024
1 parent e853540 commit d8ef41a
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 78 deletions.
4 changes: 2 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ config:
# Value must be 'controller/fan' e.g. 'nct6798/fan2'. Use configuration wizard for help in selection
CPU_FAN: AUTO

# IP address used for ping sensor. Can be external (e.g. 8.8.8.8) or internal (e.g. 192.168.x.x)
# Address used for ping sensor. Can be internal/external IP (e.g. 8.8.8.8 or 192.168.0.1) or hostname (google.com)
PING: 8.8.8.8

# Weather data with OpenWeatherMap API. Only useful if you want to use a theme that displays it
Expand All @@ -57,7 +57,7 @@ display:
# - SIMU for 3.5" simulated LCD (image written in screencap.png)
# - SIMU5 for 5" simulated LCD
# To identify your smart screen: https://github.com/mathoudebine/turing-smart-screen-python/wiki/Hardware-revisions
REVISION: C
REVISION: A

# Display Brightness
# Set this as the desired %, 0 being completely dark and 100 being max brightness
Expand Down
24 changes: 17 additions & 7 deletions library/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,48 +121,50 @@ def CPUFanSpeed():


@async_job("GPU_Stats")
@schedule(timedelta(seconds=config.THEME_DATA['STATS']['GPU'].get("INTERVAL", 0)).total_seconds())
@schedule(timedelta(seconds=config.THEME_DATA['STATS'].get('GPU', {}).get("INTERVAL", 0)).total_seconds())
def GpuStats():
""" Refresh the GPU Stats """
# logger.debug("Refresh GPU Stats")
stats.Gpu.stats()


@async_job("Memory_Stats")
@schedule(timedelta(seconds=config.THEME_DATA['STATS']['MEMORY'].get("INTERVAL", 0)).total_seconds())
@schedule(timedelta(seconds=config.THEME_DATA['STATS'].get('MEMORY', {}).get("INTERVAL", 0)).total_seconds())
def MemoryStats():
# logger.debug("Refresh memory stats")
stats.Memory.stats()


@async_job("Disk_Stats")
@schedule(timedelta(seconds=config.THEME_DATA['STATS']['DISK'].get("INTERVAL", 0)).total_seconds())
@schedule(timedelta(seconds=config.THEME_DATA['STATS'].get('DISK', {}).get("INTERVAL", 0)).total_seconds())
def DiskStats():
# logger.debug("Refresh disk stats")
stats.Disk.stats()


@async_job("Net_Stats")
@schedule(timedelta(seconds=config.THEME_DATA['STATS']['NET'].get("INTERVAL", 0)).total_seconds())
@schedule(timedelta(seconds=config.THEME_DATA['STATS'].get('NET', {}).get("INTERVAL", 0)).total_seconds())
def NetStats():
# logger.debug("Refresh net stats")
stats.Net.stats()


@async_job("Date_Stats")
@schedule(timedelta(seconds=config.THEME_DATA['STATS']['DATE'].get("INTERVAL", 0)).total_seconds())
@schedule(timedelta(seconds=config.THEME_DATA['STATS'].get('DATE', {}).get("INTERVAL", 0)).total_seconds())
def DateStats():
# logger.debug("Refresh date stats")
stats.Date.stats()


@async_job("SystemUptime_Stats")
@schedule(timedelta(seconds=config.THEME_DATA['STATS']['UPTIME'].get("INTERVAL", 0)).total_seconds())
@schedule(timedelta(seconds=config.THEME_DATA['STATS'].get('UPTIME', {}).get("INTERVAL", 0)).total_seconds())
def SystemUptimeStats():
# logger.debug("Refresh system uptime stats")
stats.SystemUptime.stats()


@async_job("Custom_Stats")
@schedule(timedelta(seconds=config.THEME_DATA['STATS']['CUSTOM'].get("INTERVAL", 0)).total_seconds())
@schedule(timedelta(seconds=config.THEME_DATA['STATS'].get('CUSTOM', {}).get("INTERVAL", 0)).total_seconds())
def CustomStats():
# print("Refresh custom stats")
stats.Custom.stats()
Expand All @@ -174,6 +176,14 @@ def WeatherStats():
# logger.debug("Refresh Weather data")
stats.Weather.stats()


@async_job("Ping_Stats")
@schedule(timedelta(seconds=config.THEME_DATA['STATS'].get('PING', {}).get("INTERVAL", 0)).total_seconds())
def PingStats():
# logger.debug("Refresh Ping data")
stats.Ping.stats()


@async_job("Queue_Handler")
@schedule(timedelta(milliseconds=1).total_seconds())
def QueueHandler():
Expand Down
37 changes: 1 addition & 36 deletions library/sensors/sensors_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@

import math
import platform
import requests
from abc import ABC, abstractmethod
from typing import List
from ping3 import ping, verbose_ping
from datetime import datetime
import library.config as config


# Custom data classes must be implemented in this file, inherit the CustomDataSource and implement its 2 methods
Expand All @@ -51,37 +47,6 @@ def last_values(self) -> List[float]:
# If you do not want to draw a line graph or if your custom data has no numeric values, keep this function empty
pass

# Custom data class to measure ping to google.fr
class Ping(CustomDataSource):
# This list is used to store the last 500 values to display a line graph
last_val = [math.nan] * 500 # By default, it is filed with math.nan values to indicate there is no data stored

def as_numeric(self) -> float:
# Measure the ping
try:
result = ping(config.CONFIG_DATA['config'].get('PING', "8.8.8.8"))*1000
if result is not None:
# Store the value to the history list that will be used for line graph
self.last_val.append(result)
# Also remove the oldest value from history list
self.last_val.pop(0)
return result
else:
self.last_val.append(9999)
self.last_val.pop(0)
return 9999 # Return 0 if ping fails
except Exception as e:
self.last_val.append(9999)
self.last_val.pop(0)
return 9999

def as_string(self) -> str:
# Format the ping result as a string
return f'{self.as_numeric():4.0f} ms'

def last_values(self) -> List[float]:
# Since there is no historical data for ping, return an empty list
return self.last_val

# Example for a custom data class that has numeric and text values
class ExampleCustomNumericData(CustomDataSource):
Expand Down Expand Up @@ -130,4 +95,4 @@ def as_string(self) -> str:

def last_values(self) -> List[float]:
# If a custom data class only has text values, it won't be possible to display line graph
pass
pass
51 changes: 45 additions & 6 deletions library/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
from typing import List

import babel.dates
import requests
from ping3 import ping
from psutil._common import bytes2human
from uptime import uptime
import requests

import library.config as config
from library.display import display
Expand All @@ -43,6 +44,7 @@
WLO_CARD = config.CONFIG_DATA["config"].get("WLO", "")
HW_SENSORS = config.CONFIG_DATA["config"].get("HW_SENSORS", "AUTO")
CPU_FAN = config.CONFIG_DATA["config"].get("CPU_FAN", "AUTO")
PING_DEST = config.CONFIG_DATA["config"].get("PING", "127.0.0.1")

if HW_SENSORS == "PYTHON":
if platform.system() == 'Windows':
Expand Down Expand Up @@ -826,6 +828,7 @@ def stats():
if theme_data is not None and last_values is not None:
display_themed_line_graph(theme_data=theme_data, values=last_values)


class Weather:
@staticmethod
def stats():
Expand All @@ -843,17 +846,24 @@ def stats():
if 'CENTER_LENGTH' in wdescription_theme_data:
center_description_length = wdescription_theme_data['CENTER_LENGTH']

activate = True if wtemperature_theme_data.get("SHOW") or wfelt_theme_data.get("SHOW") or wupdatetime_theme_data.get("SHOW") or wdescription_theme_data.get("SHOW") or whumidity_theme_data.get("SHOW") else False

activate = True if wtemperature_theme_data.get("SHOW") or wfelt_theme_data.get(
"SHOW") or wupdatetime_theme_data.get("SHOW") or wdescription_theme_data.get(
"SHOW") or whumidity_theme_data.get("SHOW") else False

if activate:
temp = None
feel = None
desc = None
time = None
humidity = None
if HW_SENSORS in ["STATIC", "STUB"]:
temp = "17.5°C"
feel = "(17.2°C)"
desc = "Cloudy"
time = "@15:33"
humidity = "45%"
if wdescription_theme_data['CENTER_LENGTH']:
desc = "x"*center_description_length
desc = "x" * center_description_length
else:
# API Parameters
lat = config.CONFIG_DATA['config'].get('WEATHER_LATITUDE', "")
Expand Down Expand Up @@ -892,10 +902,39 @@ def stats():
# Display Temperature
display_themed_value(theme_data=wtemperature_theme_data, value=temp)
# Display Temperature Felt
display_themed_value(theme_data=wfelt_theme_data, value=feel)
display_themed_value(theme_data=wfelt_theme_data, value=feel)
# Display Update Time
display_themed_value(theme_data=wupdatetime_theme_data, value=time)
display_themed_value(theme_data=wupdatetime_theme_data, value=time)
# Display Humidity
display_themed_value(theme_data=whumidity_theme_data, value=humidity)
# Display Weather Description
display_themed_value(theme_data=wdescription_theme_data, value=desc)


class Ping:
last_values_ping = []

@classmethod
def stats(cls):
theme_data = config.THEME_DATA['STATS']['PING']

delay = ping(dest_addr=PING_DEST, unit="ms")

save_last_value(delay, cls.last_values_ping,
theme_data['LINE_GRAPH'].get("HISTORY_SIZE", DEFAULT_HISTORY_SIZE))
# logger.debug(f"Ping delay: {delay}ms")

display_themed_progress_bar(theme_data['GRAPH'], delay)
display_themed_radial_bar(
theme_data=theme_data['RADIAL'],
value=int(delay),
unit="ms",
min_size=6
)
display_themed_value(
theme_data=theme_data['TEXT'],
value=int(delay),
unit="ms",
min_size=6
)
display_themed_line_graph(theme_data['LINE_GRAPH'], cls.last_values_ping)
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ def on_win32_wm_event(hWnd, msg, wParam, lParam):
scheduler.SystemUptimeStats()
scheduler.CustomStats()
scheduler.WeatherStats()
scheduler.PingStats()
scheduler.QueueHandler()

if tray_icon and platform.system() == "Darwin": # macOS-specific
Expand Down
50 changes: 24 additions & 26 deletions res/themes/ColoredFlat/theme.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -540,30 +540,28 @@ STATS:
FONT_SIZE: 18
FONT_COLOR: 247, 227, 227
BACKGROUND_IMAGE: background.png
CUSTOM:
# For now the refresh interval (in seconds) is the same for all custom data classes
PING:
INTERVAL: 1
Ping:
TEXT:
SHOW: True
X: 325
Y: 495
FONT: jetbrains-mono/JetBrainsMono-Bold.ttf
FONT_SIZE: 25
FONT_COLOR: 247, 227, 227
BACKGROUND_IMAGE: background.png
ALIGN: right
LINE_GRAPH:
SHOW: true
X: 30
Y: 680
WIDTH: 415
HEIGHT: 70
MIN_VALUE: 0
MAX_VALUE: 250
HISTORY_SIZE: 120
AUTOSCALE: false
LINE_COLOR: 247, 227, 227
AXIS: True
AXIS_COLOR: 247, 227, 227
BACKGROUND_IMAGE: background.png
TEXT:
SHOW: True
X: 325
Y: 495
FONT: jetbrains-mono/JetBrainsMono-Bold.ttf
FONT_SIZE: 25
FONT_COLOR: 247, 227, 227
BACKGROUND_IMAGE: background.png
ALIGN: right
LINE_GRAPH:
SHOW: true
X: 30
Y: 680
WIDTH: 415
HEIGHT: 70
MIN_VALUE: 0
MAX_VALUE: 250
HISTORY_SIZE: 120
AUTOSCALE: false
LINE_COLOR: 247, 227, 227
AXIS: True
AXIS_COLOR: 247, 227, 227
BACKGROUND_IMAGE: background.png
27 changes: 27 additions & 0 deletions res/themes/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,32 @@ STATS:
FORMATTED:
TEXT:
SHOW: False
WEATHER:
INTERVAL: 0
TEMPERATURE:
TEXT:
SHOW: False
TEMPERATURE_FELT:
TEXT:
SHOW: False
UPDATE_TIME:
TEXT:
SHOW: False
HUMIDITY:
TEXT:
SHOW: False
WEATHER_DESCRIPTION:
TEXT:
SHOW: False
PING:
INTERVAL: 0
GRAPH:
SHOW: False
RADIAL:
SHOW: False
LINE_GRAPH:
SHOW: False
TEXT:
SHOW: False
CUSTOM:
INTERVAL: 0
Loading

0 comments on commit d8ef41a

Please sign in to comment.