Skip to content

Commit 4bbbbf6

Browse files
committed
Add gude8210 (snmp) power driver integration
Signed-off-by: Onur Celep <[email protected]>
1 parent 83d306c commit 4bbbbf6

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

doc/configuration.rst

+3
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ Currently available are:
192192
``gude8031``
193193
Controls *Gude Expert Power Control 8031 PDUs* and *Gude Expert Power Control 87-1210-18 PDUs* via a simple HTTP API.
194194

195+
``gude8210``
196+
Controls *Gude Expert Power Control 8210 PDUs* via SNMP.
197+
195198
``gude8225``
196199
Controls *Gude Expert Power Control 8225 PDUs* via a simple HTTP API.
197200

labgrid/driver/power/gude8210.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import logging
2+
3+
from ...util.snmp import SimpleSNMP
4+
from ..exception import ExecutionError
5+
6+
# Base OID for the Gude power switch as per the MIB file from gude8210
7+
POWER_OID_BASE = "1.3.6.1.4.1.28507.1.1.2.2.1.3"
8+
NUMBER_OF_OUTLETS = 8 # Max number of outlets from the MIB
9+
10+
11+
def power_set(host, port, index, value):
12+
"""
13+
Sets the power state of a specific outlet on the Gude power switch.
14+
15+
Args:
16+
host (str): The IP address of the power switch.
17+
port (int or None): SNMP port, default is 161 or None.
18+
index (int): Outlet index.
19+
value (bool): True to turn on, False to turn off.
20+
community (str): SNMP community string.
21+
"""
22+
# Validate index within allowable range
23+
if index is None or not (1 <= int(index) <= NUMBER_OF_OUTLETS):
24+
raise ExecutionError("Invalid outlet index. Ensure the index is within the range 1 to 8.")
25+
26+
# Setup SNMP connection and OID for the target outlet
27+
_snmp = SimpleSNMP(host, 'private', port=port)
28+
oid = f"{POWER_OID_BASE}.{index}"
29+
snmp_value = "1" if value else "0" # SNMP value for on/off
30+
logging.debug(f"Attempting SNMP SET on host {host}, OID {oid}, value {snmp_value}")
31+
32+
try:
33+
# Set the power state for the specified outlet
34+
_snmp.set(oid, snmp_value)
35+
logging.debug(f"SNMP SET successful for OID {oid} with value {snmp_value}")
36+
except Exception as e:
37+
logging.debug(f"SNMP SET failed for OID {oid} with exception {e}")
38+
raise ExecutionError(f"Failed to set power state on outlet {index}: {e}") from e
39+
40+
41+
def power_get(host, port, index):
42+
"""
43+
Retrieves the current power state of a specific outlet on the Gude power switch.
44+
45+
Args:
46+
host (str): The IP address of the power switch.
47+
port (int or None): SNMP port, default is 161 or None.
48+
index (int): Outlet index.
49+
community (str): SNMP community string.
50+
51+
Returns:
52+
bool: True if the outlet is on, False if it's off.
53+
"""
54+
# Validate index within allowable range
55+
if index is None or not (1 <= int(index) <= NUMBER_OF_OUTLETS):
56+
raise ExecutionError("Invalid outlet index. Ensure the index is within the range 1 to 8.")
57+
58+
# Setup SNMP connection and OID for the target outlet
59+
_snmp = SimpleSNMP(host, 'public', port=port)
60+
oid = f"{POWER_OID_BASE}.{index}"
61+
62+
logging.debug(f"Attempting SNMP GET on host {host}, OID {oid}")
63+
64+
try:
65+
# Retrieve the current power state for the specified outlet
66+
value = _snmp.get(oid)
67+
logging.debug(f"SNMP GET returned value {value} for OID {oid}")
68+
69+
# Verify and interpret the SNMP response
70+
if str(value).strip() == "1":
71+
return True # Outlet is on
72+
elif str(value).strip() == "0":
73+
return False # Outlet is off
74+
else:
75+
raise ExecutionError(f"Unexpected SNMP value '{value}' for outlet {index}")
76+
except Exception as e:
77+
logging.debug(f"SNMP GET failed for OID {oid} with exception {e}")
78+
raise ExecutionError(f"Failed to get power state for outlet {index}: {e}") from e

tests/test_powerdriver.py

+1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ def test_import_backends(self):
282282
import labgrid.driver.power.digitalloggers_restapi
283283
import labgrid.driver.power.eth008
284284
import labgrid.driver.power.gude
285+
import labgrid.driver.power.gude8210
285286
import labgrid.driver.power.gude24
286287
import labgrid.driver.power.netio
287288
import labgrid.driver.power.netio_kshell

0 commit comments

Comments
 (0)