zhamenzs.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. <template>
  2. <view class="facilitystate-page">
  3. <custom-card>
  4. <block slot="backText">
  5. <view class="facilitystate-top__title">
  6. <text :class="devStatus == '1' ? 'online' : 'offline'">{{
  7. devStatus == '1' ? '在线' : '离线'
  8. }}</text>
  9. {{ devName }}
  10. </view>
  11. </block>
  12. </custom-card>
  13. <view class="facilitystate-top">
  14. <view
  15. class="facilitystate-top__item"
  16. v-for="(item, index) in deviceList"
  17. :key="index"
  18. @click="nativeTo(item)"
  19. >
  20. <view class="item-icon-container" :class="item.className">
  21. <image class="item-icon" :src="item.icon" />
  22. </view>
  23. <view class="facilitystate-top__item-text">{{ item.title }}</view>
  24. </view>
  25. </view>
  26. <view class="irrMode">
  27. <view class="irrMode__content">
  28. <view class="irrMode__content-header">
  29. <view class="irrMode__content-header-list">
  30. <view class="irrMode__content-header-title">
  31. <view class="irrMode__content-header-container">
  32. <image class="header-item-icon" :src="rain" />
  33. <text style="color:#333;font-size: 24rpx;margin-left: 10rpx">当前灌溉区域:</text>{{ runTitle }}
  34. </view>
  35. <view class="timer-text">{{ timer }}分</view>
  36. </view>
  37. </view>
  38. <view class="irrMode__content-item">
  39. <view class="irrMode__content-item-raduis" :class="runStatus == '1' ? 'active-radius' : ''" @click="changeActive('1')">
  40. <image class="item-icon" :src="runStatus=='1'?mediaPlayFill:mediaPlay" />
  41. <view class="item-text">启动</view>
  42. </view>
  43. <view class="irrMode__content-item-raduis" :class="runStatus == '2' ? 'active-radius' : ''" @click="changeActive('2')">
  44. <image class="item-icon" :src="runStatus=='2'?mediaPauseFill:mediaPause" />
  45. <view class="item-text">暂停</view>
  46. </view>
  47. <view class="irrMode__content-item-raduis" :class="runStatus == '0' ? 'active-radius' : ''" @click="changeActive('0')" style="margin-right:0">
  48. <image class="item-icon" :src="runStatus=='0'?stopFill:stop" />
  49. <view class="item-text">停止</view>
  50. </view>
  51. </view>
  52. <view class="irr-run-list">
  53. <text v-for="(item, index) in selectAreaList" :key="index">
  54. {{ item.group_name }}
  55. <text v-if="index != selectAreaList.length - 1">、</text>
  56. </text>
  57. </view>
  58. </view>
  59. </view>
  60. <facilitystate
  61. :devBid="devBid"
  62. :irrigatedAreaList="irrigatedAreaList"
  63. :webSockedData="webSockedData"
  64. />
  65. </view>
  66. </view>
  67. </template>
  68. <script>
  69. import manualControl from './assets/manualControl.png';
  70. import mediaPause from './assets/media-pause.png';
  71. import mediaPauseFill from './assets/media-pause-fill.png';
  72. import mediaPlay from './assets/media-play.png';
  73. import mediaPlayFill from './assets/media-play-fill.png';
  74. import rain from './assets/rain.png';
  75. import stop from './assets/stop.png';
  76. import stopFill from './assets/stop-fill.png';
  77. import wheelIrrigation from './assets/wheelIrrigation.png';
  78. import time from './assets/timing.png';
  79. import operatingRecord from './assets/operatingRecord.png';
  80. import facilitystate from './components/facilitystate.vue';
  81. export default {
  82. data() {
  83. return {
  84. ws: null,
  85. currentMode: '1',
  86. title: '水肥一体机',
  87. manualControl,
  88. devBid: '',
  89. devName: '',
  90. devStatus: '1',
  91. irrigatedAreaList: [],
  92. mediaPause,
  93. mediaPauseFill,
  94. mediaPlay,
  95. mediaPlayFill,
  96. rain,
  97. stop,
  98. stopFill,
  99. deviceList: [
  100. {
  101. icon: manualControl,
  102. title: '手动控制',
  103. className: 'manualControl',
  104. url: '/pages/cb/zhamenFirst/control',
  105. },
  106. {
  107. icon: wheelIrrigation,
  108. title: '自动控制',
  109. className: 'wheelIrrigation',
  110. url: '/pages/cb/zhamenFirst/autoSetting?devBid=' + this.devBid,
  111. }, {
  112. icon: time,
  113. title: '定时设置',
  114. className: 'timedSetting',
  115. url: '/pages/cb/zhamenFirst/timingSetting?devBid=' + this.devBid,
  116. }, {
  117. icon: operatingRecord,
  118. title: '操作记录',
  119. className: 'operatingRecord',
  120. url: '/pages/cb/zhamenFirst/history?devBid=' + this.devBid,
  121. },
  122. ],
  123. sfToken: '',
  124. entityId: '',
  125. heartbeatTimer: null,
  126. info: {},
  127. dataArray:[],
  128. webSockedData: {},
  129. reconnectCount: 0,
  130. isActive: '0',
  131. runStatus: -1,
  132. workStatus: -1,
  133. clearIrr: {},
  134. dataList: [],
  135. selectAreaList: [],
  136. runTitle: '--',
  137. timer:'--'
  138. };
  139. },
  140. components: {
  141. facilitystate,
  142. },
  143. onLoad(options) {
  144. console.log(options,'optionsoptiopns')
  145. /*
  146. 0 水源 泵类 负责水源进入管道的总泵或者阀
  147. 1 肥料 泵类 负责肥料进入管道的总阀或者泵
  148. 2 吸肥 泵类 控制肥料桶出肥的阀或者泵,每个肥料桶一个
  149. 3 搅拌 泵类 肥料桶的搅拌电机或者泵 每个肥料桶一个
  150. 4 肥料桶 泵类 肥料桶,每个施肥机有多个或者没有,不一定真实存在,只是逻辑上的概念
  151. 5 电磁阀 无 管道最末端每个田地里控制出水的阀门
  152. 6 传感器 无 水肥机上的 温度,压力,流速,PH EC等监测类要素
  153. 7 灌区 无 逻辑区域,电磁阀的分组
  154. */
  155. const { devBid, devName, devStatus } = options;
  156. this.devBid = devBid;
  157. this.devName = devName;
  158. this.devStatus = devStatus;
  159. this.deviceList[0].url = `/pages/cb/zhamenFirst/control?devBid=${options.devBid}`;
  160. this.deviceList[1].url = `/pages/cb/zhamenFirst/autoSetting?devBid=${options.devBid}`;
  161. this.deviceList[2].url = `/pages/cb/zhamenFirst/timingSetting?devBid=${options.devBid}`;
  162. this.deviceList[
  163. this.deviceList.length - 1
  164. ].url = `/pages/cb/zhamenFirst/history?devBid=${options.devBid}`;
  165. },
  166. onShow(){
  167. if (this.devBid) {
  168. this.init();
  169. }
  170. },
  171. onUnload() {
  172. this.ws.close();
  173. },
  174. methods: {
  175. isHaveTime(childrenList) {
  176. let time = 0;
  177. let currentKey = '';
  178. let currentItem = {};
  179. for (let i = 0; i < this.selectAreaList.length; i++) {
  180. for (let key in this.selectAreaList[i]) {
  181. if (key.includes(':Status')) {
  182. currentKey = key;
  183. currentItem = this.selectAreaList[i];
  184. break;
  185. }
  186. }
  187. for (let i = 0; i < childrenList.length; i++) {
  188. const item = childrenList[i];
  189. if (item.sfCode === currentKey) {
  190. for (let key in currentItem) {
  191. if (key.includes(':PartTim')) {
  192. time = currentItem[key];
  193. }
  194. }
  195. break;
  196. }
  197. }
  198. }
  199. return time;
  200. },
  201. isAutoRun(childrenList) {
  202. for (let i = 0; i < childrenList.length; i++) {
  203. const item = childrenList[i];
  204. if (item.value == 1) {
  205. return true
  206. }
  207. }
  208. return false
  209. },
  210. async initsfyunshangAutoConfigInfo() {
  211. const selectAreaList = [];
  212. const res = await this.$myRequest({
  213. url:'/api/v2/iot/device/fkq/yunshang/auto/config/info/',
  214. method:'post',
  215. data: {
  216. devBid: String(this.devBid),
  217. },
  218. })
  219. const list = res;
  220. const group_list = list.group_list || [];
  221. group_list.forEach((item) => {
  222. if (item.group_value == 1) {
  223. selectAreaList.push(item);
  224. }
  225. });
  226. this.selectAreaList = selectAreaList;
  227. },
  228. async devctlContorl(data){
  229. const params = {
  230. devBid: Number(this.devBid),
  231. data,
  232. };
  233. const res = await this.$myRequest({
  234. url: '/api/v2/iot/mobile/device/fkq/devctl/',
  235. method: 'post',
  236. data:params,
  237. header: {
  238. 'Content-Type': 'application/json',
  239. },
  240. });
  241. uni.showToast({
  242. title: res.msg,
  243. icon: 'none',
  244. });
  245. },
  246. changeActive(active) {
  247. this.runStatus = active;
  248. const params = {
  249. IrrStatus:active
  250. }
  251. this.devctlContorl(params);
  252. },
  253. changeMode(mode) {
  254. if(this.runStatus == '1' || this.runStatus == '2'){
  255. uni.showToast({
  256. title: '请先停止运行',
  257. icon: 'none',
  258. });
  259. return
  260. }
  261. this.currentMode = mode;
  262. const params = {
  263. IrrMode:mode
  264. }
  265. this.devctlContorl(params);
  266. },
  267. initWebSocket() {
  268. const url = 'wss://things.ysiot.net:18080/api/ws';
  269. this.ws = uni.connectSocket({
  270. url: url,
  271. success: () => {
  272. console.log('WebSocket 连接初始化成功');
  273. }
  274. });
  275. uni.onSocketOpen(() => {
  276. console.log('WebSocket 已连接');
  277. // 发送测试参数
  278. const testParams = {
  279. cmds: [
  280. {
  281. type: 'TIMESERIES',
  282. entityType: 'DEVICE',
  283. entityId: this.entityId,
  284. scope: 'LATEST_TELEMETRY',
  285. cmdId: 1
  286. }
  287. ],
  288. authCmd: {
  289. cmdId: 0,
  290. token: this.sfToken,
  291. }
  292. };
  293. console.log('发送测试参数:', testParams);
  294. uni.sendSocketMessage({
  295. data: JSON.stringify(testParams)
  296. });
  297. // 心跳:每 30 秒 ping 一次
  298. this.heartbeatTimer = setInterval(() => {
  299. uni.sendSocketMessage({
  300. data: JSON.stringify({ type: 'ping' })
  301. });
  302. }, 30 * 1000);
  303. });
  304. uni.onSocketMessage((evt) => {
  305. try {
  306. const data = JSON.parse(evt.data);
  307. this.webSockedData = data;
  308. // 收到实时数据后刷新右侧组件
  309. if(this.dataArray.length){
  310. this.mergeTwoObject(this.dataArray,this.webSockedData?.data);
  311. this.initData(this.dataArray);
  312. }
  313. } catch (e) {
  314. console.error('WebSocket 消息解析失败', e);
  315. }
  316. });
  317. uni.onSocketError((e) => {
  318. console.error('WebSocket 错误', e);
  319. });
  320. uni.onSocketClose(() => {
  321. console.warn('WebSocket 已断开,3 秒后尝试重连');
  322. clearInterval(this.heartbeatTimer);
  323. this.reconnectCount = (this.reconnectCount || 0) + 1;
  324. if (this.reconnectCount <= 10) {
  325. setTimeout(() => this.initWebSocket(), 3000);
  326. } else {
  327. console.warn('WebSocket 重连次数已达上限,停止重连');
  328. }
  329. });
  330. },
  331. mergeTwoObject(firstObject, secondObject) {
  332. // 构建 sfCode 到对象的映射,实现 O(1) 查找
  333. const sfCodeMap = new Map();
  334. // 递归构建映射
  335. const buildMap = (items) => {
  336. for (const item of items) {
  337. if (item.sfCode) {
  338. sfCodeMap.set(item.sfCode, item);
  339. }
  340. if (item.childrenList && item.childrenList.length) {
  341. buildMap(item.childrenList);
  342. }
  343. }
  344. };
  345. buildMap(firstObject);
  346. // 遍历 secondObject 并更新值
  347. for (const key in secondObject) {
  348. let newKey = '';
  349. if(key.includes('GHChannelDev:ChannelParamSet')){
  350. newKey = key + ':Status';
  351. const item = sfCodeMap.get(newKey);
  352. if (item) {
  353. const params = JSON.parse(secondObject[key][0][1]);
  354. this.$set(item,'value',params.Status)
  355. }
  356. }else{
  357. const item = sfCodeMap.get(key);
  358. if (item) {
  359. this.$set(item,'value',secondObject[key][0][1])
  360. }
  361. }
  362. }
  363. },
  364. // 关闭 WebSocket
  365. closeWebSocket() {
  366. clearInterval(this.heartbeatTimer);
  367. if (this.ws) {
  368. uni.closeSocket();
  369. this.ws = null;
  370. }
  371. },
  372. async getsfrhinfo() {
  373. const res = await this.$myRequest({
  374. url: '/api/v2/iot/device/fkq/info/',
  375. method: 'post',
  376. data: {
  377. devBid: String(this.devBid),
  378. },
  379. });
  380. this.info = res;
  381. this.sfToken = this.info?.sfToken;
  382. this.entityId = this.info?.sfUuid;
  383. this.closeWebSocket();
  384. this.initWebSocket();
  385. },
  386. async init() {
  387. this.getdeviceSfStatus();
  388. this.getsfrhinfo();
  389. this.initsfyunshangAutoConfigInfo();
  390. },
  391. initData(res){
  392. this.irrigatedAreaList = [];
  393. this.dataList = [];
  394. res?.forEach((item) => {
  395. if (item.sfType === '6') {
  396. this.dataList.push(item);
  397. }else if (item.sfType === '7') {
  398. this.irrigatedAreaList.push(item);
  399. } else if (item.sfType === '9') {
  400. if (item.sfCode === 'IrrStatus') {
  401. this.runStatus = item.value;
  402. } else if (item.sfCode === 'IrrMode') {
  403. this.workStatus = item.value;
  404. } else if (item.sfCode === 'WaterFlowSum:Value') {
  405. this.clearIrr = item;
  406. }
  407. }
  408. });
  409. this.runTitle = '--';
  410. this.timer = '--';
  411. if(this.runStatus == '1'){
  412. this.irrigatedAreaList.forEach((item) => {
  413. if (this.isAutoRun(item.childrenList)) {
  414. this.runTitle = item.sfDisplayname || item.sfName;
  415. this.timer = this.isHaveTime(item.childrenList);
  416. }
  417. });
  418. }else{
  419. this.runTitle = '--';
  420. this.timer = '--';
  421. }
  422. },
  423. async getdeviceSfStatus() {
  424. const res = await this.$myRequest({
  425. url: '/api/v2/iot/mobile/device/fkq/status/',
  426. method: 'post',
  427. data: {
  428. devBid: this.devBid,
  429. },
  430. });
  431. this.dataArray = res || [];
  432. this.initData(this.dataArray);
  433. },
  434. nativeTo(item) {
  435. if(item.className == 'manualControl'){
  436. if(this.runStatus == '1' || this.runStatus == '2'){
  437. uni.showToast({
  438. title: '请先停止运行',
  439. icon: 'none',
  440. });
  441. return
  442. }
  443. }
  444. uni.navigateTo({
  445. url: item.url,
  446. });
  447. },
  448. },
  449. };
  450. </script>
  451. <style scoped lang="scss">
  452. uni-page-body {
  453. position: relative;
  454. height: 100%;
  455. }
  456. .irrMode{
  457. margin-top: 30rpx;
  458. border-radius: 32rpx 32rpx 0 0;
  459. margin-bottom: 30rpx;
  460. padding: 32rpx;
  461. border: 3rpx solid #FFF;
  462. background: linear-gradient(180deg, #D3F3F2 0%, #FFF 41.06%);
  463. .irrMode__header{
  464. display: flex;
  465. width:320rpx;
  466. padding:0 10rpx;
  467. align-items: center;
  468. background: #ffffff;
  469. border-radius: 12rpx;
  470. margin-bottom: 24rpx;
  471. font-family: "Source Han Sans CN VF";
  472. font-size: 28rpx;
  473. font-style: normal;
  474. font-weight: 400;
  475. line-height: normal;
  476. color:#999999;
  477. height: 68rpx;
  478. line-height: 68rpx;
  479. view{
  480. width: 160rpx;
  481. text-align: center;
  482. }
  483. }
  484. .active{
  485. display: flex;
  486. align-items: center;
  487. justify-content: center;
  488. border-radius: 8rpx;
  489. color: #ffffff;
  490. background: #0BBC58;
  491. height: 56rpx;
  492. }
  493. .irrMode__content{
  494. border-radius: 16rpx;
  495. padding: 2rpx;
  496. background: linear-gradient(180deg, #FFF 0%, #F5F6FA 100%);
  497. .irrMode__content-header{
  498. width: calc(100% - 32rpx);
  499. padding: 16rpx 16rpx 16rpx 16rpx;
  500. border-radius: 12rpx;
  501. background: linear-gradient(180deg, #BBF1EA 0%, #E6F4F9 100%);
  502. .irrMode__content-header-list{
  503. display:flex;
  504. justify-content: space-between;
  505. align-items: center;
  506. margin-bottom: 12rpx;
  507. }
  508. .irrMode__content-header-title{
  509. color: #020305;
  510. text-align: right;
  511. font-family: "Source Han Sans CN VF";
  512. font-size: 30rpx;
  513. font-weight: 500;
  514. display: flex;
  515. align-items: center;
  516. justify-content: space-between;
  517. width: 100%;
  518. .irrMode__content-header-container{
  519. display: flex;
  520. align-items: center;
  521. }
  522. .header-item-icon{
  523. width: 48rpx;
  524. height: 48rpx;
  525. }
  526. .timer-text{
  527. color: #0bbc58;
  528. text-align: right;
  529. font-family: "Source Han Sans CN VF";
  530. font-size: 28rpx;
  531. font-weight: 400;
  532. }
  533. }
  534. .irrMode__content-header-desc{
  535. color: #999999;
  536. text-align: right;
  537. font-family: "Source Han Sans CN VF";
  538. font-size: 28rpx;
  539. font-weight: 400;
  540. }
  541. }
  542. .irrMode__content-item{
  543. display:flex;
  544. align-items: center;
  545. justify-content: center;
  546. padding: 16rpx 0;
  547. border-radius: 12rpx;
  548. .irrMode__content-item-raduis{
  549. display: flex;
  550. height: 88rpx;
  551. padding: 0rpx 32rpx;
  552. flex-direction: column;
  553. justify-content: center;
  554. align-items: center;
  555. flex: 1 0 0;
  556. border-radius: 16rpx;
  557. margin-right: 16rpx;
  558. border: 2rpx solid #0BBC58;
  559. color: #0bbc58;
  560. text-align: right;
  561. font-family: "Source Han Sans CN VF";
  562. font-size: 20rpx;
  563. image{
  564. width: 48rpx;
  565. height: 48rpx;
  566. }
  567. }
  568. .active-radius{
  569. background: #0BBC58;
  570. color:#ffffff;
  571. }
  572. }
  573. .irr-run-list{
  574. padding: 16rpx;
  575. font-family: "Source Han Sans CN VF";
  576. color: #999999;
  577. font-family: "Source Han Sans CN VF";
  578. font-size: 24rpx;
  579. font-weight: 400;
  580. }
  581. }
  582. }
  583. .online {
  584. height: 32rpx;
  585. line-height: 32rpx;
  586. padding: 0 8rpx;
  587. border-radius: 4rpx;
  588. background: #14a478;
  589. color: #ffffff;
  590. font-family: 'Source Han Sans CN';
  591. font-size: 20rpx;
  592. font-weight: 500;
  593. margin-right: 8rpx;
  594. top: -4rpx;
  595. position: relative;
  596. }
  597. .manualControl{
  598. background: linear-gradient(326deg, #FFAB3D 6.86%, #FED55A 93.52%);
  599. }
  600. .wheelIrrigation{
  601. background: linear-gradient(152deg, #83D2FF 14.82%, #02A2FC 92.92%);
  602. }
  603. .timedSetting{
  604. background: linear-gradient(335deg, #0BBC58 6.91%, #60D799 89.95%);
  605. }
  606. .operatingRecord{
  607. background: linear-gradient(152deg, #B9B9F6 11.22%, #8080F8 94.92%);
  608. }
  609. .facilitystate-top__title{
  610. width: 80%;
  611. overflow: hidden;
  612. white-space: nowrap;
  613. text-overflow: ellipsis;
  614. }
  615. .offline {
  616. height: 32rpx;
  617. line-height: 32rpx;
  618. padding: 0 8rpx;
  619. border-radius: 4rpx;
  620. background: #ff4d4f;
  621. color: #ffffff;
  622. font-family: 'Source Han Sans CN';
  623. font-size: 20rpx;
  624. font-weight: 500;
  625. margin-right: 8rpx;
  626. top: -4rpx;
  627. position: relative;
  628. }
  629. .facilitystate-page {
  630. background: linear-gradient(180deg, #f5f6fa00 0%, #F5F6FA 23.46%, #FFF 25.24%, #FFF 100%), linear-gradient(102deg, #BFEADD 6.77%, #B8F1E7 40.15%, #B9EEF5 84.02%);
  631. height: 100%;
  632. width: 100%;
  633. overflow-x: hidden;
  634. overflow-y: scroll;
  635. .facilitystate-top {
  636. display: grid;
  637. grid-template-columns: repeat(5, 1fr);
  638. text-align: center;
  639. margin-top: 18rpx;
  640. &__item {
  641. margin-bottom: 32rpx;
  642. .item-icon-container{
  643. width: 96rpx;
  644. height: 96rpx;
  645. border-radius: 26rpx;
  646. overflow: hidden;
  647. margin: 0 auto;
  648. display:flex;
  649. align-items: center;
  650. justify-content: center;
  651. }
  652. .item-icon {
  653. width: 64rpx;
  654. height: 64rpx;
  655. }
  656. }
  657. &__item-text {
  658. color: #042118;
  659. font-family: 'Source Han Sans CN VF';
  660. font-size: 28rpx;
  661. font-weight: 400;
  662. margin-top: 16rpx;
  663. }
  664. }
  665. }
  666. </style>