170 lines
5.0 KiB
JavaScript
170 lines
5.0 KiB
JavaScript
import mOnFire from './other/onfire.js'
|
||
import $wxBlufiImpl from './xBlufi-wx-impl.js' // 0表示阿里支付宝小程序 1表示微信小程序
|
||
|
||
export const XMQTT_SYSTEM = {
|
||
Alis: 0,
|
||
WeChat: 1,
|
||
}
|
||
|
||
export const XBLUFI_TYPE = {
|
||
TYPE_STATUS_CONNECTED: '-2', // 设备连接状态回调
|
||
TYPE_CLOSE_CONNECTED: '-1', // 主动关闭连接
|
||
TYPE_CONNECTED: '0', // 主动连接
|
||
TYPE_GET_DEVICE_LISTS: '1', // 发现设备列表回调
|
||
TYPE_INIT_ESP32_RESULT: '2',
|
||
TYPE_RECIEVE_CUSTON_DATA: '3', // 接收到自定义数据
|
||
TYPE_CONNECT_ROUTER_RESULT: '4',
|
||
TYPE_RECIEVE_MY_DATA: '5',
|
||
TYPE_GET_DEVICE_LISTS_START: '41', // 发现设备列表回调开始
|
||
TYPE_GET_DEVICE_LISTS_STOP: '42', // 停止发现设备列表回调
|
||
}
|
||
|
||
export const OnFireEvent = {
|
||
EVENT_START_DISCONORY: '0', // 蓝牙状态事件 发现设备
|
||
EVENT_CONNECT_DISCONNECT: '1', // 通知连接或断开蓝牙
|
||
EVENT_NOFITY_INIT_ESP32: '3', // 通知获取蓝牙设备的服务uuid列表等初始化工作
|
||
ENENT_ALL: '6',
|
||
EVENT_NOFITY_SEND_ROUTER_SSID_PASSWORD: '50', // 通知发送路由器的ssid和password
|
||
EVENT_NOFITY_SEND_CUSTON_DATA: '51', // 通知发送自定义数据
|
||
}
|
||
|
||
let once = 0
|
||
let myPath = ''
|
||
|
||
/**
|
||
* 初始化
|
||
* @param {number} type 参考 XMQTT_SYSTEM
|
||
*/
|
||
export const initXBlufi = type => {
|
||
if (type === XMQTT_SYSTEM.WeChat && once === 0) {
|
||
console.log('初始化@@@@@@@!!!!!!!!!!!!')
|
||
once = 1
|
||
$wxBlufiImpl.init()
|
||
}
|
||
}
|
||
|
||
export const setPath = path => {
|
||
myPath = path
|
||
}
|
||
|
||
export const getPath = () => myPath
|
||
|
||
export const notifyDeviceMsgEvent = options => {
|
||
mOnFire.fire(OnFireEvent.ENENT_ALL, options)
|
||
}
|
||
|
||
export const listenDeviceMsgEvent = (isSetListener, callback) => {
|
||
isSetListener ? mOnFire.on(OnFireEvent.ENENT_ALL, callback) : mOnFire.un(callback)
|
||
}
|
||
|
||
/**
|
||
* 开始或停止发现附近的蓝牙设备
|
||
* @param {Object} options 连接参数 {"isStart":true , "filter":"名字过滤"} :是否开始发现设备
|
||
*/
|
||
export const notifyStartDiscoverBle = options => {
|
||
mOnFire.fire(OnFireEvent.EVENT_START_DISCONORY, options)
|
||
}
|
||
|
||
/**
|
||
* 监听开始/停止发现设备事件
|
||
* @param {boolean} isSetListener 是否设置监听
|
||
* @param {Function} callback 回调函数
|
||
*/
|
||
export const listenStartDiscoverBle = (isSetListener, callback) => {
|
||
isSetListener ? mOnFire.on(OnFireEvent.EVENT_START_DISCONORY, callback) : mOnFire.un(callback)
|
||
}
|
||
|
||
/**
|
||
* 连接或断开蓝牙连接
|
||
* @param {Object} options 连接参数 {"connect":true,"deviceID":"设备id,蓝牙发现列表获取"}
|
||
*/
|
||
export const notifyConnectBle = options => {
|
||
mOnFire.fire(OnFireEvent.EVENT_CONNECT_DISCONNECT, options)
|
||
}
|
||
|
||
export const listenConnectBle = (isSetListener, callback) => {
|
||
isSetListener ? mOnFire.on(OnFireEvent.EVENT_CONNECT_DISCONNECT, callback) : mOnFire.un(callback)
|
||
}
|
||
|
||
/**
|
||
* 通知初始化获取设备的服务列表等信息
|
||
* @param {Object} options 连接参数 {"deviceId":"设备的设备id"}
|
||
*/
|
||
export const notifyInitBleEsp32 = options => {
|
||
mOnFire.fire(OnFireEvent.EVENT_NOFITY_INIT_ESP32, options)
|
||
}
|
||
|
||
export const listenInitBleEsp32 = (isSetListener, callback) => {
|
||
isSetListener ? mOnFire.on(OnFireEvent.EVENT_NOFITY_INIT_ESP32, callback) : mOnFire.un(callback)
|
||
}
|
||
|
||
/**
|
||
* 发送要连接的路由器的ssid和密码
|
||
* @param {Object} options 连接参数 {"deviceId":"设备的设备id","serverId":"服务id","characterId":"通道","ssid":"路由器名字","password":"密码"}
|
||
*/
|
||
export const notifySendRouterSsidAndPassword = options => {
|
||
mOnFire.fire(OnFireEvent.EVENT_NOFITY_SEND_ROUTER_SSID_PASSWORD, options)
|
||
}
|
||
|
||
export const listenSendRouterSsidAndPassword = (isSetListener, callback) => {
|
||
isSetListener
|
||
? mOnFire.on(OnFireEvent.EVENT_NOFITY_SEND_ROUTER_SSID_PASSWORD, callback)
|
||
: mOnFire.un(callback)
|
||
}
|
||
|
||
/**
|
||
* 发送自定义数据
|
||
* @param {Object} options 连接参数 {"deviceId":"设备的设备id","serverId":"服务id","characterId":"通道","customData":"自定义数据""}
|
||
*/
|
||
export const notifySendCustomData = options => {
|
||
mOnFire.fire(OnFireEvent.EVENT_NOFITY_SEND_CUSTON_DATA, options)
|
||
}
|
||
|
||
export const listenSendCustomData = (isSetListener, callback) => {
|
||
isSetListener
|
||
? mOnFire.on(OnFireEvent.EVENT_NOFITY_SEND_CUSTON_DATA, callback)
|
||
: mOnFire.un(callback)
|
||
}
|
||
|
||
export const mDeviceEvent = {
|
||
XMQTT_SYSTEM,
|
||
XBLUFI_TYPE,
|
||
OnFireEvent,
|
||
notifyDeviceMsgEvent,
|
||
listenDeviceMsgEvent,
|
||
notifyStartDiscoverBle,
|
||
listenStartDiscoverBle,
|
||
notifyConnectBle,
|
||
listenConnectBle,
|
||
notifyInitBleEsp32,
|
||
listenInitBleEsp32,
|
||
notifySendRouterSsidAndPassword,
|
||
listenSendRouterSsidAndPassword,
|
||
notifySendCustomData,
|
||
listenSendCustomData,
|
||
initXBlufi,
|
||
setPath,
|
||
getPath,
|
||
}
|
||
|
||
export default {
|
||
XMQTT_SYSTEM,
|
||
XBLUFI_TYPE,
|
||
OnFireEvent,
|
||
notifyDeviceMsgEvent,
|
||
listenDeviceMsgEvent,
|
||
notifyStartDiscoverBle,
|
||
listenStartDiscoverBle,
|
||
notifyConnectBle,
|
||
listenConnectBle,
|
||
notifyInitBleEsp32,
|
||
listenInitBleEsp32,
|
||
notifySendRouterSsidAndPassword,
|
||
listenSendRouterSsidAndPassword,
|
||
notifySendCustomData,
|
||
listenSendCustomData,
|
||
initXBlufi,
|
||
setPath,
|
||
getPath,
|
||
}
|