Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(service): refactor connection & history service #661

Merged
merged 2 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 6 additions & 104 deletions src/api/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,10 @@ export const loadConnections = (): ConnectionModel[] | [] => {
return db.get<ConnectionModel[] | []>('connections')
}

export const loadSuggestConnections = (): ConnectionModel[] | [] => {
return db.get<ConnectionModel[] | []>('suggestConnections')
}

export const createSuggestConnection = (data: ConnectionModel): ConnectionModel => {
const suggestConnections = loadSuggestConnections()
if (suggestConnections.length > 9) {
const deleteId = suggestConnections[0].id
if (deleteId !== undefined) {
deleteSuggestConnection(deleteId.toString())
}
}
return db.insert<ConnectionModel>('suggestConnections', data)
}

export const deleteSuggestConnection = (id: string): ConnectionModel => {
return db.remove<ConnectionModel>('suggestConnections', id)
}

export const loadAllConnectionsIds = async (type: 'connections' | 'suggestConnections'): Promise<string[]> => {
const connectionsIds: string[] = []
let allConnections: ConnectionModel[]
allConnections = type === 'connections' ? await loadConnections() : await loadSuggestConnections()
allConnections = await loadConnections()
allConnections.forEach((connection: ConnectionModel) => {
if (connection.id) {
connectionsIds.push(connection.id.toString())
Expand All @@ -39,85 +20,10 @@ export const loadAllConnectionsIds = async (type: 'connections' | 'suggestConnec
return connectionsIds
}

// Message history
export const loadHistoryMessageHeaders = async (): Promise<HistoryMessageHeaderModel[] | []> => {
return db.get<HistoryMessageHeaderModel[] | []>('historyMessageHeader')
}

export const createHistoryMessageHeader = async (
data: HistoryMessageHeaderModel,
): Promise<HistoryMessageHeaderModel> => {
const headers = await loadHistoryMessageHeaders()
if (headers.length > 9) {
const deleteId = headers[0].id
if (deleteId !== undefined) {
deleteHistoryMessageHeader(deleteId)
}
}
return db.insert<HistoryMessageHeaderModel>('historyMessageHeader', data)
}

export const deleteHistoryMessageHeader = (id: string): HistoryMessageHeaderModel => {
return db.remove<HistoryMessageHeaderModel>('historyMessageHeader', id)
}

export const cleanUpHistoryMessageHeader = (): HistoryMessageHeaderModel[] | [] => {
return db.cleanUpArray<HistoryMessageHeaderModel[] | []>('historyMessageHeader')
}

export const loadHistoryMessagePayloads = async (): Promise<HistoryMessagePayloadModel[] | []> => {
return db.get<HistoryMessagePayloadModel[] | []>('historyMessagePayload')
}

export const updateHistoryMessagePayload = (
id: string,
headerData: HistoryMessagePayloadModel,
): HistoryMessagePayloadModel => {
return db.update<HistoryMessagePayloadModel>('historyMessagePayload', id, headerData)
}

export const createHistoryMessagePayload = async (
data: HistoryMessagePayloadModel,
): Promise<HistoryMessagePayloadModel> => {
const payloads = await loadHistoryMessagePayloads()
if (payloads.length > 9) {
const deleteId = payloads[0].id
if (deleteId !== undefined) {
deleteHistoryMessagePayload(deleteId)
}
}
return db.insert<HistoryMessagePayloadModel>('historyMessagePayload', data)
}

export const deleteHistoryMessagePayload = (id: string): HistoryMessagePayloadModel => {
return db.remove<HistoryMessagePayloadModel>('historyMessagePayload', id)
}

export const cleanUpHistoryMessagePayload = (): HistoryMessagePayloadModel[] | [] => {
return db.cleanUpArray<HistoryMessagePayloadModel[] | []>('historyMessagePayload')
}

export const createConnection = (data: ConnectionModel): ConnectionModel => {
loadAllConnectionsIds('suggestConnections').then((res) => {
if (data.id && res.indexOf(data.id.toString()) === -1) {
createSuggestConnection(data)
}
})
return db.insert<ConnectionModel>('connections', data)
}

export const deleteConnection = (id: string): ConnectionModel => {
return db.remove<ConnectionModel>('connections', id)
}

export const deleteMessage = (id: string, mid: string): ConnectionModel => {
return db.findChild<any>('connections', id).get('messages').remove({ mid }).write()
}

export const updateConnection = (id: string, data: ConnectionModel): ConnectionModel => {
return db.update<ConnectionModel>('connections', id, data)
}

export const updateConnectionMessage = (id: string, message: MessageModel): ConnectionModel => {
const connection: ConnectionModel = loadConnection(id)
if (connection) {
Expand All @@ -133,11 +39,11 @@ export const importConnections = (data: ConnectionModel[]): Promise<string> => {
data.forEach((item: ConnectionModel) => {
const { id } = item
if (id) {
if (res.indexOf(id.toString()) === -1) {
createConnection(item)
} else {
updateConnection(id.toString() as string, item)
}
// if (res.indexOf(id.toString()) === -1) {
// createConnection(item)
// } else {
// updateConnection(id.toString() as string, item)
// }
oceanlvr marked this conversation as resolved.
Show resolved Hide resolved
}
})
return 'ok'
Expand All @@ -151,8 +57,4 @@ export const importConnections = (data: ConnectionModel[]): Promise<string> => {
return importDataResult
}

export const cleanUpSuggestConnections = (): ConnectionModel[] | [] => {
return db.cleanUpArray<ConnectionModel[] | []>('suggestConnections')
}

export default {}
8 changes: 3 additions & 5 deletions src/components/ClearUpHistoryData.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import { Component, Vue, Prop, Watch } from 'vue-property-decorator'
import { Getter } from 'vuex-class'
import MyDialog from './MyDialog.vue'
import { cleanUpHistoryMessageHeader, cleanUpHistoryMessagePayload, cleanUpSuggestConnections } from '@/api/connection'
import useServices from '@/database/useServices'

@Component({
Expand All @@ -36,11 +35,10 @@ export default class ClearUpHistoryData extends Vue {
}

private async cleanHistoryData() {
const { connectionService } = useServices()
const { connectionService, historyMessageHeaderService, historyMessagePayloadService } = useServices()
await connectionService.cleanLeatest()
await cleanUpHistoryMessageHeader()
await cleanUpHistoryMessagePayload()
await cleanUpSuggestConnections()
await historyMessageHeaderService.clean()
await historyMessagePayloadService.clean()
this.$message.success(this.$t('connections.cleanHistorySuccess') as string)
this.resetData()
}
Expand Down
4 changes: 3 additions & 1 deletion src/components/ImportData.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import MyDialog from './MyDialog.vue'
import XMLConvert from 'xml-js'
import CSVConvert from 'csvtojson'
import ExcelConvert from 'xlsx'
import useServices from '@/database/useServices'

type ImportFormat = 'JSON' | 'XML' | 'CSV' | 'Excel'

Expand Down Expand Up @@ -353,11 +354,12 @@ export default class ImportData extends Vue {
}

private async importData() {
const { connectionService } = useServices()
if (!this.record.fileContent.length) {
this.$message.error(this.$t('connections.uploadFileTip') as string)
return
}
const importDataResult = await importConnections(this.record.fileContent)
const importDataResult = await connectionService.import(this.record.fileContent)
if (importDataResult === 'ok') {
this.$message.success(this.$t('common.importSuccess') as string)
this.$emit('updateData')
Expand Down
13 changes: 10 additions & 3 deletions src/components/MsgPublish.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ import Editor from '@/components/Editor.vue'
import convertPayload from '@/utils/convertPayload'
import { v4 as uuidv4 } from 'uuid'
import _ from 'lodash'
import { loadHistoryMessageHeaders, loadHistoryMessagePayloads } from '@/api/connection'
import validFormatJson from '@/utils/validFormatJson'
import useServices from '@/database/useServices'

@Component({
components: {
Expand Down Expand Up @@ -240,8 +240,15 @@ export default class MsgPublish extends Vue {
}

private async loadHistoryData(isNewPayload?: boolean) {
this.headersHistory = await loadHistoryMessageHeaders()
this.payloadsHistory = await loadHistoryMessagePayloads()
const { historyMessageHeaderService, historyMessagePayloadService } = useServices()
const headersHistory = await historyMessageHeaderService.getAll()
const payloadsHistory = await historyMessagePayloadService.getAll()
if (headersHistory) {
this.headersHistory = headersHistory
}
if (payloadsHistory) {
this.payloadsHistory = payloadsHistory
}
if (isNewPayload) {
this.historyIndex = this.payloadsHistory.length - 1
}
Expand Down
14 changes: 8 additions & 6 deletions src/components/SubscriptionsList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@
<script lang="ts">
import { Component, Vue, Prop, Watch } from 'vue-property-decorator'
import { Getter, Action } from 'vuex-class'
import { updateConnection } from '@/api/connection'
import { defineColors, getRandomColor } from '@/utils/colors'
import LeftPanel from '@/components/LeftPanel.vue'
import MyDialog from '@/components/MyDialog.vue'
import VueI18n from 'vue-i18n'
import { ClientSubscribeCallback, MqttClient } from 'mqtt'
import { MqttClient } from 'mqtt'
import useServices from '@/database/useServices'

enum SubscribeErrorReason {
normal,
Expand Down Expand Up @@ -264,7 +264,7 @@ export default class SubscriptionsList extends Vue {
this.subRecord.color = getRandomColor()
}
if (this.client.subscribe) {
this.client.subscribe(topic, { qos: qos as QoS }, (error, res) => {
this.client.subscribe(topic, { qos: qos as QoS }, async (error, res) => {
if (error) {
this.$message.error(error)
this.$log.error(`Topic: ${topic} subscribe error ${error} `)
Expand Down Expand Up @@ -293,7 +293,8 @@ export default class SubscriptionsList extends Vue {
}
this.record.subscriptions = this.subsList
if (this.record.id) {
updateConnection(this.record.id.toString() as string, this.record)
const { connectionService } = useServices()
await connectionService.updateWithCascade(this.record.id, this.record)
this.changeSubs({ id: this.connectionId, subscriptions: this.subsList })
this.showDialog = false
this.$log.info(`Topic: ${topic} successfully subscribed`)
Expand All @@ -315,7 +316,7 @@ export default class SubscriptionsList extends Vue {
}
const { topic, qos } = row
if (this.client.unsubscribe) {
this.client.unsubscribe(topic, { qos }, (error) => {
this.client.unsubscribe(topic, { qos }, async (error) => {
if (error) {
this.$message.error(error)
return false
Expand All @@ -330,7 +331,8 @@ export default class SubscriptionsList extends Vue {
}
this.record.subscriptions = payload.subscriptions
if (this.record.id) {
updateConnection(this.record.id.toString() as string, this.record)
const { connectionService } = useServices()
await connectionService.updateWithCascade(this.record.id, this.record)
this.changeSubs(payload)
this.subsList = payload.subscriptions
this.$emit('deleteTopic')
Expand Down
2 changes: 2 additions & 0 deletions src/database/models/WillEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default class WillEntity {
@Column({ type: 'boolean', default: false })
lastWillRetain!: boolean

// WillPropertiesModel begin
@Column({ type: 'integer', nullable: true })
willDelayInterval?: number

Expand All @@ -31,6 +32,7 @@ export default class WillEntity {

@Column({ type: 'varchar', default: '', nullable: true })
contentType?: string
// WillPropertiesModel end

@OneToOne(() => ConnectionEntity, (connection) => connection.will, { onDelete: 'CASCADE' })
connection?: ConnectionEntity
Expand Down
Loading