10 Commits

Author SHA1 Message Date
2a445b0bf5 .DS_Store gelöscht 2025-08-22 23:55:46 +02:00
Adrian Zuercher
775f079eb7 repo name change 2025-08-06 22:23:59 +02:00
Adrian Zuercher
6dffd1fad4 bug fix and improvment remove driver 2025-07-31 08:35:18 +02:00
Adrian Zuercher
c15710ddec add remove buses when empty 2025-07-30 17:14:46 +02:00
Adrian Zuercher
2924d95f4b add remove driver function 2025-07-30 12:12:18 +02:00
Adrian Zuercher
a39dcc06f4 add remove driver function 2025-07-30 11:55:30 +02:00
Adrian Zuercher
e720f0d2d3 change parameter to pointer 2025-07-29 22:17:13 +02:00
Adrian Zuercher
ce3ea4746b add missing return 2025-07-29 22:15:01 +02:00
Adrian Zuercher
0150a0d120 fix tipo 2025-07-29 14:56:37 +02:00
Adrian Zuercher
b9c6aa9d02 change map to slice 2025-07-29 14:20:54 +02:00
10 changed files with 136 additions and 40 deletions

BIN
.DS_Store vendored

Binary file not shown.

2
go.mod
View File

@@ -1,4 +1,4 @@
module github.com/tecamino/tecamino-json_data
module gitea.tecamino.com/paadi/tecamino-json_data
go 1.21.0

View File

@@ -4,7 +4,7 @@ import "slices"
// bus model
type Bus struct {
Name string `json:"name,omitempty"`
Name string `json:"name"`
Address []uint `json:"address,omitempty"` // address of bus
Topic *Topic `json:"topic,omitempty"` // address of bus
}
@@ -16,9 +16,7 @@ func (b *Bus) AddAddress(address ...uint) {
}
b.Address = append(b.Address, a)
slices.Sort(b.Address)
}
}
func (b *Bus) AddSubscription(sub ...string) {
@@ -34,3 +32,25 @@ func (b *Bus) AddPublish(pub ...string) {
}
b.Topic.AddPublish(pub...)
}
func (b *Bus) RemoveAddress(address ...uint) {
if len(b.Address) == 1 {
b.Address = []uint{}
return
}
for _, toDelete := range address {
for i, a := range b.Address {
if a == toDelete {
b.Address = slices.Delete(b.Address, i, i+1)
}
}
}
}
func (b *Bus) RemoveSubscription(sub ...string) {
b.Topic.RemoveSubscription(sub...)
}
func (b *Bus) RemovePublish(pub ...string) {
b.Topic.RemovePublish(pub...)
}

View File

@@ -2,42 +2,70 @@ package models
// driver model
type Driver struct {
Type string `json:"type"` // driver type name of driver in collection
Buses map[string]*Bus `json:"buses"` // name of driver bus
Type string `json:"type"` // driver type name of driver in collection
Buses []*Bus `json:"buses,omitempty"` // name of driver bus
}
func (d *Driver) AddNewBus(name string) *Bus {
if d.Buses == nil {
d.Buses = make(map[string]*Bus)
for _, b := range d.Buses {
if b.Name == name {
return b
}
}
if b, ok := d.Buses[name]; ok {
return b
}
d.Buses[name] = &Bus{Name: name}
return d.Buses[name]
b := &Bus{Name: name}
d.Buses = append(d.Buses, b)
return b
}
func (d *Driver) AddBuses(buses map[string]*Bus) {
if d.Buses == nil {
d.Buses = make(map[string]*Bus)
}
for key, newBus := range buses {
if currentBus, ok := d.Buses[key]; ok {
func (d *Driver) AddBuses(buses []*Bus) {
next:
for _, newBus := range buses {
for _, currentBus := range d.Buses {
if currentBus.Name != newBus.Name {
continue
}
currentBus.AddAddress(newBus.Address...)
if newBus.Topic == nil {
continue
continue next
}
currentBus.AddSubscription(newBus.Topic.Subscribe...)
currentBus.AddPublish(newBus.Topic.Publish...)
continue
continue next
}
d.Buses = append(d.Buses, newBus)
}
}
func (d *Driver) RemoveBuses(buses []*Bus) {
for _, bus := range buses {
for i := 0; i < len(d.Buses); i++ {
currentBus := d.Buses[i]
if currentBus.Name != bus.Name {
continue
}
currentBus.RemoveAddress(bus.Address...)
shouldRemove := true
if bus.Topic != nil {
currentBus.RemoveSubscription(bus.Topic.Subscribe...)
currentBus.RemovePublish(bus.Topic.Publish...)
shouldRemove = len(currentBus.Topic.Subscribe) == 0 && len(currentBus.Topic.Publish) == 0
}
if len(currentBus.Address) == 0 && shouldRemove {
d.Buses = append(d.Buses[:i], d.Buses[i+1:]...)
i-- // adjust index after removal
}
break
}
d.Buses[key] = newBus
}
}
func (d *Driver) GetBus(name string) *Bus {
if b, ok := d.Buses[name]; ok {
return b
for _, b := range d.Buses {
if b.Name == name {
return b
}
}
return nil
}

View File

@@ -7,14 +7,30 @@ func (d *Drivers) AddNewDriver(typ string) *Driver {
if drv, ok := (*d)[typ]; ok {
return drv
}
(*d)[typ] = &Driver{Type: typ}
return (*d)[typ]
drv := &Driver{Type: typ}
(*d)[typ] = drv
return drv
}
func (d *Drivers) AddDriver(drv Driver) {
func (d *Drivers) AddDriver(drv *Driver) {
if driver, ok := (*d)[drv.Type]; ok {
driver.AddBuses(drv.Buses)
(*d)[drv.Type] = driver
return
}
(*d)[drv.Type] = drv
}
func (d *Drivers) RemoveDriver(drv *Driver) {
if driver, ok := (*d)[drv.Type]; ok {
driver.RemoveBuses(drv.Buses)
}
d.CleanEmptyDrivers()
}
func (d *Drivers) CleanEmptyDrivers() {
for key, drv := range *d {
if len(drv.Buses) == 0 {
delete(*d, key)
}
}
(*d)[drv.Type] = &drv
}

View File

@@ -3,17 +3,18 @@ package models
import (
"fmt"
"gitea.tecamino.com/paadi/tecamino-json_data/utils"
"github.com/google/uuid"
"github.com/tecamino/tecamino-json_data/utils"
)
// publish model
type Publish struct {
Event string `json:"event,omitempty"` // publish event onCreate|onChange|onDelete
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
Path string `json:"path,omitempty"` // dbm path
Type Type `json:"type,omitempty"` // dbm datatype
Value any `json:"value,omitempty"` // dbm value
Event string `json:"event,omitempty"` // publish event onCreate|onChange|onDelete
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
Path string `json:"path,omitempty"` // dbm path
Type Type `json:"type,omitempty"` // dbm datatype
Value any `json:"value,omitempty"` // dbm value
}
// returns input parameter as assigned dbm datatype

View File

@@ -1,10 +1,12 @@
package models
import "slices"
import (
"slices"
)
// topic model
type Topic struct {
Publish []string `json:"Publish,omitempty"`
Publish []string `json:"publish,omitempty"`
Subscribe []string `json:"subscribe,omitempty"`
}
@@ -27,3 +29,32 @@ func (t *Topic) AddPublish(pubs ...string) {
slices.Sort(t.Publish)
}
}
func (t *Topic) RemoveSubscription(subs ...string) {
if len(t.Subscribe) == 1 {
t.Subscribe = []string{}
return
}
for _, toDelete := range subs {
for i, a := range t.Subscribe {
if a == toDelete {
t.Subscribe = slices.Delete(t.Subscribe, i, i+1)
}
}
}
}
func (t *Topic) RemovePublish(pubs ...string) {
if len(t.Publish) == 1 {
t.Publish = []string{}
return
}
for _, toDelete := range pubs {
for i, a := range t.Publish {
if a == toDelete {
t.Publish = slices.Delete(t.Publish, i, i+1)
}
}
}
}

View File

@@ -3,7 +3,7 @@ package models
import (
"fmt"
"github.com/tecamino/tecamino-json_data/utils"
"gitea.tecamino.com/paadi/tecamino-json_data/utils"
)
// all avaiable datatypes of dbm

View File

@@ -4,7 +4,7 @@ import (
"encoding/json"
"io"
"github.com/tecamino/tecamino-json_data/models"
"gitea.tecamino.com/paadi/tecamino-json_data/models"
)
// Returns a new json_data Request model

View File

@@ -4,7 +4,7 @@ import (
"encoding/json"
"io"
"github.com/tecamino/tecamino-json_data/models"
"gitea.tecamino.com/paadi/tecamino-json_data/models"
)
// Returns a new json_data Response model