wuyouming666
2024-05-11 7dfc5501decb84bce7fe0563956eae705844a2f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
<template>
  <div class="app">
    <el-breadcrumb separator-class="el-icon-arrow-right" class="el-breadcrumb">
      <router-link
        to="/Electrical/alarm"
        tag="el-button"
        type="text"
        active-class="blue-button"
        >{{ $t("Alarm") }}</router-link
      >
     
      <router-link
        to="/Electrical/Sign"
        tag="el-button"
        type="text"
        active-class="blue-button"
        >{{ $t("I/O") }}</router-link
      >
      <router-link
        to="/Electrical/Servomanual"
        tag="el-button"
        type="text"
        active-class="blue-button"
        >{{ $t("Manual/Automatic") }}</router-link
      >
    </el-breadcrumb>
 
    <el-breadcrumb separator-class="el-icon-arrow-right" class="el-breadcrumb">
      <router-link
        to="/Electrical/Parameter2"
        tag="el-button"
        type="text"
        active-class="blue-button"
        >{{ $t("Glass Data") }}</router-link
      >
 
 
      <router-link
        to="/Electrical/ManualonePosition"
        tag="el-button"
        type="text"
        active-class="blue-button"
        >{{ $t("Manual Control") }}</router-link
      >
 
      <router-link v-show="false" v-if="hasPermission" to="/Electrical/Servomanualone" tag="el-button" type="text" active-class="blue-button">
    {{ $t('Servomanualone') }}
  </router-link>
      
     
      <router-link   v-if="hasPermission" to="/Electrical/Positioning1" tag="el-button" type="text" active-class="blue-button">{{
        $t('Cage Positions')
      }}</router-link>
  
      <router-link   v-if="hasPermission" to="/Electrical/AutomaticParameterSetting" tag="el-button" type="text" active-class="blue-button">{{
        $t('Speed parameter settings')
      }}</router-link>
 
<router-link  v-if="plcmes"
        to="/Electrical/InteractionState"
        tag="el-button"
        type="text"
        active-class="blue-button"
        >{{ $t("PLC-MES Info") }}</router-link
      >
    </el-breadcrumb>
 
    <el-row :gutter="40">
      <el-col
        :span="6"
        v-for="(dataGroup, groupIndex) in jsonData"
        :key="groupIndex"
        style="margin-bottom: -20px"
      >
        <el-card class="json-block" style="width: 300px; margin-bottom: 2px;height:200px"  :height="groupIndex === 12 ? '190px' : '170px'" >
          <div
            class="button-row"
            style="display: flex; justify-content: space-between"
          >
            <div v-for="(item, itemIndex) in dataGroup" :key="itemIndex">
              <span
                v-if="item.title"
                :name="item.title.name"
                class="action-button"
                >{{ item.title.name }}</span
              >
            </div>
          </div>
 
          <div
            class="button-row"
            style="display: flex; justify-content: space-between"
          >
            <div v-for="(item, itemIndex) in dataGroup" :key="itemIndex">
              <el-button
                v-if="item.button && language === 'en-US'"
                :name="item.button.name"
                v-model="item.button.value"
                class="action-button"
                @click="
                  updateButtonValue(dataGroup, itemIndex);
                  submitDataToBackend(item.button.name);
                "
                :class="{
                  'green-button':
                    item.button.value === 0 && item.button.name !== 'Clear ID',
                  'red-button':
                    item.button.value === 2 && item.button.name !== 'Clear ID',
                  'yellow-button':
                    item.button.value === 1 && item.button.name !== 'Clear ID',
                  'id':
                    item.button.value === 1 && item.button.name === 'Clear ID',
                    'A01jiting':item.button.value === 0 && item.button.name === 'A01Abort/ResumeTasks',
                    'A02jiting':item.button.value === 0 && item.button.name === 'A02Abort/ResumeTasks',
                    'emgHMI':item.button.value === 0 && item.button.name === 'emgHMI',
                    'GA01jiting':item.button.value ===1 && item.button.name === 'A01Abort/ResumeTasks',
                    'GA02jiting':item.button.value === 1 && item.button.name === 'A02Abort/ResumeTasks',
                    'GemgHMI':item.button.value === 1 && item.button.name === 'emgHMI'
 
                }"
                >{{ item.button.name }}</el-button
              >
 
              <el-button
                v-if="item.button && language === 'zh-CN'"
                :name="item.button.name"
                v-model="item.button.value"
                class="action-button"
                @click="
                  updateButtonValue(dataGroup, itemIndex);
                  submitDataToBackend(item.button.name);
                "
                :class="{
                  'green-button':
                    item.button.value === 0 && item.button.name !== '清除ID',
                  'red-button':
                    item.button.value === 2 && item.button.name !== '清除ID',
                  'yellow-button':
                    item.button.value === 1 && item.button.name !== '清除ID',
                  'id': item.button.value === 1 && item.button.name === '清除ID',
                  'A01jiting':item.button.value === 0 && item.button.name === 'A01终止/继续',
                  'A02jiting':item.button.value === 0 && item.button.name === 'A02终止/继续',
                  'emgHMI':item.button.value === 0 && item.button.name === '软急停',
                  'GA01jiting':item.button.value === 1 && item.button.name === 'A01终止/继续',
                  'GA02jiting':item.button.value === 1 && item.button.name === 'A02终止/继续',
                  'GemgHMI':item.button.value === 1 && item.button.name === '软急停'
                 
                }"
                >{{ item.button.name }}</el-button
              >
            </div>
          </div>
          <div
            v-for="(item, itemIndex) in dataGroup"
            :key="itemIndex"
            class="item-container"
          >
            <div class="item-row">
              <span
                class="name"
                v-if="groupIndex === 12"
                style="width: 350px"
                >{{ item.name }}</span
              >
              <span class="name" v-else style="width: 150px">{{
                item.name
              }}</span>
  <el-input
                v-if="item.type === '0' && groupIndex==15"
                v-model="item.value"
                @keyup.enter.native="timeout($event)"
                style="width: 300px"
                class="input-box2"
              ></el-input>
              <el-input
                v-else-if="item.type === '0' "
                v-model="item.value"
                readonly="readonly"
                style="width: 300px"
                class="input-box"
              ></el-input>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <!-- <el-button @click="submitDataToBackend">提交数据到后端</el-button> -->
  </div>
</template>
  
  <script>
import LanguageMixin from "../../lang/LanguageMixin";
import data from "../../configuration/parameter2";
import {
SelectPermissionByUserName, currentUsername
} from "../../api/home";
// import { throttle } from 'lodash';
let socket;
 
// window.onload=function(){
//   console.log("44");
// }
export default {
  destroyed() {
    socket.close();
  },
  name: "Parameter2",
  mixins: [LanguageMixin],
  data() {
    return {
      jsonData: data,
      inputValue: "",
      language: this.$i18n.locale,
      
    
      hasPermission: false, // 默认没有权限
      plcmes:false
      // 加载整个data.json文件作为jsonData
    };
  },
 
  activated() {
    document.body.style.zoom = "90%";
    this.initWebSocket();
    this.filteredData();
    const language = this.$i18n.locale;
    console.log(language);
    if (language === "en-US") {
      this.replaceChineseWithEnglish();
    }
    this.$forceUpdate();
    currentUsername().then(res => {
                SelectPermissionByUserName(res.data).then(res => {
                    res.data.permission.forEach(item => {
                        if (item.permissionId == "36" && item.state == 1) {
                            this.hasPermission = true;
                        }
                        if (item.permissionId == "50" && item.state == 1) {
                            this.plcmes = true;
                        }
                       
                    });
                });
 
            });
  },
 
  computed: {},
 
  methods: {
    filteredData() {
      return this.jsonData.map((item) => {
        const filteredValue = item.map((obj) => {
          if (
            typeof obj.button !== "undefined" &&
            typeof obj.button.value !== "undefined"
          ) {
            obj.button.value = obj.button.value
              .toString()
              .replace(/[^a-zA-Z0-9]/g, "");
          } else if (typeof obj.value !== "undefined") {
            obj.value = obj.value.toString().replace(/[^a-zA-Z0-9]/g, "");
          }
          return obj;
        });
        return filteredValue;
      });
    },
 
    updateButtonValue(dataGroup, itemIndex) {
      console.log(dataGroup)
      console.log(itemIndex)
   
      if (dataGroup[itemIndex].button.value2 === 1) {
        // 如果当前按钮的值是 0,将其设为 1
        this.$set(dataGroup[itemIndex].button, "value2", 0);
      } else {
        // 否则将其设为 0
        this.$set(dataGroup[itemIndex].button, "value2", 1);
      }
    },
    //范围 0-100
    timeout(event){
 
    
        const data3 = [];
        const resetButtonValues3 = [this.jsonData[15][0].value];
 
        if (resetButtonValues3.length > 0) {
 
let firstValue = resetButtonValues3[0];
if (!(firstValue >= "0" && firstValue <= "100")) {
  resetButtonValues3.shift();
  this.$message.error("Out of range");
 
  return false;
}
}
 
        // 将得到的值添加到 data 数组中
        data3.push(resetButtonValues3.flat());
        data3.unshift([],[]);
        // 在 data3 数组前面添加两个空数组
 
        // 创建 jsonObject3 对象,并添加 data3 属性
        const jsonObject3 = { data: data3 };
 
        // 提交数据到后端
        const jsonString3 = JSON.stringify(jsonObject3);
        console.log("Storage wait delay:", jsonString3);
        socket?.send(jsonString3);
        // setTimeout(()=>
        //   window.location.reload(),2000
        // )
        event.target.blur(); // 取消输入框焦点
    },
 
 
 
    submitDataToBackend(currentButtonName) {
      if (currentButtonName === "清除ID" || currentButtonName === "Clear ID") {
        const data2 = [];
        const resetButtonValues = this.jsonData
          .map((item) => {
            const buttonItem = item.find(
              (subItem) =>
                subItem.button &&
                ["清除ID", "Clear ID"].includes(subItem.button.name)
            );
 
            return buttonItem ? buttonItem.button.value2 : null;
          })
          .filter((value2) => value2 !== null);
 
        // 将得到的值添加到 data 数组中
        data2.push(resetButtonValues.flat());
        data2.push([],[]);
        // 在 data2 数组前面添加两个空数组
 
        // 创建 jsonObject2 对象,并添加 data2 属性
        const jsonObject2 = { data: data2 };
 
        // 提交数据到后端
        const jsonString2 = JSON.stringify(jsonObject2);
        console.log("所有Type为0的value:", jsonString2);
        socket?.send(jsonString2);
 
        // 延迟2秒发送指定内容
setTimeout(() => {
  const jsonObject = { data: [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [], []] };
  const jsonString = JSON.stringify(jsonObject);
  console.log("发送的内容:", jsonString);
  socket?.send(jsonString);
}, 1000);
 
      }
 
      if (
        currentButtonName === "A01Abort/ResumeTasks" ||
        currentButtonName === "A01终止/继续" ||
        currentButtonName === "A02Abort/ResumeTasks" ||
        currentButtonName === "A02终止/继续" ||
        currentButtonName === "软急停" || 
        currentButtonName === "emgHMI" 
      ) {
        const data3 = [];
        const resetButtonValues3 = [this.jsonData[13][0].button.value2,this.jsonData[14][0].button.value2,this.jsonData[16][0].button.value2];
 
        // 将得到的值添加到 data 数组中
        data3.push(resetButtonValues3.flat());
        data3.unshift([]);
        data3.push([]);
        // 在 data3 数组前面添加两个空数组
 
        // 创建 jsonObject3 对象,并添加 data3 属性
        const jsonObject3 = { data: data3 };
 
        // 提交数据到后端
        const jsonString3 = JSON.stringify(jsonObject3);
        console.log("急停后中止/继续选择:", jsonString3);
        socket?.send(jsonString3);
 
        setTimeout(() => {
  const jsonObject = { data: [ [],[0, 0, 0], []] };
  const jsonString = JSON.stringify(jsonObject);
  console.log("发送的回弹内容:", jsonString);
  socket?.send(jsonString);
}, 1000);
 
 
 
        // setTimeout(()=>
        //   window.location.reload(),2000
        // )
      }
 
      
    },
 
    
    initWebSocket() {
      let viewname = "Parameter2";
 
      if (typeof WebSocket === "undefined") {
        console.log("您的浏览器不支持WebSocket");
      } else {
        let socketUrl =
          "ws://" +
          this.$t("ip") +
          ":8888" +
          "/springboot-vue3/api/talk/" +
          viewname;
 
        if (socket != null) {
          socket.close();
          socket = null;
        }
 
        // 开启一个websocket服务
        socket = new WebSocket(socketUrl);
 
        // 打开事件
        socket.onopen = function () {
          console.log("websocket已打开");
        };
//         let isMouseInInputBox = false; // 标记鼠标是否在输入框内
        let isUpdated = false;  // 定义标志变量
// const inputBox = document.querySelectorAll('.input-box2');
// inputBox.forEach(box => {
//   box.addEventListener('mouseenter', () => {
//     isMouseInInputBox = true;
//   });
//   box.addEventListener('mouseleave', () => {
//     isMouseInInputBox = false;
//   });
// });
        // 收到消息
        socket.onmessage = (msg) => {
          if (!msg.data) {
            return; // 如果收到空数据,则直接返回,不执行后续逻辑
          }
          if (document.activeElement.tagName.toLowerCase() === 'input') {
    return;
  }
 
          let obj = JSON.parse(msg.data);
 
          const jsonData2 = this.jsonData;
 
          const parms = obj.params;
 
          //console.log(obj);
 
          // const handleMouseMove = throttle(function(event) {
          // const inputBox = document.querySelectorAll('.id');
          // const target = event.target;
 
          if (parms ) {
            //急停按钮
            jsonData2[13][0].button.value = obj.jiting[0][0];
            jsonData2[14][0].button.value = obj.jiting[0][1];
            jsonData2[16][0].button.value = obj.jiting[0][2];
            
 
           
              
 
            //清除id
 
            for (let i = 0; i < jsonData2.length; i++) {
              let arr = jsonData2[i];
              for (let j = 0; j < arr.length; j++) {
                let obj = arr[j];
                if (obj.type === "3") {
                  obj.button.value = parms[0][i];
                  // console.log(obj.button.value);
                }
              }
            }
 
            //ID
            const specialChars = /[^\w\s]/g; // 正则表达式匹配非字母、数字、下划线和空格的字符
 
            for (let i = 0; i < jsonData2.length; i++) {
              let arr = jsonData2[i];
              for (let j = 0; j < arr.length; j++) {
                let obj = arr[j];
 
                if (obj.type === "0") {
                  if (parms[1] && parms[1][i]) {
                    obj.value = parms[1][i].replace(specialChars, "");
                    // console.log(obj.value);
                  }
                  if (obj.name === "ID2") {
                    obj.value = parms[1][7].replace(specialChars, ""); // 将ID2对应的值赋为parms[1][7]
                  }
 
                  if (obj.name === "A02ID") {
                    obj.value = parms[1][8].replace(specialChars, "");
                  }
                  if (obj.name === "B01ID") {
                    obj.value = parms[1][9].replace(specialChars, "");
                  }
                  if (obj.name === "B02ID") {
                    obj.value = parms[1][10].replace(specialChars, "");
                  }
                }
              }
            }
 
            //手动状态
            for (let i = 0; i < jsonData2.length; i++) {
              let arr = jsonData2[i];
              for (let j = 0; j < arr.length; j++) {
                let obj = arr[j];
                if (obj.type === "4") {
                  obj.button.value = parms[2][i];
                }
              }
            }
            //改变状态文字
            for (let i = 0; i < jsonData2.length; i++) {
              let arr = jsonData2[i];
              for (let j = 0; j < arr.length; j++) {
                let obj = arr[j];
                if (obj.type === "4" && obj.button.value === 1) {
                  if (
                    obj.button.name.includes("状态") &&
                    this.language === "zh-CN"
                  ) {
                    obj.button.name = obj.button.name.replace("手动", "自动");
                  }
                  if (obj.button.name.includes("Status")) {
                    obj.button.name = obj.button.name.replace("Manual", "Auto");
                  }
                }
              }
            }
            this.$forceUpdate();
 
            //扫描枪扫码的ID
 
            jsonData2.forEach((item) => {
              item.forEach((subItem) => {
                if (subItem.read === 66) {
                  subItem.value = parms[3][0].replace(specialChars, "");
                }
              });
            });
 
            jsonData2.forEach((item) => {
              item.forEach((subItem) => {
                if (subItem.read === 666) {
                  subItem.value = parms[3][1].replace(specialChars, "");
                }
              });
            });
 
            //A01当前格子
            let lastParmsArray = parms[parms.length - 1];
 
            let lastParmsIndex = 0;
 
            for (let i = 0; i < jsonData2.length; i++) {
              for (let j = 0; j < jsonData2[i].length; j++) {
                if (jsonData2[i][j].read === 333) {
                  jsonData2[i][j].value = lastParmsArray[lastParmsIndex];
                  lastParmsIndex++;
                  if (lastParmsIndex === lastParmsArray.length) {
                    lastParmsIndex = 0;
                  }
                }
              }
            }
        
             
             jsonData2[15][0].value =  obj.timeout[0];
 
             if (!isUpdated) {
      for (let i = 0; i < jsonData2.length; i++) {
        for (let j = 0; j < jsonData2[i].length; j++) {
          if(jsonData2[i][j].button){
 
          
          jsonData2[i][j].button.value2 = jsonData2[i][j].button.value;
        }
        }
      }
      isUpdated = true;  // 更新标志变量的值
    }
                 
 
          }
        };
 
        //  console.log(jsonData2);
        // }, 50);
 
        // document.addEventListener('mouseover', handleMouseMove);
 
        // };
 
        // 关闭事件
        socket.onclose = function () {
          console.log("websocket已关闭");
        };
 
        // 发生错误事件
        socket.onerror = function () {
          console.log("websocket发生了错误");
        };
 
        this.$router.afterEach(function () {
          socket.close();
        });
      }
    },
    replaceChineseWithEnglish() {
      const nameMap = this.$t("langParameter2");
      this.jsonData = this.jsonData.map((item) => {
        return item.map((obj) => {
          if (obj.name) {
            const newName = nameMap[obj.name] || obj.name;
            return { ...obj, name: newName };
          } else if (obj.button && obj.button.name) {
            const newName = nameMap[obj.button.name] || obj.button.name;
            return { ...obj, button: { ...obj.button, name: newName } };
          } else {
            return obj;
          }
        });
      });
 
      // console.log(this.jsonData)
    },
    send() {
      socket?.send(JSON.stringify(this.messagepack));
    },
  },
};
</script>
  
  <style scoped>
html,
body {
  width: 100%;
  height: 85%;
}
.item-row {
  display: flex;
  align-items: center;
  margin-bottom: 5px; /* 调整每个项目之间的间距 */
}
 
.action-button {
  margin-right: 30px;
  background: #66b1ff;
  color: #fff;
}
.blue-button {
  background-color: skyblue;
}
 
.green-button {
  background-color: green;
}
 
.red-button {
  background-color: red;
}
.yellow-button {
  background-color: blue;
}
.id {
  background-color: green;
}
.A02jiting{
 
  background-color: #66b1ff;
}
.A01jiting{
 
background-color: #66b1ff;
}
.emgHMI{
 
  background-color: #66b1ff;
}
.GA02jiting{
 
  background-color: green;
}
.GA01jiting{
  background-color: green;
}
.GemgHMI{
  background-color: green;
}
</style>