Skip to content. | Skip to navigation

Personal tools

Navigation

Personal tools

You are here: Home / Projects / code snippets / sispmctl.py

sispmctl.py

by datacop last modified Apr 07, 2012 05:18 PM
sispmctl.py is a small python wrapper I wrote in 2010 for the sispmctl binary, which controls Silver Shield PM USB power switches (see http://sispmctl.sourceforge.net/) Released under Gnu GPLv2. Rembember: there is also an official python wrapper for sispmctl.

Python Source icon sispmctl.py — Python Source, 2 kB (2514 bytes)

File contents

#
# sispmctl.py
# Python wrapper for sispmctl, Silver Shield PM control
# see http://sispmctl.sourceforge.net/
# 
# (C) Clemens Hopfer, 2010
# released under Gnu GPLv2
#
# usage:
# 
# create a new power socket: (SOCK_NR can also be 'all' for all sockets)
# >>> import sispmctl
# >>> socket = sispmctl.PowerSocket(SOCK_NR)
# 
# turn socket on: (any of the following)
# >>> socket.on()
# >>> socket.enable()
# >>> socket.set(1)
#
## turn socket off: (any of the following)
# >>> socket.off()
# >>> socket.disable()
# >>> socket.set(0)
#
# query socket state:
# >>> socket.query()
# 
# enable/disable buzzer
# >>> socket.buzzer(1)
# or
# >>> socket.buzzer(0)
# 

import os

class PowerSocket:
	def __init__(self, nr, device = 0):
		if (nr > 0 and nr < 5) or nr == 'all':
			self.socketnr = nr
		else:
			raise sispmctlException, "Invalid outlet number, expected: 1, 2, 3, 4, or 'all'"
		
		device = int(device)
		if (device >= 0 and device < 255):
			self.devicenr = int(device)
		else:
			raise sispmctlException, "Invalid device number"
		self.basecmd = 'sispmctl -q -d ' + str(self.devicenr) + ' '
		
	def enable(self):
		self.set(1)
	def disable(self):
		self.set(0)
	
	def on(self):
		self.set(1)
	def off(self):
		self.set(0)
	
	def set(self, state):
		if (state == False or state == 0):
			values_args = '-f' + str(self.socketnr)
		elif (state == True or state == 1):
			values_args = '-o' + str(self.socketnr)
		else:
			raise sispmctlException, "Unknown state: " + str(state)
		cmd_output = self.__cmd(values_args)
		if len(cmd_output) > 0:
			raise sispmctlException, "Unable to set the state: " + cmd_output
	
	def toggle(self):
		values_args = '-t' + str(self.socketnr)
		cmd_output = self.__cmd(values_args)
		if len(cmd_output) > 0:
			raise sispmctlException, "Unable to set the state: " + cmd_output
	
	def query(self):
		values_args = '-n -m ' + str(self.socketnr)
		cmd_output = self.__cmd(values_args)
		return map(int, cmd_output.strip().split('\n'))
		
	def buzzer(self, state):
		if (state == False or state == 0):
			values_args = '-b off'
		elif (state == True or state == 1):
			values_args = '-b on'
		else:
			raise sispmctlException, "Unknown state: " + str(state)
		cmd_output = self.__cmd(values_args)
		if len(cmd_output) > 0:
			raise sispmctlException, "Unable to set the state: " + cmd_output
	
	def __cmd(self, command):
		cmd = os.popen4(self.basecmd + command)
		cmd_output = cmd[1].read()
		for fd in cmd: fd.close()
		return cmd_output

class sispmctlException(Exception): pass
Filed under: ,