diff --git a/.vscode/settings.json b/.vscode/settings.json index 5ec8246c..b9939f84 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "idf.flashType": "UART", - "idf.portWin": "COM30", + "idf.portWin": "COM36", "idf.adapterTargetName": "esp32s3", "idf.openOcdConfigs": [ "interface/ftdi/esp32_devkitj_v1.cfg", diff --git a/main/http_server/axe-os/src/app/components/edit/edit.component.ts b/main/http_server/axe-os/src/app/components/edit/edit.component.ts index d9a1594b..aab0cfcd 100644 --- a/main/http_server/axe-os/src/app/components/edit/edit.component.ts +++ b/main/http_server/axe-os/src/app/components/edit/edit.component.ts @@ -37,8 +37,6 @@ export class EditComponent implements OnInit { window.addEventListener('resize', this.checkDevTools); this.checkDevTools(); - - } ngOnInit(): void { this.systemService.getInfo(this.uri) diff --git a/main/http_server/axe-os/src/app/components/swarm/swarm.component.html b/main/http_server/axe-os/src/app/components/swarm/swarm.component.html index d26064ac..2bc14865 100644 --- a/main/http_server/axe-os/src/app/components/swarm/swarm.component.html +++ b/main/http_server/axe-os/src/app/components/swarm/swarm.component.html @@ -2,6 +2,7 @@ +
diff --git a/main/http_server/axe-os/src/app/components/swarm/swarm.component.ts b/main/http_server/axe-os/src/app/components/swarm/swarm.component.ts index 8a3df9d9..25b78a6d 100644 --- a/main/http_server/axe-os/src/app/components/swarm/swarm.component.ts +++ b/main/http_server/axe-os/src/app/components/swarm/swarm.component.ts @@ -1,7 +1,7 @@ import { Component } from '@angular/core'; import { FormBuilder, FormGroup } from '@angular/forms'; import { ToastrService } from 'ngx-toastr'; -import { BehaviorSubject, catchError, map, Observable, of, startWith, switchMap } from 'rxjs'; +import { BehaviorSubject, catchError, forkJoin, map, Observable, of, startWith, switchMap } from 'rxjs'; import { SystemService } from 'src/app/services/system.service'; @Component({ @@ -58,11 +58,19 @@ export class SwarmComponent { public add() { - const ip = this.form.value.ip; + const newIp = this.form.value.ip; this.systemService.getSwarmInfo().pipe( - switchMap((swarm: any) => { - return this.systemService.updateSwarm([{ ip }, ...swarm]) + switchMap((swarmInfo) => { + + const swarmUpdate = swarmInfo.map(({ ip }) => { + return this.systemService.updateSwarm('http://' + ip, [{ ip: newIp }, ...swarmInfo]) + }); + + const newAxeOs = this.systemService.updateSwarm('http://' + newIp, [{ ip: newIp }, ...swarmInfo]) + + return forkJoin([newAxeOs, ...swarmUpdate]); + }) ).subscribe({ next: () => { @@ -95,10 +103,19 @@ export class SwarmComponent { this.toastr.success('Success!', 'Bitaxe restarted'); } - public remove(axe: any) { + public remove(axeOs: any) { this.systemService.getSwarmInfo().pipe( - switchMap((swarm: any) => { - return this.systemService.updateSwarm(swarm.filter((s: any) => s.ip != axe.ip)) + switchMap((swarmInfo) => { + + const newSwarm = swarmInfo.filter((s: any) => s.ip != axeOs.ip); + + const swarmUpdate = newSwarm.map(({ ip }) => { + return this.systemService.updateSwarm('http://' + ip, newSwarm) + }); + + const removedAxeOs = this.systemService.updateSwarm('http://' + axeOs.ip, []); + + return forkJoin([removedAxeOs, ...swarmUpdate]); }) ).subscribe({ next: () => { diff --git a/main/http_server/axe-os/src/app/services/system.service.ts b/main/http_server/axe-os/src/app/services/system.service.ts index df27e04d..a5a96f69 100644 --- a/main/http_server/axe-os/src/app/services/system.service.ts +++ b/main/http_server/axe-os/src/app/services/system.service.ts @@ -104,7 +104,7 @@ export class SystemService { return this.httpClient.get(`/api/swarm/info`) as Observable<{ ip: string }[]>; } - public updateSwarm(swarmConfig: any) { - return this.httpClient.patch(`/api/swarm`, swarmConfig); + public updateSwarm(uri: string = '', swarmConfig: any) { + return this.httpClient.patch(`${uri}/api/swarm`, swarmConfig); } } diff --git a/main/http_server/http_server.c b/main/http_server/http_server.c index 2665cca1..4634c7cf 100644 --- a/main/http_server/http_server.c +++ b/main/http_server/http_server.c @@ -109,6 +109,15 @@ static esp_err_t set_content_type_from_file(httpd_req_t * req, const char * file } return httpd_resp_set_type(req, type); } +static esp_err_t set_cors_headers(httpd_req_t * req) +{ + + return httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*") == ESP_OK && + httpd_resp_set_hdr(req, "Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS") == ESP_OK && + httpd_resp_set_hdr(req, "Access-Control-Allow-Headers", "Content-Type") == ESP_OK + ? ESP_OK + : ESP_FAIL; +} /* Send HTTP response with the contents of the requested file */ static esp_err_t rest_common_get_handler(httpd_req_t * req) @@ -174,6 +183,12 @@ static esp_err_t rest_common_get_handler(httpd_req_t * req) static esp_err_t PATCH_update_swarm(httpd_req_t * req) { + // Set CORS headers + if (set_cors_headers(req) != ESP_OK) { + httpd_resp_send_500(req); + return ESP_FAIL; + } + int total_len = req->content_len; int cur_len = 0; char * buf = ((rest_server_context_t *) (req->user_ctx))->scratch; @@ -199,15 +214,6 @@ static esp_err_t PATCH_update_swarm(httpd_req_t * req) return ESP_OK; } -static esp_err_t set_cors_headers(httpd_req_t * req) -{ - return httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*") == ESP_OK && - httpd_resp_set_hdr(req, "Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS") == ESP_OK && - httpd_resp_set_hdr(req, "Access-Control-Allow-Headers", "Content-Type") == ESP_OK - ? ESP_OK - : ESP_FAIL; -} - static esp_err_t handle_options_request(httpd_req_t * req) { // Set CORS headers for OPTIONS request @@ -534,6 +540,14 @@ esp_err_t start_rest_server(void * pvParameters) .uri = "/api/swarm", .method = HTTP_PATCH, .handler = PATCH_update_swarm, .user_ctx = rest_context}; httpd_register_uri_handler(server, &update_swarm_uri); + httpd_uri_t swarm_options_uri = { + .uri = "/api/swarm", + .method = HTTP_OPTIONS, + .handler = handle_options_request, + .user_ctx = NULL, + }; + httpd_register_uri_handler(server, &swarm_options_uri); + httpd_uri_t system_restart_uri = { .uri = "/api/system/restart", .method = HTTP_POST, .handler = POST_restart, .user_ctx = rest_context}; httpd_register_uri_handler(server, &system_restart_uri); @@ -542,13 +556,13 @@ esp_err_t start_rest_server(void * pvParameters) .uri = "/api/system", .method = HTTP_PATCH, .handler = PATCH_update_settings, .user_ctx = rest_context}; httpd_register_uri_handler(server, &update_system_settings_uri); - httpd_uri_t options_uri = { + httpd_uri_t system_options_uri = { .uri = "/api/system", .method = HTTP_OPTIONS, .handler = handle_options_request, .user_ctx = NULL, }; - httpd_register_uri_handler(server, &options_uri); + httpd_register_uri_handler(server, &system_options_uri); httpd_uri_t update_post_ota_firmware = { .uri = "/api/system/OTA", .method = HTTP_POST, .handler = POST_OTA_update, .user_ctx = NULL};