Asset profile Operations

This example demonstrates:

  • Creating an Asset: Learn how to generate a new asset within your application, enabling the introduction of new assets into your tracking system.

  • Binding an Existing Asset ID to the Current Device: Discover the process of associating an existing asset ID with the device you're currently using, facilitating tracking and management of that asset.

  • Updating the Current Asset Profile: Gain insights into how to modify the profile of an asset, ensuring that the asset's information remains accurate and up-to-date.

  • Retrieving Asset Details for the Bound Asset ID: Learn how to access and retrieve detailed information about an asset that has been successfully bound to the current device, enabling comprehensive asset management.

For all code examples, refer to iOS Tracking Android Code Examples

AssetOperationViewController view source

1
class AssetOperationViewController: UIViewController {
2
@IBOutlet weak var createNewAssetBtn: UIButton!
3
@IBOutlet weak var bindAssetBtn: UIButton!
4
@IBOutlet weak var updateAssetBtn: UIButton!
5
@IBOutlet weak var getAssetInfoBtn: UIButton!
6
@IBOutlet weak var assetInfo: UILabel!
7
8
private var assetId = ""
9
private var assetName = "testName"
10
private var assetDescription = "testDescription"
11
private var assetAttributes = ["attribute 1": "test 1", "attribute 2": "test 2"]
12
13
override func viewDidLoad() {
14
super.viewDidLoad()
15
AssetTracking.shared.initialize(apiKey: "YOUR_API_KEY")
16
17
initView()
18
}
19
20
override func viewWillAppear(_ animated: Bool) {
21
super.viewWillAppear(animated)
22
self.navigationController?.setNavigationBarHidden(false, animated: true)
23
}
24
25
override func viewWillDisappear(_ animated: Bool) {
26
super.viewWillDisappear(animated)
27
self.navigationController?.setNavigationBarHidden(true, animated: true)
28
}
29
30
func initView(){
31
createNewAssetBtn.addTarget(self, action: #selector(createAsset), for: .touchUpInside)
32
bindAssetBtn.addTarget(self, action: #selector(bindAsset), for: .touchUpInside)
33
updateAssetBtn.addTarget(self, action: #selector(updateAsset), for: .touchUpInside)
34
getAssetInfoBtn.addTarget(self, action: #selector(getAssetInfo), for: .touchUpInside)
35
assetInfo.text = ""
36
}
37
38
@objc func createAsset(){
39
let assetProfile: AssetProfile = AssetProfile.init(customId: UUID().uuidString.lowercased(), assetDescription: assetDescription, name: assetName, attributes: assetAttributes)
40
41
AssetTracking.shared.createAsset(assetProfile: assetProfile) { assetCreationResponse in
42
let assetId = assetCreationResponse.data.id
43
self.assetId = assetId
44
45
let toastView = ToastView(message: "Create asset successfully with id: " + self.assetId)
46
toastView.show()
47
48
do {
49
let encoder = JSONEncoder()
50
let jsonData = try encoder.encode(assetProfile)
51
if let jsonString = String(data: jsonData, encoding: .utf8) {
52
self.assetInfo.text = jsonString
53
}
54
} catch {
55
print("Error encoding JSON: \(error)")
56
}
57
58
} errorHandler: { error in
59
let errorMessage = error.localizedDescription
60
let toastView = ToastView(message: "Create asset failed: " + errorMessage)
61
toastView.show()
62
}
63
}
64
65
@objc func bindAsset(){
66
AssetTracking.shared.bindAsset(assetId: self.assetId) { responseCode in
67
let toastView = ToastView(message: "Bind asset successfully with id: " + self.assetId)
68
toastView.show()
69
} errorHandler: { error in
70
let errorMessage = error.localizedDescription
71
let toastView = ToastView(message: "Bind asset failed: " + errorMessage)
72
toastView.show()
73
}
74
}
75
76
@objc func updateAsset() {
77
assetName = "newName"
78
assetDescription = "newDescription"
79
80
let assetProfile: AssetProfile = AssetProfile.init(customId: assetId, assetDescription: assetDescription, name: assetName, attributes: assetAttributes)
81
82
AssetTracking.shared.updateAsset(assetProfile: assetProfile) {responseCode in
83
let toastView = ToastView(message: "update asset profile successfully with id: " + self.assetId)
84
toastView.show()
85
86
do {
87
let encoder = JSONEncoder()
88
let jsonData = try encoder.encode(assetProfile)
89
if let jsonString = String(data: jsonData, encoding: .utf8) {
90
self.assetInfo.text = jsonString
91
}
92
} catch {
93
print("Error encoding JSON: \(error)")
94
}
95
96
} errorHandler: {
97
error in
98
let errorMessage = error.localizedDescription
99
let toastView = ToastView(message: "Update asset profile failed: " + errorMessage)
100
toastView.show()
101
}
102
}
103
104
@objc func getAssetInfo(){
105
AssetTracking.shared.getAssetDetail(){getAssetResponse in
106
let data: GetAssetResponseData = getAssetResponse.data
107
do {
108
let encoder = JSONEncoder()
109
let jsonData = try encoder.encode(data)
110
if let jsonString = String(data: jsonData, encoding: .utf8) {
111
self.assetInfo.text = jsonString
112
}
113
} catch {
114
print("Error encoding JSON: \(error)")
115
}
116
} errorHandler: { error in
117
let errorMessage = error.localizedDescription
118
let toastView = ToastView(message: "Get asset profile failed: " + errorMessage)
119
toastView.show()
120
}
121
}
122
}

Upon executing the code example provided above, your app's appearance will resemble the following snippet:

documentation image

Code Highlights

The above code snippet is a class inherited from the UIViewController class, which is the base class for all view controllers in iOS.

  • The class has four outlet properties: createNewAssetBtn, bindAssetBtn, updateAssetBtn, and getAssetInfoBtn. These properties are linked to the corresponding UI elements in the view controller's storyboard.

  • The class has four private variables: assetId, assetName, assetDescription, and assetAttributes. These variables store the asset's ID, name, description, and attributes.

  • The viewDidLoad() method is called when the view controller is first loaded. In this method, the class initializes the AssetTracking shared instance and calls the initView() method to initialize the view controller's user interface.

  • The viewWillAppear() and viewWillDisappear() methods are called when the view controller is about to appear or disappear, respectively. In these methods, the class hides or shows the navigation bar, depending on the view controller's visibility.

  • The initView() method is called to initialize the view controller's user interface. In this method, the class adds target-action listeners to the four buttons to perform the corresponding operations when the buttons are tapped.

  • The createAsset() method creates a new asset with the specified attributes. The method calls the AssetTracking.shared.createAsset() method to make the API request. The method also handles the success and failure cases of the API request.

  • The bindAsset() method binds the asset to the current view controller. The method calls the AssetTracking.shared.bindAsset() method to make the API request. The method also handles the success and failure cases of the API request.

  • The updateAsset() method updates the asset's profile with the specified name and description. The method calls the AssetTracking.shared.updateAsset() method to make the API request. The method also handles the success and failure cases of the API request.

  • The getAssetInfo() method gets the asset's profile. The method calls the AssetTracking.shared.getAssetDetail() method to make the API request. The method also handles the success and failure cases of the API request.

Here is a more detailed explanation of some of the key concepts in the code:

  • AssetTracking: This is a class that provides methods for making API requests to the Asset Tracking API.

  • ToastView: This is a class that displays a short message on the screen.

© 2024 NextBillion.ai all rights reserved.