|
| 1 | +/** |
| 2 | + * @file Queue.cpp |
| 3 | + * @author Edison |
| 4 | + * @brief Encolador de mensajes para main |
| 5 | + * @version 1.0 |
| 6 | + * @date 2021-01-05 |
| 7 | + * |
| 8 | + * @copyright DataAnalitic S.A. (c) {2020} |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 12 | +#include "Arduino.h" |
| 13 | +#include "Queue.h" |
| 14 | + |
| 15 | + |
| 16 | +//Message functions |
| 17 | + |
| 18 | +/** |
| 19 | + * [Message::set_ID description] |
| 20 | + * @param _id [description] |
| 21 | + */ |
| 22 | +void Message::set_subject(String _subject){ |
| 23 | + Subject = _subject; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * [Message::set_priority description] |
| 28 | + * @param _priority [description] |
| 29 | + */ |
| 30 | +void Message::set_priority(int _priority){ |
| 31 | + priority = _priority; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * [Message::set_pushed description] |
| 36 | + * @param _pushed [description] |
| 37 | + */ |
| 38 | +void Message::set_pushed(int _pushed){ |
| 39 | + pushed = _pushed; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * [Message::set_data description] |
| 44 | + * @param _data [description] |
| 45 | + */ |
| 46 | +void Message::set_data(String _data){ |
| 47 | + data = _data; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | +* [Message::set_timestamp description] |
| 52 | +* @return [description] |
| 53 | +*/ |
| 54 | +void Message::set_timestamp(DateTime _now){ |
| 55 | + timestamp = TimeSpan(_now.day(), |
| 56 | + _now.hour(), |
| 57 | + _now.minute(), |
| 58 | + _now.second()); |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * [Message::get_ID description] |
| 63 | + * @return [description] |
| 64 | + */ |
| 65 | +String Message::get_subject(){ |
| 66 | + return Subject; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * [Message::get_priority description] |
| 71 | + * @return [description] |
| 72 | + */ |
| 73 | +int Message::get_priority(){ |
| 74 | + return priority; |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * [Message::get_data description] |
| 79 | + * @return [description] |
| 80 | + */ |
| 81 | +String Message::get_data(){ |
| 82 | + return data; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * [Message::get_pushed description] |
| 87 | + * @return [description] |
| 88 | + */ |
| 89 | +int Message::get_pushed(){ |
| 90 | + return pushed; |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * [Message::get_timestamp description] |
| 95 | + * @return [description] |
| 96 | + */ |
| 97 | +TimeSpan Message::get_timestamp(){ |
| 98 | + return timestamp; |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +int Message::get_age(DateTime now){ |
| 103 | + DateTime difference = DateTime(now - timestamp); |
| 104 | + return difference.minute()*60+difference.second(); |
| 105 | +} |
| 106 | +/** |
| 107 | + * [Message::to_string description] |
| 108 | + * @return [description] |
| 109 | + */ |
| 110 | +String Message::to_string(){ |
| 111 | + |
| 112 | + return "{SUBJ:"+Subject+","+"priority:"+String(priority)+","+"pushed:"+String(pushed)+","+"data:"+data+"}"; |
| 113 | +} |
| 114 | + |
| 115 | +//Queue functions |
| 116 | + |
| 117 | +/** |
| 118 | + * [Queue::push description] |
| 119 | + * @param msg [description] |
| 120 | + */ |
| 121 | +void Queue::push(Message msg){ |
| 122 | + msg.set_pushed(msg.get_pushed()+1); |
| 123 | + queue.InsertTail(msg); |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * [Queue::requeue description] |
| 128 | + * @param msg [description] |
| 129 | + */ |
| 130 | +void Queue::requeue(Message msg){ |
| 131 | + queue.InsertHead(msg); |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * [Queue::pop description] |
| 136 | + * @return [description] |
| 137 | + */ |
| 138 | +Message Queue::pop(){ |
| 139 | + Message first; |
| 140 | + |
| 141 | + if (queue.GetSize()==1){ |
| 142 | + first = queue.GetTail(); |
| 143 | + queue.RemoveTail(); |
| 144 | + } |
| 145 | + |
| 146 | + else{ |
| 147 | + first = queue.GetHead(); |
| 148 | + queue.RemoveHead(); |
| 149 | + } |
| 150 | + |
| 151 | + return first; |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * [Queue::is_empty description] |
| 156 | + * @return [description] |
| 157 | + */ |
| 158 | +bool Queue::is_empty(){ |
| 159 | + return queue.GetSize()==0; |
| 160 | +} |
| 161 | + |
| 162 | +/** |
| 163 | + * [Queue::sort description] |
| 164 | + */ |
| 165 | +void Queue::sort(){ |
| 166 | + int pass; |
| 167 | +} |
| 168 | + |
| 169 | +/** |
| 170 | + * [Queue::get_size description] |
| 171 | + * @return [description] |
| 172 | + */ |
| 173 | +int Queue::get_size(){ |
| 174 | + return queue.GetSize(); |
| 175 | +} |
| 176 | + |
| 177 | +/** |
| 178 | + * [Queue::to_string description] |
| 179 | + * @return [description] |
| 180 | + */ |
| 181 | +String Queue::to_string(){ |
| 182 | + |
| 183 | + String data; |
| 184 | + |
| 185 | + |
| 186 | + if (is_empty()){ |
| 187 | + data = ""; |
| 188 | + } |
| 189 | + |
| 190 | + else if (queue.GetSize()==1){ |
| 191 | + data = queue.GetTail().to_string(); |
| 192 | + } |
| 193 | + |
| 194 | + else{ |
| 195 | + for (int i = 0 ; i< queue.GetSize(); i++){ |
| 196 | + String info = queue.GetAt(i).to_string(); |
| 197 | + data = data + info + "\n"; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + |
| 202 | + return data; |
| 203 | +} |
0 commit comments