camera.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // pages/light/light.js
  2. import { connectStateMap, connectStateTypes } from '../../data/devType.js'
  3. import { MAX_COLOR_TEMPERATURE, MIN_COLOR_TEMPERATURE, COLOR_TEMPERATURE_STEP, bleInfo } from '../../data/lampType.js'
  4. import { handle } from '../../utils/mjs_handle.js'
  5. import { calculateColor } from '../../utils/mjs_color.js'
  6. import light_cmd from '../../utils/light_cmd.js'
  7. import { _curry } from '../../utils/mjs_curry.js'
  8. import BLE from '../../utils/mjs_wxble.js'
  9. console.log(BLE);
  10. const ble = "";
  11. // new BLE(null, ["bleLight"]);
  12. const tmpDevs = [
  13. {
  14. id: 1,
  15. name: "123",
  16. deviceId: "1234"
  17. },
  18. {
  19. id: 2,
  20. name: "设备2",
  21. deviceId: "23456"
  22. }
  23. ]
  24. // 蓝牙模块连接对象信息参数
  25. const connectBleParams = [];
  26. // 蓝牙服务信息
  27. let bleServices = [];
  28. Page({
  29. /**
  30. * 页面的初始数据
  31. */
  32. data: {
  33. // unConnect scaning connecting connected
  34. connectStateTypes: connectStateTypes,
  35. ble: {
  36. state: connectStateTypes.unConnect,
  37. devName: "",
  38. deviceId: "",
  39. init: false,
  40. // 控制uuid
  41. controlUuid: "",
  42. // 监听数据uuid
  43. notifyUuid: "",
  44. },
  45. lamp: {
  46. // 信息相关
  47. temp_max: MAX_COLOR_TEMPERATURE,
  48. temp_min: MIN_COLOR_TEMPERATURE,
  49. temp_step: COLOR_TEMPERATURE_STEP,
  50. // 开关状态
  51. switch: false,
  52. // 全开状态(无色温调节)
  53. fullOpen: false,
  54. // 亮度
  55. brightness: 50,
  56. // 色温
  57. colorTemperature: MAX_COLOR_TEMPERATURE,
  58. // 色彩
  59. bgc: "#3b3b3b"
  60. },
  61. bleDevs: tmpDevs,
  62. },
  63. // 切换设备handle
  64. switchCameraHandle(e){
  65. let deviceCode = e.currentTarget.dataset.dev;
  66. this.excuteSwitchCamera(deviceCode);
  67. },
  68. /**
  69. * 切换控制的设备
  70. * @param {*} deviceCode
  71. */
  72. excuteSwitchCamera(deviceCode){
  73. // 使用策略模式
  74. },
  75. // 获取蓝牙发送数据对象
  76. excuteBleSend(data){
  77. console.log('获取蓝牙发送数据对象');
  78. // 固定值
  79. let controlUuid = bleInfo.controlUuid;
  80. let characteristicFirstUuid = bleInfo.characteristicFirstUuid;
  81. let deviceId = this.data.ble.deviceId;
  82. let serverUuid = '';
  83. let characteristicUuid = '';
  84. let characteristic;
  85. let server = bleServices.find(s=>{
  86. console.log(`服务uuid ${s.uuid}`);
  87. console.log(s);
  88. return s.uuid.startsWith(controlUuid);
  89. });
  90. if(!server)
  91. {
  92. serverUuid = bleInfo.controlUuid;
  93. }else
  94. {
  95. serverUuid = server.uuid;
  96. characteristic = server.characteristics.find(c=>
  97. c.uuid.startsWith(characteristicFirstUuid)
  98. );
  99. }
  100. //获取
  101. if(!characteristic)
  102. {
  103. // return bleFail('获取服务失败', '该设备不支持该程序');
  104. characteristicUuid = bleInfo.controlUuid;
  105. }
  106. else
  107. {
  108. characteristicUuid = characteristic.uuid;
  109. }
  110. console.log(`服务uuid ${serverUuid} 特征uuid ${characteristicUuid}`);
  111. return ble.sendData(deviceId, serverUuid, characteristicUuid, data);
  112. },
  113. bleFail(title,msg){
  114. wx.showModal({
  115. title: title,
  116. content: msg,
  117. success (res) {
  118. this.setData({
  119. ble: {...this.data.ble,
  120. state: connectStateTypes.unConnect,
  121. init: false,
  122. devName: "",
  123. deviceId: "",
  124. },
  125. bleDevs: []
  126. });
  127. bleServices = [];
  128. }
  129. });
  130. },
  131. // 蓝牙控制板块
  132. searchDeviceHandle() {
  133. // 开始搜索设备
  134. this.excuteSearchDevice();
  135. },
  136. async excuteSearchDevice(){
  137. console.log("搜索 蓝牙设备");
  138. let err,res;
  139. if(!this.data.ble.init){
  140. [err,res] = await handle(ble.initBle());
  141. if(err){
  142. return this.bleFail('蓝牙初始化失败', err.errMsg);
  143. }
  144. ble.onSearch = this.onSearchHandle;
  145. }
  146. this.setData({
  147. ble: {...this.data.ble,
  148. state: connectStateTypes.scaning,
  149. init: true
  150. },
  151. bleDevs: []
  152. });
  153. ble.search(this.onSearchHandle);
  154. },
  155. // 搜索到设备基础函数
  156. onSearchHandle(device){
  157. console.log('搜索到设备');
  158. if(this.data.bleDevs.find(dev=>dev.deviceId === device.deviceId )){
  159. return console.log('设备再次被搜索到');
  160. }
  161. console.log(device);
  162. device.id = device.deviceId;
  163. let arr = this.data.bleDevs;
  164. arr.push(device);
  165. this.setData({
  166. bleDevs: arr
  167. })
  168. },
  169. // 连接设备
  170. async connectDevHandle(e) {
  171. console.log("click connectDev")
  172. let err,res;
  173. let dev = e.detail;
  174. console.log(dev);
  175. this.setData({
  176. ble: {...this.data.ble,state: connectStateTypes.connecting,
  177. devName: dev.name,
  178. deviceId: dev.deviceId
  179. },
  180. });
  181. // 停止搜索
  182. [err,res] = await handle(ble.stopSearch());
  183. if(err){
  184. return this.bleFail('停止搜索失败', err.errMsg);
  185. }
  186. wx.showToast({title: `连接${dev.name}中...`, })
  187. // 连接至设备
  188. [err,res] = await handle(ble.connectDev(dev.deviceId));
  189. if(err){ return this.bleFail('连接失败', err.errMsg);}
  190. // 连接成功
  191. this.setData({
  192. ble:{...this.data.ble, state:connectStateTypes.connected }
  193. });
  194. // 设备服务管理
  195. this.connectServices(dev.deviceId);
  196. },
  197. // 获取蓝牙服务
  198. async connectServices(deviceId){
  199. // uuid FF01
  200. let [err,res] = await handle(ble.getBleServices(deviceId));
  201. if(err){ return this.bleFail('获取服务失败', err.errMsg);}
  202. // 保存服务信息;
  203. bleServices = res;
  204. },
  205. /**
  206. * 生命周期函数--监听页面加载
  207. */
  208. onLoad(options) {
  209. },
  210. /**
  211. * 生命周期函数--监听页面初次渲染完成
  212. */
  213. onReady() {
  214. },
  215. /**
  216. * 生命周期函数--监听页面显示
  217. */
  218. onShow() {
  219. // 配置导航栏
  220. wx.setNavigationBarTitle({
  221. title: '相机控制器'
  222. })
  223. },
  224. /**
  225. * 生命周期函数--监听页面隐藏
  226. */
  227. onHide() {
  228. },
  229. /**
  230. * 生命周期函数--监听页面卸载
  231. */
  232. onUnload() {
  233. },
  234. /**
  235. * 页面相关事件处理函数--监听用户下拉动作
  236. */
  237. onPullDownRefresh() {
  238. },
  239. /**
  240. * 页面上拉触底事件的处理函数
  241. */
  242. onReachBottom() {
  243. },
  244. /**
  245. * 用户点击右上角分享
  246. */
  247. onShareAppMessage() {
  248. }
  249. })