Skip to content

Commit

Permalink
#84 run button start websocket connection and recieve and display logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Benjamin Alveberg authored and Haakon Karstensen committed Nov 5, 2024
1 parent 7c43156 commit c63142a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 5 deletions.
2 changes: 2 additions & 0 deletions client/src/components/common/BaseButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const variantClass = computed(() => {
return 'bg-accent-400 text-bg-700 '
case 'light':
return 'bg-accent-500 text-bg-900 hover:bg-accent-600'
case 'red':
return 'bg-error text-white-100'
default:
return 'bg-accent-600 text-white-100'
}
Expand Down
13 changes: 10 additions & 3 deletions client/src/components/flow/FlowHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import ExitIcon from '@/icons/ExitIcon.vue'
import NodesList from '@/components/flow/NodesList.vue'
import ExportIcon from '@/icons/ExportIcon.vue'
defineEmits(['update:displayLog'])
defineEmits(['toggle-log', 'toggle-web-socket'])
defineProps({
displayLog: Boolean,
isRunning: Boolean,
nodes: Array
})
</script>
Expand All @@ -24,10 +25,16 @@ defineProps({
</h1>

<div class="ml-auto flex gap-2">
<base-button variant="default" class="h-9 rounded-lg leading-tight"> Run </base-button>
<base-button
@click="$emit('toggle-web-socket')"
:variant="isRunning ? 'red' : 'default'"
class="h-9 rounded-lg leading-tight"
>
{{ isRunning ? 'Stop' : 'Run' }}
</base-button>
<base-button
:aria-expanded="displayLog"
@click="$emit('update:displayLog')"
@click="$emit('toggle-log')"
class="h-9 rounded-lg leading-tight aria-expanded:bg-accent-500"
>
Logs
Expand Down
6 changes: 5 additions & 1 deletion client/src/components/flow/FlowLogList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ watch(
{{ logItems.find((item) => item.id === deviceId)?.name || 'All Devices' }}
</h2>
<ul v-if="deviceId !== 0">
<li v-for="log in state.logs.filter((item) => item.id === deviceId)" :key="log.id">
<li
v-for="log in state.logs.filter((item) => item.id === deviceId)"
:key="log.id"
class="m-2"
>
{{ log.log }}
</li>
</ul>
Expand Down
1 change: 1 addition & 0 deletions client/src/interfaces/IWebSocketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Log } from '@/types/WebSocketServiceTypes'

export interface IWebSocketService {
connect(port: number): void
disconnect(): void
subscribe(listener: (log: Log) => void): void
unsubscribe(listener: (log: Log) => void): void
notifyListeners(log: Log): void
Expand Down
10 changes: 10 additions & 0 deletions client/src/services/WebSocketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ class WebSocketService implements IWebSocketService {
public notifyListeners(log: Log) {
this.eventListeners.forEach((listener) => listener(log))
}

public disconnect() {
if (this.socket) {
console.log('this.socket:', this.socket)
this.socket.close()
console.log('WebSocket disconnected')
} else {
console.log('WebSocket already disconnected')
}
}
}

export const webSocketService = new WebSocketService()
29 changes: 28 additions & 1 deletion client/src/views/FlowDetailedView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import FlowEdge from '@/components/flow/FlowEdge.vue'
import { ref, type Ref } from 'vue'
import { inject, computed } from 'vue'
import { useRoute } from 'vue-router'
import { webSocketService } from '@/services/WebSocketService'
import type { Log } from '@/types/WebSocketServiceTypes'
const flows = inject<Ref<Flows>>('flows', ref([]))
Expand All @@ -41,14 +43,39 @@ function onEdgeChange({ edge, connection }: { edge: GraphEdge; connection: Conne
}
const displayLog = ref(false)
const isRunning = ref(false)
const handleNewLog = (log: Log) => {
console.log('new log', log)
}
// toggle log display
const toggleLog = () => {
displayLog.value = !displayLog.value
}
// toggle websocket connection
const toggleWebSocket = () => {
isRunning.value = !isRunning.value
if (isRunning.value) {
webSocketService.connect(8765)
webSocketService.subscribe(handleNewLog)
} else {
webSocketService.unsubscribe(handleNewLog)
webSocketService.disconnect()
}
}
</script>

<template>
<main class="flex flex-col">
<flow-header :display-log="displayLog" @update:display-log="toggleLog" />
<flow-header
:is-running="isRunning"
:display-log="displayLog"
@toggle-log="toggleLog"
@toggle-web-socket="toggleWebSocket"
/>
<flow-log :show="displayLog" />
<div v-if="!displayLog" class="mt-2 h-[calc(100vh-6rem)] w-[calc(100vw-18rem)]" @drop="onDrop">
<vue-flow
Expand Down

0 comments on commit c63142a

Please sign in to comment.