Skip to content

Commit 2d06b30

Browse files
selengalpbisguzar
authored andcommitted
feat: Make changes below before base structure PR
* Add native HTTP function by using manager module * Clean test server info
1 parent d88bb5a commit 2d06b30

File tree

6 files changed

+133
-27
lines changed

6 files changed

+133
-27
lines changed

core/modem.py

Lines changed: 123 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,6 @@ def open_mqtt_connection(self, cid=0, host="", port=8883):
14481448

14491449
if result["status"] == Status.SUCCESS:
14501450
result = self.atcom.get_response(desired_response, timeout=60)
1451-
return result
14521451
return result
14531452

14541453
def close_mqtt_connection(self, cid=0):
@@ -1477,7 +1476,6 @@ def close_mqtt_connection(self, cid=0):
14771476
if result["status"] == Status.SUCCESS:
14781477
desired_response = f"+QMTCLOSE: {cid},0"
14791478
result = self.atcom.get_response(desired_response, timeout=60)
1480-
return result
14811479
return result
14821480

14831481
def connect_mqtt_broker(self, cid=0, client_id_string="picocell", username=None, password=None):
@@ -1528,7 +1526,6 @@ def connect_mqtt_broker(self, cid=0, client_id_string="picocell", username=None,
15281526
if result["status"] == Status.SUCCESS:
15291527
desired_response = f"+QMTCONN: {cid},0,0"
15301528
result = self.atcom.get_response(desired_response, timeout=60)
1531-
return result
15321529
return result
15331530

15341531
def disconnect_mqtt_broker(self, cid=0):
@@ -1604,7 +1601,6 @@ def subscribe_mqtt_topic(self, cid=0, message_id=1, topic="", qos=0):
16041601
if result["status"] == Status.SUCCESS:
16051602
desired_response = f"+QMTSUB: {cid},{message_id},0"
16061603
result = self.atcom.get_response(desired_response, timeout=60)
1607-
return result
16081604
return result
16091605

16101606
def unsubscribe_mqtt_topic(self, cid=0, message_id=1, topic=""):
@@ -1687,16 +1683,15 @@ def publish_mqtt_message(self, cid=0, message_id=1, qos=1, retain=0, topic="", p
16871683
2 --> Failed to send a packet
16881684
value : int
16891685
If <result> is 1, number of times a packet has been retransmitted.
1690-
If <result> is 0 or 2, it will not be presente
1686+
If <result> is 0 or 2, it will not be presented
16911687
"""
16921688
command = f'AT+QMTPUB={cid},{message_id},{qos},{retain},"{topic}"'
16931689
result = self.atcom.send_at_comm(command,">")
16941690

16951691
if result["status"] == Status.SUCCESS:
1696-
self.atcom.send_at_comm_once(payload,"OK") # Send message
1697-
return self.atcom.send_at_comm(self.CTRL_Z,"OK") # Send end char --> CTRL+Z
1698-
else:
1699-
return result
1692+
self.atcom.send_at_comm_once(payload, line_end=False) # Send message
1693+
result = self.atcom.send_at_comm(self.CTRL_Z,"OK") # Send end char --> CTRL+Z
1694+
return result
17001695

17011696
def check_any_mqtt_messages(self, cid=0):
17021697
"""
@@ -1985,6 +1980,17 @@ def publish_message_to_aws(self, payload, host=None, port=None, topic=None):
19851980
"""
19861981
Function for publishing a message to AWS IoT by using MQTT.
19871982
1983+
Parameters
1984+
----------
1985+
payload : str
1986+
Payload of the message.
1987+
host : str
1988+
Host of the MQTT broker.
1989+
port : int
1990+
Port of the MQTT broker.
1991+
topic : str
1992+
Topic of the message.
1993+
19881994
Returns
19891995
-------
19901996
(status, modem_response) : tuple
@@ -2091,3 +2097,111 @@ def publish_message_to_aws(self, payload, host=None, port=None, topic=None):
20912097
elif result["status"] == Status.ERROR:
20922098
return result
20932099
time.sleep(result["interval"])
2100+
2101+
def publish_message_to_aws_https(self, payload, url=None):
2102+
"""
2103+
Function for publishing a message to AWS IoT by using HTTPS.
2104+
2105+
Parameters
2106+
----------
2107+
payload : str
2108+
Payload of the message.
2109+
url : str
2110+
URL of the AWS device shadow
2111+
2112+
Returns
2113+
-------
2114+
(status, modem_response) : tuple
2115+
status : int
2116+
Status of the command.
2117+
modem_response : str
2118+
Response of the modem.
2119+
"""
2120+
2121+
step_network_reg = Step(
2122+
function=self.register_network,
2123+
name="register_network",
2124+
success="pdp_deactivate",
2125+
fail="failure",
2126+
)
2127+
2128+
step_pdp_deactivate = Step(
2129+
function=self.deactivate_pdp_context,
2130+
name="pdp_deactivate",
2131+
success="pdp_activate",
2132+
fail="failure",
2133+
)
2134+
2135+
step_pdp_activate= Step(
2136+
function=self.activate_pdp_context,
2137+
name="pdp_activate",
2138+
success="ssl_configuration",
2139+
fail="failure",
2140+
cachable=True,
2141+
)
2142+
2143+
step_ssl_configuration = Step(
2144+
function=self.configure_modem_ssl_for_x509_certification,
2145+
name="ssl_configuration",
2146+
success="http_ssl_configuration",
2147+
fail="failure",
2148+
cachable=True,
2149+
)
2150+
2151+
step_http_ssl_configuration = Step(
2152+
function=self.set_modem_http_ssl_context_id,
2153+
name="http_ssl_configuration",
2154+
success="set_server_url",
2155+
fail="failure",
2156+
function_params={"id": 2},
2157+
cachable=True,
2158+
)
2159+
2160+
step_set_server_url = Step(
2161+
function=self.set_modem_http_server_url,
2162+
name="set_server_url",
2163+
success="post_request",
2164+
fail="failure",
2165+
function_params={"url": url},
2166+
cachable=True,
2167+
)
2168+
2169+
step_post_request = Step(
2170+
function=self.http_post_request,
2171+
name="post_request",
2172+
success="read_response",
2173+
fail="failure",
2174+
function_params={"data": payload},
2175+
cachable=True,
2176+
)
2177+
2178+
step_read_response = Step(
2179+
function=self.http_read_response,
2180+
name="read_response",
2181+
success="success",
2182+
fail="failure",
2183+
)
2184+
2185+
# Add cache if it is not already existed
2186+
function_name = "publish_message_to_aws_https"
2187+
2188+
sm = StateManager(first_step = step_network_reg,
2189+
cache=self.cache, function_name=function_name)
2190+
2191+
sm.add_step(step_network_reg)
2192+
sm.add_step(step_pdp_deactivate)
2193+
sm.add_step(step_pdp_activate)
2194+
sm.add_step(step_ssl_configuration)
2195+
sm.add_step(step_http_ssl_configuration)
2196+
sm.add_step(step_set_server_url)
2197+
sm.add_step(step_post_request)
2198+
sm.add_step(step_read_response)
2199+
2200+
while True:
2201+
result = sm.run()
2202+
2203+
if result["status"] == Status.SUCCESS:
2204+
return result
2205+
elif result["status"] == Status.ERROR:
2206+
return result
2207+
time.sleep(result["interval"])

examples/aws_https_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
modem = Modem(config)
1818
atcom = ATCom()
1919

20-
HOST = "a2q4ztq1aigmmt-ats.iot.us-west-2.amazonaws.com"
21-
TOPIC = "$aws/things/picocell_test/shadow/update"
20+
HOST = "[CHANGE WITH YOUR AWS IOT ENDPOINT]"
21+
TOPIC = "[CHANGE WITH YOUR AWS IOT TOPIC]"
2222
PAYLOAD_JSON = {"state": {"reported": {"Status": "HTTPS TEST MESSAGE!"}}}
2323
payload = json.dumps(PAYLOAD_JSON)
2424
publish_url = 'https://' + HOST + ':8443/topics/' + TOPIC + '?qos=1'

examples/aws_https_example_with_manager.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111
modem = Modem(config)
1212
atcom=ATCom()
1313

14-
HOST = "a2q4ztq1aigmmt-ats.iot.us-west-2.amazonaws.com"
15-
PORT = 8883
16-
TOPIC = "$aws/things/picocell_test/shadow/update"
14+
HOST = "[CHANGE WITH YOUR AWS IOT ENDPOINT]"
15+
TOPIC = "[CHANGE WITH YOUR AWS IOT TOPIC]"
1716
PAYLOAD_JSON = {"state": {"reported": {"Status": "Test message from Picocell!"}}}
17+
server_url = 'https://' + HOST + ':8443/topics/' + TOPIC + '?qos=1'
1818
payload = json.dumps(PAYLOAD_JSON)
1919

20-
print(modem.set_modem_http_context_id(1))
21-
time.sleep(1)
22-
print(atcom.send_at_comm('AT+QHTTPCFG="contextid"',"OK"))
20+
print(modem.publish_message_to_aws_https(payload, server_url))

examples/aws_mqtts_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
modem = Modem(config)
1717
atcom = ATCom()
1818

19-
HOST = "a2q4ztq1aigmmt-ats.iot.us-west-2.amazonaws.com"
19+
HOST = "[CHANGE WITH YOUR AWS IOT ENDPOINT]"
20+
TOPIC = "[CHANGE WITH YOUR AWS IOT TOPIC]"
2021
PORT = 8883
21-
TOPIC = "$aws/things/picocell_test/shadow/update"
2222
PAYLOAD_JSON = {"state": {"reported": {"Status": "Hello from Picocell!"}}}
2323
payload = json.dumps(PAYLOAD_JSON)
2424

examples/aws_mqtts_example_with_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
config = {}
99
modem = Modem(config)
1010

11-
HOST = "a2q4ztq1aigmmt-ats.iot.us-west-2.amazonaws.com"
11+
HOST = "[CHANGE WITH YOUR AWS IOT ENDPOINT]"
12+
TOPIC = "[CHANGE WITH YOUR AWS IOT TOPIC]"
1213
PORT = 8883
13-
TOPIC = "$aws/things/picocell_test/shadow/update"
1414
PAYLOAD_JSON = {"state": {"reported": {"Status": "Test message from Picocell!"}}}
1515
payload = json.dumps(PAYLOAD_JSON)
1616

examples/listener_example.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212
atcom = ATCom()
1313
modem = Modem(config)
1414

15-
HOST = "a2q4ztq1aigmmt-ats.iot.us-west-2.amazonaws.com"
16-
PORT = 8883
17-
TOPIC = "$aws/things/picocell_test/shadow/update"
18-
PAYLOAD_JSON = {"state": {"reported": {"Status": "Test message from Picocell!"}}}
19-
payload = json.dumps(PAYLOAD_JSON)
20-
2115

2216
def callback(message):
2317
"""Example callback function"""

0 commit comments

Comments
 (0)