123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- package platform
- import (
- "errors"
- "strings"
- "github.com/yuguorong/go/log"
- bus "github.com/ammeter/Bus"
- "github.com/ammeter/cloudserver"
- "github.com/ammeter/config"
- drviers "github.com/ammeter/drivers"
- )
- /*
- gZdomIS9Hz3d7HxvcoNx
- data [{"accessToken":"AZM7QSPRqsV4CUxIGldn",
- "id":"1ec23004b88a020bf52b38d8579115b",
- "mqttServer":"test-sbuilding.pacom.cn:1885",
- "name":"FGGW10-202110020001"}]
- */
- var debugName = true
- const (
- DEF_VERDOR = "GW4G"
- DEF_INF_NAME = "eth0"
- )
- type Gateway struct {
- Mqttcfg bus.QMqtt
- Name string
- Uid string
- modelList map[string]string
- NorthRoute interface{}
- DeviceList map[string]drviers.IDevice
- }
- func (gw *Gateway) SyncCloudModelDevice(url string, model interface{}) error {
- var err = errors.New("Nil model?")
- if model != nil {
- prof := config.GetSysConfig().GetProfile("remote_config", cloudserver.GetCloudConfig())
- cs := cloudserver.GetCloudServer(prof)
- param := map[string]string{
- "gwDevId": gw.Uid,
- }
- err = cs.GetClientData(url, model, ¶m)
- log.Info(model)
- }
- return err
- }
- type netinfo struct {
- name string
- uuid string
- prority int
- }
- func priotyInf(ni *netinfo, name string) bool {
- name = strings.ToUpper(name)
- ifnames := []string{"WLAN", "ETH", "ENS", "ENO", "本地连接"}
- for i := 0; i < ni.prority && i < len(ifnames); i++ {
- if strings.Contains(name, ifnames[i]) {
- ni.name = name
- ni.prority = i
- return true
- }
- }
- return false
- }
- func (gw *Gateway) InitGateway(modelList map[string]string) {
- //gw.GetGatewayName()
- gw.DeviceList = make(map[string]drviers.IDevice)
- gw.modelList = modelList
- }
- //
- func (gw *Gateway) ApplyProfile(uid string) {
- // name := "ammeter"
- // drvU := drviers.Install("SAIR10", nil)
- for name, url := range gw.modelList { //model name + model cloud api url
- drv := drviers.Install(name, nil) //If already installed ,return installed instance.
- model := drv.GetModel() //model: cloud information of device, create exact device absctrace from model
- if url != "" {
- if gw.SyncCloudModelDevice(url, model) != nil {
- model = config.GetSysConfig().GetProfile("gateway/"+name+"/amlist", model) //Cloud failed ,using local saved model
- } else {
- config.GetSysConfig().SetProfile("gateway/"+name+"/amlist", model) //update local model
- }
- }
- if sz, devlist := drv.CreateDevice(model, gw.Mqttcfg.Uid); sz > 0 {
- for _, dev := range devlist {
- gw.DeviceList[dev.Name()] = dev
- dev.SetRoute(nil, "ThingsBoards", "mqtt", "v1/gateway/telemetry", &gw.Mqttcfg)
- dev.Open()
- }
- }
- }
- }
- func InitGateway(mqttcfg *bus.QMqtt, modelList map[string]string) *Gateway {
- gw := &Gateway{
- Uid: mqttcfg.Uid,
- Mqttcfg: *mqttcfg,
- }
- gw.InitGateway(modelList)
- return gw
- }
|