songshankun
2023-11-14 621693acae8dad181eb8be6e60ffd73593e1177d
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
<template>
  <div class="device-status-info">
    <div v-if="type === 1" class="color-one">
      设备状态
      <!-- 1断开2生产3待机 -->
      <span v-if="plc?.plcStatus" class="device-m">
        <el-popover
          v-if="plc?.plcStatus === 1 && plc?.plcNotConnected"
          :width="180"
          placement="top-end"
          trigger="click"
        >
          <template #reference>
            <el-icon class="duan">
              <Link />
            </el-icon>
            断开
          </template>
          {{ plc?.plcNotConnected }}
        </el-popover>
        <span v-else>
          <el-icon v-if="plc?.plcStatus == 1 && !plc?.plcNotConnected" class="duan">
            <Link />
          </el-icon>
          <el-icon v-else class="lian">
            <Link />
          </el-icon>
          <!-- :class="{
              'status-running': device.plcStatus == 2,
              'status-off': device.plcStatus != 2,
            }" -->
          <span class="status-running">
            {{ plc?.plcStatus === 1 ? '断开' : plc?.plcStatus === 2 ? '生产中' : '待机' }}
          </span>
        </span>
      </span>
      <div class="device-b">工序运行时间:{{ runningTime }}</div>
    </div>
    <div v-if="type == 2" class="color-two">
      集群状态
      <span class="device-m">
        <span>
          <template v-if="device?.clusterStatus">
            <el-icon class="lian">
              <SuccessFilled />
            </el-icon>
            <span class="status-running"> 在线 </span>
          </template>
 
          <template v-else>
            <span>不在集群中</span>
          </template>
        </span>
      </span>
      <div
        class="device-info"
        :class="{ master: device?.clusterStatus === 'master', slave: device?.clusterStatus === 'slave' }"
      >
        {{ nodeTypeText }}
      </div>
      <div class="device-b title-bng">
        <img src="~@/assets/images/leaf.png" alt="" />
        <span>节点数</span> <i>{{ device?.clusterNodeQuantity ?? 0 }}</i>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { computed, toRefs } from 'vue'
import { Link, SuccessFilled } from '@element-plus/icons-vue'
import type { Devices } from '@/api/device'
import type { PLCResponse } from '@/api/plc'
import { useTasksStore } from '@/stores/tasks'
import { storeToRefs } from 'pinia'
import type { TasksGroupByChannel } from '@/api/task'
import { isNumber } from 'lodash-es'
 
export interface DeviceStatusInfoProps {
  plc?: PLCResponse
  device?: Devices
  /** 1: 设备状态 2:集群状态*/
  type?: 1 | 2
}
 
const props = withDefaults(defineProps<DeviceStatusInfoProps>(), {
  type: 1,
  device: undefined,
  plc: undefined
})
const { type, plc, device } = toRefs(props)
 
// 集群节点文本
const nodeTypeText = computed(() => {
  switch (device?.value?.clusterStatus) {
    case 'master':
      return '主节点'
    case 'slave':
      return '从节点'
    case '':
    default:
      return ''
  }
})
 
/**
 * 工序运行时间
 * 根据接口返回 realStartTime realEndTime 判断如何展示
 * 如果 realEndTime 为 0 则 用当前时间 - realStartTime
 * 如果 存在 realEndTime, 则 realEndTime - realStartTime
 * 注意 realStartTime realEndTime 是接口返回的10位时间戳
 * @param realStartTime
 * @param realEndTime
 * @return
 */
function getTaskRunningTime(realStartTime?: number, realEndTime?: number) {
  if (realStartTime && realEndTime) {
    return calcRunningDuration(realStartTime * 1000, realEndTime * 1000)
  } else if (!realEndTime && realStartTime) {
    const now = Math.floor(new Date().getTime() / 1000) * 1000
    return calcRunningDuration(realStartTime * 1000, now)
  } else {
    return '--'
  }
}
 
/**
 * 计算时间段, 注意参数要求时间戳为 JS Date 的13位时间戳
 * @param startTime
 * @param endTime
 * @return
 */
function calcRunningDuration(startTime: number, endTime: number) {
  let timeDuration = endTime - startTime
  if (timeDuration < 0) {
    return '0天0时0分'
  }
 
  let seconds = Math.floor(timeDuration / 1000)
  let minutes = Math.floor(seconds / 60)
 
  let days = Math.floor(timeDuration / 1000 / 60 / 60 / 24)
  let hours = Math.floor(minutes / 60) - days * 24
  let m = minutes - days * 24 * 60 - hours * 60
  return `${days}天${hours}时${m}分`
}
 
/**
 * 获取某任务所在通道的运行中的任务
 * @param channelMap
 * @param channelNumber
 */
function getChannelRunningTask(channelMap?: TasksGroupByChannel, channelNumber?: number) {
  if (!channelMap || !isNumber(channelNumber)) {
    return
  }
 
  const channel = channelMap[channelNumber]
  if (channel) {
    const taskList = channel?.Tasks ?? []
 
    return taskList.find((ele) => ele.Procedure.Status === 2)
  }
}
 
// 工序运行时间
const taskStore = useTasksStore()
const { activeTask, channels } = storeToRefs(taskStore)
// 工序运行时间: 使用当前选中任务所在通道的正处于运行中的任务来展示
const runningTime = computed(() => {
  const runningTask = getChannelRunningTask(channels?.value, activeTask?.value?.Channel)
  return getTaskRunningTime(runningTask?.Procedure?.realStartTime, runningTask?.Procedure?.realEndTime)
})
</script>
 
<style scoped lang="scss">
$status-default: #13235a;
$status-running: #00ff00f0;
$status-ready: #13235a;
$status-done: #2c5dbb82;
$status-border: #ffffff21;
$status-dark-font: #efefef;
$status-off: red;
.device-status-info {
  width: 50%;
  height: 150px;
  line-height: 40px;
  margin-top: 10px;
  color: #fff;
  overflow: hidden;
  float: left;
  text-align: center;
  font-size: 15px;
  .duan {
    color: $status-off;
    font-size: 26px;
    margin-right: 5px;
    margin-top: 2px;
    float: left;
  }
  .lian {
    color: $status-running;
    font-size: 26px;
    margin-right: 5px;
    float: left;
    margin-top: 2px;
  }
  .color-one,
  .color-two {
    height: 100%;
    width: calc(100% - 10px);
    margin-right: 20px;
    background: $status-done;
    border: 1px solid $status-border;
    box-sizing: border-box;
    border-radius: 4px;
    padding-top: 10px;
    .device-m {
      width: 100%;
      display: inline-block;
      font-size: 15px;
      line-height: 25px;
      span {
        display: inline-block;
        line-height: 30px;
      }
    }
    .device-b {
      font-size: 12px;
      margin-top: 10px;
      width: 100%;
      line-height: 25px;
      color: $status-dark-font;
    }
  }
  .color-two {
    margin-right: 0px;
    position: relative;
    .device-info {
      position: absolute;
      top: 5px;
      right: 5px;
      padding: 0px 6px;
      font-size: 12px;
      line-height: 20px;
      color: #fff;
      border-radius: 8px;
      &.master {
        background: $status-running;
      }
      &.slave {
        background-color: #3399ff;
      }
    }
    .title-bng {
      width: calc(90% - 1px);
      margin: 10px auto 0;
      height: 29px;
      line-height: 29px;
      font-size: 14px;
      background: url('../../../assets/images/nodeNumber.png') no-repeat center center / cover;
      img {
        height: 16px;
        display: inline-block;
        float: left;
        margin: 8px 5px 0 5px;
      }
      span {
        height: 32px;
        display: inline-block;
        text-align: center;
        display: inline-block;
        float: left;
        font-weight: bold;
      }
      i {
        float: right;
        margin-right: 15px;
        font-weight: bold;
      }
    }
  }
}
</style>