1
0
mirror of https://github.com/AndrewX192/lenovo-sa120-fanspeed-utility synced 2025-12-06 01:23:19 +00:00

Clean up code

This commit is contained in:
d10n
2017-04-15 16:55:48 -04:00
parent cb8fbfca3a
commit 0958af2bc2

View File

@@ -1,85 +1,116 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf8 -*-
import os
import sys import sys
import time import time
import glob import glob
import stat
try: try:
from StringIO import StringIO from StringIO import StringIO
except ImportError: except ImportError:
from io import StringIO from io import StringIO
from subprocess import check_output, Popen, PIPE, STDOUT
from subprocess import check_output, Popen, PIPE, STDOUT, CalledProcessError
devices_to_check = ['/dev/sg*', '/dev/ses*']
def usage():
print('python fancontrol.py 1-7')
sys.exit(-1)
def get_requested_fan_speed():
if len(sys.argv) < 2:
return usage()
try:
speed = int(sys.argv[1])
except ValueError:
return usage()
if not 1 <= speed <= 7:
return usage()
return speed
def print_speeds(device): def print_speeds(device):
for i in range(0, 6): for i in range(0, 6):
print("Fan {} speed: {}".format(i, check_output( print('Fan {} speed: {}'.format(i, check_output(
['sg_ses', '--index=coo,{}'.format(i), '--get=1:2:11', device]).decode('utf-8').split('\n')[0])) ['sg_ses', '--index=coo,{}'.format(i), '--get=1:2:11', device]).decode('utf-8').split('\n')[0]))
if len(sys.argv) < 1: def find_sa120_devices():
print("python fancontrol.py 1-7") devices = []
sys.exit(-1) for device_glob in devices_to_check:
for device in glob.glob(device_glob):
try:
output = check_output(['sg_ses', device], stderr=STDOUT)
if b'ThinkServerSA120' in output:
print('Enclosure found on ' + device)
devices.append(device)
else:
print('Enclosure not found on ' + device)
except CalledProcessError:
print('Enclosure not found on ' + device)
return devices
fan = int(sys.argv[1])
if fan <= 0 or fan > 6: def set_fan_speeds(device, speed):
raise Exception("Fan speed must be between 1 and 7") print_speeds(device)
print('Reading current configuration...')
out = check_output(['sg_ses', '-p', '0x2', device, '--raw']).decode('utf-8')
devices_to_check = ['/dev/sg*', '/dev/ses*'] s = out.split()
device = "" for i in range(0, 6):
print('Setting fan {} to {}'.format(i, speed))
idx = 88 + 4 * i
s[idx + 0] = '80'
s[idx + 1] = '00'
s[idx + 2] = '00'
s[idx + 3] = format(1 << 5 | speed & 7, 'x')
for device_glob in devices_to_check: output = StringIO()
for dev_node in glob.glob(device_glob): off = 0
try: count = 0
out = check_output(["sg_ses", dev_node], stderr=STDOUT)
if 'ThinkServerSA120' in out:
device = dev_node
print("Enclosure found on " + device)
print_speeds(device) while True:
print("Reading current configuration...") output.write(s[off])
out = check_output(["sg_ses", "-p", "0x2", device, "--raw"]).decode('utf-8') off = off + 1
s = out.split() count = count + 1
if count == 8:
output.write(' ')
elif count == 16:
output.write('\n')
count = 0
else:
output.write(' ')
if off >= len(s):
break
for i in range(0, 6): output.write('\n')
print("Setting fan {} to {}".format(i, fan)) p = Popen(['sg_ses', '-p', '0x2', device, '--control', '--data', '-'], stdout=PIPE, stdin=PIPE,
idx = 88 + 4 * i stderr=PIPE)
s[idx + 0] = "80" print('Set fan speeds... Waiting to get fan speeds (ctrl+c to skip)')
s[idx + 1] = "00" print(p.communicate(input=bytearray(output.getvalue(), 'utf-8'))[0].decode('utf-8'))
s[idx + 2] = "00" try:
s[idx + 3] = format(1 << 5 | fan & 7, "x") time.sleep(10)
print_speeds(device)
except KeyboardInterrupt:
pass
output = StringIO()
off = 0
count = 0
line = ''
while True: def main():
output.write(s[off]) speed = get_requested_fan_speed()
off = off + 1 devices = find_sa120_devices()
count = count + 1 if not devices:
if count == 8: print('Could not find enclosure')
output.write(" ") sys.exit(1)
elif count == 16: for device in devices:
output.write("\n") set_fan_speeds(device, speed)
count = 0 print('\nDone')
else:
output.write(" ")
if off >= len(s):
break
output.write("\n")
p = Popen(['sg_ses', '-p', '0x2', device, '--control', '--data', '-'], stdout=PIPE, stdin=PIPE,
stderr=PIPE)
print("Set fan speeds... Waiting to get fan speeds (ctrl+c to skip)")
print(p.communicate(input=bytearray(output.getvalue(), 'utf-8'))[0].decode('utf-8'))
time.sleep(10)
print_speeds(device)
except:
print("Enclosure not found on " + dev_node)
if device == "":
print("Could not find enclosure")
sys.exit(1)
if __name__ == '__main__':
main()