Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a445b0bf5 | |||
|
|
775f079eb7 | ||
|
|
6dffd1fad4 | ||
|
|
c15710ddec | ||
|
|
2924d95f4b | ||
|
|
a39dcc06f4 | ||
|
|
e720f0d2d3 | ||
|
|
ce3ea4746b |
2
go.mod
2
go.mod
@@ -1,4 +1,4 @@
|
|||||||
module github.com/tecamino/tecamino-json_data
|
module gitea.tecamino.com/paadi/tecamino-json_data
|
||||||
|
|
||||||
go 1.21.0
|
go 1.21.0
|
||||||
|
|
||||||
|
|||||||
@@ -16,9 +16,7 @@ func (b *Bus) AddAddress(address ...uint) {
|
|||||||
}
|
}
|
||||||
b.Address = append(b.Address, a)
|
b.Address = append(b.Address, a)
|
||||||
slices.Sort(b.Address)
|
slices.Sort(b.Address)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bus) AddSubscription(sub ...string) {
|
func (b *Bus) AddSubscription(sub ...string) {
|
||||||
@@ -34,3 +32,25 @@ func (b *Bus) AddPublish(pub ...string) {
|
|||||||
}
|
}
|
||||||
b.Topic.AddPublish(pub...)
|
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...)
|
||||||
|
}
|
||||||
|
|||||||
@@ -36,6 +36,31 @@ next:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (d *Driver) GetBus(name string) *Bus {
|
func (d *Driver) GetBus(name string) *Bus {
|
||||||
for _, b := range d.Buses {
|
for _, b := range d.Buses {
|
||||||
if b.Name == name {
|
if b.Name == name {
|
||||||
|
|||||||
@@ -7,14 +7,30 @@ func (d *Drivers) AddNewDriver(typ string) *Driver {
|
|||||||
if drv, ok := (*d)[typ]; ok {
|
if drv, ok := (*d)[typ]; ok {
|
||||||
return drv
|
return drv
|
||||||
}
|
}
|
||||||
(*d)[typ] = &Driver{Type: typ}
|
drv := &Driver{Type: typ}
|
||||||
return (*d)[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 {
|
if driver, ok := (*d)[drv.Type]; ok {
|
||||||
driver.AddBuses(drv.Buses)
|
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,14 +3,15 @@ package models
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"gitea.tecamino.com/paadi/tecamino-json_data/utils"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/tecamino/tecamino-json_data/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// publish model
|
// publish model
|
||||||
type Publish struct {
|
type Publish struct {
|
||||||
Event string `json:"event,omitempty"` // publish event onCreate|onChange|onDelete
|
Event string `json:"event,omitempty"` // publish event onCreate|onChange|onDelete
|
||||||
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
|
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
|
||||||
|
Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
|
||||||
Path string `json:"path,omitempty"` // dbm path
|
Path string `json:"path,omitempty"` // dbm path
|
||||||
Type Type `json:"type,omitempty"` // dbm datatype
|
Type Type `json:"type,omitempty"` // dbm datatype
|
||||||
Value any `json:"value,omitempty"` // dbm value
|
Value any `json:"value,omitempty"` // dbm value
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import "slices"
|
import (
|
||||||
|
"slices"
|
||||||
|
)
|
||||||
|
|
||||||
// topic model
|
// topic model
|
||||||
type Topic struct {
|
type Topic struct {
|
||||||
@@ -27,3 +29,32 @@ func (t *Topic) AddPublish(pubs ...string) {
|
|||||||
slices.Sort(t.Publish)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/tecamino/tecamino-json_data/utils"
|
"gitea.tecamino.com/paadi/tecamino-json_data/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// all avaiable datatypes of dbm
|
// all avaiable datatypes of dbm
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/tecamino/tecamino-json_data/models"
|
"gitea.tecamino.com/paadi/tecamino-json_data/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Returns a new json_data Request model
|
// Returns a new json_data Request model
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/tecamino/tecamino-json_data/models"
|
"gitea.tecamino.com/paadi/tecamino-json_data/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Returns a new json_data Response model
|
// Returns a new json_data Response model
|
||||||
|
|||||||
Reference in New Issue
Block a user