golang redis and rabbitmq mqtt

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"reflect"

	mqtt "github.com/eclipse/paho.mqtt.golang"
	"github.com/go-redis/redis/v8"
	"github.com/streadway/amqp"
)

var ctx = context.Background()

// RedisClient is a interface for any type of golang redis-client package
type RedisClient interface {
	rclient()
	set(key string, value string) error
	get(key string) (string, error)
	lrange(key string) ([]string, error)
	rpush(key string, valueList []string) error
	//delete(c *redis.Client, key string) error
}

type redisclient struct {
	Addr     string
	Password string
	DB       int
	rdb      *redis.Client
}

func (c *redisclient) rclient() {
	c.rdb = redis.NewClient(&redis.Options{
		Addr:     c.Addr,
		Password: c.Password,
		DB:       c.DB, // use default DB
	})
	//return rdb
}

func (c *redisclient) set(key string, value string) error {
	err := c.rdb.Set(ctx, key, value, 0).Err()
	if err != nil {
		return err
	}
	return nil

}
func (c *redisclient) rpush(key string, valueList []string) error {

	err := c.rdb.RPush(ctx, key, valueList).Err()
	if err != nil {
		return err
	}
	return nil

}
func (c *redisclient) lrange(key string) ([]string, error) {

	ret, err := c.rdb.LRange(ctx, key, 0, -1).Result()
	if err != nil {
		return ret, err
	}
	return ret, nil

}
func (c *redisclient) get(key string) (string, error) {
	value, err := c.rdb.Get(ctx, key).Result()
	if err != nil {
		return value, err
	}
	return value, nil
}

// MqClient is an interface of any type of golang mq package
type MqClient interface {
	cclient()
	sendMsg(topic string, message string) error
	//SubTop(topic string)
}

type mqttclient struct {
	Addr string
	clt  mqtt.Client
}

func (c *mqttclient) cclient() {
	opts := mqtt.NewClientOptions().AddBroker(c.Addr).SetClientID("sample")
	c.clt = mqtt.NewClient(opts)
	//return clt
}
func (c *mqttclient) sendMsg(topic string, message string) error {
	if token := c.clt.Connect(); token.Wait() && token.Error() != nil {
		//panic(token.Error())
		return token.Error()
	}
	if token := c.clt.Publish(topic, 0, false, message); token.Wait() && token.Error() != nil {
		//panic(token.Error())
		return token.Error()
	}
	return nil

}

type rabbitMqClient struct {
	Addr string
	clt  *amqp.Connection
}

func (c *rabbitMqClient) cclient() {
	conn, err := amqp.Dial(c.Addr)
	if err != nil {
		panic(err)
	}
	c.clt = conn
}
func (c *rabbitMqClient) sendMsg(topic string, message string) error {

	ch, err := c.clt.Channel()
	if err != nil {
		return err
	}
	q, err := ch.QueueDeclare(
		topic, // name
		false, // durable
		false, // delete when unused
		false, // exclusive
		false, // no-wait
		nil,   // arguments
	)
	if err != nil {
		return err
	}

	err = ch.Publish(
		"",     // exchange
		q.Name, // routing key
		false,  // mandatory
		false,  // immediate
		amqp.Publishing{
			ContentType: "text/plain",
			Body:        []byte(message),
		})
	if err != nil {
		return err
	}
	return nil

}

func newRedisClt() RedisClient {
	return &redisclient{Addr: "localhost:6379", Password: "", DB: 0}
}
func newMqClient() MqClient {

	//return &mqttclient{Addr: "tcp://localhost:1883"}
	return &rabbitMqClient{Addr: "amqp://guest:guest@localhost:5672/"}

}
func parseMsg(message []string) error {
	//ret := make(map[string]string)
	for _, value := range message {
		fmt.Println(reflect.TypeOf(value))
		fmt.Println(value)
		ent, ext := 0.0, 0.0
		//m := make(map[string]interface{})
		var a []map[string]interface{}
		err := json.Unmarshal([]byte(value), &a)
		if err != nil {
			return err
		}
		for _, val := range a {
			if val["Type"] == "Enter" {

				ent = ent + val["PeopleCount"].(float64)
				fmt.Println(ent)
			} else {
				ext = ext + val["PeopleCount"].(float64)
				fmt.Println(ext)
			}
		}

	}
	return nil

}

func main() {

	var myclt = newRedisClt()
	myclt.rclient()
	//value, err := myclt.get("name")
	value, err := myclt.lrange("people_count")
	if err != nil {

		panic(err)

	}
	parseMsg(value)

	// c := time.Tick(5 * time.Second)
	// for {
	// 	<-c
	// 	go func() {
	// 		var myclt = newRedisClt()
	// 		myclt.rclient()
	// 		//value, err := myclt.get("name")
	// 		value, err := myclt.lrange("people_count")
	// 		if err != nil {

	// 			panic(err)

	// 		}
	// 		parseMsg(value)
	// 		/*mqclt := newMqClient()
	// 		mqclt.cclient()
	// 		mqerr := mqclt.sendMsg("peopleFlowControl", value)
	// 		if mqerr != nil {
	// 			panic(mqerr)
	// 		}*/

	// 	}()
	// }

}

猜你喜欢

转载自blog.csdn.net/niekunhit/article/details/108872181