github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/plugins/mqtt/message.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2016-2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package mqtt 20 21 import "github.com/e154/smart-home/system/supervisor" 22 23 // NewMessage ... 24 func NewMessage() (m *Message) { 25 m = &Message{ 26 storage: NewStorage(), 27 } 28 return 29 } 30 31 // Message ... 32 type Message struct { 33 Payload string `json:"payload"` 34 Topic string `json:"topic"` 35 Qos uint8 `json:"qos"` 36 Duplicate bool `json:"duplicate"` 37 storage Storage `json:"storage"` 38 Error string `json:"error"` 39 Success bool `json:"success"` 40 NewState supervisor.EntityStateParams `json:"new_state"` 41 } 42 43 func (m *Message) clearError() { 44 m.Error = "" 45 } 46 47 // SetError ... 48 func (m *Message) SetError(err string) { 49 m.Error = err 50 } 51 52 // Ok ... 53 func (m *Message) Ok() { 54 m.Success = true 55 } 56 57 // Clear ... 58 func (m *Message) Clear() { 59 m.storage.pull = make(map[string]interface{}) 60 m.Error = "" 61 } 62 63 // Copy ... 64 func (m *Message) Copy() (msg *Message) { 65 msg = NewMessage() 66 for k, v := range m.storage.pull { 67 msg.storage.SetVar(k, v) 68 } 69 return 70 } 71 72 // GetVar ... 73 func (m *Message) GetVar(key string) (value interface{}) { 74 return m.storage.GetVar(key) 75 } 76 77 // SetVar ... 78 func (m *Message) SetVar(key string, value interface{}) { 79 m.storage.SetVar(key, value) 80 } 81 82 // Update ... 83 func (m *Message) Update(newMsg *Message) { 84 m.Error = newMsg.Error 85 m.Success = newMsg.Success 86 m.Payload = newMsg.Payload 87 m.Topic = newMsg.Topic 88 m.Qos = newMsg.Qos 89 m.Duplicate = newMsg.Duplicate 90 m.storage.copy(newMsg.storage.pull) 91 }