Files
SoonWorkerD/src/renderer/store/modules/chat.js
T
2024-11-27 16:51:53 +08:00

461 lines
13 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// import { getAllList, getChatList, bindId, chatBegin } from '@/api/message'
let timeouter = null
let websock = null
// 状态JSON
// 打印机状态
const PrinterStatus = {
I: '空闲',
B: '忙碌',
P: '正在打印'
}
// 打印机报错
const printer_errors = {
'0x00010': '光盘仓为空',
'0x00011': '抓取光盘错误',
}
// 提交任务报错
const submitErros = {
'0x00001': '读取任务JSON失败',
'0x00003': '新建任务失败',
'0x00004': '新建任务文件夹失败',
'0x00005': '没有任务数量',
'0x00006': '打印参数错误',
'0x00007': '刻录参数错误',
'0x00008': '没有任务',
'0x00009': '模板文件解析失败',
'0x0000A': '打印类型错误',
'0x0000B': 'csv文件读取失败',
'0x0000C': '拷贝路径错误',
'0x0000D': '新建ISO文件失败',
'0x0000E': '更新任务数据失败',
}
// 错误状态
const Error1 = {
0: '托盘移动错误',
1: '墨盒类型错误',
2: '抓取光盘时未检测到光盘光盘已用完',
3: '光盘掉落',
4: '墨水车空警告',
5: '光盘抓手错误',
6: '抓取多张光盘错误',
7: '机械臂挂钩错误或者机械臂归位错误'
}
const Error2 = {
0: '墨水车伺服系统错误',
1: '墨水车失速错误',
2: '墨盒数据读取错误',
3: '打印图像超出范围',
4: '环境温度超出正常使用范围',
5: '墨盒短路',
6: '型号错误',
7: '缓冲区溢出错误'
}
const Error3 = {
0: '光盘已经打印'
}
// 光驱信息
const CDName = {
1: '下光驱',
2: '上光驱'
}
const CDStatus = {
0: '无法确定光盘状态',
1: '没有光盘',
2: '光驱托盘打开',
3: '光驱未准备好',
4: '光盘状态正常,光盘已插入'
}
// 光盘类型
// CD类型:CD1DVD2 DVD_DL:3BD4,BD_DL:5,BD_TL:6,BD_QL:7
const diskTypes = {
1: 'CD',
2: 'DVD',
3: 'BD'
}
const CDTypes = {
1: 'CD',
2: 'DVD',
3: 'DVD_DL',
4: 'BD',
5: 'BD_DL',
6: 'BD_TL',
7: 'BD_QL'
}
const cd_types = [
{
value: 1,
label: 'CD 700MB',
size: 700 * 1024 * 1024
},
{
value: 2,
label: 'DVD 4.7GB',
size: 4.7 * 1024 * 1024 * 1024
},
{
value: 3,
label: 'DVD_DL 8.5GB',
size: 8.5 * 1024 * 1024 * 1024
},
{
value: 4,
label: 'BD 25GB',
size: 25 * 1024 * 1024 * 1024
},
{
value: 5,
label: 'BD_DL 50GB',
size: 50 * 1024 * 1024 * 1024
},
{
value: 6,
label: 'BD_TL 100GB',
size: 100 * 1024 * 1024 * 1024
},
{
value: 7,
label: 'BD_QL 128GB',
size: 128 * 1024 * 1024 * 1024
}
]
const defaultData = {
// 任务列表
task_list: [
// {
// task_uuid: '20204215',
// task_name: '复制任务',
// task_size: 100000,
// finish_freq: 2,
// total_freq: 10,
// task_status: 2,
// ass_task: [
// {
// task_size: 100000,
// cd_type: 2,
// start_time: '2024-04-01 12:00:00',
// finish_time: '2024-04-01 12:00:00',
// task_status: 2
// }
// ]
// }
],
// 打印机信息
printer_info: {
blue_last: 0, //蓝色墨盒遗留
calibration_flag: 0, // 是否校验打印机
cover_flag: false, //是否盖盖
printer_name: '', //打印机名称
printer_status: 'I', //打印机状态 数字转成字符为I
printer_tray: 'I', //打印机托盘状态 数字转成字符为I
red_last: 0, //红色墨盒遗留
yellow_last: 0, //黄色墨盒遗留
system_ip: '', //主机IP
system_name: '', //主机名称
// 左右盘仓
strong_list: [
{
strong_pos: 1, // 1:右盘仓,2:左盘仓
cd_type: 1, //CD类型:CD1DVD2 DVD_DL:3BD4,BD_DL:5,BD_TL:6,BD_QL:7
cd_num: 0
},
{
strong_pos: 2,
cd_type: 2,
cd_num: 0
}
],
strongList: {
1: {
strong_pos: 1,
cd_type: 1,
cd_num: 0
},
2: {
strong_pos: 2,
cd_type: 2,
cd_num: 0
}
}
},
// 光驱信息
cd_list: [
{
cd_pos: 2, // 设备位置 1:下光驱,2:上光驱
cd_status: 1, // 设备状态 0:无法确定光盘状态 ,1:没有光盘 , 2:光驱托盘打开, 3:光驱未准备好,4:光盘状态正常,光盘已插入
cd_taskid: '', // 当前任务id
dev_path: '', // 光驱路径
dev_type: 1, // 光驱类型
cd_assid: '', // 作业的卷标名
total_size: 0, // 总容量
use_size: 0, // 已使用容量
copy_scheduler: 0 // 任务进度
},
{
cd_pos: 1,
cd_status: 1,
cd_taskid: '',
dev_path: '',
dev_type: 2
}
],
// 日志信息
logList: [
// {
// time: '2024-04-01 12:00:00',
// text: '任务开始',
// status: 'INFO'
// }
],
allLogList: []
}
const getDefaultState = () => {
return {
isConnect: false,
serviceStatus: false,
reconnections: 0,
PrinterStatus,
CDName,
CDStatus,
diskTypes,
CDTypes,
cd_types,
refreshTime: 10,
// 错误信息
printer_error: {},
...defaultData
}
}
const state = getDefaultState()
const mutations = {
setData: (state, obj) => {
state[obj.name] = obj.data
}
}
const actions = {
// 初始化websocket
initWebSocket({ state, dispatch, rootState }) {
if (typeof WebSocket === 'undefined') return console.log('您的浏览器不支持websocket')
if ((websock && state.isConnect) || !rootState.user.token) {
return
}
websock = new WebSocket(process.env.VUE_APP_SOCKET_API)
websock.onmessage = function (res) {
dispatch('websocketonmessage', res)
}
websock.onopen = function (res) {
dispatch('websocketonopen', res)
}
websock.onerror = function (res) {
dispatch('websocketonerror', res)
}
websock.onclose = function (res) {
dispatch('websocketonclose', res)
}
},
// 接收消息
websocketonmessage({ state, commit, dispatch }, e) {
const data = JSON.parse(e.data) // 转json对象
if (data.resp_name) {
// 作业列表
if (data.resp_name === 'task_list') {
commit('setData', { name: 'task_list', data: data.resp_info.task_list })
// 打印机信息
} else if (data.resp_name === 'printer_info') {
let info = data.resp_info
info.printer_status = String.fromCharCode(info.printer_status)
info.printer_tray = String.fromCharCode(info.printer_tray)
let strong_list = {}
for (let i = 0; i < info.strong_list.length; i++) {
strong_list[info.strong_list[i].strong_pos] = info.strong_list[i]
}
info.strongList = strong_list
commit('setData', { name: 'printer_info', data: info })
// 驱动信息
} else if (data.resp_name === 'cd_info') {
const list = data.resp_info.cd_list
let cds = [{}, {}]
let isBD = false
for (let i = 0; i < list.length; i++) {
if (list[i].dev_type == 3) isBD = true
if (list[i].cd_pos == 2) cds[0] = list[i]
else cds[1] = list[i]
}
let types = CDTypes
let typeLists = cd_types
if (!isBD) {
types = {}
for (let key in CDTypes) {
if (parseInt(key) < 4) types[key] = CDTypes[key]
}
typeLists = cd_types.slice(0, 3)
}
commit('setData', { name: 'CDTypes', data: types })
commit('setData', { name: 'cd_types', data: typeLists })
commit('setData', { name: 'cd_list', data: cds })
// 新增日志
} else if (data.resp_name === 'add_log') {
let logs = data.resp_info.add_log
let loglist = []
let logLists = [...state.logList]
for (let i = 0; i < logs.length; i++) {
let texts = logs[i].split('|')
if (logLists.length > 20) logLists.splice(logLists.length - 1, 1)
loglist.unshift({
time: texts[0].replace(/\//g, "-").replace(/时/g, ':').replace(/分/g, ':').replace(/秒/g, ''),
text: texts[2],
status: texts[1]
})
}
let list = [...loglist, ...logLists]
commit('setData', { name: 'logList', data: list })
// 全部日志
} else if (data.resp_name === 'all_log') {
let logs = data.resp_info.add_log
let loglist = []
for (let i = 0; i < logs.length; i++) {
let texts = logs[i].split('|')
loglist.unshift({
time: texts[0].replace(/\//g, "-").replace(/时/g, ':').replace(/分/g, ':').replace(/秒/g, ''),
text: texts[2],
status: texts[1]
})
}
commit('setData', { name: 'logList', data: loglist })
// 提交任务报错处理
} else if (data.resp_name === 'submit_task_res') {
let error = data.resp_info
commit('setData', { name: 'printer_error', data: {type: 'error', notify: '任务' + error.task_uuid + '提交失败:' + submitErros[error.task_res], message: '' } })
// 打印报错处理
} else if (data.resp_name === 'printer_error') {
let error = data.resp_info
let info = {
error1: '',
error2: '',
error3: ''
}
if (error.error1) {
let errors = error.error1.split('')
for (let i = 0; i < errors.length; i++) {
if (errors[i] == 1) info.error1 = i
}
}
if (error.error2) {
let errors = error.error2.split('')
for (let i = 0; i < errors.length; i++) {
if (errors[i] == 1) info.error2 = i
}
}
if (error.error3) {
let errors = error.error3.split('')
for (let i = 0; i < errors.length; i++) {
if (errors[i] == 1) info.error3 = i
}
}
if (error.printer_error) {
commit('setData', { name: 'printer_error', data: {type: 'error', notify: printer_errors[error.printer_error], message: '' } })
dispatch('websocketsend', {
req_name: 'next_task',
req_type: 1,
req_info: {}
}).then(() => {
}).catch(() => {
})
}
setTimeout(() => {
if (info.error1 || info.error1 === 0) {
commit('setData', { name: 'printer_error', data: {type: 'error', notify: Error1[info.error1], message: '' } })
}
if (info.error2 || info.error2 === 0) {
commit('setData', { name: 'printer_error', data: {type: 'error', notify: Error2[info.error2], message: '' } })
}
if (info.error3 || info.error3 === 0) {
commit('setData', { name: 'printer_error', data: {type: 'error', notify: Error3[info.error3], message: '' } })
}
}, 100)
}
} else {
// 未知消息
console.log('未知消息: ' + JSON.stringify(data))
}
},
// 心跳检测重连
pingCheck({ state, commit, dispatch, rootState }) {
if (state.pingNum < 4) {
state.pingNum++
timeouter = setTimeout(() => {
dispatch('pingCheck')
}, 10000)
} else {
dispatch('closeWebsocket')
}
},
// 发送消息
websocketsend({ commit, dispatch }, res) {
return new Promise((resolve, reject) => {
if (!websock || !state.isConnect || !state.serviceStatus) {
commit('setData', { name: 'printer_error', data: {type: 'error', message: '服务连接失败,正在尝试重新连接', notify: '' } })
dispatch('initWebSocket')
reject()
return
}
websock.send(JSON.stringify(res))
resolve()
})
},
// 链接建立之后执行的方法
websocketonopen: ({ commit, dispatch }, res) => {
console.log('链接建立之后执行send方法发送数据', res)
commit('setData', { name: 'isConnect', data: true })
commit('setData', { name: 'serviceStatus', data: true })
},
// 链接关闭之后执行的方法
websocketonclose: ({ commit, dispatch }, res) => {
console.log('断开链接', res)
// 关闭
commit('setData', { name: 'isConnect', data: false })
for (let key in defaultData) {
commit('setData', { name: key, data: defaultData[key] })
}
},
// 链接错误之后执行的方法
websocketonerror({ state, commit, dispatch }, res) {
commit('setData', { name: 'isConnect', data: false })
commit('setData', { name: 'serviceStatus', data: false })
state.reconnections += 1
const refreshTime = parseInt(state.refreshTime)*1000
setTimeout(() => {
dispatch('initWebSocket')
}, refreshTime)
},
// 关闭
closeWebsocket: () => {
// 关闭
if (websock) websock.close()
}
}
export default {
namespaced: true,
state,
mutations,
actions
}