85 lines
2.4 KiB
Python
85 lines
2.4 KiB
Python
from networktables import NetworkTables
|
|
import time
|
|
import psutil
|
|
import argparse
|
|
import subprocess
|
|
import configparser
|
|
|
|
config = configparser.ConfigParser()
|
|
config.read('config.ini')
|
|
|
|
ip = config.get('settings', 'ip')
|
|
debug_mode = config.getboolean('settings', 'debug_mode')
|
|
table = config.get('settings', 'tables')
|
|
|
|
def ping(host):
|
|
result = subprocess.run(['ping', '-c', '1', '-W', '1', host],
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE)
|
|
# Return True if returncode is 0 (success), else False
|
|
return result.returncode == 0
|
|
|
|
parser = argparse.ArgumentParser(description="Run TablePi client")
|
|
|
|
parser.add_argument(
|
|
'--table',
|
|
type=str,
|
|
default=table,
|
|
help='Name of the NetworkTable (default: %(default)s)'
|
|
)
|
|
|
|
parser.add_argument(
|
|
'--ip',
|
|
type=str,
|
|
default=ip,
|
|
help='IP address of the server (default: %(default)s)'
|
|
)
|
|
parser.add_argument(
|
|
'--debug',
|
|
action='store_true',
|
|
help='It will show what data is being sent and also pings the rio (to show you if there is a network error)'
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
table_name = args.table
|
|
server_ip = args.ip
|
|
table = NetworkTables.getTable(table_name)
|
|
NetworkTables.initialize(server=server_ip)
|
|
ip = server_ip
|
|
if ping(ip):
|
|
print("sending data to server")
|
|
else:
|
|
print("rio is unreachable")
|
|
print (server_ip, table_name)
|
|
while True:
|
|
cpu = psutil.cpu_percent()
|
|
temps = psutil.sensors_temperatures()
|
|
mem = psutil.virtual_memory()
|
|
|
|
cputemp = "Unavailable"
|
|
for label in ["coretemp", "cpu-thermal", "k10temp", "cpu_thermal"]:
|
|
if label in temps and temps[label]:
|
|
cputemp = f"{temps[label][0].current:.1f}"
|
|
break
|
|
else:
|
|
# to force it to work when on PI
|
|
try:
|
|
with open("/sys/class/thermal/thermal_zone0/temp") as f:
|
|
cputemp = f"{int(f.read()) / 1000:.1f}"
|
|
except:
|
|
pass
|
|
|
|
table.putNumber("CPU Usage", cpu)
|
|
table.putNumber("Temperature", float(cputemp) if cputemp != "Unavailable" else -1)
|
|
table.putNumber("Memory", mem.used // (1024**2))
|
|
if args.debug or debug_mode:
|
|
print("mem", mem.used // 1024**2,"temp", float(cputemp) if cputemp != "Unavailable" else -1,"CPU", cpu)
|
|
if __name__ == '__main__':
|
|
ip = server_ip
|
|
if ping(ip):
|
|
print(f"{ip} is reachable")
|
|
else:
|
|
print(f"{ip} is unreachable")
|
|
time.sleep(1)
|
|
|