control.vue 14 KB

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