OpenHarmony Feature

OpenHarmony is an open-source operating system designed for a wide range of devices, including smartphones, wearables, smart screens, and more. It provides a unified platform that enables developers to create applications that can run on multiple devices seamlessly. In this article, we will explore one of the key features of OpenHarmony called "Distributed Schedule Service" and understand how it works with the help of code examples and diagrams.

Distributed Schedule Service

The Distributed Schedule Service allows applications to schedule tasks across multiple devices connected to the same network. It enables efficient utilization of resources and provides a seamless experience to the users. Let's understand this feature with the help of a code example.

Code Example

import ohos.distributedschedule.interwork.DeviceManager;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceGroup;
import ohos.distributedschedule.interwork.DistributedSchedAdapter;
import ohos.distributedschedule.interwork.DistributedSchedException;

public class DistributedScheduleExample {
    private static final String GROUP_ID = "group_1";
    private static final String DEVICE_ID_1 = "device_1";
    private static final String DEVICE_ID_2 = "device_2";

    public static void main(String[] args) throws DistributedSchedException {
        DeviceManager deviceManager = DistributedSchedAdapter.getDeviceManager();
        DeviceGroup deviceGroup = deviceManager.createDeviceGroup(GROUP_ID);

        DeviceInfo device1 = deviceManager.getDeviceInfo(DEVICE_ID_1);
        deviceGroup.addDevice(device1);

        DeviceInfo device2 = deviceManager.getDeviceInfo(DEVICE_ID_2);
        deviceGroup.addDevice(device2);

        deviceGroup.startGroup();
        deviceGroup.runOnGroup(() -> {
            // Code to be executed on the group of devices
        });

        deviceGroup.stopGroup();
    }
}

In the code example above, we create a device group with two devices (device_1 and device_2). We add these devices to the group and then start the group. Next, we execute a piece of code on the group of devices using the runOnGroup method. Finally, we stop the group.

Sequence Diagram

The following sequence diagram illustrates the flow of events in the code example:

sequenceDiagram
    participant App
    participant DeviceManager
    participant DeviceGroup
    participant DeviceInfo

    App->>DeviceManager: CreateDeviceGroup(GROUP_ID)
    DeviceManager->>DeviceGroup: CreateDeviceGroup(GROUP_ID)
    App->>DeviceManager: GetDeviceInfo(DEVICE_ID_1)
    DeviceManager->>DeviceInfo: GetDeviceInfo(DEVICE_ID_1)
    App->>DeviceGroup: AddDevice(device_1)
    DeviceGroup->>DeviceGroup: AddDevice(device_1)
    App->>DeviceManager: GetDeviceInfo(DEVICE_ID_2)
    DeviceManager->>DeviceInfo: GetDeviceInfo(DEVICE_ID_2)
    App->>DeviceGroup: AddDevice(device_2)
    DeviceGroup->>DeviceGroup: AddDevice(device_2)
    App->>DeviceGroup: StartGroup()
    DeviceGroup->>DeviceGroup: StartGroup()
    App->>DeviceGroup: RunOnGroup(code)
    DeviceGroup->>DeviceGroup: Execute code on devices
    App->>DeviceGroup: StopGroup()
    DeviceGroup->>DeviceGroup: StopGroup()

The sequence diagram shows the interaction between the application, DeviceManager, DeviceGroup, and DeviceInfo.

State Diagram

The state diagram represents the different states of the device group during its lifecycle:

stateDiagram
    [*] --> Created
    Created --> Started: startGroup()
    Started --> Running: runOnGroup()
    Running --> Stopped: stopGroup()
    Stopped --> [*]

The state diagram shows that the device group starts in the "Created" state. After calling the startGroup method, it transitions to the "Started" state. When the runOnGroup method is invoked, the group enters the "Running" state. Finally, calling the stopGroup method transitions the group back to the "Stopped" state.

Conclusion

The Distributed Schedule Service is a powerful feature of OpenHarmony that enables developers to distribute tasks across multiple devices. It provides a seamless and efficient way to utilize resources and enhance the user experience. In this article, we explored the feature with the help of a code example, sequence diagram, and state diagram. OpenHarmony's Distributed Schedule Service opens up new possibilities for developing innovative applications in the multi-device era.