Skip to content

Commit

Permalink
[CP-3170] Minor refactor of device-flash (#2128)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarski authored Oct 16, 2024
1 parent 56752cd commit 3bfe9ac
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 279 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useDeviceLockedEffect } from "Core/core/hooks/use-device-locked-effect"
import { useDeviceDetachedEffect } from "Core/core/hooks/use-device-detached-effect"
import { useDeviceConnectFailedEffect } from "Core/core/hooks/use-device-connect-failed-effect"
import { useDiscoveryRedirectEffect } from "Core/core/hooks/use-discovery-redirect-effect"
import { useAbortFlashingOnDeviceDetached } from "Core/core/hooks/use-abort-flashing-on-device-detached"
import { useMscDeviceDetachedEffect } from "Core/core/hooks/use-msc-device-detached-effect"
import { useRouterListener } from "Core/core/hooks"
import {
OutboxWrapper,
Expand Down Expand Up @@ -48,7 +48,7 @@ const BaseApp: FunctionComponent = () => {
useHelp()

// MSC
useAbortFlashingOnDeviceDetached()
useMscDeviceDetachedEffect()

return (
<>
Expand Down
35 changes: 0 additions & 35 deletions libs/core/core/hooks/use-abort-flashing-on-device-detached.ts

This file was deleted.

55 changes: 55 additions & 0 deletions libs/core/core/hooks/use-msc-device-detached-effect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (c) Mudita sp. z o.o. All rights reserved.
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import { useCallback, useEffect } from "react"
import { useDispatch, useSelector } from "react-redux"
import { answerMain, useDebouncedEventsHandler } from "shared/utils"
import {
DeviceBaseProperties,
DeviceProtocolMainEvent,
DeviceType,
} from "device-protocol/models"
import {
abortMscFlashing,
FlashingProcessState,
selectIsFlashingInActivePhases,
} from "msc-flash-harmony"
import { Dispatch } from "Core/__deprecated__/renderer/store"

export const useMscDeviceDetachedEffect = () => {
const handleDevicesDetached = useHandleDevicesDetached()

const batchDeviceDetachedEvents =
useDebouncedEventsHandler<DeviceBaseProperties>(handleDevicesDetached)

useEffect(() => {
return answerMain(
DeviceProtocolMainEvent.DeviceDetached,
batchDeviceDetachedEvents
)
}, [batchDeviceDetachedEvents])
}

const useHandleDevicesDetached = () => {
const dispatch = useDispatch<Dispatch>()
const flashingInActivePhases = useSelector(selectIsFlashingInActivePhases)
return useCallback(
(deviceDetachedEvents: DeviceBaseProperties[]) => {
const mscEvents = deviceDetachedEvents.filter(
({ deviceType }) => deviceType === DeviceType.MuditaHarmonyMsc
)

if (mscEvents.length === 0) {
return
}

const reason = flashingInActivePhases
? FlashingProcessState.Failed
: undefined
dispatch(abortMscFlashing({ reason }))
},
[dispatch, flashingInActivePhases]
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

export * from "./select-flashing-state.selector"
export * from "./select-flashing-state"
export * from "./select-flashing-abort-controller"
export * from "./select-flashing-process-state"
export * from "./select-is-flashing-in-active-phases"
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { createSelector } from "@reduxjs/toolkit"
import { flashingState } from "./select-flashing-state.selector"
import { flashingState } from "./select-flashing-state"

export const selectFlashingAbortController = createSelector(
flashingState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { createSelector } from "@reduxjs/toolkit"
import { flashingState } from "./select-flashing-state.selector"
import { flashingState } from "./select-flashing-state"

export const selectFlashingProcessState = createSelector(
flashingState,
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import path from "path"
import { execPromise, execCommandWithSudo } from "shared/utils"
import {
execPromise,
execCommandWithSudo,
splitPathToDirNameAndBaseName,
} from "shared/utils"
import IDeviceFlash from "../device-flash.interface"
import LinuxPartitionParser from "./linux-partition-parser"

Expand Down Expand Up @@ -36,7 +39,7 @@ class LinuxDeviceFlashService implements IDeviceFlash {
const ejectDeviceCommand = await this.getEjectDeviceCommand(device)

const command = `${unmountDeviceCommand} && ${flashImageToDeviceCommand} && ${ejectDeviceCommand}`
await execCommandWithSudo(command, { name: "Mudita Auto Flash" })
await execCommandWithSudo(command)

console.log("Flash process completed successfully")
}
Expand All @@ -55,9 +58,8 @@ class LinuxDeviceFlashService implements IDeviceFlash {
imagePath: string,
scriptPath: string
): Promise<string> {
const [path, scriptBasename] =
this.splitPathToDirnameAndBasename(scriptPath)
const [, imageBasename] = this.splitPathToDirnameAndBasename(imagePath)
const [path, scriptBasename] = splitPathToDirNameAndBaseName(scriptPath)
const [, imageBasename] = splitPathToDirNameAndBaseName(imagePath)
return `chmod +x ${scriptPath} && cd ${path} && ./${scriptBasename} ${imageBasename} /dev/${device}`
}

Expand All @@ -77,12 +79,6 @@ class LinuxDeviceFlashService implements IDeviceFlash {
)
return LinuxPartitionParser.parsePartitions(partitions ?? "")
}

private splitPathToDirnameAndBasename(currentPath: string) {
const dirname = path.dirname(currentPath)
const basename = path.basename(currentPath)
return [dirname, basename]
}
}

export default LinuxDeviceFlashService
Loading

0 comments on commit 3bfe9ac

Please sign in to comment.