Skip to content

Commit

Permalink
dev: enhance more modules
Browse files Browse the repository at this point in the history
  • Loading branch information
wwhai committed Apr 8, 2024
1 parent 23a8354 commit ffb6bae
Show file tree
Hide file tree
Showing 33 changed files with 208 additions and 248 deletions.
1 change: 1 addition & 0 deletions component/aibase/aibase_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func InitAlgorithmRuntime(re typex.RuleX) *AlgorithmRuntime {
__DefaultAIRuntime = new(AlgorithmRuntime)
__DefaultAIRuntime.RuleEngine = re
__DefaultAIRuntime.XAlgorithms = make(map[string]XAlgorithm)
__DefaultAIRuntime.locker = sync.Mutex{}
// Yolo8
// err1 := LoadAlgorithm(NewYolo8ObjectDetectionCpu(), map[string]interface{}{})
// if err1 != nil {
Expand Down
15 changes: 14 additions & 1 deletion component/appstack/appstack_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package appstack
import (
"context"
"fmt"
"sync"
"time"

lua "github.com/hootrhino/gopher-lua"
Expand All @@ -31,6 +32,7 @@ var __DefaultAppStackRuntime *AppStackRuntime
func InitAppStack(re typex.RuleX) *AppStackRuntime {
__DefaultAppStackRuntime = &AppStackRuntime{
RuleEngine: re,
locker: sync.Mutex{},
Applications: make(map[string]*Application),
}
return __DefaultAppStackRuntime
Expand All @@ -45,7 +47,8 @@ func AppRuntime() *AppStackRuntime {
*
*/
func LoadApp(app *Application, luaSource string) error {

__DefaultAppStackRuntime.locker.Lock()
defer __DefaultAppStackRuntime.locker.Unlock()
// 重新读
app.VM().DoString(string(luaSource))
// 检查函数入口
Expand Down Expand Up @@ -73,6 +76,8 @@ func LoadApp(app *Application, luaSource string) error {
*
*/
func StartApp(uuid string) error {
__DefaultAppStackRuntime.locker.Lock()
defer __DefaultAppStackRuntime.locker.Unlock()
app, ok := __DefaultAppStackRuntime.Applications[uuid]
if !ok {
return fmt.Errorf("Application not exists:%s", uuid)
Expand Down Expand Up @@ -173,6 +178,8 @@ func StartApp(uuid string) error {
*
*/
func RemoveApp(uuid string) error {
__DefaultAppStackRuntime.locker.Lock()
defer __DefaultAppStackRuntime.locker.Unlock()
if app, ok := __DefaultAppStackRuntime.Applications[uuid]; ok {
app.Remove()
delete(__DefaultAppStackRuntime.Applications, uuid)
Expand All @@ -187,6 +194,8 @@ func RemoveApp(uuid string) error {
*
*/
func StopApp(uuid string) error {
__DefaultAppStackRuntime.locker.Lock()
defer __DefaultAppStackRuntime.locker.Unlock()
if app, ok := __DefaultAppStackRuntime.Applications[uuid]; ok {
app.Stop()
}
Expand All @@ -200,6 +209,8 @@ func StopApp(uuid string) error {
*
*/
func UpdateApp(app Application) error {
__DefaultAppStackRuntime.locker.Lock()
defer __DefaultAppStackRuntime.locker.Unlock()
if oldApp, ok := __DefaultAppStackRuntime.Applications[app.UUID]; ok {
oldApp.Name = app.Name
oldApp.AutoStart = app.AutoStart
Expand Down Expand Up @@ -237,6 +248,8 @@ func ListApp() []*Application {
}

func Stop() {
__DefaultAppStackRuntime.locker.Lock()
defer __DefaultAppStackRuntime.locker.Unlock()
for _, app := range __DefaultAppStackRuntime.Applications {
glogger.GLogger.Info("Stop App:", app.UUID)
app.Stop()
Expand Down
7 changes: 6 additions & 1 deletion component/appstack/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@

package appstack

import "github.com/hootrhino/rulex/typex"
import (
"sync"

"github.com/hootrhino/rulex/typex"
)

/*
*
* 管理器
*
*/
type AppStackRuntime struct {
locker sync.Mutex
RuleEngine typex.RuleX
Applications map[string]*Application
}
11 changes: 10 additions & 1 deletion component/eventbus/eventbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package eventbus
import (
"context"
"fmt"
"sync"

"github.com/hootrhino/rulex/typex"
"github.com/hootrhino/rulex/utils"
Expand All @@ -41,9 +42,9 @@ func (E EventMessage) String() string {
type Topic struct {
Topic string
channel chan EventMessage
Subscribers map[string]*Subscriber
ctx context.Context
cancel context.CancelFunc
Subscribers map[string]*Subscriber
}
type Subscriber struct {
id string
Expand All @@ -53,12 +54,14 @@ type EventBus struct {
// Topic, chan EventMessage
// 给每个订阅者分配一个Channel,实现消息订阅
// Topic一样的会挂在同一个树上
locker sync.Mutex
Topics map[string]*Topic // 订阅树: MAP<Topic>[]Subscribers
}

func InitEventBus() *EventBus {
__DefaultEventBus = &EventBus{
Topics: map[string]*Topic{},
locker: sync.Mutex{},
}
return __DefaultEventBus
}
Expand All @@ -69,6 +72,8 @@ func InitEventBus() *EventBus {
*
*/
func Subscribe(topic string, subscribe *Subscriber) {
__DefaultEventBus.locker.Lock()
defer __DefaultEventBus.locker.Unlock()
NewUUID := utils.MakeUUID("SUB")
subscribe.id = NewUUID
var T *Topic
Expand Down Expand Up @@ -112,6 +117,8 @@ func Subscribe(topic string, subscribe *Subscriber) {
*
*/
func UnSubscribe(topic string, subscribe Subscriber) {
__DefaultEventBus.locker.Lock()
defer __DefaultEventBus.locker.Unlock()
T, Ok1 := __DefaultEventBus.Topics[topic]
if Ok1 {
if _, Ok2 := T.Subscribers[subscribe.id]; Ok2 {
Expand Down Expand Up @@ -143,6 +150,8 @@ func Publish(topic string, Msg EventMessage) {
*
*/
func Flush() {
__DefaultEventBus.locker.Lock()
defer __DefaultEventBus.locker.Unlock()
for _, T := range __DefaultEventBus.Topics {
for _, S := range T.Subscribers {
delete(T.Subscribers, S.id)
Expand Down
118 changes: 0 additions & 118 deletions component/intercache/iotschema/iot_schema_cache.go

This file was deleted.

19 changes: 0 additions & 19 deletions component/intercache/thingsschema/things_schema_cache.go

This file was deleted.

Loading

0 comments on commit ffb6bae

Please sign in to comment.