Skip to content

Commit 28bef9b

Browse files
Merge pull request #13 from sixfab/hotfix/restructuring-issues-2
refactor: Refactor example codes
2 parents 8eb40a5 + 583a58b commit 28bef9b

File tree

4 files changed

+44
-42
lines changed

4 files changed

+44
-42
lines changed

examples/http_custom_header_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
result = modem.network.check_network_registration()
4141
if result["status"] != Status.SUCCESS:
4242
debug.error("Could not connected to the cellular network.")
43-
43+
4444

4545
# Set the first HTTP context.
4646
modem.http.set_context_id()

examples/http_requests_example.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66
import time
77

88
from core.modem import Modem
9-
from core.status import Status
9+
from core.utils.status import Status
1010

1111
def handle_cellular_network_connection():
1212
"""This function handles the connection to cellular network connection.
1313
"""
1414
# Ask for return from the modem.
15-
modem.check_modem_communication()
15+
modem.base.check_communication()
1616
# Send APN details.
17-
modem.set_modem_apn(cid=1, apn="super")
17+
modem.network.set_apn(cid=1, apn="super")
18+
1819
# Try to get a operator response for 10 times.
19-
for _index in range(0, 10):
20+
for _ in range(0, 10):
2021
# Check if there is a connection to any operator.
21-
response = modem.get_operator_information()
22+
response = modem.network.get_operator_information()
2223
if response["status"] == Status.SUCCESS:
2324
return
2425
# Wait for 1 second.
@@ -28,9 +29,9 @@ def prepare_http_connection():
2829
"""This function prepares HTTP connection for modem.
2930
"""
3031
# Set the first HTTP context.
31-
modem.set_modem_http_context_id()
32+
modem.http.set_context_id()
3233
# Activate PDP.
33-
modem.activate_pdp_context()
34+
modem.network.activate_pdp_context()
3435

3536
def prepare_http_with_query(host_url, query_string=""):
3637
"""Sets the modem's HTTP server address.
@@ -42,7 +43,7 @@ def prepare_http_with_query(host_url, query_string=""):
4243
# Add query to the server URL.
4344
url_to_post = host_url + "/?" + query_string
4445
# Set the HTTP url.
45-
modem.set_modem_http_server_url(url=url_to_post)
46+
modem.http.set_server_url(url=url_to_post)
4647

4748
def send_post_request(server_url, data_dict, query_string=""):
4849
"""POST request to the HTTP server with a payload and optional query.
@@ -57,7 +58,7 @@ def send_post_request(server_url, data_dict, query_string=""):
5758
# Convert to data to JSON.
5859
data_to_post = json.dumps(data_dict)
5960
# Send a post request to the URL.
60-
print("POST Request: ", modem.http_post_request(data=data_to_post))
61+
print("POST Request: ", modem.http.post(data=data_to_post))
6162
# Wait for six seconds before the next operation.
6263
time.sleep(6)
6364

@@ -71,7 +72,7 @@ def send_get_request(server_url, query_string=""):
7172
# Configure the HTTP address.
7273
prepare_http_with_query(server_url, query_string)
7374
# Send a GET request.
74-
print("GET Request: ", modem.http_get_request())
75+
print("GET Request: ", modem.http.get())
7576
# Wait for six seconds before the next operation.
7677
time.sleep(6)
7778

@@ -88,20 +89,20 @@ def send_put_request(server_url, data_dict, query_string=""):
8889
# Convert to data to JSON.
8990
data_to_post = json.dumps(data_dict)
9091
# Send a PUT request.
91-
print("PUT Request: ", modem.http_put_request(data_to_post))
92+
print("PUT Request: ", modem.http.put(data_to_post))
9293
# Wait for six seconds before the next operation.
9394
time.sleep(6)
9495

9596

9697
if __name__ == "__main__":
9798
# Initilizate the modem.
98-
modem = Modem({})
99+
modem = Modem()
99100
# Connect to a cellular network.
100101
handle_cellular_network_connection()
101102
# Prepare for the HTTP context.
102103
prepare_http_connection()
103104
# Select a HOST for testing purposes.
104-
HOST = "https://webhook.site/6112efa6-4f8c-4bdb-8fa3-5787cc02f80a"
105+
HOST = "https://webhook.site/e2519ab2-9f74-48c3-9346-0e2e93fa6ff3"
105106
# Send a POST request to the server.
106107
send_post_request(HOST, {"ProjectTopic": "PicocellHTTPExample"}, "device=Pico&try=0")
107108
# Send a GET request to the server.

examples/http_requests_example_with_manager.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import time
77

88
from core.modem import Modem
9-
from core.status import Status
10-
from core.manager import StateManager, Step
9+
from core.utils.status import Status
10+
from core.utils.manager import StateManager, Step
1111

1212
def step_for_setting_server_url(modem_object, next_function, url=None, query=""):
1313
"""This function returns a step for setting server's URL with a query.
@@ -25,7 +25,7 @@ def step_for_setting_server_url(modem_object, next_function, url=None, query="")
2525
url_with_query = url + "/?" + query
2626
# Set-up the steps for state manager.
2727
step_set_server_url = Step(
28-
function=modem_object.set_modem_http_server_url,
28+
function=modem_object.http.set_server_url,
2929
name="set_server_url",
3030
success=next_function,
3131
fail="failure",
@@ -49,21 +49,21 @@ def prepare_communication_for_http(modem_object):
4949

5050
# Set-up the steps for state manager.
5151
step_network_reg = Step(
52-
function=modem_object.register_network,
52+
function=modem_object.network.register_network,
5353
name="register_network",
5454
success="pdp_deactivate",
5555
fail="failure"
5656
)
5757

5858
step_pdp_deactivate = Step(
59-
function=modem_object.deactivate_pdp_context,
59+
function=modem_object.network.deactivate_pdp_context,
6060
name="pdp_deactivate",
6161
success="pdp_activate",
6262
fail="failure"
6363
)
6464

6565
step_pdp_activate = Step(
66-
function=modem_object.activate_pdp_context,
66+
function=modem_object.network.activate_pdp_context,
6767
name="pdp_activate",
6868
success="success",
6969
fail="failure",
@@ -76,7 +76,6 @@ def prepare_communication_for_http(modem_object):
7676

7777
# Create the state manager.
7878
state_manager = StateManager(first_step=step_network_reg,
79-
cache=modem_object.cache,
8079
function_name=function_name)
8180

8281
# Add each step.
@@ -112,7 +111,7 @@ def post_message_to_http_server(modem_object, payload, url=None, query=""):
112111
modem_object, "post_request", url, query)
113112

114113
step_post_request = Step(
115-
function=modem_object.http_post_request,
114+
function=modem_object.http.post,
116115
name="post_request",
117116
success="success",
118117
fail="failure",
@@ -126,7 +125,6 @@ def post_message_to_http_server(modem_object, payload, url=None, query=""):
126125

127126
# Create the state manager.
128127
state_manager = StateManager(first_step=step_set_server_url,
129-
cache=modem_object.cache,
130128
function_name=function_name)
131129

132130
# Add each step.
@@ -160,7 +158,7 @@ def get_message_to_http_server(modem_object, url=None, query=""):
160158
modem_object, "get_request", url, query)
161159

162160
step_get_request = Step(
163-
function=modem_object.http_get_request,
161+
function=modem_object.http.get,
164162
name="get_request",
165163
success="success",
166164
fail="failure",
@@ -173,7 +171,6 @@ def get_message_to_http_server(modem_object, url=None, query=""):
173171

174172
# Create the state manager.
175173
state_manager = StateManager(first_step=step_set_server_url,
176-
cache=modem_object.cache,
177174
function_name=function_name)
178175

179176
# Add each step.
@@ -208,7 +205,7 @@ def put_message_to_http_server(modem_object, payload, url=None, query=""):
208205
modem_object, "put_request", url, query)
209206

210207
step_put_request = Step(
211-
function=modem_object.http_put_request,
208+
function=modem_object.http.put,
212209
name="put_request",
213210
success="success",
214211
fail="failure",
@@ -222,7 +219,6 @@ def put_message_to_http_server(modem_object, payload, url=None, query=""):
222219

223220
# Create the state manager.
224221
state_manager = StateManager(first_step=step_set_server_url,
225-
cache=modem_object.cache,
226222
function_name=function_name)
227223

228224
# Add each step.
@@ -241,24 +237,30 @@ def put_message_to_http_server(modem_object, payload, url=None, query=""):
241237

242238
if __name__ == "__main__":
243239
# Initilize the modem object.
244-
modem = Modem({})
240+
modem = Modem()
241+
245242
# Prepare communication for HTTP.
246243
prepare_communication_for_http(modem)
244+
247245
# Server address to send requests.
248-
HOST = "https://webhook.site/6112efa6-4f8c-4bdb-8fa3-5787cc02f80a"
246+
HOST = "https://webhook.site/e2519ab2-9f74-48c3-9346-0e2e93fa6ff3"
249247
# Prepare a payload to post into the server.
250248
payload_to_post = {"ProjectTopic": "PicocellHTTPExampleWithManager"}
251249
payload_to_post_as_json = json.dumps(payload_to_post)
250+
252251
# Post it to the server with given query.
253252
post_message_to_http_server(
254253
modem, payload_to_post_as_json, HOST, "device=Pico&try=0")
255254
time.sleep(4)
255+
256256
# Get a request from the server.
257257
get_message_to_http_server(modem, HOST, "company=Sixfab&course=Picocell")
258258
time.sleep(6)
259+
259260
# Prepare a payload to put into the server.
260261
payload_to_put = {"Temperature(*C)": 28}
261262
payload_to_put_as_json = json.dumps(payload_to_put)
263+
262264
# Send the request into the server.
263265
put_message_to_http_server(
264266
modem, payload_to_put_as_json, HOST, "sensor=Garden2")

examples/telegram_example.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66
import time
77

88
from core.modem import Modem
9-
from core.atcom import ATCom
9+
from core.utils.atcom import ATCom
1010

1111
config = {}
1212

1313
############################
1414
### Telegram API example ###
1515
############################
1616

17-
modem = Modem(config)
17+
modem = Modem()
1818
atcom = ATCom()
1919

2020
# Write your text message here!
2121
user_text = 'Your text message here!'
2222

2323
# Telegram Bot configurations
24-
bot_token = 'YOUT_BOT_TOKEN'
25-
bot_chatID = 'YOUR_BOT_CHAT_ID'
24+
bot_token = '5469433192:AAH3L7t2RPrqHsY1r9rvXO_DU67Z5NdtMMw'
25+
bot_chatID = '-645292239'
2626

2727
HOST = "api.telegram.org/bot"
2828
PAYLOAD_JSON = {""}
@@ -31,22 +31,21 @@
3131
publish_url = 'https://' + HOST + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&text=' + user_text
3232

3333
# Check communication with modem
34-
print("COM: ", modem.check_modem_communication())
34+
print("COM: ", modem.base.check_communication())
3535
print("Set APN: ", atcom.send_at_comm('AT+CGDCONT=1,"IP","super"',"OK"))
3636
print("COPS: ", atcom.retry_at_comm("AT+COPS?","+COPS: 0,0", timeout=1, retry_count=10))
3737
print(atcom.send_at_comm('AT+QICSGP=1,1,"super","","",1',"OK"))
3838

3939
# TCP/IP
40-
print("TCPIP Context Configuration: ", modem.set_modem_http_context_id())
41-
print("PDP Deactivation: ", modem.deactivate_pdp_context())
42-
print("PDP Activatation: ", modem.activate_pdp_context())
40+
print("TCPIP Context Configuration: ", modem.http.set_context_id())
41+
print("PDP Deactivation: ", modem.network.deactivate_pdp_context())
42+
print("PDP Activatation: ", modem.network.activate_pdp_context())
4343
print("PDP Test: ", atcom.send_at_comm("AT+CGACT?","OK"))
4444

4545
# HTTP
46-
print("Set HTTP SSL Context", modem.set_modem_http_ssl_context_id(2))
47-
print("HTTP URL: ", modem.set_modem_http_server_url(url=publish_url))
46+
print("Set HTTP SSL Context", modem.http.set_ssl_context_id(2))
47+
print("HTTP URL: ", modem.http.set_server_url(url=publish_url))
4848
time.sleep(6)
49-
print("HTTP GET: ", modem.http_get_request())
49+
print("HTTP GET: ", modem.http.get())
5050
time.sleep(6)
51-
print("HTTP READ: ", modem.http_read_response())
52-
51+
print("HTTP READ: ", modem.http.read_response())

0 commit comments

Comments
 (0)