lucas
2 months ago
13 changed files with 222 additions and 42 deletions
@ -0,0 +1,20 @@ |
|||||
|
{ |
||||
|
"consumer": "consumerHttpProxy", |
||||
|
"ioConfig": { |
||||
|
"in": { |
||||
|
"httpServer": { |
||||
|
"port": 19700, |
||||
|
"userName": "goInOut", |
||||
|
"password": "", |
||||
|
"routes": [ |
||||
|
"upload/uds/+", |
||||
|
"upload/ZD/+" |
||||
|
] |
||||
|
} |
||||
|
}, |
||||
|
"out": { |
||||
|
"url": "http://127.0.0.1:4009/write?u=mingyuexia_wkd&p=mingyuexia_wkd&db=MingYueXia_Bridge&rp=autogen", |
||||
|
"method": "post" |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,25 @@ |
|||||
|
package HTTP_PRPXY |
||||
|
|
||||
|
import "goInOut/config" |
||||
|
|
||||
|
type ConfigFile struct { |
||||
|
config.Consumer |
||||
|
IoConfig ioConfig `json:"ioConfig"` |
||||
|
OtherInfo map[string]string `json:"info"` |
||||
|
} |
||||
|
type ioConfig struct { |
||||
|
In In `json:"in"` |
||||
|
Out OUT `json:"out"` |
||||
|
} |
||||
|
type In struct { |
||||
|
ApiServer config.ApiServerConfig `json:"apiServer"` |
||||
|
} |
||||
|
|
||||
|
type OUT struct { |
||||
|
HttpPost config.HttpConfig `json:"httpPost"` |
||||
|
} |
||||
|
|
||||
|
type SensorInfo struct { |
||||
|
Name string `json:"name"` //测点名称
|
||||
|
Code string `json:"code"` //测点编号 宜由“桥名简称-监测类别简称-构件类型编码-截面序号-构件序号-测点编号”组成
|
||||
|
} |
@ -0,0 +1,94 @@ |
|||||
|
package consumers |
||||
|
|
||||
|
import ( |
||||
|
"encoding/json" |
||||
|
"goInOut/adaptors" |
||||
|
"goInOut/consumers/HTTP_PRPXY" |
||||
|
"goInOut/dbHelper" |
||||
|
"log" |
||||
|
"strings" |
||||
|
"time" |
||||
|
) |
||||
|
|
||||
|
type consumerHttpProxy struct { |
||||
|
//数据缓存管道
|
||||
|
ch chan []adaptors.NeedPush |
||||
|
//具体配置
|
||||
|
Info HTTP_PRPXY.ConfigFile |
||||
|
InApiServer *dbHelper.ApiServerHelper |
||||
|
outHttpPost *dbHelper.HttpHelper |
||||
|
} |
||||
|
|
||||
|
func (the *consumerHttpProxy) LoadConfigJson(cfgStr string) { |
||||
|
// 将 JSON 格式的数据解析到结构体中
|
||||
|
err := json.Unmarshal([]byte(cfgStr), &the.Info) |
||||
|
if err != nil { |
||||
|
log.Printf("读取配置文件[%s]异常 err=%v", cfgStr, err.Error()) |
||||
|
panic(err) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func (the *consumerHttpProxy) Initial(cfg string) error { |
||||
|
the.LoadConfigJson(cfg) |
||||
|
err := the.InputInitial() |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
err = the.OutputInitial() |
||||
|
return err |
||||
|
} |
||||
|
func (the *consumerHttpProxy) InputInitial() error { |
||||
|
the.ch = make(chan []adaptors.NeedPush, 200) |
||||
|
//数据入口
|
||||
|
the.InApiServer = dbHelper.NewApiServer( |
||||
|
the.Info.IoConfig.In.ApiServer.Port, |
||||
|
the.Info.IoConfig.In.ApiServer.Routes, |
||||
|
) |
||||
|
////inTopic := "Upload/#" //荔枝乌江大桥
|
||||
|
//for _, inTopic := range the.Info.IoConfig.In.Mqtt.Topics {
|
||||
|
// the.InMqtt.Subscribe(inTopic, the.onData)
|
||||
|
//}
|
||||
|
the.InApiServer.Initial() |
||||
|
return nil |
||||
|
} |
||||
|
func (the *consumerHttpProxy) OutputInitial() error { |
||||
|
//数据出口
|
||||
|
the.outHttpPost.Initial() |
||||
|
return nil |
||||
|
} |
||||
|
func (the *consumerHttpProxy) Work() { |
||||
|
go func() { |
||||
|
for { |
||||
|
|
||||
|
log.Printf("取出ch数据,剩余[%d] ", len(the.ch)) |
||||
|
|
||||
|
time.Sleep(100 * time.Millisecond) |
||||
|
} |
||||
|
}() |
||||
|
} |
||||
|
func (the *consumerHttpProxy) onData(inTopic string, Msg string) { |
||||
|
if len(Msg) > 100 { |
||||
|
log.Printf("mqtt-recv:[%s]:%s ...", inTopic, Msg[:100]) |
||||
|
} |
||||
|
|
||||
|
topicPrefixIndex := strings.LastIndex(inTopic, "/") |
||||
|
matchTopic := inTopic[:topicPrefixIndex] |
||||
|
adaptor := the.getAdaptor(matchTopic) |
||||
|
if adaptor != nil { |
||||
|
needPush := adaptor.Transform(matchTopic, Msg) |
||||
|
if len(needPush) > 0 { |
||||
|
the.ch <- needPush |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
func (the *consumerHttpProxy) getAdaptor(flag string) (adaptor adaptors.IAdaptor4) { |
||||
|
bridgeCode := "" |
||||
|
if v, ok := the.Info.OtherInfo["bridgeCode"]; ok { |
||||
|
bridgeCode = v |
||||
|
} |
||||
|
if bridgeCode == "" { |
||||
|
panic("无正确的 bridgeCode") |
||||
|
} |
||||
|
|
||||
|
return adaptor |
||||
|
} |
@ -1,38 +0,0 @@ |
|||||
package dbHelper |
|
||||
|
|
||||
import ( |
|
||||
"fmt" |
|
||||
"github.com/gin-gonic/gin" |
|
||||
"log" |
|
||||
"net/http" |
|
||||
"time" |
|
||||
) |
|
||||
|
|
||||
type RouterFunc struct { |
|
||||
relativePath string //相对路由 如/gzm/data/upload
|
|
||||
funcType string // 方法类型 如 post ,get
|
|
||||
fun func(c *gin.Context) //方法
|
|
||||
} |
|
||||
|
|
||||
type ApiServerHelper struct { |
|
||||
Port uint16 |
|
||||
RoutFun map[string]RouterFunc |
|
||||
} |
|
||||
|
|
||||
func (the *ApiServerHelper) Initial() { |
|
||||
router := gin.Default() |
|
||||
for name, routerFunc := range the.RoutFun { |
|
||||
switch routerFunc.funcType { |
|
||||
case http.MethodGet: |
|
||||
router.GET(routerFunc.relativePath, routerFunc.fun) |
|
||||
case http.MethodPost: |
|
||||
router.GET(routerFunc.relativePath, routerFunc.fun) |
|
||||
default: |
|
||||
log.Printf("不支持的 [%s]方法类型 %s", routerFunc.relativePath, routerFunc.funcType) |
|
||||
continue |
|
||||
} |
|
||||
log.Printf("注册路由 %s,监听地址=%s", name, routerFunc.relativePath) |
|
||||
} |
|
||||
router.Run(fmt.Sprintf("0.0.0.0:%d", the.Port)) |
|
||||
time.Sleep(time.Second * 1) |
|
||||
} |
|
@ -0,0 +1,46 @@ |
|||||
|
package dbHelper |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"log" |
||||
|
"net/http" |
||||
|
) |
||||
|
|
||||
|
type ApiServerHelper struct { |
||||
|
mux *http.ServeMux |
||||
|
route map[string]string |
||||
|
port uint |
||||
|
} |
||||
|
|
||||
|
func NewApiServer(port uint, routes map[string]string) *ApiServerHelper { |
||||
|
return &ApiServerHelper{ |
||||
|
mux: http.NewServeMux(), |
||||
|
route: routes, |
||||
|
port: port, |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func (the *ApiServerHelper) Initial() { |
||||
|
the.mux = http.NewServeMux() |
||||
|
// 创建 HTTP 服务器
|
||||
|
ser := http.Server{ |
||||
|
Handler: the.mux, |
||||
|
Addr: fmt.Sprintf(":%d", the.port), |
||||
|
} |
||||
|
log.Printf("apiServer监听端口 %d", the.port) |
||||
|
go log.Fatal(ser.ListenAndServe()) |
||||
|
} |
||||
|
|
||||
|
func (the *ApiServerHelper) routeRegister() { |
||||
|
the.mux.HandleFunc("GET /nodeList", func(w http.ResponseWriter, r *http.Request) { |
||||
|
w.Header().Set("Content-Type", "application/json") |
||||
|
s := `{"a":1}` |
||||
|
fmt.Fprintf(w, s) |
||||
|
}) |
||||
|
|
||||
|
the.mux.HandleFunc("GET /namespaceList", func(w http.ResponseWriter, r *http.Request) { |
||||
|
w.Header().Set("Content-Type", "application/json") |
||||
|
s := `{"b":1}` |
||||
|
fmt.Fprintf(w, s) |
||||
|
}) |
||||
|
} |
Loading…
Reference in new issue