bt_cmd.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // 设备命令
  2. // TLV格式(16进制)
  3. // 二进制协议使用 TLV (Type-Length-Value)编码格式。
  4. // - Type:固定长度, 一字节。
  5. // - Length:可变长度。 0 或 1 个字节。
  6. // - Value:二进制数据。 长度由 Length 字段决定。 数据本身可以上数字或者字符串。
  7. const EnumOpen = {
  8. close: 0,
  9. open: 1
  10. };
  11. const EnumConnectType = {
  12. bt: 0,
  13. ble: 1,
  14. upnp: 2,
  15. mqtt: 3
  16. }
  17. class BtCmd {
  18. // BtCmd._();
  19. constructor() { }
  20. // 获取版本号
  21. static get queryVersion() {
  22. return this._build(CmdBase.queryVersion, [0x0]);
  23. }
  24. // 查询电量 电量值 0-9,0对应10%,9对应100%,10充电
  25. static get queryKwh() {
  26. return this._build(CmdKwh.queryKwh, [0x0]);
  27. }
  28. // 查询耳机电量
  29. static get queryKwhEarPhone() {
  30. return this._build(CmdEarPhone.queryEarPhoneKwh, [0x0]);
  31. }
  32. // 查询EQ音效
  33. static get queryEQ() {
  34. return this._build(CmdEarPhone.queryEQ, [0x0]);
  35. }
  36. // 设置EQ音效
  37. static setEQ(list) {
  38. return this._build(CmdEarPhone.setEQ, list);
  39. }
  40. // B5耳机-低电耗模式
  41. static get queryLowPower() {
  42. return this._build(CmdEarPhone.queryLowPower, [0x0]);
  43. }
  44. static setLowPowerMode(isOpen) {
  45. return this._build(CmdEarPhone.setLowPowerMode, [isOpen]);
  46. }
  47. // B5耳机-自定义操作手势
  48. static get queryCtrlStatus() {
  49. return this._build(CmdEarPhone.queryCtrlStatus, [0x0]);
  50. }
  51. static setCtrlStatus(singleClick, doubleClick, longClick) {
  52. return this._build(CmdEarPhone.setCtrlStatus, [singleClick, doubleClick, longClick]);
  53. }
  54. // 查询低延时模式
  55. static get queryLowDelayMode() {
  56. return this._build(CmdEarPhone.queryLowDelayMode, [0x0]);
  57. }
  58. // 低延时模式
  59. static setLowDelayMode(open, mode) {
  60. return this._build(CmdEarPhone.setLowDelayMode, [open, mode]);
  61. }
  62. // 低电量提示音
  63. static get queryLowKwhWarningTone() {
  64. return this._build(CmdKwh.queryLowKwhWarningTone, [0x00]);
  65. }
  66. static setLowKwhWarningTone(isNotify) {
  67. return this._build(CmdKwh.setLowKwhWarningTone, isNotify ? [0x01] : [0x00]);
  68. }
  69. // 蓝牙自动播放
  70. static setAutoPlay(switchStatus) {
  71. return this._build(CmdBase.setAutoPlay, [switchStatus]);
  72. }
  73. static get getAutoPlay() {
  74. return this._build(CmdBase.getAutoPlay, [0x00]);
  75. }
  76. // 查询音量
  77. static get queryVolume() {
  78. return this._build(CmdBase.queryVolume, [0x00]);
  79. }
  80. static setVolume(volume) {
  81. return this._build(CmdBase.setVolume, [volume]);
  82. }
  83. // 查询闹钟
  84. static get queryAlarm() {
  85. return this._build(CmdRtc.queryAlarm, [0x00]);
  86. }
  87. // 设置闹钟
  88. static setAlarm(switchStatus, weekCycle, hour, minutes, channel = 1) {
  89. const weeks = CmdWeek.week2Cmd(weekCycle);
  90. return this._build(CmdRtc.setAlarm, [switchStatus, weeks, hour, minutes, channel]);
  91. }
  92. // 查询休眠
  93. static get querySleep() {
  94. return this._build(CmdRtc.querySleep, [0x00]);
  95. }
  96. static get querySleepAfterPlayPause() {
  97. return this._build(CmdRtc.querySleepAfterPlayPause, [0x00]);
  98. }
  99. static setSleepAfterPlayPause(time) {
  100. return this._build(CmdRtc.setSleepAfterPlayPause, [time]);
  101. }
  102. // 设置休眠
  103. static setSleep(switchStatus, hour, minutes) {
  104. return this._build(CmdRtc.setSleep, [switchStatus, hour, minutes]);
  105. }
  106. // 设置时间
  107. static get setDevTime() {
  108. const now = new Date();
  109. const y = now.getFullYear();
  110. const year = Math.floor(y / 100);
  111. const _year = y % 100;
  112. const month = now.getMonth() + 1; // getMonth() 返回 0-11,所以需要加 1
  113. const day = now.getDate();
  114. const hour = now.getHours();
  115. const minutes = now.getMinutes();
  116. const seconds = now.getSeconds();
  117. // console.log(`setTime========${CmdRtc.setTime},${year},${_year},${month},${day},${hour},${minutes},${seconds}`);
  118. // console.log(`setTime======== 2, ${CmdRtc.setTime}`);
  119. let cmd = this._build(0x50, [year, _year, month, day, hour, minutes, seconds]);
  120. console.log(`setTime========${cmd}`);
  121. return cmd;
  122. }
  123. // 查询时间
  124. static get queryTime() {
  125. return this._build(CmdRtc.queryTime, [0x00]);
  126. }
  127. // 设置RGB
  128. static setRGB(r, g, b) {
  129. return this._build(CmdRgb.set, [r, g, b]);
  130. }
  131. // 查询RGB
  132. static get queryRGB() {
  133. return this._build(CmdRgb.query, [0x0]);
  134. }
  135. static stringToUint8Array(str) {
  136. // 54 44 44 48 01 11 28 09 73 6d6172745f413246
  137. // const encoder = new TextEncoder();
  138. // const uint8Array = encoder.encode(str);
  139. // return uint8Array;
  140. return [0x73, 0x6d, 0x61, 0x72, 0x74, 0x5f, 0x41, 0x32, 0x46]
  141. }
  142. // 校验设备
  143. static checkDevice() {
  144. let secretKey = this.stringToUint8Array(CmdBase.secretKey)
  145. console.log(`checkDevice========${secretKey}`);
  146. return this._build(CmdBase.checkDevice, secretKey);
  147. }
  148. // 杰理语音开始回应
  149. static get jlStartVoiceResponse() {
  150. return this._build(CmdVoice.jlStartVoiceResponse);
  151. }
  152. // 杰理语音结束回应
  153. static get jlStopVoiceResponse() {
  154. return this._build(CmdVoice.jlStopVoiceResponse);
  155. }
  156. // 领芯语音
  157. static get lxStartVoiceResponse() {
  158. return this._build(CmdVoice.lxStartVoiceResponse);
  159. }
  160. // 杰理语音结束回应
  161. static get lxStopVoiceResponse() {
  162. return this._build(CmdVoice.lxStopVoiceResponse);
  163. }
  164. // b1开始语音回调
  165. static get b1StartVoiceResponse() {
  166. const data = [0x04, 0x80, 0x01, 0x00, 0x02];
  167. return new Uint8Array(data);
  168. }
  169. // b1结束语音回调
  170. static get b1StopVoiceResponse() {
  171. const data = [0x05, 0x80, 0x01, 0x00, 0x02];
  172. return new Uint8Array(data);
  173. }
  174. // 固定头部
  175. static get _header() {
  176. return [0x54, 0x44, 0x44, 0x48];
  177. }
  178. // 版本号
  179. static get _version() {
  180. return 0x01;
  181. }
  182. // 生成命令(固定头部+版本号+命令总长度+命令类型+)
  183. static _build(cmdType, otherCmd = [], isWriteChildCmdLength = true) {
  184. const cmd = [];
  185. // 固定头部
  186. cmd.push(...this._header);
  187. // 版本号
  188. cmd.push(this._version);
  189. // 命令类型
  190. cmd.push(cmdType);
  191. if (isWriteChildCmdLength) {
  192. // 子命令长度
  193. const childLength = this._int2Hex(otherCmd.length);
  194. cmd.push(childLength);
  195. }
  196. // 其他命令
  197. if (otherCmd.length > 0) {
  198. for (const element of otherCmd) {
  199. cmd.push(this._int2Hex(element));
  200. }
  201. }
  202. // 命令长度(位置在版本号之后)
  203. const length = cmd.length + 1;
  204. const l = this._int2Hex(length);
  205. cmd.splice(5, 0, l);
  206. return new Uint8Array(cmd);
  207. }
  208. // 打印cmd
  209. static printTLV(cmd, isSend = false) {
  210. let result = "";
  211. const key = isSend ? "发送" : "接收";
  212. if (isSend) {
  213. // console.log(`${key}原始-${cmd.toString()}`);
  214. }
  215. for (let i = 0; i < cmd.length; i++) {
  216. const cmdStr = `${this._int2HexString(cmd[i])}`.padStart(2, "0");
  217. result += cmdStr;
  218. }
  219. if (isSend) {
  220. mqttAddDebugCmd(`发送蓝牙指令:${cmd} \n ${result}`);
  221. }
  222. console.log(`${key}解析- [${result}]`);
  223. }
  224. static _int2Hex(value) {
  225. return value & 0xFF;
  226. }
  227. static _int2HexString(value) {
  228. return value.toString(16).toUpperCase();
  229. }
  230. }
  231. // 设备命令类型
  232. const CmdBase = {
  233. secretKey: "smart_A2F",
  234. queryVersion: 0x30,
  235. checkDevice: 0x28,
  236. checkDeviceSuccess: 0x27,
  237. setAutoPlay: 0x24,
  238. getAutoPlay: 0x23,
  239. queryChannelAngle: 0x38,
  240. switchLed: 0x39,
  241. queryVolume: 0x32,
  242. setVolume: 0x33,
  243. newFlag: "MW-Mate X\\(4G_WIFI\\)|MW-2AX\\(WIFI-N\\)|MW-SR1\\(4G_WIFI\\)|MW-SR1\\(4G_WIFI_MEIZU01\\)",
  244. // volumeMax: (new RegExp(`^(${CmdBase.newFlag})$`, 'i').test(DeviceManager.instance.clientType || "")) ? 0x20 : 0xF,
  245. volumeMax: 15,
  246. parseVersion(version) {
  247. let ver;
  248. if (version <= 0) {
  249. ver = "1.0.0";
  250. } else {
  251. const split = version.toString().split("");
  252. if (split.length === 1) {
  253. split.unshift("0", "0");
  254. } else if (split.length === 2) {
  255. split.unshift("0");
  256. }
  257. ver = split.join(".");
  258. }
  259. console.log(`parseVersion======${version},,,,,,,${ver.toString()}`);
  260. return ver;
  261. }
  262. };
  263. // 耳机相关指令
  264. const CmdEarPhone = {
  265. queryLowDelayMode: 0xb2,
  266. setLowDelayMode: 0xb3,
  267. queryEQ: 0xb4,
  268. setEQ: 0xb5,
  269. queryEarPhoneKwh: 0xb1,
  270. queryCtrlStatus: 0xB6,
  271. setCtrlStatus: 0xB7,
  272. queryLowPower: 0xB8,
  273. setLowPowerMode: 0xB9
  274. };
  275. // 时间相关
  276. const CmdRtc = {
  277. setTime: 0x50,
  278. queryTime: 0x51,
  279. setAlarm: 0x52,
  280. queryAlarm: 0x54,
  281. setSleep: 0x56,
  282. querySleep: 0x57,
  283. setSleepAfterPlayPause: 0x5a,
  284. querySleepAfterPlayPause: 0x5b
  285. };
  286. // 设置设备颜色
  287. const CmdRgb = {
  288. query: 0x80,
  289. set: 0x81
  290. };
  291. // 电量相关命令
  292. const CmdKwh = {
  293. queryKwh: 0x22,
  294. setLowKwhWarningTone: 0x26,
  295. queryLowKwhWarningTone: 0x25
  296. };
  297. // 按键
  298. const CmdKey = {
  299. previousSong: 0x0a,
  300. nextSong: 0x0b,
  301. previousChannel: 0x0c,
  302. nextChannel: 0x0d,
  303. type: 0x21,
  304. pressShort: 0x02,
  305. pressLongUp: 0x04,
  306. pressLong: 0x08,
  307. pressDouble: 0x09,
  308. next: 0x01,
  309. previous: 0x02,
  310. changeProgram: 0x03
  311. };
  312. // 语音
  313. const CmdVoice = {
  314. startVoice: 0x01,
  315. jlStartVoice: 0x66,
  316. jlStopVoice: 0x68,
  317. jlStartVoiceResponse: 0x67,
  318. jlStopVoiceResponse: 0x69,
  319. jlReceiveVoiceData: 0x70,
  320. lxStartVoice: 0x71,
  321. lxStopVoice: 0x72,
  322. lxStartVoiceResponse: 0x40,
  323. lxStopVoiceResponse: 0x41,
  324. sjStartVoice: 0x23,
  325. sjStopVoice: 0x24
  326. };
  327. // 一周
  328. const CmdWeek = {
  329. sunday: 0x01,
  330. monday: 0x02,
  331. tuesday: 0x04,
  332. wednesday: 0x08,
  333. thursday: 0x10,
  334. friday: 0x20,
  335. saturday: 0x40,
  336. weeks: [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40],
  337. week2Cmd(weekCycle) {
  338. let result = 0;
  339. for (let i = 0; i < weekCycle.length; i++) {
  340. const item = weekCycle[i];
  341. if (item === EnumOpen.open) {
  342. result += this.weeks[i];
  343. }
  344. }
  345. return result;
  346. },
  347. cmd2Week(cmd) {
  348. const w = [0, 0, 0, 0, 0, 0, 0];
  349. for (let i = this.weeks.length - 1; i >= 0; i--) {
  350. const week = this.weeks[i];
  351. if (week <= cmd) {
  352. w[i] = EnumOpen.open;
  353. cmd -= week;
  354. }
  355. }
  356. return w;
  357. }
  358. };
  359. module.exports = {
  360. BtCmd,
  361. CmdBase,
  362. CmdEarPhone,
  363. CmdRtc,
  364. CmdRgb,
  365. CmdKwh,
  366. CmdKey,
  367. CmdVoice,
  368. CmdWeek
  369. };