Skip to main content
Version: v3

Migration from version 1.0

Introduction

This document is an migration guide for the Mini Program API from Zepp OS 1.0 to Zepp OS 2.0.

tip

Before reading this document, it is recommended to finish reading 2.0 API Introduction. Especially the section on API_LEVEL compatibility!

info

To facilitate quick migration for developers, the Zepp OS team provides command line tools to improve migration efficiency @zeppos/zmt.

Benefits of the Migration 2.0 API

  • Access to the latest open capabilities of Zepp OS
  • The Zepp OS team guarantees API compatibility through API_LEVEL and ensures backward compatibility of API_LEVEL in subsequent version upgrades
  • Developers do not need to care about the firmware version number and Zepp OS version number, they only need to make compatibility adaptations to API_LEVEL, which greatly simplifies the workload of compatibility adaptations

Start migration

Scope of influence

First, let's clarify the scope of the Zepp OS 2.0 JS API upgrade.

The upgraded Mini Program API only applies to Device App, i.e. the development mode for Settings App and Side Service is the same as in version 1.0 without modification.

The development language of the device application is JavaScript following the ES6 standard, and only code related to the ability to use the Zepp OS system needs to be changed.

As an example, in 1.0, Zepp OS system capabilities are exposed to the public through global variables such as hmUI, hmApp, etc. Anywhere system capabilities are used, changes need to be made.

Migration ideas

The capabilities of the 2.0 JS API are a superset of the 1.0 JS API. All of the capabilities of the 1.0 JS API can be found in the 2.0 JS API in a corresponding way. There are different ways to upgrade the 1.0 JS API for different types of 1.0 JS APIs, which are divided into several categories below and handled in different ways.

Case 1: substitution by reference (only for hmUI and hmBle modules)

The hmUI and hmBle modules, for now, just transform the reference method and replace the reference method in the file header.

An example

v1.0page.js
Page({
build() {
const text = hmUI.createWidget(hmUI.widget.TEXT, {
x: 96,
y: 120,
w: 288,
h: 46,
color: 0xffffff,
text_size: 36,
align_h: hmUI.align.CENTER_H,
align_v: hmUI.align.CENTER_V,
text_style: hmUI.text_style.NONE,
text: 'HELLO ZEPPOS'
})
}
})

Just add a quote in the form of an ESM in the header.

v2.0page.js
import * as hmUI from '@zos/ui'

Page({
build() {
const text = hmUI.createWidget(hmUI.widget.TEXT, {
x: 96,
y: 120,
w: 288,
h: 46,
color: 0xffffff,
text_size: 36,
align_h: hmUI.align.CENTER_H,
align_v: hmUI.align.CENTER_V,
text_style: hmUI.text_style.NONE,
text: 'HELLO ZEPPOS'
})
}
})

Case 2: Method name substitution

A part of the method function remains unchanged, only the method name is modified, after adding the relevant method reference statement, the calling method name and parameters can be modified.

1.0 API2.0 API2.0 API Module
hmApp.gotoHomehome@zos/router
hmApp.goBackback@zos/router
hmApp.exitexit@zos/router
hmApp.alarmCancelclearLaunchAppTimeout@zos/router
hmFS.open_assetopenAssetsSync@zos/fs
hmFS.openopenSync@zos/fs
hmFS.closecloseSync@zos/fs
hmFS.removermSync@zos/fs
hmFS.renamerenameSync@zos/fs
hmFS.mkdirmkdirSync@zos/fs
hmApp.setScreenKeepsetWakeUpRelaunch@zos/display
hmSetting.setScreenAutoBrightsetAutoBrightness@zos/display
hmSetting.getScreenAutoBrightgetAutoBrightness@zos/display
hmSetting.setBrightnesssetBrightness@zos/display
hmSetting.getBrightnessgetBrightness@zos/display
hmSetting.setScreenOffsetScreenOff@zos/display
hmSetting.setBrightScreenCancelresetPageBrightTime@zos/display
hmSetting.getUserDatagetProfile@zos/user
hmSetting.getDiskInfogetDiskInfo@zos/device
hmSetting.getDeviceInfogetDeviceInfo@zos/device
hmSetting.getLanguagegetLanguage@zos/settings
hmSetting.getDateFormatgetDateFormat@zos/settings
hmSetting.getMileageUnitgetDistanceUnit@zos/settings
hmSetting.getWeightUnitgetWeightUnit@zos/settings
hmSetting.getWeightTargetgetWeightTarget@zos/settings
hmSetting.getSleepTargetgetSleepTarget@zos/settings
hmApp.registerGestureEventonGesture@zos/interaction
hmApp.unregisterGestureEventoffGesture@zos/interaction
hmApp.registerKeyEventonKey@zos/interaction
hmApp.unregisterKeyEventoffKey@zos/interaction
hmApp.registerSpinEventonDigitalCrown@zos/interaction
hmApp.unregisterSpinEventoffDigitalCrown@zos/interaction
hmApp.getPackageInfogetPackageInfo@zos/app
hmSetting.getScreenTypegetScene@zos/app
hmApp.setLayerYscrollTo@zos/page
hmApp.getLayerYgetScrollTop@zos/page
hmUI.getScrollCurrentPagegetSwiperIndex@zos/page

An example

v1.0page.js
hmApp.setLayerY(100)
const result = hmSetting.getUserData()
hmApp.gotoHome()

The replacement can be done quickly by comparing the table above, as well as the 2.0 API documentation.

v2.0page.js
import { scrollTo } from '@zos/page'
import { home } from '@zos/router'
import { getProfile } from '@zos/user'

scrollTo({
y: 100
})
const result = getProfile()
home()

Case 3: API capability upgrade, need to pay attention to parameters when replacing

Some methods have been enhanced with new parameters and can be called equivalently based on the functionality of the 2.0 API.

1.0 API2.0 Enhanced Capabilities API2.0 API Module
hmApp.setBrightScreensetPageBrightTime@zos/display
hmApp.statstatSync@zos/fs
hmApp.open_assetopenAssetsSync@zos/fs
hmApp.openopenSync@zos/fs
hmApp.closecloseSync@zos/fs
hmApp.readreadSync@zos/fs
hmApp.writewriteSync@zos/fs
hmUI.showToastshowToast@zos/interaction
hmUI.createDialogcreateModal@zos/interaction
hmUI.scrollToPageswipeToIndex@zos/page
hmUI.setScrollViewsetScrollMode@zos/page
hmApp.startApplaunchApp@zos/router
hmApp.gotoPagepush@zos/router
hmApp.reloadPagereplace@zos/router
hmApp.gotoHomehome@zos/router
hmApp.alarmNewsetLaunchAppTimeout@zos/router

Case 4: Module constants

Similar to the method name transformation in case 2, some constants need to be replaced similarly.

1.0 Constants2.0 Constants2.0 Module
hmSetting.screen_type.APPSCENE_APP@zos/app
hmSetting.screen_type.WATCHFACESCENE_WATCHFACE@zos/app
hmSetting.screen_type.SETTINGSSCENE_SETTINGS@zos/app
hmSetting.screen_type.AODSCENE_AOD@zos/app
hmFS.O_RDONLYO_RDONLY@zos/fs
hmFS.O_WRONLYO_WRONLY@zos/fs
hmFS.O_RDWRO_RDWR@zos/fs
hmFS.O_APPENDO_APPEND@zos/fs
hmFS.O_CREATO_CREAT@zos/fs
hmFS.O_EXCLO_EXCL@zos/fs
hmFS.O_TRUNCO_TRUNC@zos/fs
hmApp.gesture.UPGESTURE_UP@zos/interaction
hmApp.gesture.DOWNGESTURE_DOWN@zos/interaction
hmApp.gesture.LEFTGESTURE_LEFT@zos/interaction
hmApp.gesture.RIGHTGESTURE_RIGHT@zos/interaction
hmApp.key.BACKKEY_BACK@zos/interaction
hmApp.key.SELECTKEY_SELECT@zos/interaction
hmApp.key.HOMEKEY_HOME@zos/interaction
hmApp.key.UPKEY_UP@zos/interaction
hmApp.key.DOWNKEY_DOWN@zos/interaction
hmApp.key.SHORTCUTKEY_SHORTCUT@zos/interaction
hmApp.action.CLICKKEY_EVENT_CLICK@zos/interaction
hmApp.action.LONG_PRESSKEY_EVENT_LONG_PRESS@zos/interaction
hmApp.action.DOUBLE_CLICKKEY_EVENT_DOUBLE_CLICK@zos/interaction
hmApp.action.RELEASEKEY_EVENT_RELEASE@zos/interaction
hmApp.action.PRESSKEY_EVENT_PRESS@zos/interaction

An example

v1.0page.js
const fd = hmFS.open('test_file.txt', hmFS.O_RDWR | hmFS.O_CREAT)

Quickly complete the replacement against the table above, and against the 2.0 API documentation.

v2.0page.js
import { openSync, O_RDWR, O_CREAT } from '@zos/fs'

const fd = openSync({
path: 'test.txt',
flag: O_RDWR | O_CREAT
})

Case 5: Sensor Module

ZeppOS 2.0 sensor module is all Class type, the call method has changed significantly, eliminating the attribute access, all changed to method calls, here refer to the sensor documentation can be, a simple example.

v1.0page.js
const battery = hmSensor.createSensor(hmSensor.id.BATTERY)

console.log(battery.current)
v2.0page.js
import { Battery } from '@zos/sensor'

const battery = new Battery()
console.log(battery.getCurrent())

Case 6: i18n module

Direct Reference Replacement Example

v1.0page.js
import { gettext } from 'i18n'

gettext('i18n_key')
v2.0page.js
import { getText } from '@zos/i18n'

getText('i18n_key')

Case 7: Logging methods called from DeviceRuntimeCore

Direct Reference Replacement Example

v1.0page.js
const logger = DeviceRuntimeCore.HmLogger.getLogger('app.js')

logger.log('app.js onCreate')
v2.0page.js
import { log } from '@zos/utils'

const logger = log.getLogger('app.js')
logger.log('app.js onCreate')

Case 8: Globally used px and assets utility functions

In Zepp OS 2.0 these two methods need to be introduced from the utils module.

v1.0page.js
px(480)

const assetsPathFunc = assets('img')
v2.0page.js
import { px, assets } from '@zos/utils'

px(480)

const assetsPathFunc = assets('img')

Case 9: 1.0 timer timer module is deprecated and needs to be replaced with JS timer API

Refer to setTimeout.

Case 10: hmFS.seek is used

To facilitate developers' file system operations, the 2.0 API greatly enhances the ease of use of file system-related APIs.

It is recommended that the file logic be rewritten using the new API.

Or use the capabilities of LocalStorage directly to implement persistent key-value pair storage, supporting standard JSON objects.

Case 11: Message.js Bluetooth communication capability is used

In the Zepp OS 1.0 sample code, we provide the ability to communicate with Bluetooth via /shared/message.js.

In Zepp OS 2.0, the device app needs to update /shared/message.js and the companion service still uses /shared/message-side.js.

Please refer to Sample - ToDoList for details.

Migration Tools

A command line migration tool @zeppos/zmt is provided to facilitate the migration efforts of developers.

Most of the above replacement work can be done automatically, and some of the logic that needs to be handled manually by the developer will be prompted on the command line.

npm i @zeppos/zmt -g

or

yarn global add @zeppos/zmt

Execute the migration tool script.

zmt -p /path/to/1.0 -o /path/to/2.0

For more information, please refer to npm - @zeppos/zmt.