Skip to content

Commit

Permalink
Set fan speed by percent.
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewHink committed Apr 12, 2018
1 parent 6bfae3f commit 76600c2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 20 deletions.
8 changes: 8 additions & 0 deletions synse/commands/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ async def write(rack, board, device, data):
Returns:
WriteResponse: The "write" response scheme model.
"""
# Log what we are writing at info, the default logging level, so that we
# can tell what we are doing.
logger.info('write. rack: {}, board: {}, device: {}, data: {}'.format(
rack, board, device, data))

# lookup the known info for the specified device
plugin_name, _ = await cache.get_device_meta(rack, board, device)
Expand Down Expand Up @@ -51,6 +55,10 @@ async def write(rack, board, device, data):
)
raw = [str.encode(raw)]

# Log what we are writing at info, the default logging level, so that we
# can tell what we are doing.
logger.info('write. action: {}, raw: {}'.format(
action, raw))
wd = WriteData(action=action, raw=raw)

# perform a gRPC write on the device's managing plugin
Expand Down
84 changes: 64 additions & 20 deletions synse/routes/aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from synse import commands, const, errors, validate
from synse.i18n import gettext
from synse.log import logger
from synse.version import __api_version__

bp = Blueprint(__name__, url_prefix='/synse/' + __api_version__)
Expand Down Expand Up @@ -102,33 +103,76 @@ async def fan_route(request, rack, board, device):
Returns:
sanic.response.HTTPResponse: The endpoint response.
"""
logger.info('fan_route. request: {}, request.raw_args: {}'.format(request, request.raw_args))

await validate.validate_device_type(const.TYPE_FAN, rack, board, device)

qparams = validate.validate_query_params(
request.raw_args,
'speed'
'speed', # speed in rpm
'speed_percent' # speed of 0 (off) or 10% to 100%
)

param_speed = qparams.get('speed')

# if a request parameter is specified, this will translate to a
# write request.
if param_speed is not None:
# FIXME - is the below true? could we check against the device prototype's
# "range.min" and "range.max" fields for this?
# no validation is done on the fan speed. valid fan speeds vary based on
# fan make/model, so it is up to the underlying implementation to do the
# validation.
data = {
'action': 'speed',
'raw': param_speed
}
transaction = await commands.write(rack, board, device, data)
return transaction.to_json()

# if no request parameter is specified, this will translate to a
# read request.
# This sucks. Get the first key in request.raw_args. Example: request.raw_args: {'speed': '0'}
if len(request.raw_args) == 1:

if list(request.raw_args)[0] == 'speed':
param_speed = qparams.get('speed')
logger.info(
'fan_route. set speed: request {}, raw_args {}'.format(
request, request.raw_args))

# if a request parameter is specified, this will translate to a
# write request.
if param_speed is not None:
# FIXME - is the below true? could we check against the device prototype's
# "range.min" and "range.max" fields for this?
# no validation is done on the fan speed. valid fan speeds vary based on
# fan make/model, so it is up to the underlying implementation to do the
# validation.
# ^ mhink - Yes it absolutely is and the underlying code should fail as needed.
# Also max and min speeds vary by the fan motor, not the controller. (the device)
data = {
'action': 'speed',
'raw': param_speed,
}
transaction = await commands.write(rack, board, device, data)
return transaction.to_json()

# If a request parameter is specified, this will translate to a
# write request.
elif list(request.raw_args)[0] == 'speed_percent':
param_speed_percent = qparams.get('speed_percent')
logger.info(
'fan_route. set speed: request {}, raw_args {}'.format(
request, request.raw_args))

# elif param_speed_percent is not None:
# FIXME - is the below true? could we check against the device prototype's
# "range.min" and "range.max" fields for this?
# no validation is done on the fan speed. valid fan speeds vary based on
# fan make/model, so it is up to the underlying implementation to do the
# validation.
# ^ mhink - Yes it absolutely is and the underlying code should fail as needed.
# Also max and min speeds vary by the fan motor, not the controller. (the device)
data = {
'action': 'speed_percent',
'raw': param_speed_percent,
}
transaction = await commands.write(rack, board, device, data)
return transaction.to_json()
# If no request parameter is specified, this will translate to a
# read request.
else:
# TODO: This is dodgy.
logger.info('fan_route. defaulting to read request')
reading = await commands.read(rack, board, device)
return reading.to_json()

# If no request parameter is specified, this will translate to a
# read request. Why this makes sense I can't say. This is side effect code.
else:
logger.info('fan_route. defaulting to read request')
reading = await commands.read(rack, board, device)
return reading.to_json()

Expand Down

0 comments on commit 76600c2

Please sign in to comment.