device.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. const data_trans = getApp()
  2. const utils_base64 = require('../../utils/util.js')
  3. const websocket_api = require('../../utils/socket_api.js')
  4. const get_date_api = require('../../utils/getDate.js')
  5. var initData = '以下为操作结果:'
  6. var extraLine = [];
  7. Page({
  8. data: {
  9. log_text: initData,
  10. inputText: '',
  11. receiveText: '',
  12. log_text_area: '',
  13. connectedDeviceId: '',
  14. services: {},
  15. characteristics: {},
  16. name:'',
  17. advertisData:'',
  18. connected: true,
  19. salt:'',
  20. //下拉框相关的.
  21. display: 'none',
  22. proShow: false,
  23. proindex: 0, //255
  24. proData: [],
  25. phyAddrInfoLIst: [], //保存物理地址信息,包括中文名,英文名,枚举值
  26. claShow: false,
  27. claindex: 0, //255
  28. claData: [],
  29. didNameInfoList:[], //保存did信息,包括中文名,英文名
  30. },
  31. add: function (e) {
  32. extraLine.push(e)
  33. this.setData({
  34. log_text_area: initData + '\n' + extraLine.join('\n')
  35. })
  36. },
  37. remove: function (e) {
  38. if (extraLine.length > 0) {
  39. extraLine.pop(e)
  40. this.setData({
  41. log_text_area: initData + '\n' + extraLine.join('\n')
  42. })
  43. }
  44. },
  45. // 点击下拉专业显示框
  46. selectCom() {
  47. var that = this;
  48. console.log(that.data.proShow)
  49. if (that.data.proShow) {
  50. that.setData({
  51. display: 'none'
  52. })
  53. } else {
  54. that.setData({
  55. display: 'block',
  56. })
  57. }
  58. that.setData({
  59. proShow: !that.data.proShow,
  60. claShow: false
  61. })
  62. },
  63. //点击选择部件
  64. optionUnit(e) {
  65. var that = this;
  66. var phyAddrIndex = e.currentTarget.dataset.proindex;
  67. console.log("phyAddrIndex:", phyAddrIndex)
  68. console.log("name_cn:", that.data.phyAddrInfoLIst.args[phyAddrIndex].name_cn)
  69. console.log("enum_value:", that.data.phyAddrInfoLIst.args[phyAddrIndex].enum_value)
  70. that.setData({
  71. proindex: phyAddrIndex,
  72. proShow: !that.data.proShow,
  73. display: 'none',
  74. })
  75. wx.request({
  76. url: 'http://www.id27149.com:2222/codecInfo',//服务器接口地址
  77. method:'POST',//请求类型
  78. header: {'content-type': 'application/json'},
  79. data: {'method':'get_read_did_name_list','args':["BMW_790", that.data.phyAddrInfoLIst.args[phyAddrIndex].enum_value]},
  80. success: function(res) {
  81. console.log('请求did列表成功,res:', res.data)
  82. //把请求到的原始数据放到phyAddrInfoLIst里
  83. that.setData({
  84. didNameInfoList: res.data
  85. })
  86. //把请求到的部件名称放到下拉框列表里面
  87. var did_list = []
  88. for (var i = 0; i < res.data.args.length; i++) {
  89. did_list[i] = res.data.args[i][1]
  90. }
  91. that.setData({
  92. claData: did_list
  93. })
  94. },
  95. fail: function() {
  96. console.log('请求did列表失败')
  97. }
  98. })
  99. },
  100. //点击下拉框:功能支持情况
  101. selectFeature() {
  102. var that = this;
  103. if (that.data.claShow) {
  104. that.setData({
  105. display: 'none',
  106. })
  107. } else {
  108. that.setData({
  109. display: 'block',
  110. })
  111. }
  112. that.setData({
  113. claShow: !that.data.claShow,
  114. proShow: false
  115. })
  116. },
  117. optionFeature(e) {
  118. var that = this;
  119. var claindex = e.currentTarget.dataset.claindex;
  120. console.log("optionFeature:", claindex)
  121. that.setData({
  122. claindex: claindex,
  123. claShow: !that.data.claShow,
  124. display: 'none'
  125. })
  126. },
  127. //隐藏遮罩层
  128. hiddenShadow:function(){
  129. this.setData({
  130. display:'none',
  131. proShow:false,
  132. claShow:false
  133. })
  134. },
  135. GetSelectListInfo:function(){
  136. var that = this;
  137. console.log("按下获取下拉框选择情况按钮")
  138. if(that.data.proData != null){
  139. console.log("当前选择物理地址情况如下:")
  140. console.log("proindex:", that.data.proindex)
  141. console.log("name_cn:", that.data.phyAddrInfoLIst.args[that.data.proindex].name_cn)
  142. console.log("enum_value:", that.data.phyAddrInfoLIst.args[that.data.proindex].enum_value)
  143. }else{
  144. console.log("部件下拉列表框更新失败")
  145. }
  146. if(that.data.claData != null){
  147. console.log("当前选择did情况如下:")
  148. console.log("optionFeature:", that.data.claindex)
  149. console.log("name_en:", that.data.didNameInfoList.args[that.data.claindex][0])
  150. console.log("name_ch:", that.data.didNameInfoList.args[that.data.claindex][1])
  151. }else{
  152. console.log("功能下拉列表框更新失败")
  153. }
  154. console.log("点击获取信息按钮,向服务器请求did")
  155. var phy_enum_val = that.data.phyAddrInfoLIst.args[that.data.proindex].enum_value
  156. var did_name = that.data.didNameInfoList.args[that.data.claindex][0]
  157. websocket_api.SocketTxMsg(
  158. JSON.stringify({
  159. "method": "read_did",
  160. "args": {'N_TA':phy_enum_val,'did_list':[did_name]}
  161. }),
  162. this.WebsocketSendSuccessEvent,
  163. this.WebsocketSendFailEvent
  164. )
  165. },
  166. //获取文字输入框内容
  167. bindInput: function (e) {
  168. this.setData({
  169. inputText: e.detail.value
  170. })
  171. console.log(e.detail.value)
  172. },
  173. WebsocketSendSuccessEvent:function (res) {
  174. console.log("socket发送成功,res:",res)
  175. },
  176. WebsocketSendFailEvent:function (res) {
  177. console.log("socket发送失败,res:", res)
  178. },
  179. // // 获取设备生产日期
  180. // GetManufactureDate: function()
  181. // {
  182. // console.log("点击获取生产日期按钮,向服务器请求did")
  183. // websocket_api.SocketTxMsg(
  184. // JSON.stringify({
  185. // "method": "read_did",
  186. // "args": {'N_TA':18,'did_list':['UDS_MANUFACTURING_DATA']}
  187. // }),
  188. // this.WebsocketSendSuccessEvent,
  189. // this.WebsocketSendFailEvent
  190. // )
  191. // },
  192. // // 获取设备制造厂商
  193. // GetManufacture: function()
  194. // {
  195. // var data = "521214"
  196. // // this.setData({inputText: "521214"})
  197. // this.SendBleData(data)
  198. // },
  199. //发送指令
  200. SendBleData: function (str) {
  201. var that = this
  202. //正常的下发蓝牙数据
  203. if (that.data.connected) {
  204. // 把string转换为hex后进行发送
  205. var buffer = data_trans.stringToHexBuffer(str)
  206. wx.writeBLECharacteristicValue({
  207. deviceId: that.data.connectedDeviceId,
  208. serviceId: '0000FEE7-0000-1000-8000-00805F9B34FB',
  209. characteristicId: '000036F5-0000-1000-8000-00805F9B34FB',
  210. value: buffer,
  211. success: function (res) {
  212. console.log('发送成功')
  213. }
  214. })
  215. }
  216. else {
  217. wx.showModal({
  218. title: '提示',
  219. content: '蓝牙已断开',
  220. showCancel: false,
  221. success: function (res) {
  222. that.setData({
  223. searching: false
  224. })
  225. }
  226. })
  227. }
  228. },
  229. // //点击发送按钮发送指令
  230. // SendButton: function () {
  231. // var that = this
  232. // console.log("发送蓝牙数据:", that.data.inputText)
  233. // this.SendBleData(that.data.inputText)
  234. // },
  235. onSocketMessageCallback:function(res){
  236. console.log("收到服务器消息:res", res);
  237. if(res == "heartbeat") {
  238. console.log("rx server heartbeat")
  239. } else {
  240. //字符串转为字典.
  241. const dict = JSON.parse(res)
  242. if(dict['method'] == "send_data"){
  243. console.log("将数据通过蓝牙发送至设备,", dict['args'])
  244. this.SendBleData(dict['args'])
  245. } else if(dict['method'] == "task_result") {
  246. //显示到界面上
  247. var that = this
  248. // that.setData({receiveText: JSON.stringify(dict['args'])})
  249. var date = new Date()
  250. // var nowtime = util.formatTime(date);
  251. this.add(get_date_api.formatTime(date) + ":" + JSON.stringify(dict['args']))
  252. // this.add(JSON.stringify(dict['args']))
  253. // that.setData({log_text_area: JSON.stringify(dict['args'])})
  254. }
  255. }
  256. },
  257. onLoad: function (options) {
  258. var that = this
  259. console.log(options)
  260. //刚进入界面时请求下下拉框列表
  261. wx.request({
  262. url: 'http://www.id27149.com:2222/codecInfo',//更新下拉框内容服务器接口地址
  263. method:'POST',//请求类型
  264. header: {'content-type': 'application/json'},
  265. data: {'method':'get_device_phy_value_list','args':["BMW_790"]},
  266. success: function(res) {
  267. console.log('请求物理列表成功,res:', res.data)
  268. //把请求到的原始数据放到phyAddrInfoLIst里
  269. that.setData({
  270. phyAddrInfoLIst: res.data
  271. })
  272. //把请求到的部件名称放到下拉框列表里面
  273. var phy_list = []
  274. for (var i = 0; i < res.data.args.length; i++) {
  275. phy_list[i] = res.data.args[i].name_cn
  276. }
  277. that.setData({
  278. proData: phy_list
  279. })
  280. console.log("当前选中的物理列表index:", that.data.proindex)
  281. console.log("当前选中的物理value:", that.data.phyAddrInfoLIst.args[that.data.proindex].enum_value)
  282. //紧接着请求当前物理部件的did list
  283. wx.request({
  284. url: 'http://www.id27149.com:2222/codecInfo',//服务器接口地址
  285. method:'POST',//请求类型
  286. header: {'content-type': 'application/json'},
  287. data: {'method':'get_read_did_name_list','args':["BMW_790", that.data.phyAddrInfoLIst.args[that.data.proindex].enum_value]},
  288. success: function(res) {
  289. console.log('请求did列表成功,res:', res.data)
  290. //把请求到的原始数据放到phyAddrInfoLIst里
  291. that.setData({
  292. didNameInfoList: res.data
  293. })
  294. //把请求到的部件名称放到下拉框列表里面
  295. var did_list = []
  296. for (var i = 0; i < res.data.args.length; i++) {
  297. did_list[i] = res.data.args[i][1]
  298. }
  299. that.setData({
  300. claData: did_list
  301. })
  302. },
  303. fail: function() {
  304. console.log('请求did列表失败')
  305. }
  306. })
  307. },
  308. fail: function() {
  309. console.log('请求物理列表失败')
  310. }
  311. })
  312. // 设置socket接收到消息的回调
  313. websocket_api.onSocketMessageCallback = this.onSocketMessageCallback;
  314. that.setData({
  315. connectedDeviceId: options.connectedDeviceId,
  316. name:options.name,
  317. advertisData:options.advertisData
  318. })
  319. wx.getBLEDeviceServices({
  320. deviceId: that.data.connectedDeviceId,
  321. success: function (res) {
  322. console.log(res.services)
  323. that.setData({
  324. services: res.services
  325. })
  326. wx.getBLEDeviceCharacteristics({
  327. deviceId: options.connectedDeviceId,
  328. serviceId: res.services[0].uuid,
  329. success: function (res) {
  330. console.log(res.characteristics)
  331. that.setData({
  332. characteristics: res.characteristics
  333. })
  334. wx.notifyBLECharacteristicValueChange({
  335. state: true,
  336. deviceId: options.connectedDeviceId,
  337. serviceId: '0000FEE7-0000-1000-8000-00805F9B34FB',
  338. characteristicId: '000036F6-0000-1000-8000-00805F9B34FB',
  339. success: function (res) {
  340. console.log('启用notify成功')
  341. }
  342. })
  343. }
  344. })
  345. }
  346. })
  347. wx.onBLEConnectionStateChange(function (res) {
  348. console.log(res.connected)
  349. that.setData({
  350. connected: res.connected
  351. })
  352. })
  353. //注册接收到蓝牙消息之后的回调函数
  354. //蓝牙设备发送的消息
  355. wx.onBLECharacteristicValueChange(function (res) {
  356. var receiveText = data_trans.ab2hex(res.value)
  357. console.log('接收到以下蓝牙数据透传至服务器:' + receiveText)
  358. //发送至服务器,
  359. websocket_api.SocketTxMsg(
  360. JSON.stringify({"method": "data","args": receiveText}),
  361. this.WebsocketSendSuccessEvent,
  362. this.WebsocketSendFailEvent
  363. )
  364. // //显示到界面上
  365. // that.setData({receiveText: receiveText})
  366. })
  367. },
  368. onReady: function () {
  369. },
  370. onShow: function () {
  371. },
  372. onHide: function () {
  373. console.log('hide')
  374. }
  375. })