|
| 1 | +""" |
| 2 | +Module for including functions of AWS IoT operations of picocell module. |
| 3 | +""" |
| 4 | + |
| 5 | +import time |
| 6 | + |
| 7 | +from core.temp import config |
| 8 | +from core.utils.manager import StateManager, Step |
| 9 | +from core.utils.status import Status |
| 10 | + |
| 11 | +class AWS: |
| 12 | + """ |
| 13 | + Class for including functions of AWS IoT operations of picocell module. |
| 14 | + """ |
| 15 | + cache = config["cache"] |
| 16 | + |
| 17 | + def __init__(self, base, network, ssl, mqtt, http): |
| 18 | + """ |
| 19 | + Constructor of the class. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + cache : dict |
| 24 | + Cache of the class. |
| 25 | + """ |
| 26 | + self.base = base |
| 27 | + self.network = network |
| 28 | + self.ssl = ssl |
| 29 | + self.mqtt = mqtt |
| 30 | + self.http = http |
| 31 | + |
| 32 | + def publish_message(self, payload, host=None, port=None, topic=None): |
| 33 | + """ |
| 34 | + Function for publishing a message to AWS IoT by using MQTT. |
| 35 | +
|
| 36 | + Parameters |
| 37 | + ---------- |
| 38 | + payload : str |
| 39 | + Payload of the message. |
| 40 | + host : str |
| 41 | + Host of the MQTT broker. |
| 42 | + port : int |
| 43 | + Port of the MQTT broker. |
| 44 | + topic : str |
| 45 | + Topic of the message. |
| 46 | +
|
| 47 | + Returns |
| 48 | + ------- |
| 49 | + (status, modem_response) : tuple |
| 50 | + status : int |
| 51 | + Status of the command. |
| 52 | + modem_response : str |
| 53 | + Response of the modem. |
| 54 | + """ |
| 55 | + |
| 56 | + step_network_reg = Step( |
| 57 | + function=self.network.register_network, |
| 58 | + name="register_network", |
| 59 | + success="pdp_deactivate", |
| 60 | + fail="failure", |
| 61 | + ) |
| 62 | + |
| 63 | + step_pdp_deactivate = Step( |
| 64 | + function=self.network.deactivate_pdp_context, |
| 65 | + name="pdp_deactivate", |
| 66 | + success="pdp_activate", |
| 67 | + fail="failure", |
| 68 | + ) |
| 69 | + |
| 70 | + step_pdp_activate= Step( |
| 71 | + function=self.network.activate_pdp_context, |
| 72 | + name="pdp_activate", |
| 73 | + success="set_mqtt_version", |
| 74 | + fail="failure", |
| 75 | + ) |
| 76 | + |
| 77 | + step_set_mqtt_version = Step( |
| 78 | + function=self.mqtt.set_version_config, |
| 79 | + name="set_mqtt_version", |
| 80 | + success="set_mqtt_ssl_mode", |
| 81 | + fail="failure", |
| 82 | + ) |
| 83 | + |
| 84 | + step_set_mqtt_ssl_mode = Step( |
| 85 | + function=self.mqtt.set_ssl_mode_config, |
| 86 | + name="set_mqtt_ssl_mode", |
| 87 | + success="open_mqtt_connection", |
| 88 | + fail="failure", |
| 89 | + ) |
| 90 | + |
| 91 | + step_open_mqtt_connection = Step( |
| 92 | + function=self.mqtt.open_connection, |
| 93 | + name="open_mqtt_connection", |
| 94 | + success="connect_mqtt_broker", |
| 95 | + fail="failure", |
| 96 | + function_params={"host":host, "port":port}, |
| 97 | + cachable=True, |
| 98 | + ) |
| 99 | + |
| 100 | + step_connect_mqtt_broker = Step( |
| 101 | + function=self.mqtt.connect_broker, |
| 102 | + name="connect_mqtt_broker", |
| 103 | + success="publish_message", |
| 104 | + fail="failure", |
| 105 | + cachable=True, |
| 106 | + ) |
| 107 | + |
| 108 | + step_publish_message = Step( |
| 109 | + function=self.mqtt.publish_message, |
| 110 | + name="publish_message", |
| 111 | + success="success", |
| 112 | + fail="failure", |
| 113 | + function_params={"payload":payload, "topic":topic}, |
| 114 | + cachable=True, |
| 115 | + ) |
| 116 | + |
| 117 | + # Add cache if it is not already existed |
| 118 | + function_name = "aws.publish_message" |
| 119 | + |
| 120 | + sm = StateManager(first_step = step_network_reg, function_name=function_name) |
| 121 | + |
| 122 | + sm.add_step(step_network_reg) |
| 123 | + sm.add_step(step_pdp_deactivate) |
| 124 | + sm.add_step(step_pdp_activate) |
| 125 | + sm.add_step(step_set_mqtt_version) |
| 126 | + sm.add_step(step_set_mqtt_ssl_mode) |
| 127 | + sm.add_step(step_open_mqtt_connection) |
| 128 | + sm.add_step(step_connect_mqtt_broker) |
| 129 | + sm.add_step(step_publish_message) |
| 130 | + |
| 131 | + while True: |
| 132 | + result = sm.run() |
| 133 | + |
| 134 | + if result["status"] == Status.SUCCESS: |
| 135 | + return result |
| 136 | + elif result["status"] == Status.ERROR: |
| 137 | + return result |
| 138 | + time.sleep(result["interval"]) |
| 139 | + |
| 140 | + def post_message(self, payload, url=None): |
| 141 | + """ |
| 142 | + Function for publishing a message to AWS IoT by using HTTPS. |
| 143 | +
|
| 144 | + Parameters |
| 145 | + ---------- |
| 146 | + payload : str |
| 147 | + Payload of the message. |
| 148 | + url : str |
| 149 | + URL of the AWS device shadow |
| 150 | +
|
| 151 | + Returns |
| 152 | + ------- |
| 153 | + (status, modem_response) : tuple |
| 154 | + status : int |
| 155 | + Status of the command. |
| 156 | + modem_response : str |
| 157 | + Response of the modem. |
| 158 | + """ |
| 159 | + |
| 160 | + step_network_reg = Step( |
| 161 | + function=self.network.register_network, |
| 162 | + name="register_network", |
| 163 | + success="pdp_deactivate", |
| 164 | + fail="failure", |
| 165 | + ) |
| 166 | + |
| 167 | + step_pdp_deactivate = Step( |
| 168 | + function=self.network.deactivate_pdp_context, |
| 169 | + name="pdp_deactivate", |
| 170 | + success="pdp_activate", |
| 171 | + fail="failure", |
| 172 | + ) |
| 173 | + |
| 174 | + step_pdp_activate= Step( |
| 175 | + function=self.network.activate_pdp_context, |
| 176 | + name="pdp_activate", |
| 177 | + success="ssl_configuration", |
| 178 | + fail="failure", |
| 179 | + cachable=True, |
| 180 | + ) |
| 181 | + |
| 182 | + step_ssl_configuration = Step( |
| 183 | + function=self.ssl.configure_for_x509_certification, |
| 184 | + name="ssl_configuration", |
| 185 | + success="http_ssl_configuration", |
| 186 | + fail="failure", |
| 187 | + cachable=True, |
| 188 | + ) |
| 189 | + |
| 190 | + step_http_ssl_configuration = Step( |
| 191 | + function=self.http.set_ssl_context_id, |
| 192 | + name="http_ssl_configuration", |
| 193 | + success="set_server_url", |
| 194 | + fail="failure", |
| 195 | + function_params={"id": 2}, |
| 196 | + cachable=True, |
| 197 | + ) |
| 198 | + |
| 199 | + step_set_server_url = Step( |
| 200 | + function=self.http.set_server_url, |
| 201 | + name="set_server_url", |
| 202 | + success="post_request", |
| 203 | + fail="failure", |
| 204 | + function_params={"url": url}, |
| 205 | + cachable=True, |
| 206 | + ) |
| 207 | + |
| 208 | + step_post_request = Step( |
| 209 | + function=self.http.post, |
| 210 | + name="post_request", |
| 211 | + success="read_response", |
| 212 | + fail="failure", |
| 213 | + function_params={"data": payload}, |
| 214 | + cachable=True, |
| 215 | + ) |
| 216 | + |
| 217 | + step_read_response = Step( |
| 218 | + function=self.http.read_response, |
| 219 | + name="read_response", |
| 220 | + success="success", |
| 221 | + fail="failure", |
| 222 | + ) |
| 223 | + |
| 224 | + # Add cache if it is not already existed |
| 225 | + function_name = "aws.post_message" |
| 226 | + |
| 227 | + sm = StateManager(first_step = step_network_reg, function_name=function_name) |
| 228 | + |
| 229 | + sm.add_step(step_network_reg) |
| 230 | + sm.add_step(step_pdp_deactivate) |
| 231 | + sm.add_step(step_pdp_activate) |
| 232 | + sm.add_step(step_ssl_configuration) |
| 233 | + sm.add_step(step_http_ssl_configuration) |
| 234 | + sm.add_step(step_set_server_url) |
| 235 | + sm.add_step(step_post_request) |
| 236 | + sm.add_step(step_read_response) |
| 237 | + |
| 238 | + while True: |
| 239 | + result = sm.run() |
| 240 | + |
| 241 | + if result["status"] == Status.SUCCESS: |
| 242 | + return result |
| 243 | + elif result["status"] == Status.ERROR: |
| 244 | + return result |
| 245 | + time.sleep(result["interval"]) |
0 commit comments