notification.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package notification
  2. import (
  3. "container/list"
  4. "log"
  5. )
  6. /*
  7. Notification Producer and Consumer Queue
  8. This module is designed to route the notification from module that produce it
  9. to all the devices or agent that can reach the user
  10. */
  11. type NotificationPayload struct {
  12. ID string //Notification ID, generate by producer
  13. Title string //Title of the notification
  14. Message string //Message of the notification
  15. Receiver []string //Receiver, username in arozos system
  16. Sender string //Sender, the sender or module of the notification
  17. ReciverAgents []string //Agent name that have access to this notification
  18. }
  19. type AgentProducerFunction func(*NotificationPayload) error
  20. type Agent interface {
  21. //Defination of the agent
  22. Name() string //The name of the notification agent, must be unique
  23. Desc() string //Basic description of the agent
  24. IsConsumer() bool //Can receive notification can arozos core
  25. IsProducer() bool //Can produce notification to arozos core
  26. ConsumerNotification(*NotificationPayload) error //Endpoint for arozos -> this agent
  27. ProduceNotification(*AgentProducerFunction) //Endpoint for this agent -> arozos
  28. }
  29. type NotificationQueue struct {
  30. Agents []*Agent
  31. MasterQueue *list.List
  32. }
  33. func NewNotificationQueue() *NotificationQueue {
  34. thisQueue := list.New()
  35. return &NotificationQueue{
  36. Agents: []*Agent{},
  37. MasterQueue: thisQueue,
  38. }
  39. }
  40. //Add a notification agent to the queue
  41. func (q *NotificationQueue) RegisterNotificationAgent(agent Agent) {
  42. q.Agents = append(q.Agents, &agent)
  43. }
  44. func (q *NotificationQueue) BroadcastNotification(message *NotificationPayload) error {
  45. //Send notification to consumer agents
  46. for _, agent := range q.Agents {
  47. thisAgent := *agent
  48. inAgentList := false
  49. for _, enabledAgent := range message.ReciverAgents {
  50. if enabledAgent == thisAgent.Name() {
  51. //This agent is activated
  52. inAgentList = true
  53. break
  54. }
  55. }
  56. if !inAgentList {
  57. //Skip this agent and continue
  58. continue
  59. }
  60. //Send this notification via this agent
  61. err := thisAgent.ConsumerNotification(message)
  62. if err != nil {
  63. log.Println("[Notification] Unable to send message via notification agent: " + thisAgent.Name())
  64. }
  65. }
  66. log.Println("[Notification] Message titled: " + message.Title + " (ID: " + message.ID + ") broadcasted")
  67. return nil
  68. }