Commit 2027d77b authored by El Mghazli Yacine's avatar El Mghazli Yacine

APIv2.1

parent 6e3f09a3
...@@ -3,21 +3,22 @@ import { Injectable } from '@angular/core'; ...@@ -3,21 +3,22 @@ import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment'; import { environment } from 'src/environments/environment';
export enum IOptionType { export interface IVariable {
subcommand = "subcommand", name: string;
variable = "variable" value: string;
type: IArgType;
modifiable: boolean; //set command ?
} }
export interface IOption {
type: IOptionType; export enum IArgType {
boolean = "boolean",
list = "list",
range = "range",
string = "string"
} }
export interface IVariable extends IOption { export interface ICommand {
name: string; name: string;
value: string;
modifiable: boolean;
}
export interface ISubCommands extends IOption {
name: string[];
} }
const route = '/oaisoftmodem'; const route = '/oaisoftmodem';
...@@ -28,14 +29,16 @@ const route = '/oaisoftmodem'; ...@@ -28,14 +29,16 @@ const route = '/oaisoftmodem';
export class CommandsApi { export class CommandsApi {
constructor(private httpClient: HttpClient) { } constructor(private httpClient: HttpClient) { }
public readStatus$ = () => this.httpClient.get<IVariable[]>(environment.backend + route + '/status/'); public readVariables$ = () => this.httpClient.get<IVariable[]>(environment.backend + route + '/variables/');
public readCommands$ = () => this.httpClient.get<ICommand[]>(environment.backend + route + '/commands/');
public readModules$ = () => this.httpClient.get<string[]>(environment.backend + route + '/modules/'); public readModuleVariables$ = (moduleName: string) => this.httpClient.get<IVariable[]>(environment.backend + route + '/variables/' + moduleName);
public getOptions$ = (cmdName: string) => this.httpClient.get<IOption[]>(environment.backend + route + '/module/' + cmdName); public readModuleCommands$ = (moduleName: string) => this.httpClient.get<ICommand[]>(environment.backend + route + '/commands/' + moduleName);
public runCommand$ = (cmdName: string) => this.httpClient.post<string>(environment.backend + route + '/command/' + cmdName, {}); public runCommand$ = (command: ICommand) => this.httpClient.post<string>(environment.backend + route + '/run/', command);
public setVariable$ = (variable: IVariable) => this.httpClient.post<string>(environment.backend + route + '/variable/' + variable.name, variable.value); public setVariable$ = (variable: IVariable) => this.httpClient.post<string>(environment.backend + route + '/set/', variable);
} }
<div *ngIf="status$ | async as statusVariables"> <div *ngIf="vars$ | async as vars">
<h1 mat-dialog-title>Status</h1> <h1 mat-dialog-title>Variables</h1>
<div *ngFor="let status of statusVariables"> <div *ngFor="let variable of vars">
<mat-form-field> <mat-form-field>
<mat-label>{{ status.nameFC.value }}</mat-label> <mat-label>{{ variable.nameFC.value }}</mat-label>
<input input matInput [formControl]="status.valueFC" /> <input input matInput [formControl]="variable.valueFC" />
</mat-form-field> </mat-form-field>
<button mat-raised-button color="primary" [disabled]="!status.modifiableFC.value" (click)="onVarSubmit(status)"> <button mat-raised-button color="primary" [disabled]="!variable.modifiableFC.value" (click)="onVarSubmit(variable)">
set set
</button> </button>
</div> </div>
</div> </div>
<div *ngIf="cmds$ | async as cmds" fxLaypout="row"> <div *ngIf="cmds$ | async as cmds" fxLaypout="row">
<h1 mat-dialog-title>Commands</h1> <h1 mat-dialog-title>Menus</h1>
<mat-form-field> <mat-form-field>
<mat-label>Command</mat-label> <mat-label>Command</mat-label>
<mat-select [formControl]="selectedCmd" [value]="selectedCmd?.value" (selectionChange)="onSelect(selectedCmd)"> <mat-select [formControl]="selectedCmd!.nameFC" [value]="selectedCmd!.nameFC.value"
<mat-option *ngFor="let cmd of cmds" [value]="cmd">{{ cmd }} (selectionChange)="onCmdSelect(selectedCmd!)">
<mat-option *ngFor="let cmd of cmds" [value]="cmd">{{ cmd.nameFC.value }}
</mat-option> </mat-option>
</mat-select> </mat-select>
</mat-form-field> </mat-form-field>
<div *ngIf="variables$ | async as variables" fxLaypout="column"> <div *ngIf="subvars$ | async as subvars" fxLaypout="column">
<h1 mat-dialog-title>Variables</h1> <h1 mat-dialog-title>Sub-Variables</h1>
<div *ngFor="let variable of variables"> <div *ngFor="let variable of subvars">
<mat-form-field> <mat-form-field>
<mat-label>{{ variable.nameFC.value }}</mat-label> <mat-label>{{ variable.nameFC.value }}</mat-label>
<input input matInput [formControl]="variable.valueFC" /> <input input matInput [formControl]="variable.valueFC" />
</mat-form-field> </mat-form-field>
<button mat-raised-button color="primary" [disabled]="!variable.modifiableFC.value" <button mat-raised-button color="primary" [disabled]="!variable.modifiableFC.value"
(click)="onVarSubmit(variable)"> (click)="onVarSubmit(variable)">
set submit
</button> </button>
</div> </div>
</div> </div>
<div *ngIf="subcmds$ | async as subcmds" fxLaypout="column" fxLayoutGap="10px"> <div *ngIf="subcmds$ | async as subcmds" fxLaypout="column" fxLayoutGap="10px">
<h1 mat-dialog-title>Sub-commands</h1> <h1 mat-dialog-title>Sub-commands</h1>
<button *ngFor="let control of subcmds.namesFA.controls" mat-raised-button color="primary" <mat-form-field>
(click)="onCmdSubmit(control.value)"> <mat-label>Command</mat-label>
{{ control.value }} <mat-select [formControl]="selectedSubCmd!.nameFC" [value]="selectedSubCmd!.nameFC.value"
</button> (selectionChange)="onSubCmdSelect(selectedSubCmd!)">
<mat-option *ngFor="let cmd of subcmds" [value]="cmd">{{ cmd.nameFC.value }}
</mat-option>
</mat-select>
</mat-form-field>
<div *ngIf="args$ | async as args" fxLaypout="column">
<h1 mat-dialog-title>Sub-Variables</h1>
<div *ngFor="let variable of args">
<mat-form-field>
<mat-label>{{ variable.nameFC.value }}</mat-label>
<input input matInput [formControl]="variable.valueFC" />
</mat-form-field>
<button mat-raised-button color="primary" [disabled]="!variable.modifiableFC.value"
(click)="onVarSubmit(variable)">
submit
</button>
</div>
</div>
</div> </div>
</div> </div>
\ No newline at end of file
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { FormControl } from '@angular/forms'; import { FormControl } from '@angular/forms';
import { BehaviorSubject } from 'rxjs';
import { Observable } from 'rxjs/internal/Observable'; import { Observable } from 'rxjs/internal/Observable';
import { map } from 'rxjs/internal/operators/map'; import { map } from 'rxjs/internal/operators/map';
import { CommandsApi, IOptionType, ISubCommands, IVariable } from 'src/app/api/commands.api'; import { CommandsApi, IArgType } from 'src/app/api/commands.api';
import { SubCmdCtrl } from 'src/app/controls/cmds.control'; import { CmdCtrl } from 'src/app/controls/cmd.control';
import { VariableCtrl } from 'src/app/controls/variable.control'; import { VariableCtrl as VarCtrl } from 'src/app/controls/var.control';
import { LoadingService } from 'src/app/services/loading.service'; import { LoadingService } from 'src/app/services/loading.service';
...@@ -16,41 +15,59 @@ import { LoadingService } from 'src/app/services/loading.service'; ...@@ -16,41 +15,59 @@ import { LoadingService } from 'src/app/services/loading.service';
}) })
export class CommandsComponent { export class CommandsComponent {
IOptionType = IOptionType; IOptionType = IArgType;
status$: Observable<VariableCtrl[]> vars$: Observable<VarCtrl[]>
cmds$: Observable<string[]> cmds$: Observable<CmdCtrl[]>
variables$ = new BehaviorSubject<VariableCtrl[]>([]) selectedCmd?: CmdCtrl
subcmds$: BehaviorSubject<SubCmdCtrl> | undefined selectedVar?: VarCtrl
selectedCmd = new FormControl() subvars$?: Observable<VarCtrl[]>
selectedSubCmd = new FormControl() subcmds$?: Observable<CmdCtrl[]>
selectedVariable?: VariableCtrl selectedSubCmd?: CmdCtrl
selectedSubVar?: VarCtrl
args$?: Observable<VarCtrl[]>
selectedArg?: VarCtrl
constructor( constructor(
public commandsApi: CommandsApi, public commandsApi: CommandsApi,
public loadingService: LoadingService, public loadingService: LoadingService,
) { ) {
this.status$ = this.commandsApi.readStatus$().pipe( this.vars$ = this.commandsApi.readVariables$().pipe(
map((ivars) => ivars.map(ivar => new VariableCtrl(ivar))) map((ivars) => ivars.map(ivar => new VarCtrl(ivar)))
); );
this.cmds$ = this.commandsApi.readModules$(); this.cmds$ = this.commandsApi.readCommands$().pipe(
map((ivars) => ivars.map(ivar => new CmdCtrl(ivar)))
);
} }
onCmdSubmit(subCmdName: string) { onCmdSubmit(control: CmdCtrl) {
this.commandsApi.runCommand$(subCmdName).subscribe(); this.commandsApi.runCommand$(control.api()).subscribe();
}
onCmdSelect(control: CmdCtrl) {
this.subcmds$ = this.commandsApi.readModuleCommands$(control.nameFC.value).pipe(
map(cmds => cmds.map(cmd => new CmdCtrl(cmd))),
// tap(cmds => [this.selectedSubCmd] = cmds)
)
this.subvars$ = this.commandsApi.readModuleVariables$(control.nameFC.value).pipe(
map(vars => vars.map(v => new VarCtrl(v))),
// tap(vars => [this.selectedSubVar] = vars)
)
} }
onVarSubmit(varCtrl: VariableCtrl) { onSubCmdSelect(control: CmdCtrl) {
this.commandsApi.setVariable$(varCtrl.api()).subscribe(); this.args$ = this.commandsApi.readModuleVariables$(control.nameFC.value).pipe(
map(vars => vars.map(v => new VarCtrl(v))),
// tap(vars => [this.selectedSubVar] = vars)
)
} }
onSelect(cmdNameFC: FormControl) { onVarSubmit(control: VarCtrl) {
this.commandsApi.getOptions$(cmdNameFC.value).subscribe(opts => { control.nameFC = new FormControl(`${this.selectedCmd?.nameFC.value} ${this.selectedSubCmd?.nameFC.value} ${control.nameFC.value}`.trim())
this.variables$.next(opts.filter(iopt => iopt.type === IOptionType.variable).map(iopt => new VariableCtrl(iopt as IVariable))) this.commandsApi.setVariable$(control.api()).subscribe();
const [subCmds] = opts.filter(iopt => iopt.type === IOptionType.subcommand)
this.subcmds$ = new BehaviorSubject<SubCmdCtrl>(new SubCmdCtrl(subCmds as ISubCommands))
})
} }
} }
import { FormControl, FormGroup } from '@angular/forms';
import { ICommand } from '../api/commands.api';
const enum CmdFCN {
name = 'name',
}
export class CmdCtrl extends FormGroup {
constructor(cmd: ICommand) {
super({});
this.addControl(CmdFCN.name, new FormControl(cmd.name));
}
api() {
const doc: ICommand = {
name: this.nameFC.value
};
return doc;
}
get nameFC() {
return this.get(CmdFCN.name) as FormControl;
}
set nameFC(fc: FormControl) {
this.setControl(CmdFCN.name, fc);
}
}
import { FormArray, FormControl } from '@angular/forms';
import { ISubCommands } from '../api/commands.api';
import { OptionsCtrl } from './options.control';
const enum SubCmdsFCN {
names = 'names',
}
export class SubCmdCtrl extends OptionsCtrl {
constructor(isubs: ISubCommands) {
super(isubs);
this.addControl(SubCmdsFCN.names, new FormArray(isubs.name.map((name) => new FormControl(name))));
}
api() {
const doc: ISubCommands = {
type: this.type,
name: this.namesFA.value
};
return doc;
}
get namesFA() {
return this.get(SubCmdsFCN.names) as FormArray;
}
set namesFA(fa: FormArray) {
this.setControl(SubCmdsFCN.names, fa);
}
}
import { FormGroup } from "@angular/forms";
import { IOption, IOptionType } from "../api/commands.api";
export class OptionsCtrl extends FormGroup {
type: IOptionType
constructor(ioption: IOption) {
super({});
this.type = ioption.type
}
}
\ No newline at end of file
import { FormControl } from '@angular/forms'; import { FormControl, FormGroup } from '@angular/forms';
import { IOptionType, IVariable } from '../api/commands.api'; import { IVariable } from '../api/commands.api';
import { OptionsCtrl } from './options.control';
const enum VariablesFCN { const enum VariablesFCN {
name = 'name', name = 'name',
value = "value", value = "value",
type = "type",
modifiable = "modifiable" modifiable = "modifiable"
} }
export class VariableCtrl extends OptionsCtrl { export class VariableCtrl extends FormGroup {
constructor(ivar: IVariable) { constructor(ivar: IVariable) {
super(ivar); super({});
this.addControl(VariablesFCN.name, new FormControl(ivar.name)); this.addControl(VariablesFCN.name, new FormControl(ivar.name));
this.addControl(VariablesFCN.value, new FormControl(ivar.value)); this.addControl(VariablesFCN.value, new FormControl(ivar.value));
this.addControl(VariablesFCN.type, new FormControl(ivar.type));
this.addControl(VariablesFCN.modifiable, new FormControl(ivar.modifiable)); this.addControl(VariablesFCN.modifiable, new FormControl(ivar.modifiable));
} }
api() { api() {
const doc: IVariable = { const doc: IVariable = {
type: this.type,
name: this.nameFC.value, name: this.nameFC.value,
value: this.valueFC.value, value: this.valueFC.value,
type: this.typeFC.value,
modifiable: this.modifiableFC.value modifiable: this.modifiableFC.value
}; };
...@@ -48,6 +48,14 @@ export class VariableCtrl extends OptionsCtrl { ...@@ -48,6 +48,14 @@ export class VariableCtrl extends OptionsCtrl {
this.setControl(VariablesFCN.value, control); this.setControl(VariablesFCN.value, control);
} }
get typeFC() {
return this.get(VariablesFCN.value) as FormControl;
}
set typeFC(control: FormControl) {
this.setControl(VariablesFCN.value, control);
}
get modifiableFC() { get modifiableFC() {
return this.get(VariablesFCN.modifiable) as FormControl; return this.get(VariablesFCN.modifiable) as FormControl;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment