Added steps settings API

This commit is contained in:
Mark van Renswoude 2018-01-26 11:50:42 +01:00
parent 4c57f17c8c
commit 5d6514f6e2
8 changed files with 48 additions and 29 deletions

8
API.md
View File

@ -6,8 +6,8 @@
- [POST /api/connection](#post-apiconnection) - [POST /api/connection](#post-apiconnection)
- [GET /api/system](#get-apisystem) - [GET /api/system](#get-apisystem)
- [POST /api/system](#post-apisystem) - [POST /api/system](#post-apisystem)
- [GET /api/steps](#get-apisteps) - [GET /api/steps/values](#get-apistepsvalues)
- [POST /api/steps](#post-apisteps) - [POST /api/steps/values](#post-apistepsvalues)
- [GET /api/triggers/time](#get-apitriggerstime) - [GET /api/triggers/time](#get-apitriggerstime)
- [POST /api/triggers/time](#post-apitriggerstime) - [POST /api/triggers/time](#post-apitriggerstime)
- [GET /api/triggers/motion](#get-apitriggersmotion) - [GET /api/triggers/motion](#get-apitriggersmotion)
@ -111,7 +111,7 @@ Updates the settings of the WiFi connections. The module will apply the new sett
## POST /api/system ## POST /api/system
## GET /api/steps ## GET /api/steps/values
Returns the current brightness value for each step. The number of items in the array is equal to the number of configured steps. Each value has a range of 0 to 255. Returns the current brightness value for each step. The number of items in the array is equal to the number of configured steps. Each value has a range of 0 to 255.
@ -123,7 +123,7 @@ Returns the current brightness value for each step. The number of items in the a
] ]
``` ```
## POST /api/steps ## POST /api/steps/values
Changes the brightness value for each step. If the number of values in the array is less than the number of configured steps, each subsequent step is considered to be off. Changes the brightness value for each step. If the number of values in the array is less than the number of configured steps, each subsequent step is considered to be off.

View File

@ -1,4 +1,5 @@
## Bill of materials ## Bill of materials
1. ESP8266 module (I based mine on a vanilla ESP8266 ESP-12F, but friendlier modules like the Wemos D1 should work as well) 1. ESP8266 module (I based mine on a vanilla ESP8266 ESP-12F, but friendlier modules like the Wemos D1 should work as well)
1. PCA9685 16-channel PWM module 1. PCA9685 16-channel PWM module
1. TODO complete this list

View File

@ -99,9 +99,9 @@ void setup()
_dln("Setup :: registering routes"); _dln("Setup :: registering routes");
registerStaticRoutes(&server); registerStaticRoutes(&server);
registerAPIRoutes(&server);
registerSettingsRoutes(&server); registerSettingsRoutes(&server);
registerFirmwareRoutes(&server); registerFirmwareRoutes(&server);
registerAPIRoutes(&server);
_dln("Setup :: starting HTTP server"); _dln("Setup :: starting HTTP server");
server.onNotFound(handleNotFound); server.onNotFound(handleNotFound);

View File

@ -16,7 +16,7 @@
#include "../settings/triggers/time.h" #include "../settings/triggers/time.h"
void handleGetSteps(AsyncWebServerRequest *request) void handleGetStepValues(AsyncWebServerRequest *request)
{ {
_dln("API :: get steps"); _dln("API :: get steps");
@ -35,7 +35,7 @@ void handleGetSteps(AsyncWebServerRequest *request)
} }
void handlePostSteps(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) void handlePostStepValues(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total)
{ {
_dln("API :: post steps"); _dln("API :: post steps");
@ -125,8 +125,8 @@ void handlePostMotionTriggers(AsyncWebServerRequest *request, uint8_t *data, siz
void registerAPIRoutes(AsyncWebServer* server) void registerAPIRoutes(AsyncWebServer* server)
{ {
server->on("/api/steps", HTTP_GET, handleGetSteps); server->on("/api/steps/values", HTTP_GET, handleGetStepValues);
server->on("/api/steps", HTTP_POST, devNullRequest, devNullFileUpload, handlePostSteps); server->on("/api/steps/values", HTTP_POST, devNullRequest, devNullFileUpload, handlePostStepValues);
server->on("/api/triggers/time", HTTP_GET, handleGetTimeTriggers); server->on("/api/triggers/time", HTTP_GET, handleGetTimeTriggers);
server->on("/api/triggers/time", HTTP_POST, devNullRequest, devNullFileUpload, handlePostTimeTriggers); server->on("/api/triggers/time", HTTP_POST, devNullRequest, devNullFileUpload, handlePostTimeTriggers);

View File

@ -116,6 +116,35 @@ void handlePostSystem(AsyncWebServerRequest *request, uint8_t *data, size_t len,
} }
void handleGetSteps(AsyncWebServerRequest *request)
{
_dln("API :: get steps");
AsyncResponseStream *response = request->beginResponseStream("application/json");
stepsSettings->toJson(*response);
request->send(response);
}
void handlePostSteps(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total)
{
_dln("API :: post steps");
bool changed;
if (stepsSettings->fromJson((char*)data, &changed))
{
stepsSettings->write();
if (changed)
stepsSettingsChanged = true;
request->send(200);
}
else
request->send(400);
}
void registerSettingsRoutes(AsyncWebServer* server) void registerSettingsRoutes(AsyncWebServer* server)
{ {
server->on("/api/version", HTTP_GET, handleVersion); server->on("/api/version", HTTP_GET, handleVersion);
@ -127,4 +156,7 @@ void registerSettingsRoutes(AsyncWebServer* server)
server->on("/api/system", HTTP_GET, handleGetSystem); server->on("/api/system", HTTP_GET, handleGetSystem);
server->on("/api/system", HTTP_POST, devNullRequest, devNullFileUpload, handlePostSystem); server->on("/api/system", HTTP_POST, devNullRequest, devNullFileUpload, handlePostSystem);
server->on("/api/steps", HTTP_GET, handleGetSteps);
server->on("/api/steps", HTTP_POST, devNullRequest, devNullFileUpload, handlePostSteps);
} }

View File

@ -1,6 +1,5 @@
function startApp() function startApp()
{ {
// TODO support for disable checkboxes
Vue.component('check', { Vue.component('check', {
template: '<div class="check" :class="{ checked: value, disabled: disabled }" @keydown="handleKeyDown" @click="handleClick" tabindex="0"><div class="control"><div class="inner"></div></div><div class="label">{{ title }}</div></div>', template: '<div class="check" :class="{ checked: value, disabled: disabled }" @keydown="handleKeyDown" @click="handleClick" tabindex="0"><div class="control"><div class="inner"></div></div><div class="label">{{ title }}</div></div>',
props: { props: {
@ -370,7 +369,7 @@ function startApp()
loadSteps: function() loadSteps: function()
{ {
var self = this; var self = this;
return axios.get('/api/steps') return axios.get('/api/steps/values')
.then(function(response) .then(function(response)
{ {
if (Array.isArray(response.data)) if (Array.isArray(response.data))
@ -627,7 +626,7 @@ function startApp()
self.disableStepsChanged = false; self.disableStepsChanged = false;
axios.post('/api/steps', { axios.post('/api/steps/values', {
transitionTime: 1000, transitionTime: 1000,
values: steps values: steps
}) })

2
web/dist/bundle.js vendored

File diff suppressed because one or more lines are too long

View File

@ -53,18 +53,6 @@
<div><radio :title="$t('status.allStepsTrue')" v-model="allSteps" :id="true"></radio></div> <div><radio :title="$t('status.allStepsTrue')" v-model="allSteps" :id="true"></radio></div>
<div><radio :title="$t('status.allStepsFalse')" v-model="allSteps" :id="false"></radio></div> <div><radio :title="$t('status.allStepsFalse')" v-model="allSteps" :id="false"></radio></div>
<!--
<div>
<input type="radio" name="allSteps" :value="true" v-model="allSteps" id="allStepsTrue">
<label class="label-inline" for="allStepsTrue">{{ $t('status.allStepsTrue') }}</label>
</div>
<div>
<input type="radio" name="allSteps" :value="false" v-model="allSteps" id="allStepsFalse">
<label class="label-inline" for="allStepsFalse">{{ $t('status.allStepsFalse') }}</label>
</div>
-->
<div class="sliders"> <div class="sliders">
<div class="step" v-if="allSteps"> <div class="step" v-if="allSteps">
<span class="value">{{ Math.floor(allStepsValue / 255 * 100) }}%</span> <span class="value">{{ Math.floor(allStepsValue / 255 * 100) }}%</span>
@ -271,7 +259,7 @@
System tab System tab
--> -->
<form @submit.prevent="uploadFirmware" id="system"> <form @submit.prevent="uploadFirmware">
<h3>{{ $t('system.firmwareTitle') }}</h3> <h3>{{ $t('system.firmwareTitle') }}</h3>
<input type="file" id="firmwareFile"> <input type="file" id="firmwareFile">
@ -285,7 +273,7 @@
</div> </div>
</form> </form>
<form @submit.prevent="applySystem" id="system"> <form @submit.prevent="applySystem">
<h3>{{ $t('system.ntpTitle') }}</h3> <h3>{{ $t('system.ntpTitle') }}</h3>
<div class="warning" v-if="!wifiStatus.station.enabled || wifiStatus.station.status != 3"> <div class="warning" v-if="!wifiStatus.station.enabled || wifiStatus.station.status != 3">
@ -359,7 +347,6 @@
</div> </div>
<script language="javascript"> <script language="javascript">
console.log('Initializing...');
startApp(); startApp();
</script> </script>
</body> </body>