This content originally appeared on DEV Community and was authored by ZHZL-m
1 -> Development process
In addition to traditional cloud functions, you can also develop cloud objects under the cloud-side project of device-cloud integration. A cloud object is a special kind of cloud function, which is essentially an encapsulation of cloud function, and the client can directly use the method of this object by importing a cloud object, providing a development experience of directly calling cloud code on the device side. Compared with ordinary SCF, the code of cloud objects is more concise and the logic is clearer, and it is recommended to use cloud objects instead of traditional SCF in most scenarios. The development process is roughly as follows:
Create a cloud object: You can create a cloud object directly in DevEco Studio.
Develop cloud objects: After the cloud objects are created, you can start writing cloud object business code.
Debugging Cloud Objects: You can debug cloud objects to test whether the cloud object code is running correctly.
Deploy cloud objects: After the cloud object code development and debugging are completed, you can deploy the cloud object to the AGC cloud, which supports single deployment and batch deployment.
illustrate
Generally, it is recommended to debug cloud objects before deploying them to the cloud, but in some business scenarios, you need to deploy cloud objects before debugging. Perform the following operations based on your business needs.
2 -> Create a cloud object
First, you need to create a cloud object in the cloud-side project.
Right-click on the cloudfunctions directory and select New > Cloud Function.
Select Cloud Object in the “Select the Cloud Function Type” column, enter the name of the cloud object (e.g. “my-cloud-object”), and click “OK”.
Like the function name, the name of an object must be between 2 and 63 characters in length and can only support lowercase letters, digits, and hyphens (-), with the first character being lowercase letters and ending with hyphens (-).
The cloudfunctions directory generates a new cloud object directory, which contains the following files:
function-config.json configuration file contains information such as handlers and triggers.
handler: the portal module of the cloud object and the class exported from the cloud object, via the “.” Connect.
functionType: indicates the function type, 0 indicates the cloud function, and 1 indicates the cloud object.
triggers: Defines the type of triggers used by cloud objects, and currently only HTTP triggers are supported for cloud objects.
illustrate
It is not recommended to manually modify the configuration file “function-config.json” of the cloud object, otherwise it will cause the cloud object to fail to deploy or other errors.
Cloud object entry file xxx.ts (for example, myCloudObject.ts): Write cloud object code in this file.
Cloud Object Dependency Profile “package.json”: Add dependencies to this file.
3 -> Develop cloud objects
Once the cloud object is created, you can write the methods you need to implement directly in the cloud object. For example, you can use cloud objects to implement two methods: add and subtract.
- Open the cloud object entry file (in this case, “myCloudObject.ts” is used as an example) and add the add and subtract methods.
export class MyCloudObject {
add(num1: number, num2: number) {
return { result: num1 + num2 };
}
subtract(num1: number, num2: number) {
return { result: num1 - num2 };
}
}
note
Cloud objects are stateless. After a cloud object is deployed to the cloud side, each call may be a different backend node, so it makes no sense to define class member variables on the cloud object. It is wrong to assign a class member property from one Method and then expect to get the class member property from another Method.
There is no need to write constructors for cloud objects. When the cloud side receives a request for a function of a cloud object, it calls the default parameterless constructor of the cloud object.
The input of the cloud object method is deserialized from JSON, which can only be string, number, or Object, and does not support date, Uint8Array, etc. If you need to pass Date or Uint8Array in the process of writing cloud object code, it is recommended to define it as a number or array, and explicitly call the constructor of Date or Uint8Array in the Method to achieve the purpose.
The output of a cloud object’s method does not currently support a single number return.
The input and output of methods of cloud objects can use custom objects, and cannot use objects or types defined by third-party dependencies. Note that it is not that cloud objects cannot have third-party dependencies, but that the input and output of cloud objects cannot have third-party dependencies, otherwise in the “Generator Invoke Interface” stage, they will fail because they cannot find dependencies, the fundamental reason is that the device-side code runs in the HarmonyOS support ark runtime, while the cloud-side runs in the Node.js, and the dependency management of the two is different.
- (Optional) If there are dependencies on cloud objects, you can add the required dependencies under “dependencies” in the “package.json” file, and then click “Sync Now” in the upper right corner.
illustrate
You can also install dependencies by right-clicking on the package.json file and selecting the “Run ‘npm install'” menu.
All installed dependencies are stored in the “node_modules” directory of the current cloud object.
4 -> Debugging cloud objects
Once the cloud object is developed, you can debug it to verify that the cloud object code is working properly.
Currently, DevEco Studio supports local and remote object debugging.
Debugging Cloud Objects by Using Local Invocation: Debugging Locally Developed Cloud Objects in DevEco Studio. It supports single debugging and batch debugging, and supports two modes, Run and Debug, with rich debugging functions, and is often used in the process of cloud object development or problem locating.
Debugging cloud objects by using remote invocation: Deploy cloud objects to AGC Cloud, and then directly invoke cloud objects in DevEco Studio. This method is mainly used to test the operation of cloud objects in the cloud, or to supplement the test of problems that cannot be found in the local invocation mode due to various factors.
4.1 -> Prerequisites
Make sure you’re logged in.
If the project has code logic that involves cloud objects calling the cloud database, you need to deploy the entire cloud project to the AGC cloud before debugging, otherwise there will be no relevant data and environment variables in the cloud.
4.2 -> Debugging cloud objects by local calls
You can debug locally developed cloud objects in DevEco Studio, support single debugging and batch debugging, and support two modes: Run and Debug.
The single debugging and batch debugging processes are the same, except that a single debugging starts local debugging for only one cloud object at a time, and then only that cloud object can be called; Batch debugging starts local debugging for all cloud objects in the CloudFunctions directory at one time, and then calls each cloud object one by one.
The difference between Run mode and Debug mode is that Debug mode supports the use of breakpoints to track the running status of cloud objects, while Run mode does not.
The following section uses my-cloud-object in Debug mode as an example to describe how to debug a local cloud object in DevEco Studio.
- Right-click the my-cloud-object directory and select Debug ‘my-cloud-object’.
illustrate
If you want to debug multiple cloud objects in batches, right-click the cloudfunctions directory and select Debug Cloud Functions. If there are both SCFs and Cloud Objects in the “CloudFunctions” directory, all SCFs and Cloud Objects will be started.
In the CloudFunctions window at the bottom of the notification bar, view the debugging logs. If the message “Cloud Functions loaded successfully” is displayed, the cloud object has been successfully loaded to the HTTP server running locally and the corresponding function URI is generated.
If you need to set a breakpoint for debugging, select a valid line of code to set a breakpoint in the function code, and click the left mouse button after the line number (as shown in line 3 in the figure below) to set the breakpoint (as shown in the red dot in the figure below).
When a breakpoint is set, debugging can break at the breakpoint and highlight the line.
Select “View > Tool Windows > Cloud Functions Requestor” in the menu bar and use the Cloud Functions Requestor to trigger a cloud object call.
In the Cloud Functions Requestor panel that appears, set the trigger event parameters.
Cloud Function: Select the cloud object to be triggered, in this example, the cloud object my-cloud-object is used as an example.
Environment: Select the environment to be called by the cloud object. In this case, Local is selected to indicate a local call.
Method: Required: Enter the method name of the cloud object, such as add.
Event: a list of method parameters, in JSON array format, representing the input parameters of the Method. If the add method receives two number parameters, num1 and num2, then “[1, 2]” is used to construct num1=1 and num2=2.
note
If one of the input arguments of a Method is of type array[], then the Event will contain at least two layers of square brackets ‘[‘, and the outer square brackets represent the list of parameters.
- (Optional) Click “Save” to save the current triggering event.
Click on the top right corner
You can expand the saved trigger event, and then you can directly click “Load” to load the event. For trigger events that do not need to be saved, you can also click “Delete” to delete.
- Click “Trigger”, the method of executing the cloud object will be triggered, and the execution result will be displayed in the “Result” box.
illustrate
The Logs panel to the right of the Result box is only used when debugging cloud objects by calling them remotely.
- Click on the menu bar
to stop debugging.
- After modifying the cloud object code based on the debugging results, click
Restart debugging in Debug mode until there are no issues.
- Refer to steps 5~9 to complete the debugging of other methods of cloud objects or other cloud objects.
4.3 -> Debugging cloud objects by remote calling
You can deploy cloud objects to the AGC cloud, and then call the cloud cloud objects in DevEco Studio to test the running of cloud objects in the cloud, or to supplement the problems that cannot be found in local debugging due to various factors.
Deploy the cloud object to be debugged to AGC Cloud by referring to Deploy Cloud Objects.
Select “View > Tool Windows > Cloud Functions Requestor” in the menu bar and use the Cloud Functions Requestor to trigger a cloud object call.
In the Cloud Functions Requestor panel that appears, configure the trigger event parameters.
Cloud Function: Select the cloud object to be triggered, in this example, “my-cloud-object” is still used.
Environment: Select the environment to be called by the cloud object. If you select Remote, you can make a remote call.
Method: Enter a method name for the cloud object, for example, add.
Event: a list of method parameters, in JSON array format, representing the input parameters of the Method in order, for example, the add method receives two number parameters, num1 and num2, then fill in “[1, 2]” to indicate the request to construct num1=1 and num2=2, for example, “[1, 2]”.
note
If one of the input arguments of a Method is of type array[], then the Event will contain at least two layers of square brackets, such as ‘[[1, 2], 3]’, and the outer square brackets represent the list of parameters.
Click “Trigger”, the cloud object method will be executed, and the execution result will be displayed in the “Result” box.
Click the “Logs” tab to view the printed logs to locate the problem. Modify the cloud object code, redeploy the cloud object, and then perform the remote call again until there is no problem.
Refer to steps 1~5 to complete the debugging of other cloud objects or other cloud objects.
5 -> Deploy cloud objects
After you complete the code development of the cloud object, you can deploy the cloud object to the AGC cloud, which supports single deployment or batch deployment.
A single deployment deploys only the selected cloud objects, while a batch deployment deploys all cloud objects in the entire cloudfunctions directory to the AGC cloud at the same time.
The following section uses my-cloud-object as an example to describe how to deploy a cloud object.
- Right-click the my-cloud-object directory and select Deploy ‘my-cloud-object’.
illustrate
If you want to deploy multiple cloud objects in batches, right-click the cloudfunctions directory and select Deploy Cloud Functions to deploy all cloud objects in the directory. If both SCF and CF exist in the “cloudfunctions” directory, the SF and CF objects will be deployed to AGC Cloud together.
- You can view the packaging and deployment progress of cloud objects on the right side of the status bar at the bottom.
Wait patiently until the “Deploy successfully” message appears, indicating that the current cloud object has been successfully deployed.
Select Tools > CloudDev in the menu bar.
In the CloudDev panel that opens, click Go to console under Serverless > Cloud Functions to enter the SCF page of the current project.
Check that the “my-cloud-object” cloud object has been successfully deployed to AGC Cloud, and the cloud object name is the same as the cloud object directory name of the local project.
After the deployment is successful, you can call the cloud object from the device side.
This content originally appeared on DEV Community and was authored by ZHZL-m