|
| 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 |
0 commit comments