210 lines
6.3 KiB
JavaScript
210 lines
6.3 KiB
JavaScript
import { getStorage, setStorage } from './export'
|
||
import * as device from './star-cloud/device.js'
|
||
import * as lock from './star-cloud/lock.js'
|
||
import * as other from './star-cloud/other.js'
|
||
import * as password from './star-cloud/password.js'
|
||
import * as record from './star-cloud/record.js'
|
||
import * as user from './star-cloud/user.js'
|
||
import * as common from './common.js'
|
||
import { onBLECharacteristicValueChange } from './uni/basic'
|
||
|
||
/**
|
||
* 账户信息信息
|
||
* @typedef {Object} AccountInfo
|
||
* @property {Number} uid 用户ID
|
||
* @property {String} username 用户名
|
||
* @property {String} password 密码
|
||
* @property {String} token token 非必填
|
||
*/
|
||
|
||
/**
|
||
* 锁信息
|
||
* @typedef {Object} lockInfo
|
||
* @property {Number} keyId - 钥匙ID
|
||
* @property {Number} lockId - 锁ID
|
||
* @property {String} lockName - 锁名称
|
||
* @property {String} lockAlias - 锁别名
|
||
* @property {Number} electricQuantity - 电量
|
||
* @property {Number} electricQuantityStandby - 备用电量
|
||
* @property {Number} electricQuantityDate - 电量更新时间
|
||
* @property {String} fwVersion - 固件版本
|
||
* @property {String} hwVersion - 硬件版本
|
||
* @property {Number} keyType - 钥匙类型
|
||
* @property {Number} passageMode - 常开模式
|
||
* @property {Number} userType - 用户类型
|
||
* @property {Number} startDate - 有效期开始时间
|
||
* @property {Number} endDate - 有效期结束时间
|
||
* @property {Object} weekDays - 循环周期
|
||
* @property {Number} remoteEnable - 是否支持远程开锁:0-未知,1-是,2-否
|
||
* @property {Number} faceAuthentication - 是否实名认证:0-未知,1-是,2-否
|
||
* @property {Number} lastFaceValidateTime - 最后一次人脸验证时间
|
||
* @property {Number} nextFaceValidateTime - 下次人脸验证时间
|
||
* @property {Number} keyRight - 是否授权管理员钥匙: 0-未知,1-是,2-否
|
||
* @property {Number} keyStatus - 钥匙状态
|
||
* @property {Object} bluetooth - 蓝牙
|
||
* @property {Number} sendDate - 发送时间
|
||
* @property {Number} isLockOwner - 是否是锁主
|
||
* @property {Object} lockFeature - 锁特征
|
||
* @property {Object} lockSetting - 锁设置
|
||
* @property {Number} lockUserNo - 用户编号
|
||
* @property {Number} senderUserId - 发送者用户ID
|
||
* @property {Number} isOnlyManageSelf - 如果是授权管理员此字段区分是否仅管理自己发的钥匙
|
||
* @property {Number} restoreCount - 重置次数
|
||
* @property {String} model - 模式
|
||
*/
|
||
|
||
/**
|
||
* 锁蓝牙信息
|
||
* @typedef {Object} bluetooth
|
||
* @property {String} bluetoothDeviceId - 设备ID
|
||
* @property {String} bluetoothDeviceName - 设备名称
|
||
* @property {String} publicKey - 公钥
|
||
* @property {String} privateKey - 私钥
|
||
* @property {String} signKey - 签名密钥
|
||
* @property {String} passwordKey - 密码密钥
|
||
*/
|
||
|
||
/**
|
||
* 锁设置信息
|
||
* @typedef {Object} lockSetting
|
||
* @property {Number} appUnlockOnline - 开门是否需要联网
|
||
*/
|
||
|
||
/**
|
||
* 请求返回数据
|
||
* @typedef {Object} requestData
|
||
* @property {String} userNos - 设备ID
|
||
*/
|
||
|
||
class StarCloud {
|
||
constructor() {
|
||
// 环境
|
||
this.env = null
|
||
//sdk服务器地址
|
||
this.url = ''
|
||
// 客户端Id
|
||
this.clientId = null
|
||
// 客户端密码
|
||
this.clientSecret = null
|
||
// 小程序环境
|
||
this.envVersion = ''
|
||
// 锁信息
|
||
this.lockInfo = null
|
||
// 服务器时间
|
||
this.serverTimestamp = 0
|
||
// 时间差
|
||
this.timeDifference = 0
|
||
// 搜索设备列表
|
||
this.searchDeviceList = []
|
||
// 星云用户信息
|
||
this.userInfo = null
|
||
// 账号列表
|
||
this.accounts = null
|
||
// 账户信息
|
||
this.accountInfo = null
|
||
// 消息序号
|
||
this.messageCount = 1
|
||
// 是否上报日志
|
||
this.isReportLog = false
|
||
// 请求参数
|
||
this.requestParams = null
|
||
// 特性值回调
|
||
this.characteristicValueCallback = null
|
||
// 完整数据
|
||
this.completeArray = []
|
||
// 完整内容数据长度
|
||
this.length = 0
|
||
// 计时器
|
||
this.timer = null
|
||
// 平台
|
||
this.platform = 1
|
||
|
||
this.loadFromStorage()
|
||
}
|
||
|
||
static saveFieldToStorage(key, value) {
|
||
setStorage(`StarCloud_${key}`, value)
|
||
}
|
||
|
||
/**
|
||
* 初始化星云
|
||
* @param {Object} params
|
||
* @param {String} params.clientId 客户端Id
|
||
* @param {String} params.clientSecret 客户端密码
|
||
* @param {String} params.env 环境
|
||
* @param {Array<AccountInfo>} params.accounts 账号列表,后续方法中需传入uid,若未传入则默认使用第一个账号
|
||
* @param {Number} params.platform 平台 1:uni 2:web 默认为1
|
||
* @param {Boolean} params.isReportLog 是否上报日志
|
||
*/
|
||
init(params) {
|
||
Object.assign(StarCloud.prototype, device, lock, other, password, record, user, common)
|
||
|
||
const { clientId, clientSecret, env, platform, accounts,url } = params
|
||
this.envVersion = 'release'
|
||
if (platform !== 2) {
|
||
const appInfo = uni.getAccountInfoSync()
|
||
this.envVersion = appInfo.miniProgram.envVersion
|
||
|
||
// 监听特性值变化
|
||
onBLECharacteristicValueChange(this.listenCharacteristicValue.bind(this))
|
||
}
|
||
|
||
this.url = url
|
||
this.isReportLog = params.isReportLog
|
||
|
||
this.env = env || 'XHJ'
|
||
this.clientId = clientId
|
||
this.clientSecret = clientSecret
|
||
this.platform = platform || 1
|
||
this.accounts = accounts
|
||
|
||
this.accountSave(accounts)
|
||
}
|
||
|
||
loadFromStorage() {
|
||
Object.keys(this).forEach(key => {
|
||
const value = getStorage(`StarCloud_${key}`)
|
||
if (value !== undefined) {
|
||
this[key] = value
|
||
}
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 账号保存
|
||
* @param {Array<AccountInfo>} accounts
|
||
*/
|
||
// eslint-disable-next-line class-methods-use-this
|
||
accountSave(accounts) {
|
||
let starCloudAccount = getStorage('starCloudAccount')
|
||
if (!starCloudAccount) {
|
||
starCloudAccount = {}
|
||
}
|
||
accounts.forEach(account => {
|
||
const accountInfo = accounts[account.uid]
|
||
if (accountInfo) {
|
||
starCloudAccount[account.uid] = {
|
||
...accountInfo,
|
||
...account
|
||
}
|
||
} else {
|
||
starCloudAccount[account.uid] = account
|
||
}
|
||
})
|
||
setStorage('starCloudAccount', starCloudAccount)
|
||
}
|
||
}
|
||
|
||
const starCloudInstance = new Proxy(StarCloud.prototype, {
|
||
get(target, key) {
|
||
return Reflect.get(target, key)
|
||
},
|
||
set(target, key, value) {
|
||
StarCloud.saveFieldToStorage(key, value)
|
||
return Reflect.set(target, key, value)
|
||
}
|
||
})
|
||
Object.setPrototypeOf(StarCloud, starCloudInstance)
|
||
|
||
export default starCloudInstance
|