control.vue 14 KB

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