2020-03-31 21:03:07 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""Fetch hard drive temeperature data from a hddtemp daemon
|
|
|
|
that runs on localhost and default port (7634)
|
|
|
|
"""
|
|
|
|
|
|
|
|
import socket
|
|
|
|
|
2020-03-31 21:06:44 +02:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
2020-03-31 21:03:07 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
HOST = "localhost"
|
2020-03-31 21:03:07 +02:00
|
|
|
PORT = 7634
|
|
|
|
|
|
|
|
CHUNK_SIZE = 1024
|
|
|
|
RECORD_SIZE = 5
|
2020-05-03 11:15:52 +02:00
|
|
|
SEPARATOR = "|"
|
|
|
|
|
2020-03-31 21:03:07 +02:00
|
|
|
|
2020-03-31 21:06:44 +02:00
|
|
|
class Module(core.module.Module):
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.hddtemps))
|
2020-03-31 21:06:44 +02:00
|
|
|
self.__hddtemps = self.__get_hddtemps()
|
2020-03-31 21:03:07 +02:00
|
|
|
|
2020-03-31 21:06:44 +02:00
|
|
|
def hddtemps(self, _):
|
|
|
|
return self.__hddtemps
|
2020-03-31 21:03:07 +02:00
|
|
|
|
2020-03-31 21:06:44 +02:00
|
|
|
def __fetch_data(self):
|
2020-03-31 21:03:07 +02:00
|
|
|
"""fetch data from hddtemp service"""
|
|
|
|
try:
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
|
|
|
sock.connect((HOST, PORT))
|
2020-05-03 11:15:52 +02:00
|
|
|
data = ""
|
2020-03-31 21:03:07 +02:00
|
|
|
while True:
|
|
|
|
chunk = sock.recv(CHUNK_SIZE)
|
|
|
|
if chunk:
|
|
|
|
data += str(chunk)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
return data
|
|
|
|
except (AttributeError, socket.error) as e:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
2020-03-31 21:06:44 +02:00
|
|
|
def __get_parts(data):
|
2020-03-31 21:03:07 +02:00
|
|
|
"""
|
|
|
|
split data using | separator and remove first item
|
|
|
|
(because the first item is empty)
|
|
|
|
"""
|
2020-05-03 11:15:52 +02:00
|
|
|
parts = data.split("|")[1:]
|
2020-03-31 21:03:07 +02:00
|
|
|
return parts
|
|
|
|
|
|
|
|
@staticmethod
|
2020-03-31 21:06:44 +02:00
|
|
|
def __partition_parts(parts):
|
2020-03-31 21:03:07 +02:00
|
|
|
"""
|
|
|
|
partition parts: one device record is five (5) items
|
|
|
|
"""
|
2020-05-03 11:15:52 +02:00
|
|
|
per_disk = [
|
|
|
|
parts[i : i + RECORD_SIZE] for i in range(len(parts))[::RECORD_SIZE]
|
|
|
|
]
|
2020-03-31 21:03:07 +02:00
|
|
|
return per_disk
|
|
|
|
|
|
|
|
@staticmethod
|
2020-03-31 21:06:44 +02:00
|
|
|
def __get_name_and_temp(device_record):
|
2020-03-31 21:03:07 +02:00
|
|
|
"""
|
|
|
|
get device name (without /dev part, to save space on bar)
|
|
|
|
and temperature (in °C) as tuple
|
|
|
|
"""
|
2020-05-03 11:15:52 +02:00
|
|
|
device_name = device_record[0].split("/")[-1]
|
2020-03-31 21:03:07 +02:00
|
|
|
device_temp = device_record[2]
|
|
|
|
return (device_name, device_temp)
|
|
|
|
|
|
|
|
@staticmethod
|
2020-03-31 21:06:44 +02:00
|
|
|
def __get_hddtemp(device_record):
|
2020-03-31 21:03:07 +02:00
|
|
|
name, temp = device_record
|
2020-05-03 11:15:52 +02:00
|
|
|
hddtemp = "{}+{}°C".format(name, temp)
|
2020-03-31 21:03:07 +02:00
|
|
|
return hddtemp
|
|
|
|
|
2020-03-31 21:06:44 +02:00
|
|
|
def __get_hddtemps(self):
|
|
|
|
data = self.__fetch_data()
|
2020-03-31 21:03:07 +02:00
|
|
|
if data is None:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "n/a"
|
2020-03-31 21:06:44 +02:00
|
|
|
parts = self.__get_parts(data)
|
|
|
|
per_disk = self.__partition_parts(parts)
|
|
|
|
names_and_temps = [self.__get_name_and_temp(x) for x in per_disk]
|
|
|
|
hddtemps = [self.__get_hddtemp(x) for x in names_and_temps]
|
2020-03-31 21:03:07 +02:00
|
|
|
return SEPARATOR.join(hddtemps)
|
|
|
|
|
2020-03-31 21:06:44 +02:00
|
|
|
def update(self):
|
|
|
|
self.__hddtemps = self.__get_hddtemps()
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-03-31 21:06:44 +02:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|