Skip to content

Commit

Permalink
sync swarm
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-wilson committed Nov 26, 2023
1 parent 91a2242 commit 6cd5e2c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"idf.flashType": "UART",
"idf.portWin": "COM30",
"idf.portWin": "COM36",
"idf.adapterTargetName": "esp32s3",
"idf.openOcdConfigs": [
"interface/ftdi/esp32_devkitj_v1.cfg",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ export class EditComponent implements OnInit {
window.addEventListener('resize', this.checkDevTools);
this.checkDevTools();



}
ngOnInit(): void {
this.systemService.getInfo(this.uri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<label>AxeOS Device IP: </label>
<input formControlName="ip" type="text">
<button style="margin-left: 15px;" class="btn btn-primary" (click)="add()">Add</button>
<!-- <input type="checkbox" formControlName="sync"> Sync All -->
</form>
<div>
<button class="btn btn-primary" (click)="refresh()">Refresh</button>
Expand Down
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -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: () => {
Expand Down Expand Up @@ -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: () => {
Expand Down
4 changes: 2 additions & 2 deletions main/http_server/axe-os/src/app/services/system.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
36 changes: 25 additions & 11 deletions main/http_server/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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};
Expand Down

0 comments on commit 6cd5e2c

Please sign in to comment.