Skip to content

Commit

Permalink
Implement profiles on MCU side (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealKasumi authored Feb 19, 2023
1 parent 1b2fd03 commit 0befc7f
Show file tree
Hide file tree
Showing 26 changed files with 1,714 additions and 312 deletions.
324 changes: 268 additions & 56 deletions documentation/api/OpenApi.yaml

Large diffs are not rendered by default.

169 changes: 167 additions & 2 deletions documentation/api/TesLight API.postman_collection.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"info": {
"name": "TesLight API",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "12963851"
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
Expand Down Expand Up @@ -651,6 +650,172 @@
"response": []
}
]
},
{
"name": "Profile",
"item": [
{
"name": "Get Active Profile",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://{{TESLIGHTIP}}/api/config/profile/active",
"protocol": "http",
"host": [
"{{TESLIGHTIP}}"
],
"path": [
"api",
"config",
"profile",
"active"
]
}
},
"response": []
},
{
"name": "Set Active Profile",
"request": {
"method": "PATCH",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"profile\": {\r\n \"name\": \"New Profile\"\r\n }\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://{{TESLIGHTIP}}/api/config/profile/active",
"protocol": "http",
"host": [
"{{TESLIGHTIP}}"
],
"path": [
"api",
"config",
"profile",
"active"
]
}
},
"response": []
},
{
"name": "Get Profile List",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://{{TESLIGHTIP}}/api/config/profile",
"protocol": "http",
"host": [
"{{TESLIGHTIP}}"
],
"path": [
"api",
"config",
"profile"
]
}
},
"response": []
},
{
"name": "Create Profile",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"profile\": {\r\n \"name\": \"New Profile\"\r\n }\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://{{TESLIGHTIP}}/api/config/profile",
"protocol": "http",
"host": [
"{{TESLIGHTIP}}"
],
"path": [
"api",
"config",
"profile"
]
}
},
"response": []
},
{
"name": "Clone Profile",
"request": {
"method": "PUT",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"profile\": {\r\n \"name\": \"Clone Profile\"\r\n }\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://{{TESLIGHTIP}}/api/config/profile?source=Default Profile",
"protocol": "http",
"host": [
"{{TESLIGHTIP}}"
],
"path": [
"api",
"config",
"profile"
],
"query": [
{
"key": "source",
"value": "Default Profile"
}
]
}
},
"response": []
},
{
"name": "Delete Profile",
"request": {
"method": "DELETE",
"header": [],
"url": {
"raw": "http://{{TESLIGHTIP}}/api/config/profile?name=Clone Profile",
"protocol": "http",
"host": [
"{{TESLIGHTIP}}"
],
"path": [
"api",
"config",
"profile"
],
"query": [
{
"key": "name",
"value": "Clone Profile"
}
]
}
},
"response": []
}
]
}
],
"event": [
Expand Down
10 changes: 5 additions & 5 deletions mcu/include/SystemInformation.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#define SYSTEM_INFORMATION_H

#include <stdint.h>
#include <cstring>
#include "Esp.h"
#include <WString.h>
#include <Esp.h>
#include "configuration/SystemConfiguration.h"

namespace TL
Expand All @@ -34,9 +34,9 @@ namespace TL
public:
struct SocInfo
{
char chipModel[32];
String chipModel;
uint8_t chipRevision;
char fwVersion[8];
String fwVersion;
uint8_t cpuCores;
uint32_t cpuClock;
uint32_t freeHeap;
Expand All @@ -48,7 +48,7 @@ namespace TL

struct HardwareInformation
{
char hwVersion[8];
String hwVersion;
uint8_t regulatorCount;
float regulatorVoltage;
float regulatorCurrentLimit;
Expand Down
1 change: 1 addition & 0 deletions mcu/include/TesLight.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "wifi/WiFiManager.h"
#include "server/WebServerManager.h"
#include "server/ConnectionTestEndpoint.h"
#include "server/ProfileEndpoint.h"
#include "server/SystemInformationEndpoint.h"
#include "server/SystemConfigurationEndpoint.h"
#include "server/LedConfigurationEndpoint.h"
Expand Down
52 changes: 40 additions & 12 deletions mcu/include/configuration/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
#ifndef CONFIGURATION_H
#define CONFIGURATION_H

#include <stdint.h>
#include <vector>
#include <tuple>
#include <Arduino.h>
#include <WString.h>
#include <FS.h>

#include "configuration/SystemConfiguration.h"
Expand All @@ -37,12 +39,17 @@ namespace TL
public:
enum class Error
{
OK, // No error
ERROR_FILE_OPEN, // Failed to open file
ERROR_FILE_READ, // Failed to read file
ERROR_FILE_WRITE, // Failed to write file
ERROR_FILE_VERSION, // Unmatching file version
ERROR_FILE_HASH // Unmatching file hash
OK, // No error
ERROR_PROFILE_NOT_FOUND, // No profile with the given name was found
ERROR_PROFILE_NAME_EXISTS, // The profile name is already in use
ERROR_PROFILE_IS_ACTIVE, // The profile is currently used and can not be deleted
ERROR_TOO_MANY_PROFILES, // Too many profiles to load
ERROR_OUT_OF_BOUNDS, // The index is out of bounds
ERROR_FILE_OPEN, // Failed to open file
ERROR_FILE_READ, // Failed to read file
ERROR_FILE_WRITE, // Failed to write file
ERROR_FILE_VERSION, // Unmatching file version
ERROR_FILE_HASH // Unmatching file hash
};

struct SystemConfig
Expand Down Expand Up @@ -123,15 +130,34 @@ namespace TL
bool expertMode;
};

struct Profile
{
String name; // Name of the profile
TL::Configuration::SystemConfig systemConfig; // System configuration of the profile
TL::Configuration::LedConfig ledConfig[LED_NUM_ZONES]; // LED configuration of the profile
TL::Configuration::UIConfiguration uiConfiguration; // UI configuration of the profile
};

static void begin(FS *fileSystem, const String fileName);
static void end();
static bool isInitialized();

static size_t getProfileCount();
static TL::Configuration::Error getProfileNameByIndex(const size_t profileIndex, String &profileName);
static TL::Configuration::Error getProfile(const String &profileName, TL::Configuration::Profile &profile);
static TL::Configuration::Error createProfile(const String &profileName);
static TL::Configuration::Error cloneProfile(const String &sourceName, const String &destinationName);
static TL::Configuration::Error renameProfile(const String &profileName, const String &newProfileName);
static TL::Configuration::Error deleteProfile(const String &profileName);

static String getActiveProfile();
static TL::Configuration::Error setActiveProfile(const String &profileName);

static TL::Configuration::SystemConfig getSystemConfig();
static void setSystemConfig(TL::Configuration::SystemConfig &systemConfig);

static TL::Configuration::LedConfig getLedConfig(const uint8_t index);
static void setLedConfig(const TL::Configuration::LedConfig &ledConfig, const uint8_t index);
static TL::Configuration::Error getLedConfig(const uint8_t zoneIndex, TL::Configuration::LedConfig &ledConfig);
static TL::Configuration::Error setLedConfig(const uint8_t zoneIndex, const TL::Configuration::LedConfig &ledConfig);

static TL::Configuration::WiFiConfig getWiFiConfig();
static void setWiFiConfig(TL::Configuration::WiFiConfig &wifiConfig);
Expand All @@ -157,12 +183,14 @@ namespace TL
static String fileName;
static uint16_t configurationVersion;

static TL::Configuration::SystemConfig systemConfig;
static TL::Configuration::LedConfig ledConfig[LED_NUM_ZONES];
static size_t activeProfile;
static std::vector<TL::Configuration::Profile> profiles;
static TL::Configuration::WiFiConfig wifiConfig;
static TL::Configuration::MotionSensorCalibration motionSensorCalibration;
static TL::Configuration::AudioUnitConfig audioUnitConfig;
static TL::Configuration::UIConfiguration uiConfiguration;

static TL::Configuration::Error loadProfileDefaults(const size_t profileIndex);
static TL::Configuration::Error getProfileIndexByName(const String &profileName, size_t &profileIndex);

static uint16_t getSimpleHash();
static uint16_t getSimpleStringHash(const String input);
Expand Down
3 changes: 2 additions & 1 deletion mcu/include/configuration/SystemConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@
#define LOG_DEFAULT_LEVEL 1 // Default log level

// Configuration of the runtime configuration
#define CONFIGURATION_FILE_VERSION 13 // Version of the configuration file
#define CONFIGURATION_FILE_VERSION 14 // Version of the configuration file
#define CONFIGURATION_FILE_NAME "/config.tli" // File name of the configuration file
#define CONFIGURATION_MAX_PROFILES 50 // Maximum number of profiles

// LED and effect configuration
#define LED_NUM_ZONES 8 // Number of LED zones
Expand Down
1 change: 1 addition & 0 deletions mcu/include/hardware/Fan.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#ifndef FAN_CONTROLLER_H
#define FAN_CONTROLLER_H

#include <stdint.h>
#include <Arduino.h>
#include "configuration/Configuration.h"
#include "sensor/TemperatureSensor.h"
Expand Down
4 changes: 3 additions & 1 deletion mcu/include/logging/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#ifndef LOGGER_H
#define LOGGER_H

#include <Arduino.h>
#include <stdint.h>
#include <HardwareSerial.h>
#include <WString.h>
#include <FS.h>

#define SOURCE_LOCATION __FILE__, __func__, __LINE__
Expand Down
52 changes: 52 additions & 0 deletions mcu/include/server/ProfileEndpoint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @file ProfileEndpoint.h
* @author TheRealKasumi
* @brief Contains a REST endpoint to manage user profiles.
*
* @copyright Copyright (c) 2022-2023 TheRealKasumi
*
* This project, including hardware and software, is provided "as is". There is no warranty
* of any kind, express or implied, including but not limited to the warranties of fitness
* for a particular purpose and noninfringement. TheRealKasumi (https://github.com/TheRealKasumi)
* is holding ownership of this project. You are free to use, modify, distribute and contribute
* to this project for private, non-commercial purposes. It is granted to include this hardware
* and software into private, non-commercial projects. However, the source code of any project,
* software and hardware that is including this project must be public and free to use for private
* persons. Any commercial use is hereby strictly prohibited without agreement from the owner.
* By contributing to the project, you agree that the ownership of your work is transferred to
* the project owner and that you lose any claim to your contribute work. This copyright and
* license note applies to all files of this project and must not be removed without agreement
* from the owner.
*
*/
#ifndef PROFILE_ENDPOINT_H
#define PROFILE_ENDPOINT_H

#include "server/RestEndpoint.h"
#include "configuration/Configuration.h"
#include "led/LedManager.h"
#include "logging/Logger.h"

namespace TL
{
class ProfileEndpoint : public RestEndpoint
{
public:
static void begin();

private:
ProfileEndpoint();

static void getActiveProfile();
static void patchActiveProfile();

static void getProfiles();
static void postProfile();
static void cloneProfile();
static void deleteProfile();

static bool validateProfileName(const String &profileName);
};
}

#endif
Loading

0 comments on commit 0befc7f

Please sign in to comment.