lan-he-shared-core
v1.0.9
Published
Smart home automation shared core SDK for cross-platform development
Maintainers
Readme
HA Shared Core SDK
一个跨平台的核心SDK,为多种运行环境提供统一的API和服务,包括移动设备(Android/iOS)、鸿蒙系统、桌面应用、小程序和车载系统。
功能特性
- 跨平台支持:适配多种运行环境,提供统一的API接口
- 认证服务:灵活的认证策略,支持自定义认证实现
- 平台检测:自动检测运行环境并应用相应的兼容性策略
- 兼容性处理:针对不同平台的输入标准化和功能特性支持检测
- 设备抽象:统一的设备接口,屏蔽不同平台的实现差异
- 同步引擎:数据同步机制,支持事件驱动的数据更新
支持的平台
mobile-android- Android移动设备mobile-ios- iOS移动设备harmony- 鸿蒙系统desktop- 桌面应用(Electron/Node.js)mini-program- 小程序环境car-system- 车载系统desktop-browser- 桌面浏览器
安装
npm install lanhe-core-sdk使用方法
基本导入
import {
AuthService,
detectPlatform,
DeviceAPI,
getCompatibilityManager
} from 'lanhe-core-sdk';认证服务
// 创建认证服务实例
const authService = new AuthService(logger, deviceInfoService);
// 登录
const user = await authService.login('username', 'password');
// 登出
authService.logout(user.id);
// 自定义认证策略
authService.setAuthStrategy(customAuthStrategy);平台检测
// 自动检测平台
const platform = detectPlatform();
// 在测试中设置特定平台
setPlatformForTesting('harmony');兼容性处理
// 获取兼容性管理器
const manager = getCompatibilityManager();
// 标准化输入
const normalizedText = manager.normalizeInput(" Hello\tWorld\n");
// 检查功能支持
const canUseWebSockets = manager.isFeatureSupported("websocket");
// 使用快捷函数
import { normalizeInput, isFeatureSupported } from 'lanhe-core-sdk';
const text = normalizeInput(inputValue);
if (isFeatureSupported('geolocation')) {
// 使用定位功能
}设备API
// 创建设备API实例
const deviceAPI = new DeviceAPI();
// 获取设备ID
const deviceId = deviceAPI.getDeviceId();
// 请求权限
const hasPermission = await deviceAPI.requestPermission('record_audio');
// 获取蓝牙设备
const bluetoothDevices = await deviceAPI.getBluetoothDevices();自定义平台策略
import { registerPlatformStrategy } from 'lanhe-core-sdk';
// 注册自定义平台策略
registerPlatformStrategy('custom-platform', {
normalizeInput: input => input.trim().toLowerCase(),
resolveFeatureConflict: featureName => featureName !== 'restricted-feature'
});开发
构建项目
# 构建通用版本
npm run build
# 构建鸿蒙版本
npm run build:harmony运行测试
# 运行所有测试
npm test
# 运行鸿蒙环境测试
npm run test:harmonyReact Native项目集成
有关如何在React Native项目中集成和使用此共享核心层的详细指南,请参阅 React Native集成指南。
该指南包含:
- React Native项目目录结构建议
- UI框架选择建议
- 共享核心层npm包配置
- 在React Native项目中使用共享核心层的方法(包括npm导入和npm-link本地连接)
- 核心功能使用示例
项目结构
src/
├── core/ # 核心模块
│ ├── models/ # 数据模型
│ ├── services/ # 核心服务
│ └── sync-engine.ts # 同步引擎
├── platforms/ # 平台适配层
├── utils/ # 工具函数
└── typings/ # 类型定义许可证
MIT
