-
Notifications
You must be signed in to change notification settings - Fork 1
Konfigurowalne piny GPIO przez gpio.json i endpoint /api/secure/gpio
#2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: gpio-sm-features
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"dout": 19, "sensor": 18, "off": 20} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,23 +2,25 @@ | |
| from phew import logging | ||
| from machine import Pin | ||
| from ktime import LocalTime | ||
| from utils import load_gpio_config, save_gpio_config | ||
| import time, _thread | ||
| import ujson as json | ||
| import uasyncio | ||
|
|
||
| class WaterflowDriver: | ||
|
|
||
| def __init__(self): | ||
| self.offPin = Pin(20, Pin.OUT, value=0) | ||
| self.gpio = load_gpio_config() | ||
| self.offPin = Pin(self.gpio['off'], Pin.OUT, value=0) | ||
| self.restartCountdown = -1 | ||
| self.time = LocalTime() | ||
| self.time.timeZoneRTCCorrection() | ||
| self.onTime = 72000 | ||
| self.offTime = 28800 | ||
| self.timeDependency = True | ||
| self.nol = 3 | ||
| self.waterflow = WaterflowPixel(self.nol, 0, 19) | ||
| self.sensorPin = Pin(18, Pin.IN, Pin.PULL_UP) | ||
| self.waterflow = WaterflowPixel(self.nol, 0, self.gpio['dout']) | ||
| self.sensorPin = Pin(self.gpio['sensor'], Pin.IN, Pin.PULL_UP) | ||
| self.brightness = 255 | ||
| self.pixels = [] | ||
| self.alive = False | ||
|
|
@@ -37,6 +39,28 @@ def __repr__(self): | |
| def setOperatingTime(self, start: LocalTime, end: LocalTime): | ||
| self.onTime = start.timestamp() | ||
| self.offTime = end.timestamp() | ||
|
|
||
| def configure_gpio(self, gpio): | ||
| previous = self.gpio.copy() | ||
| if save_gpio_config(gpio): | ||
| self.gpio = load_gpio_config() | ||
| if previous.get('off') != self.gpio['off']: | ||
| self.offPin.value(0) | ||
| self.offPin = Pin(self.gpio['off'], Pin.OUT, value=0) | ||
| if previous.get('sensor') != self.gpio['sensor']: | ||
| self.sensorPin = Pin(self.gpio['sensor'], Pin.IN, Pin.PULL_UP) | ||
| if previous.get('dout') != self.gpio['dout']: | ||
| self.waterflow.removeAll() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an admin changes Useful? React with 👍 / 👎. |
||
| self.waterflow = WaterflowPixel(self.waterflow.strip.num_leds, 0, self.gpio['dout']) | ||
| logging.info(f"> GPIO configuration changed from {previous} to {self.gpio}") | ||
| return True | ||
| return False | ||
|
|
||
| def reload_gpio(self): | ||
| gpio = load_gpio_config() | ||
| if gpio != self.gpio: | ||
| return self.configure_gpio(gpio) | ||
| return True | ||
|
|
||
| def current_state(self): | ||
| state = self.on | ||
|
|
@@ -87,6 +111,7 @@ async def prepareStep(self): | |
| if (len(self.pixels) == 0): | ||
| lock = uasyncio.Lock() | ||
| async with lock: | ||
| self.reload_gpio() | ||
| try: | ||
| with open('data.json', 'r') as f: | ||
| self.data = json.load(f) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For JSON bodies containing booleans, this validation accepts them because
boolis anintsubclass in Python/MicroPython, so a request like{"pixel": true}passes the documented integer check and is then persisted/applied as GPIO 1. That makes malformed API input silently remap hardware pins; rejectboolexplicitly before callingconfigure_gpio().Useful? React with 👍 / 👎.