123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package platform
- import (
- "errors"
- "strings"
- 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 {
- ID int `gorm:"not null"`
- Mqttcfg bus.QMqtt
- Name string `gorm:"not null"`
- Uid string `gorm:"not null"`
- modelList map[string]string
- tsUpdate int64
- NorthRoute interface{} `json:"-" gorm:"-"`
- drvList map[string]drviers.IDriver
- }
- //https://test-admin.pacom.cn/platform/dev/get-sair-list
- 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() {
- //gw.GetGatewayName()
- }
- func (gw *Gateway) Remove(uid string) {
- for _, drv := range gw.drvList {
- drv.Uninstall()
- }
- gw.drvList = nil
- gw.modelList = nil
- gw.Mqttcfg = bus.QMqtt{}
- }
- //
- func (gw *Gateway) InstallDrivers(uid string) {
- //tsUpdate := time.Now().UnixMilli()
- for name, _ := range gw.modelList { //model name + model cloud api url
- drv, has := gw.drvList[name]
- if !has || drv == nil {
- drv = drviers.Install(name, nil) //If already installed ,return installed instance.
- gw.drvList[name] = drv
- }
- model := drv.GetModel() //model: cloud information of device, create exact device absctrace from model
- config.GetDB().Find(model, `gw_dev_id="`+gw.Uid+`"`)
- if sz, devlist := drv.CreateDevice(model); sz > 0 {
- for _, dev := range devlist {
- dev.SetRoute(nil, "ThingsBoards", "mqtt", "v1/gateway/telemetry", &gw.Mqttcfg)
- dev.Open() //open specail Route here
- }
- }
- }
- }
- func InitGateway(mqttcfg *bus.QMqtt, extname string, models map[string]string) *Gateway {
- gw := &Gateway{
- Uid: mqttcfg.Uid,
- Mqttcfg: *mqttcfg,
- tsUpdate: 0,
- drvList: make(map[string]drviers.IDriver),
- Name: extname,
- modelList: models,
- }
- return gw
- }
|