def post(self, unique_id): """Change the state of an output""" if not utils_general.user_has_permission('edit_controllers'): abort(403) control = DaemonControl() state = None duration = None duty_cycle = None if ns_output.payload: if 'state' in ns_output.payload: state = ns_output.payload["state"] if state is not None: try: state = bool(state) except Exception: abort(422, message='state must represent a bool value') if 'duration' in ns_output.payload: duration = ns_output.payload["duration"] if duration is not None: try: duration = float(duration) except Exception: abort(422, message='duration does not represent a number') else: duration = 0 if 'duty_cycle' in ns_output.payload: duty_cycle = ns_output.payload["duty_cycle"] if duty_cycle is not None: try: duty_cycle = float(duty_cycle) if duty_cycle < 0 or duty_cycle > 100: abort(422, message='Required: 0 <= duty_cycle <= 100') except Exception: abort(422, message='duty_cycle does not represent float value') try: if state is not None and duration is not None: return_ = control.output_on_off( unique_id, state, amount=duration) elif state is not None: return_ = control.output_on_off(unique_id, state) elif duty_cycle is not None: return_ = control.output_duty_cycle( unique_id, duty_cycle=duty_cycle) else: return {'message': 'Insufficient payload'}, 460 return return_handler(return_) except Exception: abort(500, message='An exception occurred', error=traceback.format_exc())
def output_mod(output_id, channel, state, output_type, amount): """ Manipulate output (using non-unique ID) """ if not utils_general.user_has_permission('edit_controllers'): return 'Insufficient user permissions to manipulate outputs' if is_int(channel): # if an integer was returned output_channel = int(channel) else: # if a channel ID was returned channel_dev = db_retrieve_table(OutputChannel).filter( OutputChannel.unique_id == channel).first() if channel_dev: output_channel = channel_dev.channel else: return "Could not determine channel number from channel ID '{}'".format( channel) daemon = DaemonControl() if (state in ['on', 'off'] and str_is_float(amount) and ((output_type in ['sec', 'pwm', 'vol'] and float(amount) >= 0) or (output_type == 'value'))): out_status = daemon.output_on_off(output_id, state, output_type=output_type, amount=float(amount), output_channel=output_channel) if out_status[0]: return 'ERROR: {}'.format(out_status[1]) else: return 'SUCCESS: {}'.format(out_status[1]) else: return 'ERROR: unknown parameters: ' \ 'output_id: {}, channel: {}, state: {}, output_type: {}, amount: {}'.format( output_id, channel, state, output_type, amount)
def output_mod(output_id, channel_id, state, output_type, amount): """ Manipulate output (using non-unique ID) """ if not utils_general.user_has_permission('edit_controllers'): return 'Insufficient user permissions to manipulate outputs' if channel_id == '0': # some parts of pages don't have access to the channel ID and only know there is 1 channel channel = db_retrieve_table(OutputChannel).filter(and_( OutputChannel.output_id == output_id, OutputChannel.channel == 0)).first() else: channel = db_retrieve_table(OutputChannel, unique_id=channel_id) daemon = DaemonControl() if (state in ['on', 'off'] and output_type in ['sec', 'pwm', 'vol'] and (str_is_float(amount) and float(amount) >= 0)): out_status = daemon.output_on_off( output_id, state, output_type=output_type, amount=float(amount), output_channel=channel.channel) if out_status[0]: return 'ERROR: {}'.format(out_status[1]) else: return 'SUCCESS: {}'.format(out_status[1])
def output_mod(output_id, state, out_type, amount): """ Manipulate output (using non-unique ID) """ if not utils_general.user_has_permission('edit_controllers'): return 'Insufficient user permissions to manipulate outputs' daemon = DaemonControl() if (state in ['on', 'off'] and out_type == 'sec' and (str_is_float(amount) and float(amount) >= 0)): return daemon.output_on_off(output_id, state, float(amount)) elif (state == 'on' and out_type in ['pwm', 'command_pwm'] and (str_is_float(amount) and float(amount) >= 0)): return daemon.output_on(output_id, state, duty_cycle=float(amount))
def output_mod(output_id, state, out_type, amount): """ Manipulate output (using non-unique ID) """ if not utils_general.user_has_permission('edit_controllers'): return 'Insufficient user permissions to manipulate outputs' daemon = DaemonControl() if (state in ['on', 'off'] and out_type == 'sec' and (str_is_float(amount) and float(amount) >= 0)): return daemon.output_on_off(output_id, state, float(amount)) elif (state == 'on' and out_type in ['pwm', 'command_pwm'] and (str_is_float(amount) and float(amount) >= 0)): return daemon.output_on(output_id, duty_cycle=float(amount))
def output_mod(output_id, state, output_type, amount): """ Manipulate output (using non-unique ID) """ if not utils_general.user_has_permission('edit_controllers'): return 'Insufficient user permissions to manipulate outputs' daemon = DaemonControl() if (state in ['on', 'off'] and output_type in ['sec', 'pwm', 'vol'] and (str_is_float(amount) and float(amount) >= 0)): out_status = daemon.output_on_off( output_id, state, output_type=output_type, amount=float(amount)) if out_status[0]: return 'ERROR: {}'.format(out_status[1]) else: return 'SUCCESS: {}'.format(out_status[1])
def output_mod_unique_id(unique_id, state, out_type, amount): """ Manipulate output (using unique ID) """ if not utils_general.user_has_permission('edit_controllers'): return 'Insufficient user permissions to manipulate outputs' output = Output.query.filter(Output.unique_id == unique_id).first() daemon = DaemonControl() if (state in ['on', 'off'] and out_type == 'sec' and (str_is_float(amount) and float(amount) >= 0)): return daemon.output_on_off(output.id, state, float(amount)) elif (state == 'on' and out_type == 'pwm' and (str_is_float(amount) and float(amount) >= 0)): return daemon.relay_on(output.id, state, duty_cycle=float(amount))