control.vue 14 KB

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