From 830d011c1dd4b337e3b285cbc6f5aa0aeac15262 Mon Sep 17 00:00:00 2001 From: wyhwyhwyh <573805736@qq.com> Date: Mon, 7 Mar 2022 08:37:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BF=AE=E6=94=B9=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 9 +++++---- service/ble-service.go | 34 ++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index daf4304..928bc82 100644 --- a/main.go +++ b/main.go @@ -31,10 +31,11 @@ func main() { //ble服务 service.BleDelivery() - go service.BleMessagePush() - go service.BleCacheClear() - go service.Bletokeninit() - go service.BleHeartBeat() + go service.BleWriteMqtt() + // go service.BleMessagePush() + // go service.BleCacheClear() + // go service.Bletokeninit() + // go service.BleHeartBeat() go gin.Service(&gin.Config{ RootPath: APPNAME, diff --git a/service/ble-service.go b/service/ble-service.go index 927fc5c..ca3d12d 100644 --- a/service/ble-service.go +++ b/service/ble-service.go @@ -20,14 +20,14 @@ import ( "myschools.me/wyh/ble-april/mosquitto" ) -var m map[string]*IBeancon -var chn chan *IBeancon +var m map[string]*BeaconApril +var chn chan *BeaconApril var token string var expiretime time.Time func init() { - m = make(map[string]*IBeancon, 500) - chn = make(chan *IBeancon, 500) + m = make(map[string]*BeaconApril, 500) + chn = make(chan *BeaconApril, 500) } type Bletoken struct { @@ -117,11 +117,7 @@ var serviceHandler MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Messa // 解析out1.device bles := BleDecode(out1["devices"].([]interface{})) for _, d := range bles { - chn <- &IBeancon{ - DeviceID: fmt.Sprintf("%s:%s:%s:%s:%s:%s", d.Mac[0:2], d.Mac[2:4], d.Mac[4:6], d.Mac[6:8], d.Mac[8:10], d.Mac[10:12]), - HappenTime: time.Now(), - Updated: time.Now().Unix(), - } + chn <- d } } @@ -135,6 +131,20 @@ func BleDelivery() { } +func BleWriteMqtt() { + topic := "april" + for { + if b, ok := <-chn; ok { + if err := mosquitto.DistributeObject(&topic, 1, false, b); err != nil { + logrus.WithFields(logrus.Fields{ + "func": "BleWriteMqtt", + }).Warnf("mosquitto.DistributeObject: %s", err.Error()) + continue + } + } + } +} + // 取出数据进行处理 func BleMessagePush() { @@ -186,7 +196,7 @@ func BleMessagePush() { continue } if i["msg"] == "ok" { - m[b.DeviceID] = b + m[b.Mac] = b } } @@ -293,6 +303,8 @@ type BeaconApril struct { Mac string Rssi string AdvertisementData string + HappenTime time.Time + Updated int64 `json:"-"` } // func NewiBeacon(data []byte, mac string) (*IBeancon, error) { @@ -337,6 +349,8 @@ func BleDecode(data []interface{}) []*BeaconApril { Mac: vals[2:14], Rssi: vals[14:16], AdvertisementData: vals[16:], + HappenTime: time.Now(), + Updated: time.Now().Unix(), }) } return bles