|
| 1 | +# === Example: Initialize the SDK client === |
| 2 | + |
1 | 3 | from emnify import EMnify
|
2 | 4 | from emnify import constants as emnify_constants
|
3 | 5 |
|
4 |
| -emnify = EMnify(app_token='your token') |
| 6 | +# To operate the emnify SDK, you need to generate an application token. |
| 7 | +# Step-by-step guide: https://www.emnify.com/developer-blog/how-to-use-an-application-token-for-api-authentication |
| 8 | +emnify = EMnify(app_token='YOUR_TOKEN') |
| 9 | + |
5 | 10 | # [endblock]
|
6 |
| -# === Create and activate a device === |
7 | 11 |
|
| 12 | +# === Example: Create and activate a device === |
8 | 13 |
|
9 | 14 | unassigned_sims = [i for i in emnify.sim.get_sim_list(without_device=True)]
|
10 |
| -# If no unassigned_sims - register new one by batch code |
| 15 | +# If there aren't any unassigned SIMs, register a new one via batch code: |
11 | 16 | if not unassigned_sims:
|
12 |
| - registered_sim = emnify.sim.register_sim(bic='sample_bic_code') # Returns as list |
| 17 | + registered_sim = emnify.sim.register_sim(bic='EXAMPLE_BIC_CODE') # Returns a list |
13 | 18 | sim = emnify.sim.retrieve_sim(registered_sim[0].id)
|
14 | 19 | else:
|
15 |
| - sim = unassigned_sims[0] # Taking the first one unassigned sim |
| 20 | + sim = unassigned_sims[0] # Takes the first unassigned SIM |
16 | 21 |
|
17 |
| -# Defining of new device parameters |
18 |
| -# All required models can be retrieved through manager`s properties |
| 22 | +# To create your new device, you need to define the parameters. |
| 23 | +# You can retrieve all required models through the manager properties. |
| 24 | +# API reference: https://emnify.github.io/emnify-sdk-python/autoapi/emnify/modules/device/manager/index.html |
19 | 25 | service_profile = emnify.devices.service_profile_model(id=1)
|
20 | 26 | tariff_profile = emnify.devices.tariff_profile_model(id=1)
|
21 | 27 | device_status = emnify.devices.status_model(id=0)
|
|
28 | 34 | name=name
|
29 | 35 | )
|
30 | 36 |
|
31 |
| -# After creating model sdk returns id of device |
| 37 | +# After creating a model, the SDK returns the device ID. |
32 | 38 | device_id = emnify.devices.create_device(device=device_model)
|
33 |
| -# Then we can retrieve all device details |
| 39 | +# Then, you can retrieve the device details |
34 | 40 | device = emnify.devices.retrieve_device(device_id=device_id)
|
35 |
| -# Activate device |
| 41 | +# and finally, activate the device: |
36 | 42 | emnify.devices.change_status(device=device, enable=True)
|
37 | 43 |
|
38 |
| -# Retrieving updated device details |
| 44 | +# Retrieve updated device details |
39 | 45 | device = emnify.devices.retrieve_device(device_id=device_id)
|
40 |
| -device_status = device.status.description # Will be 'Enabled' |
41 |
| -sim_status = device.sim.status.description # Will be 'Activated' |
| 46 | +device_status = device.status.description # Device status is "Enabled" |
| 47 | +sim_status = device.sim.status.description # SIM status is "Activated" |
| 48 | + |
42 | 49 | # [endblock]
|
43 | 50 |
|
44 |
| -# === Configure a device === |
| 51 | +# === Example: Configure a device === |
45 | 52 |
|
46 |
| -# Getting details of device |
| 53 | +# Get device details |
47 | 54 | device = emnify.devices.retrieve_device(device_id=device_id)
|
48 | 55 |
|
49 |
| -tags = 'arduino, meter, temp' # Sample tags |
50 |
| -name = 'new name' # Sample name |
| 56 | +tags = 'arduino, meter, temp' # Example tags |
| 57 | +name = 'new name' # Example name |
51 | 58 |
|
52 |
| -# Adjust device config |
| 59 | +# Adjust the device configuration |
53 | 60 | update_device_fields = emnify.devices.device_update_model(name='new name', tags='arduino')
|
54 | 61 | emnify.devices.update_device(device_id=device.id, device=update_device_fields)
|
55 | 62 |
|
56 |
| -# Getting details of updated device |
| 63 | +# Get updated device details |
57 | 64 | updated_device = emnify.devices.retrieve_device(device_id=device_id)
|
58 |
| -device_tags = updated_device.tags # Will be arduino |
59 |
| -deivce_name = updated_device.name # Will be 'new name' |
| 65 | +device_tags = updated_device.tags # Updated tag is "arduino" |
| 66 | +deivce_name = updated_device.name # Updated name is "new name" |
60 | 67 | # [endblock]
|
61 | 68 |
|
62 |
| -# === Configure operator blacklist for device === |
| 69 | +# === Example: Configure operator blacklist for a device === |
63 | 70 |
|
64 |
| -# List of all operators |
| 71 | +# Retrieve a list of all operators |
65 | 72 | all_operators = [i for i in emnify.operator.get_operators()]
|
66 | 73 |
|
67 |
| -device_id = 0 # Your device id |
| 74 | +# Add three operators to the blacklist: |
| 75 | +device_id = 0 # Your device ID |
68 | 76 | emnify.devices.add_device_blacklist_operator(operator_id=all_operators[0].id, device_id=device_id)
|
69 | 77 | emnify.devices.add_device_blacklist_operator(operator_id=all_operators[1].id, device_id=device_id)
|
70 | 78 | emnify.devices.add_device_blacklist_operator(operator_id=all_operators[2].id, device_id=device_id)
|
71 |
| -# Adding 3 operators to the blacklist |
72 | 79 |
|
| 80 | +# Get all blacklist operators of the device by device ID: |
73 | 81 | device_blacklist = emnify.devices.get_device_operator_blacklist(device_id=device_id)
|
74 |
| -# Getting all blacklist operators of device by device_id |
75 | 82 |
|
76 | 83 | operator_id = 0
|
77 | 84 | for operator in device_blacklist:
|
|
80 | 87 | print(operator.mnc)
|
81 | 88 | operator_id = operator.id
|
82 | 89 |
|
| 90 | +# Removes the last operator from the blacklist |
83 | 91 | emnify.devices.delete_device_blacklist_operator(device_id=device_id, operator_id=operator_id)
|
84 |
| -# Removing the last operator from blacklist |
| 92 | + |
85 | 93 | # [endblock]
|
86 | 94 |
|
87 |
| -# === Disable device === |
| 95 | +# === Example: Disable a device === |
88 | 96 |
|
| 97 | +# Get a list of all devices with SIM cards and the "Enabled" device status |
89 | 98 | device_filter = emnify.devices.get_device_filter_model(status=emnify_constants.DeviceStatuses.ENABLED_ID.value)
|
90 | 99 | all_devices_with_sim = [
|
91 | 100 | device for device in emnify.devices.get_devices_list(filter_model=device_filter) if device.sim
|
92 | 101 | ]
|
93 |
| -# Getting list of all devices with sim cards and enabled status |
94 | 102 |
|
95 | 103 | device = all_devices_with_sim[0]
|
96 | 104 |
|
97 |
| -emnify.devices.change_status(disable=True, device=device.id) |
98 | 105 | # Disable a device
|
| 106 | +emnify.devices.change_status(disable=True, device=device.id) |
99 | 107 |
|
100 | 108 | disabled_device = emnify.devices.retrieve_device(device_id=device.id)
|
101 |
| -device_status = disabled_device.status.description # Will be 'Disabled' |
102 |
| -sim_status = disabled_device.sim.status.description # Will be 'Suspended' |
| 109 | +device_status = disabled_device.status.description # Device status is "Disabled" |
| 110 | +sim_status = disabled_device.sim.status.description # SIM status is "Suspended" |
| 111 | + |
103 | 112 | # [endblock]
|
104 | 113 |
|
105 |
| -# === Delete device === |
| 114 | +# === Example: Delete a device === |
106 | 115 |
|
| 116 | +# Get a list of all devices |
107 | 117 | old_devices_list = [device for device in emnify.devices.get_devices_list()]
|
108 |
| -# Getting list of all devices |
109 | 118 |
|
110 | 119 | device_to_delete = list(
|
111 | 120 | filter(
|
112 | 121 | lambda device: device.sim and device.status.id == emnify_constants.DeviceStatuses.ENABLED_ID,
|
113 | 122 | old_devices_list
|
114 | 123 | )
|
115 | 124 | )[0]
|
116 |
| -# Picking up a device to delete with assigned sim and enabled status |
117 | 125 |
|
| 126 | +# Choose a device to delete with an assigned SIM and the "Enabled" device status |
118 | 127 | sim_id_of_deleted_device = device_to_delete.sim.id
|
119 | 128 |
|
| 129 | +# Delete the device |
120 | 130 | emnify.devices.delete_device(device_id=device_to_delete.id)
|
121 |
| -# Deleting a device |
122 | 131 |
|
| 132 | +# Retrieve a new list of all devices |
123 | 133 | new_device_list = [device for device in emnify.devices.get_devices_list()]
|
124 |
| -# Getting new list of all devices |
125 |
| - |
126 | 134 |
|
| 135 | +# Once your device is deleted, the total device count should be lower: |
127 | 136 | assert len(old_devices_list) > len(new_device_list)
|
128 |
| -# After deleting count of all devices will be lowered |
129 | 137 |
|
130 | 138 | sim = emnify.sim.retrieve_sim(sim_id=sim_id_of_deleted_device)
|
131 |
| -sim_status = sim.status.description # Will be 'Suspended' |
| 139 | +sim_status = sim.status.description # SIM status is 'Suspended' |
132 | 140 |
|
133 | 141 | # [endblock]
|
134 | 142 |
|
| 143 | +# === Example: Manage device connectivity === |
135 | 144 |
|
136 |
| -# === Manage device connectivity === |
137 |
| - |
138 |
| -# Lookup for documentation for learn more |
139 |
| -# https://www.emnify.com/developer-blog/5-ways-to-detect-and-solve-connectivity-issues#network-events |
140 |
| - |
141 |
| -# There are many reasons why connection issues arise. For example: |
142 |
| -# * The device executes the wrong procedures due to a bad firmware update. |
143 |
| -# * The device executes network registration too frequently that the network no longer allows it to register. |
144 |
| -# * You have simply changed a policy due to a blocked device. |
| 145 | +# There are many reasons why connection issues arise. |
| 146 | +# For example: |
| 147 | +# - The device executes the wrong procedures due to a bad firmware update. |
| 148 | +# - The device executes network registration too frequently, so the network no longer allows it to register. |
| 149 | +# - You changed a policy due to a blocked device. |
145 | 150 |
|
146 |
| -# For resetting a device connectivity you can use the following methods: |
147 |
| -# * Resetting the connectivity of device |
| 151 | +# To reset device connectivity, use the following methods: |
| 152 | +# - Reset the device's connectivity |
148 | 153 | device_id = 0
|
149 | 154 | emnify.devices.reset_connectivity_data(device_id=device_id)
|
150 |
| -# * Resetting the connectivity |
| 155 | +# - Reset the network connectivity |
151 | 156 | emnify.devices.reset_connectivity_network(device_id=device_id)
|
152 | 157 |
|
153 |
| -# For checking connectivity you can use the method: |
| 158 | +# Use the following method to check the connectivity: |
154 | 159 | connectivity = emnify.devices.get_device_connectivity_status(device_id=device_id)
|
155 |
| -print(connectivity.status.description) # Will be 'ATTACHED'/'ONLINE'/'OFFLINE'/'BLOCKED' |
| 160 | +print(connectivity.status.description) # Status is either "Attached", "Online", "Offline", or "Blocked" |
156 | 161 |
|
157 | 162 | # [endblock]
|
0 commit comments