control.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <template>
  2. <view class="control-container">
  3. <custom-card>
  4. <block slot="backText">{{ title }}</block>
  5. </custom-card>
  6. <view class="control-content" v-if="leftList.length > 0">
  7. <view class="control-list-left">
  8. <view
  9. class="control-list-left-item"
  10. v-for="(item, index) in leftList"
  11. :key="index"
  12. @click="controlClick(index)"
  13. :class="{ 'control-list-left-item-active': index === activeIndex }"
  14. >
  15. <image
  16. :src="borderRadius"
  17. class="borderTopRadius"
  18. v-if="index === activeIndex"
  19. />
  20. {{ item.sfDisplayname }}
  21. <image
  22. :src="borderRadius"
  23. class="borderBottomRadius"
  24. v-if="index === activeIndex"
  25. />
  26. </view>
  27. </view>
  28. <view class="control-list-right">
  29. <view
  30. v-for="(item, index) in deviceList"
  31. class="control-list-right-item"
  32. :class="item.sfType == '10' ? 'control-list-right-items' : ''"
  33. :key="index"
  34. >
  35. <view style="display: flex; align-items: center;" v-if="item.sfType != '10'">
  36. <view class="control-list-right-item-icon">
  37. <image
  38. :src="item.sfType == '3' ? stir : solenoidValve"
  39. class="solenoid-valve"
  40. />
  41. </view>
  42. <view class="control-list-right-item-title">{{
  43. item.sfDisplayname || item.sfName
  44. }}</view>
  45. <view class="control-list-right-item-action">
  46. <u-switch
  47. :value="item.value == 1 ? true : false"
  48. @change="changeSwitch(item)"
  49. activeColor="#14A478"
  50. size="40"
  51. inactiveColor="rgb(230, 230, 230)"
  52. ></u-switch>
  53. </view>
  54. </view>
  55. <view v-else style="width:100%">
  56. <view class="control-list-right-item-icon">
  57. <image
  58. :src="getSrc(item)"
  59. class="solenoid-valve"
  60. />
  61. <text style="margin-left:6rpx">
  62. {{
  63. item.sfDisplayname || item.sfName
  64. }}
  65. </text>
  66. </view>
  67. <view class="control-list-right-item-actions">
  68. <text style="color:#999999">状态 </text>
  69. <view class="control-list-right-item-actions-status">
  70. <view
  71. class="status-item"
  72. :class="item.value == '1'?'status-item-open':''"
  73. @click="changeStatus(item, '1')"
  74. >开</view>
  75. <view
  76. class="status-item"
  77. :class="item.value == '0'?'status-item-pause':''"
  78. @click="changeStatus(item, '0')"
  79. >停</view>
  80. <view
  81. class="status-item status-item-close-label"
  82. :class="item.value == '2'?'status-item-close':''"
  83. @click="changeStatus(item, '2')"
  84. >关</view>
  85. </view>
  86. </view>
  87. </view>
  88. </view>
  89. <u-empty v-if="deviceList.length === 0" text="暂无数据"></u-empty>
  90. </view>
  91. </view>
  92. <u-empty v-else text="暂无数据"></u-empty>
  93. </view>
  94. </template>
  95. <script>
  96. import solenoidValve from './assets/solenoidValve.png';
  97. import stir from './assets/stir.png';
  98. import solenoidValveGray from './assets/solenoidValveGray.png';
  99. import solenoidValveStop from './assets/solenoidValveStop.png';
  100. import borderRadius from './assets/borderRadius.png';
  101. export default {
  102. name: 'control',
  103. data() {
  104. return {
  105. activeIndex: 0,
  106. value: false,
  107. solenoidValve,
  108. solenoidValveGray,
  109. solenoidValveStop,
  110. stir,
  111. borderRadius,
  112. title: '控制面板',
  113. leftList: [],
  114. deviceList: [],
  115. devBid: '',
  116. ws: null,
  117. heartbeatTimer: null,
  118. webSockedData: null,
  119. dataArray: [],
  120. info: null,
  121. sfToken: '',
  122. entityId: '',
  123. reconnectCount: 0,
  124. };
  125. },
  126. methods: {
  127. getSrc(item){
  128. if(item.value == '1'){
  129. return this.solenoidValve;
  130. }else if(item.value == '0'){
  131. return this.solenoidValveStop;
  132. }else if(item.value == '2'){
  133. return this.solenoidValveGray;
  134. }
  135. },
  136. initWebSocket() {
  137. const url = 'wss://things.ysiot.net:18080/api/ws';
  138. this.ws = uni.connectSocket({
  139. url: url,
  140. success: () => {
  141. console.log('WebSocket 连接初始化成功');
  142. }
  143. });
  144. uni.onSocketOpen(() => {
  145. console.log('WebSocket 已连接');
  146. // 发送测试参数
  147. const testParams = {
  148. cmds: [
  149. {
  150. type: 'TIMESERIES',
  151. entityType: 'DEVICE',
  152. entityId: this.entityId,
  153. scope: 'LATEST_TELEMETRY',
  154. cmdId: 1
  155. }
  156. ],
  157. authCmd: {
  158. cmdId: 0,
  159. token: this.sfToken
  160. }
  161. };
  162. console.log('发送测试参数:', testParams);
  163. uni.sendSocketMessage({
  164. data: JSON.stringify(testParams)
  165. });
  166. // 心跳:每 30 秒 ping 一次
  167. this.heartbeatTimer = setInterval(() => {
  168. uni.sendSocketMessage({
  169. data: JSON.stringify({ type: 'ping' })
  170. });
  171. }, 30 * 1000);
  172. });
  173. uni.onSocketMessage((evt) => {
  174. try {
  175. const data = JSON.parse(evt.data);
  176. this.webSockedData = data;
  177. // 收到实时数据后刷新右侧组件
  178. if(this.dataArray.length){
  179. this.mergeTwoObject(this.dataArray,this.webSockedData?.data);
  180. this.initData(this.dataArray);
  181. }
  182. } catch (e) {
  183. console.error('WebSocket 消息解析失败', e);
  184. }
  185. });
  186. uni.onSocketError((e) => {
  187. console.error('WebSocket 错误', e);
  188. });
  189. uni.onSocketClose(() => {
  190. console.warn('WebSocket 已断开,3 秒后尝试重连');
  191. clearInterval(this.heartbeatTimer);
  192. this.reconnectCount = (this.reconnectCount || 0) + 1;
  193. if (this.reconnectCount <= 10) {
  194. setTimeout(() => this.initWebSocket(), 3000);
  195. } else {
  196. console.warn('WebSocket 重连次数已达上限,停止重连');
  197. }
  198. });
  199. },
  200. getChecked(item) {
  201. return item.value == 1 ? true : false;
  202. },
  203. mergeTwoObject(firstObject, secondObject) {
  204. // 构建 sfCode 到对象的映射,实现 O(1) 查找
  205. const sfCodeMap = new Map();
  206. // 递归构建映射
  207. const buildMap = (items) => {
  208. for (const item of items) {
  209. if (item.sfCode) {
  210. sfCodeMap.set(item.sfCode, item);
  211. }
  212. if (item.childrenList && item.childrenList.length) {
  213. buildMap(item.childrenList);
  214. }
  215. }
  216. };
  217. buildMap(firstObject);
  218. // 遍历 secondObject 并更新值
  219. for (const key in secondObject) {
  220. let newKey = '';
  221. if(key.includes('GHChannelDev:ChannelParamSet')){
  222. newKey = key + ':Status';
  223. const item = sfCodeMap.get(newKey);
  224. if (item) {
  225. const params = JSON.parse(secondObject[key][0][1]);
  226. this.$set(item,'value',params.Status)
  227. }
  228. }else{
  229. const item = sfCodeMap.get(key);
  230. if (item) {
  231. this.$set(item,'value',secondObject[key][0][1])
  232. }
  233. }
  234. }
  235. },
  236. async sfDecvtl(data){
  237. const res = await this.$myRequest({
  238. url: '/api/v2/iot/mobile/device/sf/devctl/',
  239. method: 'post',
  240. data,
  241. header: {
  242. 'Content-Type': 'application/json',
  243. },
  244. });
  245. if (res.code !== '000000') {
  246. item.value = item.value == 1 ? 0 : 1;
  247. }
  248. uni.showToast({
  249. title: res.msg,
  250. icon: 'none',
  251. });
  252. },
  253. changeStatus(item,index){
  254. const sfCode = item.sfCode;
  255. const params = {};
  256. params[sfCode] = index;
  257. const data = {
  258. devBid: this.devBid,
  259. data: params,
  260. };
  261. this.sfDecvtl(data)
  262. },
  263. changeSwitch(item) {
  264. item.value = item.value == 1 ? 0 : 1;
  265. const sfCode = item.sfCode;
  266. const params = {};
  267. params[sfCode] = item.value;
  268. const data = {
  269. devBid: this.devBid,
  270. data: params,
  271. };
  272. this.sfDecvtl(data)
  273. },
  274. controlClick(index) {
  275. this.activeIndex = index;
  276. this.deviceList = this.leftList[this.activeIndex]?.childrenList || [];
  277. },
  278. /*
  279. 0 水源 泵类 负责水源进入管道的总泵或者阀
  280. 1 肥料 泵类 负责肥料进入管道的总阀或者泵
  281. 2 吸肥 泵类 控制肥料桶出肥的阀或者泵,每个肥料桶一个
  282. 3 搅拌 泵类 肥料桶的搅拌电机或者泵 每个肥料桶一个
  283. 4 肥料桶 泵类 肥料桶,每个施肥机有多个或者没有,不一定真实存在,只是逻辑上的概念
  284. 5 电磁阀 无 管道最末端每个田地里控制出水的阀门
  285. 6 传感器 无 水肥机上的 温度,压力,流速,PH EC等监测类要素
  286. 7 灌区 无 逻辑区域,电磁阀的分组
  287. */
  288. async getsfrhinfo() {
  289. const res = await this.$myRequest({
  290. url: '/api/v2/iot/device/sf/info/',
  291. method: 'post',
  292. data: {
  293. devBid: String(this.devBid),
  294. },
  295. });
  296. this.info = res;
  297. this.sfToken = this.info?.sfToken;
  298. this.entityId = this.info?.sfUuid;
  299. this.closeWebSocket();
  300. this.initWebSocket();
  301. },
  302. // 关闭 WebSocket
  303. closeWebSocket() {
  304. clearInterval(this.heartbeatTimer);
  305. if (this.ws) {
  306. uni.closeSocket();
  307. this.ws = null;
  308. }
  309. },
  310. initData(res){
  311. this.deviceList = [];
  312. const sfCurrent = [
  313. {
  314. sfDisplayname: '水肥机',
  315. childrenList: [],
  316. },
  317. ];
  318. const irrigatedAreaList = [];
  319. res?.forEach((item) => {
  320. if (item.sfType === '0' || item.sfType === '1' || item.sfType === '2') {
  321. sfCurrent[0].childrenList.push(item);
  322. } else if (item.sfType === '4') {
  323. const childrenList = item?.childrenList || [];
  324. childrenList.forEach((child) => {
  325. if (
  326. child.sfType === '3' ||
  327. child.sfType === '2' ||
  328. child.sfType === '8'
  329. ) {
  330. sfCurrent[0].childrenList.push(child);
  331. }
  332. });
  333. } else if (item.sfType === '7') {
  334. irrigatedAreaList.push(item);
  335. }
  336. });
  337. this.leftList = [...sfCurrent, ...irrigatedAreaList];
  338. this.deviceList = this.leftList[this.activeIndex]?.childrenList || [];
  339. },
  340. async getdeviceSfStatus() {
  341. const res = await this.$myRequest({
  342. url: '/api/v2/iot/mobile/device/sf/status/',
  343. method: 'post',
  344. data: {
  345. devBid: this.devBid,
  346. },
  347. });
  348. this.dataArray = res;
  349. this.initData(res);
  350. },
  351. },
  352. onLoad(options) {
  353. const { devBid } = options;
  354. this.devBid = devBid;
  355. this.getdeviceSfStatus();
  356. this.getsfrhinfo();
  357. },
  358. };
  359. </script>
  360. <style scoped lang="scss">
  361. uni-page-body {
  362. position: relative;
  363. height: 100%;
  364. }
  365. .control-container {
  366. background: linear-gradient(180deg, #ffffff00 0%, #fff 23.64%, #fff 100%),
  367. linear-gradient(102deg, #bfeadd 6.77%, #b8f1e7 40.15%, #b9eef5 84.02%);
  368. height: 100%;
  369. width: 100%;
  370. overflow-x: hidden;
  371. overflow-y: hidden;
  372. .control-content {
  373. display: flex;
  374. width: 100%;
  375. height: calc(100% - 102rpx);
  376. overflow-x: hidden;
  377. overflow-y: auto;
  378. padding: 8px 0;
  379. border-radius: 8px 8px 0 0;
  380. background: #fff;
  381. .control-list-left {
  382. width: 250rpx;
  383. position: fixed;
  384. height: calc(100% - 90rpx);
  385. overflow-x: hidden;
  386. overflow-y: auto;
  387. .control-list-left-item {
  388. width: 240rpx;
  389. height: 96rpx;
  390. line-height: 96rpx;
  391. background: #f5f7fa;
  392. padding-left: 24rpx;
  393. color: #364d46;
  394. font-family: 'Source Han Sans CN VF';
  395. font-size: 32rpx;
  396. font-weight: 400;
  397. position: relative;
  398. }
  399. .control-list-left-item-active {
  400. background: #ffffff;
  401. border-radius: 0 -16rpx -16rpx 0;
  402. }
  403. .borderTopRadius {
  404. position: absolute;
  405. width: 40rpx;
  406. height: 40rpx;
  407. right: 10rpx;
  408. top: -32rpx;
  409. transform: rotate(90deg);
  410. z-index: 100;
  411. }
  412. .borderBottomRadius {
  413. position: absolute;
  414. z-index: 100;
  415. width: 40rpx;
  416. height: 40rpx;
  417. bottom: -32rpx;
  418. right: 10rpx;
  419. }
  420. }
  421. .control-list-right {
  422. width: calc(100% - 250rpx);
  423. margin: 0 32rpx;
  424. margin-left: 280rpx;
  425. .control-list-right-item {
  426. height: 96rpx;
  427. display: flex;
  428. padding: 0rpx 16rpx;
  429. align-items: center;
  430. border-radius: 8rpx;
  431. background: #f5f7fa;
  432. margin-bottom: 22rpx;
  433. position: relative;
  434. .control-list-right-item-icon {
  435. display: flex;
  436. align-items: center;
  437. .solenoid-valve {
  438. width: 40rpx;
  439. height: 40rpx;
  440. }
  441. }
  442. .control-list-right-item-title {
  443. margin-left: 8rpx;
  444. }
  445. .control-list-right-item-action {
  446. position: absolute;
  447. right: 16rpx;
  448. }
  449. .control-list-right-item-actions {
  450. display: flex;
  451. align-items: center;
  452. margin-top: 22rpx;
  453. justify-content: space-between;
  454. .control-list-right-item-actions-status {
  455. display: flex;
  456. align-items: center;
  457. border-radius: 12rpx;
  458. background: #FFF;
  459. padding: 6rpx;
  460. margin-left: 12rpx;
  461. height: 50rpx;
  462. .status-item{
  463. width: 92rpx;
  464. height: 50rpx;
  465. line-height: 50rpx;
  466. text-align: center;
  467. border-radius: 8rpx;
  468. background: #FFF;
  469. color:#656565;
  470. }
  471. .status-item-close-label{
  472. color:#FF5951;
  473. }
  474. .status-item-open{
  475. background: #0BBC58;
  476. color: #fff;
  477. }
  478. .status-item-pause{
  479. background: #F8B610;
  480. color: #fff;
  481. }
  482. .status-item-close{
  483. background: #DDDFE6;
  484. color: #303133;
  485. }
  486. }
  487. }
  488. }
  489. .control-list-right-items{
  490. height: 172rpx;
  491. }
  492. }
  493. }
  494. }
  495. </style>