Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
2 contributors

Users who have contributed to this file

@g-provost @4ntsu
executable file 156 lines (123 sloc) 4.36 KB
#!/usr/bin/env python3
import signal
import os
import sys
import time
import psutil
import configparser
from datetime import datetime
from luma.core import cmdline, error
from PIL import Image, ImageDraw, ImageFont
# Load presets
contrast = 255
refresh = 10
show_logo = 'yes'
net_name = 'eth0'
s1_name = 'sd'
s1_path = '/'
# Load config file
config_file = '/etc/sys-oled.conf'
if os.path.isfile(config_file):
config = configparser.ConfigParser()
config.read(config_file)
contrast = int(config.get('main', 'contrast'))
refresh = float(config.get('main', 'refresh'))
show_logo = config.get('main', 'show_logo')
net_name = config.get('device', 'network_name')
s1_name = config.get('device', 'storage1_name')
s1_path = config.get('device', 'storage1_path')
if config.has_option('device', 'storage2_name'):
s2_name = config.get('device', 'storage2_name')
s2_path = config.get('device', 'storage2_path')
# Load font
font_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
'../share/sys-oled', 'C&C Red Alert [INET].ttf'))
font = ImageFont.truetype(font_path, 12)
def get_device(actual_args=None):
if actual_args is None:
actual_args = sys.argv[1:]
parser = cmdline.create_parser(description='luma.core arguments')
args = parser.parse_args(actual_args)
if args.config:
config = cmdline.load_config(args.config)
args = parser.parse_args(config + actual_args)
try:
device = cmdline.create_device(args)
except error.Error as e:
parser.error(e)
return device
def bytes2human(n):
symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
prefix = {}
for i, s in enumerate(symbols):
prefix[s] = 1 << (i + 1) * 10
for s in reversed(symbols):
if n >= prefix[s]:
value = float(n) / prefix[s]
if s in ['K', 'M']:
return '%d%s' % (int(value), s)
else:
return '%.1f%s' % (value, s)
return "%sB" % n
def cpu_usage():
uptime = datetime.now().replace(second=0, microsecond=0) - datetime.fromtimestamp(psutil.boot_time())
av1, av2, av3 = os.getloadavg()
return "ld: %.1f %.1f %.1f up: %s" \
% (av1, av2, av3, str(uptime).split(',')[0][:-3])
def mem_usage():
usage = psutil.virtual_memory()
return "mem: %s / %s - %.0f%%" \
% (bytes2human(usage.used), bytes2human(usage.total), usage.percent)
def disk_usage(name, dir):
usage = psutil.disk_usage(dir)
return name + ": %s / %s - %.0f%%" \
% (bytes2human(usage.used), bytes2human(usage.total), usage.percent)
def network(iface):
addr = psutil.net_if_addrs()[iface]
return "%s: %s" \
% (iface, addr[0].address)
def host_time():
now = datetime.now()
hostname = os.uname()[1]
return hostname + " " + now.strftime("%Y-%m-%d %H:%M")
def display_info(device):
txt = Image.new('RGBA', device.size)
d = ImageDraw.Draw(txt)
d.text((0, 0), cpu_usage(), font=font, fill="white")
d.line([(0,13),(128,13)])
d.text((0, 15), mem_usage(), font=font, fill="white")
d.text((0, 27), disk_usage(s1_name, s1_path), font=font, fill="white")
if 's2_name' in globals():
d.text((0, 39), disk_usage(s2_name, s2_path), font=font, fill="white")
d.text((0, 51), network(net_name), font=font, fill="white")
else:
d.text((0, 39), network(net_name), font=font, fill="white")
device.display(txt.convert(device.mode))
def logo(device, msg):
img_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
'../share/sys-oled', 'helios4_logo.png'))
logo = Image.open(img_path).convert("RGBA")
background = Image.new("RGBA", device.size)
background.paste(logo, (0, 0))
txt = Image.new('RGBA', device.size)
d = ImageDraw.Draw(txt)
d.text((0,52), msg, font=font, fill="white")
out = Image.alpha_composite(background, txt)
device.display(out.convert(device.mode))
def sigterm_handler():
sys.exit(0)
signal.signal(signal.SIGTERM, sigterm_handler)
def main():
while True:
display_info(device)
time.sleep(refresh)
if show_logo == "yes":
logo(device, host_time())
time.sleep(refresh / 2)
if __name__ == "__main__":
try:
device = get_device()
device.contrast(contrast)
main()
except KeyboardInterrupt:
pass
You can’t perform that action at this time.