All checks were successful
ci/woodpecker/push/check_format Pipeline was successful
95 lines
2.3 KiB
JavaScript
95 lines
2.3 KiB
JavaScript
import { defineStore, storeToRefs } from 'pinia'
|
|
import ReconnectingWebSocket from 'reconnecting-websocket'
|
|
|
|
import axios from 'axios'
|
|
|
|
import { useConfigStore } from '@/stores/config'
|
|
|
|
export const useMowerStore = defineStore('mower', () => {
|
|
const log_lines = ref([])
|
|
|
|
const log = computed(() => {
|
|
return log_lines.value.join('\n')
|
|
})
|
|
|
|
const num_reg = /^[0-9].*/
|
|
const log_mobile = computed(() => {
|
|
const result = []
|
|
for (const i of log_lines.value) {
|
|
if (i.match(num_reg)) {
|
|
result.push(i.substring(15))
|
|
} else {
|
|
result.push(i)
|
|
}
|
|
}
|
|
return result.join('\n')
|
|
})
|
|
|
|
const ws = ref(null)
|
|
const running = ref(false)
|
|
const waiting = ref(false)
|
|
|
|
const get_task_id = ref(0)
|
|
const task_list = ref([])
|
|
|
|
const config_store = useConfigStore()
|
|
const { conf, timestamp, post_conf } = storeToRefs(config_store)
|
|
const { parse_config } = config_store
|
|
|
|
const sc_uri = ref('')
|
|
|
|
function listen_ws() {
|
|
let backend_url
|
|
if (import.meta.env.DEV) {
|
|
backend_url = import.meta.env.VITE_HTTP_URL
|
|
} else {
|
|
backend_url = location.origin
|
|
}
|
|
const ws_url = backend_url.replace(/^http/, 'ws') + '/ws'
|
|
ws.value = new ReconnectingWebSocket(ws_url)
|
|
ws.value.onmessage = ({ data }) => {
|
|
data = JSON.parse(data)
|
|
if (data.type == 'log') {
|
|
log_lines.value = log_lines.value.concat(data.data.split('\n')).slice(-100)
|
|
} else if (data.type == 'conf') {
|
|
if (timestamp.value < data.time) {
|
|
post_conf.value = false
|
|
timestamp.value = data.time
|
|
conf.value = parse_config(data.data)
|
|
}
|
|
} else if (data.type == 'sc') {
|
|
sc_uri.value = data.data
|
|
}
|
|
}
|
|
}
|
|
|
|
async function get_running() {
|
|
const response = await axios.get(`${import.meta.env.VITE_HTTP_URL}/running`)
|
|
running.value = response.data
|
|
}
|
|
|
|
async function get_tasks() {
|
|
if (running.value) {
|
|
const response = await axios.get(`${import.meta.env.VITE_HTTP_URL}/task`)
|
|
task_list.value = response.data
|
|
get_task_id.value = setTimeout(get_tasks, 3000)
|
|
} else {
|
|
task_list.value = []
|
|
}
|
|
}
|
|
|
|
return {
|
|
log,
|
|
log_mobile,
|
|
log_lines,
|
|
ws,
|
|
sc_uri,
|
|
running,
|
|
waiting,
|
|
listen_ws,
|
|
get_running,
|
|
task_list,
|
|
get_task_id,
|
|
get_tasks
|
|
}
|
|
})
|