Skip to content

Commit d8ef41a

Browse files
committed
Move Ping sensor to stats.py
1 parent e853540 commit d8ef41a

File tree

9 files changed

+188
-78
lines changed

9 files changed

+188
-78
lines changed

config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ config:
3333
# Value must be 'controller/fan' e.g. 'nct6798/fan2'. Use configuration wizard for help in selection
3434
CPU_FAN: AUTO
3535

36-
# IP address used for ping sensor. Can be external (e.g. 8.8.8.8) or internal (e.g. 192.168.x.x)
36+
# 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)
3737
PING: 8.8.8.8
3838

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

6262
# Display Brightness
6363
# Set this as the desired %, 0 being completely dark and 100 being max brightness

library/scheduler.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,48 +121,50 @@ def CPUFanSpeed():
121121

122122

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

130130

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

137137

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

144144

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

151151

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

158+
158159
@async_job("SystemUptime_Stats")
159-
@schedule(timedelta(seconds=config.THEME_DATA['STATS']['UPTIME'].get("INTERVAL", 0)).total_seconds())
160+
@schedule(timedelta(seconds=config.THEME_DATA['STATS'].get('UPTIME', {}).get("INTERVAL", 0)).total_seconds())
160161
def SystemUptimeStats():
161162
# logger.debug("Refresh system uptime stats")
162163
stats.SystemUptime.stats()
163164

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

179+
180+
@async_job("Ping_Stats")
181+
@schedule(timedelta(seconds=config.THEME_DATA['STATS'].get('PING', {}).get("INTERVAL", 0)).total_seconds())
182+
def PingStats():
183+
# logger.debug("Refresh Ping data")
184+
stats.Ping.stats()
185+
186+
177187
@async_job("Queue_Handler")
178188
@schedule(timedelta(milliseconds=1).total_seconds())
179189
def QueueHandler():

library/sensors/sensors_custom.py

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@
2121

2222
import math
2323
import platform
24-
import requests
2524
from abc import ABC, abstractmethod
2625
from typing import List
27-
from ping3 import ping, verbose_ping
28-
from datetime import datetime
29-
import library.config as config
3026

3127

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

54-
# Custom data class to measure ping to google.fr
55-
class Ping(CustomDataSource):
56-
# This list is used to store the last 500 values to display a line graph
57-
last_val = [math.nan] * 500 # By default, it is filed with math.nan values to indicate there is no data stored
58-
59-
def as_numeric(self) -> float:
60-
# Measure the ping
61-
try:
62-
result = ping(config.CONFIG_DATA['config'].get('PING', "8.8.8.8"))*1000
63-
if result is not None:
64-
# Store the value to the history list that will be used for line graph
65-
self.last_val.append(result)
66-
# Also remove the oldest value from history list
67-
self.last_val.pop(0)
68-
return result
69-
else:
70-
self.last_val.append(9999)
71-
self.last_val.pop(0)
72-
return 9999 # Return 0 if ping fails
73-
except Exception as e:
74-
self.last_val.append(9999)
75-
self.last_val.pop(0)
76-
return 9999
77-
78-
def as_string(self) -> str:
79-
# Format the ping result as a string
80-
return f'{self.as_numeric():4.0f} ms'
81-
82-
def last_values(self) -> List[float]:
83-
# Since there is no historical data for ping, return an empty list
84-
return self.last_val
8550

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

13196
def last_values(self) -> List[float]:
13297
# If a custom data class only has text values, it won't be possible to display line graph
133-
pass
98+
pass

library/stats.py

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929
from typing import List
3030

3131
import babel.dates
32+
import requests
33+
from ping3 import ping
3234
from psutil._common import bytes2human
3335
from uptime import uptime
34-
import requests
3536

3637
import library.config as config
3738
from library.display import display
@@ -43,6 +44,7 @@
4344
WLO_CARD = config.CONFIG_DATA["config"].get("WLO", "")
4445
HW_SENSORS = config.CONFIG_DATA["config"].get("HW_SENSORS", "AUTO")
4546
CPU_FAN = config.CONFIG_DATA["config"].get("CPU_FAN", "AUTO")
47+
PING_DEST = config.CONFIG_DATA["config"].get("PING", "127.0.0.1")
4648

4749
if HW_SENSORS == "PYTHON":
4850
if platform.system() == 'Windows':
@@ -826,6 +828,7 @@ def stats():
826828
if theme_data is not None and last_values is not None:
827829
display_themed_line_graph(theme_data=theme_data, values=last_values)
828830

831+
829832
class Weather:
830833
@staticmethod
831834
def stats():
@@ -843,17 +846,24 @@ def stats():
843846
if 'CENTER_LENGTH' in wdescription_theme_data:
844847
center_description_length = wdescription_theme_data['CENTER_LENGTH']
845848

846-
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
847-
849+
activate = True if wtemperature_theme_data.get("SHOW") or wfelt_theme_data.get(
850+
"SHOW") or wupdatetime_theme_data.get("SHOW") or wdescription_theme_data.get(
851+
"SHOW") or whumidity_theme_data.get("SHOW") else False
852+
848853
if activate:
854+
temp = None
855+
feel = None
856+
desc = None
857+
time = None
858+
humidity = None
849859
if HW_SENSORS in ["STATIC", "STUB"]:
850860
temp = "17.5°C"
851861
feel = "(17.2°C)"
852862
desc = "Cloudy"
853863
time = "@15:33"
854864
humidity = "45%"
855865
if wdescription_theme_data['CENTER_LENGTH']:
856-
desc = "x"*center_description_length
866+
desc = "x" * center_description_length
857867
else:
858868
# API Parameters
859869
lat = config.CONFIG_DATA['config'].get('WEATHER_LATITUDE', "")
@@ -892,10 +902,39 @@ def stats():
892902
# Display Temperature
893903
display_themed_value(theme_data=wtemperature_theme_data, value=temp)
894904
# Display Temperature Felt
895-
display_themed_value(theme_data=wfelt_theme_data, value=feel)
905+
display_themed_value(theme_data=wfelt_theme_data, value=feel)
896906
# Display Update Time
897-
display_themed_value(theme_data=wupdatetime_theme_data, value=time)
907+
display_themed_value(theme_data=wupdatetime_theme_data, value=time)
898908
# Display Humidity
899909
display_themed_value(theme_data=whumidity_theme_data, value=humidity)
900910
# Display Weather Description
901911
display_themed_value(theme_data=wdescription_theme_data, value=desc)
912+
913+
914+
class Ping:
915+
last_values_ping = []
916+
917+
@classmethod
918+
def stats(cls):
919+
theme_data = config.THEME_DATA['STATS']['PING']
920+
921+
delay = ping(dest_addr=PING_DEST, unit="ms")
922+
923+
save_last_value(delay, cls.last_values_ping,
924+
theme_data['LINE_GRAPH'].get("HISTORY_SIZE", DEFAULT_HISTORY_SIZE))
925+
# logger.debug(f"Ping delay: {delay}ms")
926+
927+
display_themed_progress_bar(theme_data['GRAPH'], delay)
928+
display_themed_radial_bar(
929+
theme_data=theme_data['RADIAL'],
930+
value=int(delay),
931+
unit="ms",
932+
min_size=6
933+
)
934+
display_themed_value(
935+
theme_data=theme_data['TEXT'],
936+
value=int(delay),
937+
unit="ms",
938+
min_size=6
939+
)
940+
display_themed_line_graph(theme_data['LINE_GRAPH'], cls.last_values_ping)

main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ def on_win32_wm_event(hWnd, msg, wParam, lParam):
215215
scheduler.SystemUptimeStats()
216216
scheduler.CustomStats()
217217
scheduler.WeatherStats()
218+
scheduler.PingStats()
218219
scheduler.QueueHandler()
219220

220221
if tray_icon and platform.system() == "Darwin": # macOS-specific

res/themes/ColoredFlat/theme.yaml

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -540,30 +540,28 @@ STATS:
540540
FONT_SIZE: 18
541541
FONT_COLOR: 247, 227, 227
542542
BACKGROUND_IMAGE: background.png
543-
CUSTOM:
544-
# For now the refresh interval (in seconds) is the same for all custom data classes
543+
PING:
545544
INTERVAL: 1
546-
Ping:
547-
TEXT:
548-
SHOW: True
549-
X: 325
550-
Y: 495
551-
FONT: jetbrains-mono/JetBrainsMono-Bold.ttf
552-
FONT_SIZE: 25
553-
FONT_COLOR: 247, 227, 227
554-
BACKGROUND_IMAGE: background.png
555-
ALIGN: right
556-
LINE_GRAPH:
557-
SHOW: true
558-
X: 30
559-
Y: 680
560-
WIDTH: 415
561-
HEIGHT: 70
562-
MIN_VALUE: 0
563-
MAX_VALUE: 250
564-
HISTORY_SIZE: 120
565-
AUTOSCALE: false
566-
LINE_COLOR: 247, 227, 227
567-
AXIS: True
568-
AXIS_COLOR: 247, 227, 227
569-
BACKGROUND_IMAGE: background.png
545+
TEXT:
546+
SHOW: True
547+
X: 325
548+
Y: 495
549+
FONT: jetbrains-mono/JetBrainsMono-Bold.ttf
550+
FONT_SIZE: 25
551+
FONT_COLOR: 247, 227, 227
552+
BACKGROUND_IMAGE: background.png
553+
ALIGN: right
554+
LINE_GRAPH:
555+
SHOW: true
556+
X: 30
557+
Y: 680
558+
WIDTH: 415
559+
HEIGHT: 70
560+
MIN_VALUE: 0
561+
MAX_VALUE: 250
562+
HISTORY_SIZE: 120
563+
AUTOSCALE: false
564+
LINE_COLOR: 247, 227, 227
565+
AXIS: True
566+
AXIS_COLOR: 247, 227, 227
567+
BACKGROUND_IMAGE: background.png

res/themes/default.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,5 +217,32 @@ STATS:
217217
FORMATTED:
218218
TEXT:
219219
SHOW: False
220+
WEATHER:
221+
INTERVAL: 0
222+
TEMPERATURE:
223+
TEXT:
224+
SHOW: False
225+
TEMPERATURE_FELT:
226+
TEXT:
227+
SHOW: False
228+
UPDATE_TIME:
229+
TEXT:
230+
SHOW: False
231+
HUMIDITY:
232+
TEXT:
233+
SHOW: False
234+
WEATHER_DESCRIPTION:
235+
TEXT:
236+
SHOW: False
237+
PING:
238+
INTERVAL: 0
239+
GRAPH:
240+
SHOW: False
241+
RADIAL:
242+
SHOW: False
243+
LINE_GRAPH:
244+
SHOW: False
245+
TEXT:
246+
SHOW: False
220247
CUSTOM:
221248
INTERVAL: 0

0 commit comments

Comments
 (0)