Skip to content

feat: support multiple device protocols list #833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,51 +102,70 @@ <h5 *ngIf="builtinProtocolTemplateProperties.length === 0 && builtinProtocolName
</ng-template>

<ng-template #customProtocolTemplate>
<div class="row border border-secondary p-2 rounded-lg mx-1">
<div class="col-md-2 col-lg-2 border-right border-secondary">
<div class="form-group">
<label i18n>Protocol Name</label>
<input type="text" class="form-control" [class.is-invalid]="!customProtocolName" [class.is-valid]="customProtocolName" name="customProtocolName" [(ngModel)]="customProtocolName" (ngModelChange)="onCustomProtocolNameChange()">
<div class="invalid-feedback">
<small i18n>the protocol name can't be empty!</small>
<p class="badge badge-secondary font-weight-bold">
<span class="fa fa-plus-circle mr-2"></span>
<span class="user-select-none" role="button" (click)="addCustomProtocol()" i18n>Add More Protocol</span>
</p>
<div class="card mb-3" *ngFor="let protocolEntry of customProtocols | keyvalue">
<div class="row card-body p-2 rounded-lg mx-1">
<!-- Protocol Name -->
<div class="col-md-2 col-lg-2 border-right border-secondary">
<div class="form-group">
<label i18n>Protocol Name</label>
<input type="text" class="form-control" [class.is-invalid]="!protocolEntry.key"
[class.is-valid]="protocolEntry.key" name="customProtocolName"
[(ngModel)]="protocolEntry.key" (focus)="onCustomProtocolNameFocus(protocolEntry.key)"
(blur)="onCustomProtocolNameBlur(protocolEntry.key, protocolEntry.value)">
</div>
<button class="btn btn-danger btn-sm" (click)="removeCustomProtocol(protocolEntry.key)">
<i class="fa fa-trash mr-1"></i>
<span i18n>Remove</span>
</button>
</div>
</div>
<div class="col-md-10 col-lg-10">
<div>
<p class="badge badge-secondary font-weight-bold">
<span class="fa fa-plus-circle mr-2"></span>
<span class="user-select-none" role="button" (click)="addCustomProtocolProperty()" i18n>Add Protocol Property</span>
</p>
</div>

<div *ngFor="let property of customProtocolPropertyBearer">
<form class="form-inline mb-1">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" i18n>PropertyName</span>
<!-- Protocol Properties -->
<div class="col-md-10 col-lg-10">
<div>
<p class="badge badge-secondary font-weight-bold" role="button"
(click)="addCustomProtocolProperty(protocolEntry.key)">
<span class="fa fa-plus-circle mr-2"></span>
<span class="user-select-none" i18n>Add Protocol Property</span>
</p>
</div>
<div *ngFor="let property of protocolEntry.value">
<form class="form-inline mb-1">
<!-- Property Name -->
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" i18n>PropertyName</span>
</div>
<input type="text" class="form-control" name="propertyName" [(ngModel)]="property.propertyName"
style="width: 300px;">
</div>
<input type="text" class="form-control" name="propertyName" [(ngModel)]="property.propertyName" style="width: 300px;">
</div>
<div class="form-group mx-2">
<i class="fa fa-long-arrow-right"></i>
</div>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" i18n>PropertyValue</span>
<!-- Property Value -->
<div class="form-group mx-2">
<i class="fa fa-long-arrow-right"></i>
</div>
<input type="text" class="form-control" name="PropertyValue" [(ngModel)]="property.propertyValue" style="width: 300px;">
</div>
<div class="input-group ml-2">
<button class="btn btn-danger btn-sm" (click)="removeCustomProtocolProperty(property)"> <i class="fa fa-trash"></i> </button>
</div>
</form>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" i18n>PropertyValue</span>
</div>
<input type="text" class="form-control" name="PropertyValue" [(ngModel)]="property.propertyValue"
style="width: 300px;">
</div>
<!-- Remove Button -->
<div class="input-group ml-2">
<button class="btn btn-danger btn-sm"
(click)="removeCustomProtocolProperty(protocolEntry.key, property)">
<i class="fa fa-trash"></i>
</button>
</div>
</form>
</div>
</div>

</div>
</div>
</ng-template>
</div>
</div>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class DeviceProtocolComponent implements OnInit {

customProtocolName?: string;
customProtocolPropertyBearer: ProtocolProperty[] = []
customProtocols: Map<string, ProtocolProperty[]> = new Map()

builtinProtocolName?: string
builtinProtocolTemplateSelected: any;
Expand Down Expand Up @@ -146,7 +147,25 @@ export class DeviceProtocolComponent implements OnInit {
this.validate()
}

onCustomProtocolNameChange() {
onCustomProtocolNameChange(protocolName: string, property: ProtocolProperty[]) {
this.customProtocols.set(protocolName, property as ProtocolProperty[])
// this.customProtocols.delete(this.customProtocolName as string)
this.customProtocolName = protocolName
this.validate()
}

onCustomProtocolNameFocus(protocolName: string) {
this.customProtocolName = protocolName
this.validate()
}

onCustomProtocolNameBlur(protocolName: string, property: ProtocolProperty[]) {
if (this.customProtocolName === protocolName) {
return
}
this.customProtocols.set(protocolName, property as ProtocolProperty[])
this.customProtocols.delete(this.customProtocolName as string)
this.customProtocolName = protocolName
this.validate()
}

Expand All @@ -155,29 +174,65 @@ export class DeviceProtocolComponent implements OnInit {
return
}

for (const [key, value] of Object.entries(this.deviceProtocols[this.customProtocolName])) {
this.customProtocolPropertyBearer.push({ propertyName: key, propertyValue: value as string })
for (const [protocolName, property] of Object.entries(this.deviceProtocols)) {
if (!this.customProtocols.has(protocolName)) {
this.customProtocols.set(protocolName, [])
}
for (const [key, value] of Object.entries(property)) {
this.customProtocols.get(protocolName)?.push({ propertyName: key, propertyValue: value as string })
}
}
}

addCustomProtocolProperty() {
this.customProtocolPropertyBearer.push({propertyName:'', propertyValue: ''})
addCustomProtocolProperty(protocolName: string) {
if (!this.customProtocols.has(protocolName)) {
this.customProtocols.set(protocolName, [])
}
this.customProtocols.forEach((p, pName) => {
if ( pName === protocolName) {
p.push({propertyName:'', propertyValue: ''})
}
})
}

removeCustomProtocolProperty(protocolName: string, property: ProtocolProperty) {
this.customProtocols.forEach((p, pName) => {
if ( pName === protocolName) {
p.splice(p.indexOf(property),1)
}
})
}

addCustomProtocol() {
this.customProtocols.set("", [])
this.customProtocolName = ""
this.validate()
}

removeCustomProtocolProperty(property: ProtocolProperty) {
this.customProtocolPropertyBearer.splice(this.customProtocolPropertyBearer.indexOf(property),1)
removeCustomProtocol(protocolName: string) {
if (this.customProtocols.has(protocolName)) {
this.customProtocols.delete(protocolName)
}
this.customProtocols.forEach((p, pName) => {
this.customProtocolName = pName
})
this.validate()
}

getDeviceProtocols(): protocol {
this.deviceProtocols = {}
if (this.protocolTemplateMode === this.TEMPLATE_BUILT_IN) {
this.deviceProtocols[this.builtinProtocolName as string] = Object.assign({}, this.builtinProtocolTemplateSelected)
} else {
let property: properties = {}
this.customProtocolPropertyBearer.forEach(p => {
property[p.propertyName] = p.propertyValue
this.customProtocols.forEach((p, pName) => {
if (pName !== "") {
let property: properties = {}
p.forEach(p => {
property[p.propertyName] = p.propertyValue
})
this.deviceProtocols[pName] = property
}
})
this.deviceProtocols[this.customProtocolName as string] = property
}
return this.deviceProtocols
}
Expand Down