Skip to content

Commit

Permalink
dev: add source type http apis
Browse files Browse the repository at this point in the history
  • Loading branch information
wwhai committed Dec 9, 2022
1 parent 7d401b7 commit 88b561d
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 5 deletions.
31 changes: 31 additions & 0 deletions core/device_namager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package core

import "github.com/i4de/rulex/typex"

type deviceTypeManager struct {
// K: 资源类型
// V: 伪构造函数
registry map[typex.DeviceType]*typex.XConfig
}

func NewDeviceTypeManager() typex.DeviceRegistry {
return &deviceTypeManager{
registry: map[typex.DeviceType]*typex.XConfig{},
}

}
func (rm *deviceTypeManager) Register(name typex.DeviceType, f *typex.XConfig) {
rm.registry[name] = f
}

func (rm *deviceTypeManager) Find(name typex.DeviceType) *typex.XConfig {

return rm.registry[name]
}
func (rm *deviceTypeManager) All() []*typex.XConfig {
data := make([]*typex.XConfig, 0)
for _, v := range rm.registry {
data = append(data, v)
}
return data
}
27 changes: 27 additions & 0 deletions device/type_loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package device

import (
"github.com/i4de/rulex/common"
"github.com/i4de/rulex/core"
"github.com/i4de/rulex/typex"
)

var DM typex.DeviceRegistry

/*
*
* 加载系统内支持的设备类型
*
*/
func LoadDt() {
DM = core.NewDeviceTypeManager()
DM.Register(typex.TSS200V02, core.GenInConfig(typex.COAP, "About TSS200V02", common.ModBusConfig{}))
DM.Register(typex.RTU485_THER, core.GenInConfig(typex.COAP, "About RTU485_THER", common.ModBusConfig{}))
DM.Register(typex.YK08_RELAY, core.GenInConfig(typex.COAP, "About YK08_RELAY", common.ModBusConfig{}))
DM.Register(typex.S1200PLC, core.GenInConfig(typex.COAP, "About S1200PLC", common.ModBusConfig{}))
DM.Register(typex.GENERIC_MODBUS, core.GenInConfig(typex.COAP, "About GENERIC_MODBUS", common.ModBusConfig{}))
DM.Register(typex.GENERIC_UART, core.GenInConfig(typex.COAP, "About GENERIC_UART", common.GenericUartConfig{}))
DM.Register(typex.GENERIC_SNMP, core.GenInConfig(typex.COAP, "About GENERIC_SNMP", common.GenericSnmpConfig{}))
DM.Register(typex.USER_G776, core.GenInConfig(typex.COAP, "About USER_G776", common.ModBusConfig{}))
DM.Register(typex.ICMP_SENDER, core.GenInConfig(typex.COAP, "About ICMP_SENDER", common.HostConfig{}))
}
4 changes: 0 additions & 4 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
"github.com/i4de/rulex/core"
"github.com/i4de/rulex/glogger"
"github.com/i4de/rulex/sidecar"
"github.com/i4de/rulex/source"
"github.com/i4de/rulex/statistics"
"github.com/i4de/rulex/target"
"github.com/i4de/rulex/typex"
"github.com/i4de/rulex/utils"
"github.com/shirou/gopsutil/v3/disk"
Expand Down Expand Up @@ -48,8 +46,6 @@ func NewRuleEngine(config typex.RulexConfig) typex.RuleX {

func (e *RuleEngine) Start() *typex.RulexConfig {
typex.StartQueue(core.GlobalConfig.MaxQueueSize)
source.LoadSt()
target.LoadTt()
return e.Config
}

Expand Down
9 changes: 9 additions & 0 deletions plugin/http_server/http_api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
"strings"
"time"

"github.com/i4de/rulex/device"
"github.com/i4de/rulex/glogger"
"github.com/i4de/rulex/source"
"github.com/i4de/rulex/target"
"github.com/i4de/rulex/typex"
"github.com/i4de/rulex/utils"

Expand Down Expand Up @@ -186,6 +189,7 @@ func (hh *HttpApiServer) Start(r typex.RuleX) error {
//
hh.ginEngine.GET(url("rType"), hh.addRoute(RType))
hh.ginEngine.GET(url("tType"), hh.addRoute(TType))
hh.ginEngine.GET(url("dType"), hh.addRoute(DType))
//
// 串口列表
//
Expand All @@ -201,11 +205,16 @@ func (hh *HttpApiServer) Start(r typex.RuleX) error {
hh.ginEngine.POST(url("devices"), hh.addRoute(CreateDevice))
hh.ginEngine.PUT(url("devices"), hh.addRoute(UpdateDevice))
hh.ginEngine.DELETE(url("devices"), hh.addRoute(DeleteDevice))

// 外挂管理
hh.ginEngine.GET(url("goods"), hh.addRoute(Goods))
hh.ginEngine.POST(url("goods"), hh.addRoute(CreateGoods))
hh.ginEngine.PUT(url("goods"), hh.addRoute(UpdateGoods))
hh.ginEngine.DELETE(url("goods"), hh.addRoute(DeleteGoods))
// 加载资源类型
source.LoadSt()
target.LoadTt()
device.LoadDt()
glogger.GLogger.Infof("Http server started on http://0.0.0.0:%v", hh.Port)
return nil
}
Expand Down
16 changes: 16 additions & 0 deletions plugin/http_server/system_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"runtime"
"time"

"github.com/i4de/rulex/device"
"github.com/i4de/rulex/source"
"github.com/i4de/rulex/statistics"
"github.com/i4de/rulex/target"
Expand Down Expand Up @@ -152,6 +153,21 @@ func TType(c *gin.Context, hh *HttpApiServer, e typex.RuleX) {

}

/*
*
* 设备配置
*
*/
func DType(c *gin.Context, hh *HttpApiServer, e typex.RuleX) {
Type, _ := c.GetQuery("type")
if Type == "" {
c.JSON(200, OkWithData(device.DM.All()))
} else {
c.JSON(200, OkWithData(device.DM.Find(typex.DeviceType(Type))))
}

}

/*
*
* 获取本地的串口列表
Expand Down
1 change: 1 addition & 0 deletions source/type_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var SM typex.SourceRegistry = core.NewSourceTypeManager()
*/

func LoadSt() {
SM = core.NewSourceTypeManager()
SM.Register(typex.COAP, core.GenInConfig(typex.COAP, "About COAP", common.HostConfig{}))
SM.Register(typex.GRPC, core.GenInConfig(typex.GRPC, "About GRPC", common.GrpcConfig{}))
SM.Register(typex.HTTP, core.GenInConfig(typex.HTTP, "About HTTP", common.HostConfig{}))
Expand Down
3 changes: 2 additions & 1 deletion target/type_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/i4de/rulex/typex"
)

var TM typex.TargetRegistry = core.NewTargetTypeManager()
var TM typex.TargetRegistry

/*
*
Expand All @@ -16,6 +16,7 @@ var TM typex.TargetRegistry = core.NewTargetTypeManager()
*/

func LoadTt() {
TM = core.NewTargetTypeManager()
TM.Register(typex.HTTP_TARGET, core.GenOutConfig(typex.HTTP_TARGET, "About HTTP_TARGET", common.HTTPConfig{}))
TM.Register(typex.MONGO_SINGLE, core.GenOutConfig(typex.MONGO_SINGLE, "About MONGO_SINGLE", common.MongoConfig{}))
TM.Register(typex.MQTT_TARGET, core.GenOutConfig(typex.MQTT_TARGET, "About MQTT_TARGET", common.MqttConfig{}))
Expand Down
7 changes: 7 additions & 0 deletions typex/device_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package typex

type DeviceRegistry interface {
Register(DeviceType, *XConfig)
Find(DeviceType) *XConfig
All() []*XConfig
}

0 comments on commit 88b561d

Please sign in to comment.