Skip to content

Commit

Permalink
feat(API): add turnOn and turnOff action
Browse files Browse the repository at this point in the history
  • Loading branch information
mattheop committed May 10, 2021
1 parent f4370ba commit cea1b87
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions octoprint_octolight/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,47 @@ def on_after_startup(self):

self._plugin_manager.send_plugin_message(self._identifier, dict(isLightOn=self.light_state))

def light_toggle(self):
# Sets the GPIO every time, if user changed it in the settings.
GPIO.setup(int(self._settings.get(["light_pin"])), GPIO.OUT)

self.light_state = not self.light_state

# Sets the light state depending on the inverted output setting (XOR)
if self.light_state ^ self._settings.get(["inverted_output"]):
GPIO.output(int(self._settings.get(["light_pin"])), GPIO.HIGH)
else:
GPIO.output(int(self._settings.get(["light_pin"])), GPIO.LOW)

self._logger.info("Got request. Light state: {}".format(
self.light_state
))

self._plugin_manager.send_plugin_message(self._identifier, dict(isLightOn=self.light_state))

def on_api_get(self, request):
action = request.args.get('action', default="toggle", type=str)

if action == "toggle":
# Sets the GPIO every time, if user changed it in the settings.
GPIO.setup(int(self._settings.get(["light_pin"])), GPIO.OUT)

self.light_state = not self.light_state
self.light_toggle()

# Sets the light state depending on the inverted output setting (XOR)
if self.light_state ^ self._settings.get(["inverted_output"]):
GPIO.output(int(self._settings.get(["light_pin"])), GPIO.HIGH)
else:
GPIO.output(int(self._settings.get(["light_pin"])), GPIO.LOW)
return flask.jsonify(state=self.light_state)

self._logger.info("Got request. Light state: {}".format(
self.light_state
))
elif action == "getState":
return flask.jsonify(state=self.light_state)

self._plugin_manager.send_plugin_message(self._identifier, dict(isLightOn=self.light_state))
elif action == "turnOn":
if not self.light_state:
self.light_toggle()

return flask.jsonify(state=self.light_state)
elif action == "getState":

elif action == "turnOff":
if self.light_state:
self.light_toggle()

return flask.jsonify(state=self.light_state)

else:
return flask.jsonify(error="action not recognized")

Expand Down

0 comments on commit cea1b87

Please sign in to comment.