Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jun 24, 2022
1 parent ae8dbe5 commit 3392a3d
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions agents/ibootbar/ibootbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def _extract_oid_field_and_value(get_result):

return field_name, oid_value, oid_description


def _build_message(get_result, names, time):
"""Build the message for publication on an OCS Feed.
Expand Down Expand Up @@ -96,13 +97,14 @@ def _build_message(get_result, names, time):

if oid_value is None:
continue

message['data'][field_name] = oid_value
message['data'][field_name + "_name"] = names[int(field_name[-1])]
message['data'][field_name + "_description"] = oid_description

return message


def update_cache(get_result, names, timestamp):
"""Update the OID Value Cache.
Expand Down Expand Up @@ -141,17 +143,18 @@ def update_cache(get_result, names, timestamp):
oid_cache[field_name]["name"] = names[int(field_name[-1])]
oid_cache[field_name]["description"] = oid_description
oid_cache['ibootbar_connection'] = {'last_attempt': time.time(),
'connected': True}
'connected': True}
oid_cache['timestamp'] = timestamp
# This is a TypeError due to nothing coming back from the yield,
# This is a TypeError due to nothing coming back from the yield,
# so get_result is None here and can't be iterated.
except TypeError:
oid_cache['ibootbar_connection'] = {'last_attempt': time.time(),
'connected': False}
'connected': False}
raise ConnectionError('No SNMP response. Check your connection.')

return oid_cache


class ibootbarAgent:
"""Monitor the ibootbar system via SNMP.
Expand Down Expand Up @@ -190,7 +193,7 @@ def __init__(self, agent, address, port=161, version=2):
self.lastGet = 0

agg_params = {
'frame_length': 10*60 # [sec]
'frame_length': 10 * 60 # [sec]
}
self.agent.register_feed('ibootbar',
record=True,
Expand Down Expand Up @@ -222,7 +225,7 @@ def acq(self, session, params=None):

# Set initial default outlet names
names = ['Outlet-1', 'Outlet-2', 'Outlet-3', 'Outlet-4',
'Outlet-5', 'Outlet-6', 'Outlet-7', 'Outlet-8']
'Outlet-5', 'Outlet-6', 'Outlet-7', 'Outlet-8']

self.is_streaming = True
while self.is_streaming:
Expand All @@ -248,7 +251,7 @@ def acq(self, session, params=None):

for item in name_result:
names.append(item[1].prettyPrint())

# Do not publish if ibootbar connection has dropped
try:
message = _build_message(get_result, names, read_time)
Expand Down Expand Up @@ -295,15 +298,15 @@ def set_outlet(self, session, params=None):
state = 0

# Issue SNMP SET command to given outlet
outlet = [('IBOOTPDU-MIB', 'outletControl', params['outlet']-1)]
outlet = [('IBOOTPDU-MIB', 'outletControl', params['outlet'] - 1)]
setcmd = yield self.snmp.set(outlet, self.version, state)
self.log.info('{}'.format(setcmd))

# Force SNMP GET status commands by rewinding the lastGet time by 60 seconds
self.lastGet = self.lastGet - 60

return True, 'Set outlet {} to {}'.\
format(params['outlet']-1, params['state'])
format(params['outlet'] - 1, params['state'])

@ocs_agent.param('outlet', choices=[1, 2, 3, 4, 5, 6, 7, 8])
@ocs_agent.param('cycle_time', default=10, type=int)
Expand All @@ -323,24 +326,24 @@ def cycle_outlet(self, session, params=None):
if not acquired:
return False, "Could not acquire lock"
# Issue SNMP SET command for cycle time
set_cycle = [('IBOOTPDU-MIB', 'outletCycleTime', params['outlet']-1)]
set_cycle = [('IBOOTPDU-MIB', 'outletCycleTime', params['outlet'] - 1)]
setcmd1 = yield self.snmp.set(set_cycle, self.version, params['cycle_time'])
self.log.info('{}'.format(setcmd1))

# Issue SNMP SET command to given outlet
outlet = [('IBOOTPDU-MIB', 'outletControl', params['outlet']-1)]
outlet = [('IBOOTPDU-MIB', 'outletControl', params['outlet'] - 1)]
setcmd2 = yield self.snmp.set(outlet, self.version, 2)
self.log.info('{}'.format(setcmd2))
self.log.info('Cycling outlet {} for {} seconds'.\
format(params['outlet']-1, params['cycle_time']))
self.log.info('Cycling outlet {} for {} seconds'.
format(params['outlet'] - 1, params['cycle_time']))

# Force SNMP GET status commands throughout the cycle time
for i in range(params['cycle_time']):
self.lastGet = self.lastGet - 60
yield dsleep(1)

return True, 'Cycled outlet {} for {} seconds'.\
format(params['outlet']-1, params['cycle_time'])
format(params['outlet'] - 1, params['cycle_time'])

@ocs_agent.param('_')
@inlineCallbacks
Expand All @@ -360,7 +363,7 @@ def reboot(self, session, params=None):
# Force SNMP GET status commands
self.lastGet = self.lastGet - 60

return True, 'Rebooting system. This will take about 30 seconds.'
return True, 'Rebooting system. This will take about 30 seconds.'


def add_agent_args(parser=None):
Expand Down Expand Up @@ -407,4 +410,4 @@ def add_agent_args(parser=None):
agent.register_task("cycle_outlet", p.cycle_outlet, blocking=False)
agent.register_task("reboot", p.reboot, blocking=False)

runner.run(agent, auto_reconnect=True)
runner.run(agent, auto_reconnect=True)

0 comments on commit 3392a3d

Please sign in to comment.