123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- package main
- import (
- "fmt"
- "github.com/gogf/gf/encoding/gjson"
- "github.com/gogf/gf/os/grpool"
- "github.com/gogf/gf/util/guid"
- "sparrow/pkg/actors"
- "sparrow/pkg/entities"
- "sparrow/pkg/klink"
- "sparrow/pkg/protocol"
- "sparrow/pkg/queue"
- "sparrow/pkg/queue/msgQueue"
- "sparrow/pkg/rpcs"
- "sparrow/pkg/rule"
- "sparrow/pkg/ruleEngine"
- "sparrow/pkg/server"
- "time"
- )
- //Controller 消息控制器
- type Controller struct {
- producer queue.QueueProducer
- timer *rule.Timer
- ift *rule.Ifttt
- actorContext *ruleEngine.SystemContext
- consumer queue.QueueConsumer
- cluster *ClusterService
- pool *grpool.Pool
- }
- //NewController 新建消息控制器
- func NewController(rabbithost string) (*Controller, error) {
- admin := msgQueue.NewRabbitMessageQueueAdmin(&msgQueue.RabbitMqSettings{Host: rabbithost}, nil)
- producer := msgQueue.NewRabbitMqProducer(admin, "default")
- consumer := msgQueue.NewRabbitConsumer(admin, "MAIN")
- tp := make([]*queue.TopicPartitionInfo, 0)
- tp = append(tp, &queue.TopicPartitionInfo{
- Topic: "MAIN",
- TenantId: "1ps9djpswi0cds7cofynkso300eql4iu",
- Partition: 0,
- MyPartition: false,
- })
- tp = append(tp, &queue.TopicPartitionInfo{
- Topic: "MAIN",
- TenantId: "1ps9djpswi0cds7cofynkso300eql4sw",
- Partition: 0,
- MyPartition: false,
- })
- _ = consumer.SubscribeWithPartitions(tp)
- if err := producer.Init(); err != nil {
- return nil, err
- }
- return &Controller{
- producer: producer,
- consumer: consumer,
- cluster: &ClusterService{producer: producer},
- pool: grpool.New(),
- }, nil
- }
- // SetStatus 设置设备状态
- func (c *Controller) SetStatus(args rpcs.ArgsSetStatus, reply *rpcs.ReplySetStatus) error {
- rpchost, err := getAccessRPCHost(args.DeviceId)
- if err != nil {
- return err
- }
- return server.RPCCallByHost(rpchost, "Access.SetStatus", args, reply)
- }
- //GetStatus 获取设备状态
- func (c *Controller) GetStatus(args rpcs.ArgsGetStatus, reply *rpcs.ReplyGetStatus) error {
- rpchost, err := getAccessRPCHost(args.Id)
- if err != nil {
- return err
- }
- return server.RPCCallByHost(rpchost, "Access.GetStatus", args, reply)
- }
- //Online 设备上线
- func (c *Controller) Online(args rpcs.ArgsGetStatus, reply *rpcs.ReplyEmptyResult) error {
- data := gjson.New(nil)
- _ = data.Set("device_id", args.Id)
- t := time.Now()
- msg := &protocol.Message{
- Id: guid.S(),
- Ts: &t,
- Type: protocol.CONNECT_EVENT,
- Data: data.MustToJsonString(),
- Callback: nil,
- MetaData: map[string]interface{}{
- "device_id": args.Id,
- "vendor_id": args.VendorId,
- },
- Originator: "device",
- }
- tpi := queue.ResolvePartition("RULE_ENGINE",
- msg.GetQueueName(),
- args.VendorId,
- args.Id)
- g, err := queue.NewGobQueueMessage(msg)
- if err != nil {
- return err
- }
- g.Headers.Put("tenant_id", []byte(args.VendorId))
- return c.producer.Send(tpi, g, nil)
- }
- //Offline 设备下线
- func (c *Controller) Offline(args rpcs.ArgsGetStatus, reply *rpcs.ReplyEmptyResult) error {
- if args.Id == "" || args.VendorId == "" {
- return nil
- }
- data := gjson.New(nil)
- _ = data.Set("device_id", args.Id)
- t := time.Now()
- msg := &protocol.Message{
- Id: guid.S(),
- Ts: &t,
- Type: protocol.DISCONNECT_EVENT,
- Data: data.MustToJsonString(),
- Callback: nil,
- MetaData: map[string]interface{}{
- "device_id": args.Id,
- "vendor_id": args.VendorId,
- },
- Originator: "device",
- }
- tpi := queue.ResolvePartition("RULE_ENGINE",
- msg.GetQueueName(),
- args.VendorId,
- args.Id)
- g, err := queue.NewGobQueueMessage(msg)
- if err != nil {
- return err
- }
- g.Headers.Put("tenant_id", []byte(args.VendorId))
- return c.producer.Send(tpi, g, nil)
- }
- //OnStatus 状态上报消息处理
- func (c *Controller) OnStatus(args rpcs.ArgsOnStatus, reply *rpcs.ReplyOnStatus) error {
- t := time.Unix(int64(args.Timestamp/1000), 0)
- data, err := c.processStatusToQueue(args)
- if err != nil {
- return err
- }
- msg := &protocol.Message{
- Id: guid.S(),
- Ts: &t,
- Type: protocol.POST_ATTRIBUTES_REQUEST,
- Data: data,
- Callback: nil,
- MetaData: map[string]interface{}{
- "tenant_id": args.VendorId,
- "device_id": args.DeviceId,
- "sub_device_id": args.SubDeviceId,
- },
- Originator: "device",
- }
- tpi := queue.ResolvePartition("RULE_ENGINE",
- msg.GetQueueName(),
- args.VendorId,
- args.DeviceId)
- g, err := queue.NewGobQueueMessage(msg)
- if err != nil {
- return err
- }
- g.Headers.Put("tenant_id", []byte(args.VendorId))
- return c.producer.Send(tpi, g, nil)
- }
- func (c *Controller) processStatusToQueue(args rpcs.ArgsOnStatus) (string, error) {
- result := gjson.New(nil)
- j, err := gjson.DecodeToJson(args.SubData)
- if err != nil {
- return "", err
- }
- switch args.Action {
- case klink.DevSendAction:
- params := j.GetMap("params")
- if err = result.Set(j.GetString("cmd"), params); err != nil {
- return "", err
- }
- }
- return result.MustToJsonString(), nil
- }
- func (c *Controller) processEventToQueue(args rpcs.ArgsOnEvent) (string, error) {
- result := gjson.New(nil)
- j, err := gjson.DecodeToJson(args.SubData)
- if err != nil {
- return "", nil
- }
- params := j.GetMap("params")
- if err = result.Set(j.GetString("cmd"), params); err != nil {
- return "", err
- }
- return result.MustToJsonString(), nil
- }
- //OnEvent 事件消息处理
- func (c *Controller) OnEvent(args rpcs.ArgsOnEvent, reply *rpcs.ReplyOnEvent) error {
- t := time.Unix(int64(args.TimeStamp/1000), 0)
- data, err := c.processEventToQueue(args)
- if err != nil {
- return err
- }
- msg := &protocol.Message{
- Id: guid.S(),
- Ts: &t,
- Type: protocol.POST_EVENT_REQUEST,
- Data: data,
- Callback: nil,
- MetaData: map[string]interface{}{
- "tenant_id": args.VendorId,
- "device_id": args.DeviceId,
- "sub_device_id": args.SubDeviceId,
- },
- Originator: "device",
- }
- tpi := queue.ResolvePartition("RULE_ENGINE",
- msg.GetQueueName(),
- args.VendorId,
- args.DeviceId)
- g, err := queue.NewGobQueueMessage(msg)
- if err != nil {
- return err
- }
- g.Headers.Put("tenant_id", []byte(args.VendorId))
- return c.producer.Send(tpi, g, nil)
- }
- // CreateRuleChain 规则链生命周期-创建
- func (c *Controller) CreateRuleChain(args rpcs.ArgsRuleChainAct, reply *rpcs.ReplyEmptyResult) error {
- if c.actorContext != nil {
- msg := &ruleEngine.ComponentLifecycleMsg{
- TenantId: args.VendorId,
- EntityId: &entities.RuleChainId{Id: args.RuleChainId},
- EventType: ruleEngine.CREATED,
- }
- c.actorContext.AppActor.TellWithHighPriority(msg)
- }
- return nil
- }
- // DeleteRuleChain 规则链生命周期-删除
- func (c *Controller) DeleteRuleChain(args rpcs.ArgsRuleChainAct, reply *rpcs.ReplyEmptyResult) error {
- if c.actorContext != nil {
- msg := &ruleEngine.ComponentLifecycleMsg{
- TenantId: args.VendorId,
- EntityId: &entities.RuleChainId{Id: args.RuleChainId},
- EventType: ruleEngine.DELETED,
- }
- c.actorContext.AppActor.TellWithHighPriority(msg)
- }
- return nil
- }
- // UpdateRuleChain 规则链生命周期-更新
- func (c *Controller) UpdateRuleChain(args rpcs.ArgsRuleChainAct, reply *rpcs.ReplyEmptyResult) error {
- if c.actorContext != nil {
- msg := &ruleEngine.ComponentLifecycleMsg{
- TenantId: args.VendorId,
- EntityId: &entities.RuleChainId{Id: args.RuleChainId},
- EventType: ruleEngine.UPDATED,
- }
- c.actorContext.AppActor.TellWithHighPriority(msg)
- }
- return nil
- }
- //SendCommand 下发设备控制指令
- func (c *Controller) SendCommand(args rpcs.ArgsSendCommand, reply *rpcs.ReplySendCommand) error {
- rpchost, err := getAccessRPCHost(args.DeviceId)
- if err != nil {
- return err
- }
- return server.RPCCallByHost(rpchost, "Access.SendCommand", args, reply)
- }
- func getAccessRPCHost(deviceid string) (string, error) {
- args := rpcs.ArgsGetDeviceOnlineStatus{
- Id: deviceid,
- }
- reply := &rpcs.ReplyGetDeviceOnlineStatus{}
- err := server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetDeviceOnlineStatus", args, reply)
- if err != nil {
- return "", err
- }
- return reply.AccessRPCHost, nil
- }
- //ActorSystem actor system
- type ActorSystem struct {
- rootActor ruleEngine.Ref
- }
- // 初始化actor system
- func (c *Controller) initActorSystem() (*ActorSystem, error) {
- system := ruleEngine.NewDefaultActorSystem(&ruleEngine.DefaultActorSystemConfig{
- SchedulerPoolSize: 5,
- AppDispatcherPoolSize: 4,
- TenantDispatcherPoolSize: 4,
- RuleEngineDispatcherPoolSize: 4,
- })
- _ = system.CreateDispatcher(ruleEngine.APP_DISPATCHER_NAME, ruleEngine.NewPoolDispatcher(0))
- _ = system.CreateDispatcher(ruleEngine.TENANT_DISPATCHER_NAME, ruleEngine.NewPoolDispatcher(0))
- _ = system.CreateDispatcher(ruleEngine.RULE_DISPATCHER_NAME, ruleEngine.NewPoolDispatcher(0))
- // init services
- tenantService := &TenantService{}
- ruleChainService := &RuleChainService{}
- actorContext := ruleEngine.NewSystemContext(system, ruleEngine.SystemContextServiceConfig{
- ClusterService: c.cluster,
- RuleChainService: ruleChainService,
- TenantService: tenantService,
- EventService: NewEventService(),
- })
- appActor, err := system.CreateRootActor(ruleEngine.APP_DISPATCHER_NAME,
- actors.NewAppActorCreator(actorContext))
- if err != nil {
- return nil, err
- }
- actorContext.AppActor = appActor
- server.Log.Debugln("actor system initialized")
- time.Sleep(time.Second * 1)
- appActor.Tell(&ruleEngine.AppInitMsg{})
- c.actorContext = actorContext
- return &ActorSystem{rootActor: appActor}, nil
- }
- // 启动mq consumers
- func (c *Controller) launchConsumer() {
- msgs, err := c.consumer.Pop(100 * time.Millisecond)
- if err != nil {
- server.Log.Error(err)
- }
- for {
- select {
- case msg := <-msgs:
- ruleEngineMsg := &protocol.Message{}
- if err := ruleEngineMsg.Decode(msg.GetData()); err != nil {
- fmt.Println("解析消息失败")
- }
- tenantId := msg.GetHeaders().Get("tenant_id")
- if c.actorContext != nil {
- c.actorContext.Tell(&ruleEngine.QueueToRuleEngineMsg{
- TenantId: string(tenantId),
- Message: ruleEngineMsg,
- })
- }
- }
- }
- }
|