Skip to content

Commit c05991b

Browse files
committed
Module: support button, new rows, nested tables
Docs ==== - some changes UI == - MultiInput: add button - Array.svelte: add localDefinition - WIP - Module.svelte: remove Array bind value - WIP Server ===== - FileService: add TAG - Module: add TAG, UpdatedItem: replace index by getParendIndex/Name , compareRecursive: refactor to catch more cases - ModuleAnimations: add right fields to scripts array, refactored onUpdate
1 parent dcdee32 commit c05991b

File tree

17 files changed

+12936
-12905
lines changed

17 files changed

+12936
-12905
lines changed

docs/custom/module/animations.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
## Technical
1919

20-
* See [Modules](https://moonmodules.org/MoonLight/custom/modules/)
20+
* See [Modules](../modules/)
2121

2222
### Server
2323

24-
[Instances.h](https://github.com/MoonModules/MoonLight/blob/main/lib/framework/Instances.h) and [Instances.cpp](https://github.com/MoonModules/MoonLight/blob/main/lib/framework/Instances.cpp)
24+
[ModuleAnimations.h](/src/custom/ModuleAnimations.h)
2525

2626
### UI
2727

28-
[Instances.svelte](https://github.com/MoonModules/MoonLight/blob/main/interface/src/routes/system/status/Instances.svelte)
28+
Generated

docs/custom/module/demo.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
## Technical
88

9-
* See [Modules](https://moonmodules.org/MoonLight/custom/modules/)
9+
* See [Modules](../modules/)
1010

1111
### Server
1212

13-
[Instances.h](https://github.com/MoonModules/MoonLight/blob/main/lib/framework/Instances.h) and [Instances.cpp](https://github.com/MoonModules/MoonLight/blob/main/lib/framework/Instances.cpp)
13+
[ModuleDemo.h](/src/custom/ModuleDemo.h)
1414

1515
### UI
1616

17-
[Instances.svelte](https://github.com/MoonModules/MoonLight/blob/main/interface/src/routes/system/status/Instances.svelte)
17+
Generated

docs/custom/modules.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ See [Demo](https://moonmodules.org/MoonLight/custom/module/demo/) and [Animation
1717
* This name will be used to set up http rest api and webserver sockets
1818
* See [ModuleDemo.h](https://github.com/ewowi/MoonBase/blob/main/src/custom/ModuleDemo.h)
1919

20-
```
20+
```cpp
2121
class ModuleDemo : public Module
2222
{
2323
public:
@@ -26,7 +26,7 @@ ModuleDemo(PsychicHttpServer *server
2626
, ESP32SvelteKit *sveltekit
2727
, FilesService *filesService
2828
) : Module("demo", server, sveltekit, filesService) {
29-
ESP_LOGD("", "ModuleDemo::constructor");
29+
ESP_LOGD(TAG, "ModuleDemo::constructor");
3030
}
3131
}
3232
```
@@ -36,7 +36,7 @@ ModuleDemo(PsychicHttpServer *server
3636
* Generate the UI
3737
* Initialy create the module data
3838
39-
```
39+
```cpp
4040
void setupDefinition(JsonArray root) override{
4141
JsonObject property;
4242
JsonArray details;
@@ -62,24 +62,27 @@ void setupDefinition(JsonArray root) override{
6262
* struct UpdatedItem defines the update (parent property, name of property, value and index (in case of multiple records)
6363
* This is run in the httpd / webserver task. To run it in the main (application task use runInLoopTask - see [ModuleAnimations](https://github.com/ewowi/MoonBase/blob/main/src/custom/ModuleAnimations.h))
6464

65-
```
65+
```cpp
6666
void onUpdate(UpdatedItem updatedItem) override
6767
{
68-
if (updatedItem.name == "lightsOn" || updatedItem.name == "brightness") {
69-
ESP_LOGD("", "handle %s.%s[%d] %s", updatedItem.parent.c_str(), updatedItem.name.c_str(), updatedItem.index, updatedItem.value.as<String>());
70-
FastLED.setBrightness(_state.data["lightsOn"]?_state.data["brightness"]:0);
71-
} else if (updatedItem.parent == "nodes" && updatedItem.name == "animation") {
72-
ESP_LOGD("", "handle %s.%s[%d] %s", updatedItem.parent.c_str(), updatedItem.name.c_str(), updatedItem.index, _state.data["nodes"][updatedItem.index]["animation"].as<String>().c_str());
73-
animation = _state.data["nodes"][updatedItem.index]["animation"].as<String>();
74-
compileAndRun(animation);
75-
} else
76-
ESP_LOGD("", "no handle for %s.%s[%d] %s (%d %s)", updatedItem.parent.c_str(), updatedItem.name.c_str(), updatedItem.index, updatedItem.value.as<String>().c_str(), _state.data["driverOn"].as<bool>(), _state.data["nodes"][0]["animation"].as<String>());
77-
}
68+
void onUpdate(UpdatedItem updatedItem) override
69+
{
70+
if (updatedItem.name == "lightsOn" || updatedItem.name == "brightness") {
71+
ESP_LOGD(TAG, "handle %s.%s = %s", updatedItem.parent.c_str(), updatedItem.name.c_str(), updatedItem.value.as<String>());
72+
FastLED.setBrightness(_state.data["lightsOn"]?_state.data["brightness"]:0);
73+
} else if (updatedItem.getParentName() == "nodes" && updatedItem.name == "animation") {
74+
int index = updatedItem.getParentIndex();
75+
animation = _state.data["nodes"][index]["animation"].as<String>();
76+
ESP_LOGD(TAG, "handle %s.%s = %s", updatedItem.parent.c_str(), updatedItem.name.c_str(), animation.c_str());
77+
compileAndRun(animation);
78+
} else
79+
ESP_LOGD(TAG, "no handle for %s.%s = %s", updatedItem.parent.c_str(), updatedItem.name.c_str(), updatedItem.value.as<String>().c_str());
80+
}
7881
```
7982
8083
* Add the module in [main.cpp](https://github.com/ewowi/MoonBase/blob/main/src/main.cpp)
8184
82-
```
85+
```cpp
8386
ModuleDemo moduleDemo = ModuleDemo(&server, &esp32sveltekit, &filesService);
8487
...
8588
moduleDemo.begin();
@@ -89,7 +92,7 @@ moduleDemo.loop();
8992

9093
* Add the module in [menu.svelte](https://github.com/ewowi/MoonBase/blob/main/interface/src/routes/menu.svelte) (this will be automated in the future)
9194

92-
```
95+
```ts
9396
submenu: [
9497
{
9598
title: 'Module Demo',

docs/esp32sveltekit.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ hide:
77
# ESP32 SvelteKit - Create Amazing IoT Projects
88

99
<div style="flex">
10-
<img src="./media/Screenshot_light.png" style="height:480px">
11-
<img src="./media/Screenshot_mobile.png" style="height:480px">
10+
<img src="../media/Screenshot_light.png" style="height:480px">
11+
<img src="../media/Screenshot_mobile.png" style="height:480px">
1212
</div>
1313

1414
A simple and extensible framework for ESP32 based IoT projects with a feature-rich, beautiful, and responsive front-end build with [Sveltekit](https://kit.svelte.dev/), [TailwindCSS](https://tailwindcss.com/) and [DaisyUI](https://daisyui.com/). This is a project template to get you started in no time backed by a powerful back end service, an amazing front end served from the ESP32 and an easy to use build chain to get everything going.

docs/general/gettingstarted.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* Read the [ESP32 Sveltekit docs](https://moonmodules.org/MoonLight/eskIndex/) (Latest version (Svelte 5) here [ESP32 Sveltekit docs](https://theelims.github.io/ESP32-sveltekit/))
4848
* Read [Customizing Sveltekit](https://moonmodules.org/MoonLight/general/customizingsveltekit/)
4949
* UI dev: configure vite.config.ts, go to interface folder, npm install, npm run dev. A local webserver starts on localhost. UI changes will directly be shown via this webserver
50+
* Changes made to the UI are not always visible in the browser, issue with caching / max-age (WIP), clear the browser cache to see latest UI.
5051
* Want to make changes: fork the repo and submit pull requests
5152

5253
## MoonBase specific

interface/src/lib/components/custom/Array.svelte

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@
4141
4242
let lastIndex: number = -1;
4343
44-
console.log("Array property", property, data, definition, changeOnInput, data[property.name], value1, value2);
44+
let localDefinition: any = $state([]);
45+
46+
// console.log("Array property", property, data, definition, changeOnInput, data[property.name], value1, value2);
4547
for (let i = 0; i < definition.length; i++) {
4648
// console.log("addItem def", propertyName, property)
4749
if (property.name == definition[i].name) {
48-
console.log("def[i]", property.name, definition[i].n)
50+
localDefinition = definition[i].n;
51+
console.log("localDefinition", property.name, definition[i].n)
4952
}
5053
}
5154
@@ -66,10 +69,10 @@
6669
dataEditable = {};
6770
6871
//set properties with their defaults
69-
for (let i = 0; i < definition.length; i++) {
70-
let property = definition[i];
71-
// console.log("addItem def", propertyName, property)
72+
for (let i = 0; i < localDefinition.length; i++) {
73+
let property = localDefinition[i];
7274
if (property.name == propertyName) {
75+
console.log("addItem def", propertyName, property)
7376
for (let i=0; i < property.n.length; i++) {
7477
let propertyN = property.n[i];
7578
// console.log("propertyN", propertyN)
@@ -80,7 +83,7 @@
8083
}
8184
8285
function handleEdit(propertyName: string, index: number) {
83-
console.log("handleEdit", propertyName, index)
86+
console.log("handleEdit", propertyName, index, data[propertyName][index])
8487
if (lastIndex != index) {
8588
showEditor = true;
8689
lastIndex = index;
@@ -196,7 +199,7 @@
196199
</div>
197200
{:else if propertyN.type == "array"}
198201
<label for="{propertyN.name}">{propertyN.name}</label>
199-
<Array property={propertyN} bind:value1={data[propertyN.name]} value2={data} data={dataEditable} definition={definition} onChange={onChange} changeOnInput={changeOnInput}></Array>
202+
<Array property={propertyN} bind:value1={data[propertyN.name]} value2={data} data={dataEditable} definition={localDefinition} onChange={onChange} changeOnInput={changeOnInput}></Array>
200203
{/if}
201204
{/each}
202205
{/if}

interface/src/lib/components/custom/MultiInput.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
on:input={(event:any) => {if (changeOnInput) onChange(event)}}
136136
/>
137137
{:else if property.type == "button"}
138-
<button class="btn btn-primary" type="button" on:click={onChange}
138+
<button class="btn btn-primary" type="button" on:click={(event:any) => {if (value==null) value = 1; else value++; onChange(event)}}
139139
>{property.name}</button
140140
>
141141
{:else}

interface/src/routes/custom/module/Module.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
}
3636
});
3737
definition = await response.json();
38-
console.log("definition", definition)
38+
// console.log("definition", definition)
3939
} catch (error) {
4040
console.error('Error:', error);
4141
}
@@ -95,6 +95,7 @@
9595
function inputChanged() {
9696
if (modeWS) {
9797
let moduleName = page.url.searchParams.get('module')||'';
98+
console.log(moduleName, data);
9899
socket.sendEvent(moduleName, data)
99100
} else {
100101
changed = true;
@@ -146,7 +147,7 @@
146147
<MultiInput property={property} bind:value={data[property.name]} onChange={inputChanged} changeOnInput={!modeWS}></MultiInput>
147148
</div>
148149
{:else if property.type == "array"}
149-
<Array property={property} bind:value1={data[property.name]} value2={data[property.name]} data={data} definition={definition} onChange={inputChanged} changeOnInput={!modeWS}></Array>
150+
<Array property={property} value2={data[property.name]} data={data} definition={definition} onChange={inputChanged} changeOnInput={!modeWS}></Array>
150151
{/if}
151152
{/each}
152153
</div>

0 commit comments

Comments
 (0)