control.vue 11 KB

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