Valentin D. Pinkman
7 years ago
7 changed files with 200 additions and 15 deletions
@ -0,0 +1,114 @@ |
|||
// @flow
|
|||
|
|||
import React, { PureComponent } from 'react' |
|||
|
|||
import type { Device, T } from 'types/common' |
|||
|
|||
import runJob from 'renderer/runJob' |
|||
|
|||
import Box, { Card } from 'components/base/Box' |
|||
import Button from 'components/base/Button' |
|||
|
|||
const CACHED_LATEST_FIRMWARE = null |
|||
|
|||
type FirmwareInfos = { |
|||
name: string, |
|||
notes: string, |
|||
} |
|||
|
|||
type Props = { |
|||
t: T, |
|||
device: Device, |
|||
} |
|||
|
|||
type State = { |
|||
latestFirmware: ?FirmwareInfos, |
|||
} |
|||
|
|||
class FirmwareUpdate extends PureComponent<Props, State> { |
|||
state = { |
|||
latestFirmware: null, |
|||
} |
|||
|
|||
componentDidMount() { |
|||
this.fetchLatestFirmware() |
|||
} |
|||
|
|||
componentWillUnmount() { |
|||
this._unmounted = true |
|||
} |
|||
|
|||
_unmounted = false |
|||
|
|||
fetchLatestFirmware = async () => { |
|||
const { device } = this.props |
|||
const latestFirmware = |
|||
CACHED_LATEST_FIRMWARE || |
|||
(await runJob({ |
|||
channel: 'manager', |
|||
job: 'getLatestFirmwareForDevice', |
|||
data: { devicePath: device.path }, |
|||
successResponse: 'manager.getLatestFirmwareForDeviceSuccess', |
|||
errorResponse: 'manager.getLatestFirmwareForDeviceError', |
|||
})) |
|||
// CACHED_LATEST_FIRMWARE = latestFirmware
|
|||
console.log(latestFirmware) |
|||
if (!this._unmounted) { |
|||
this.setState({ latestFirmware }) |
|||
} |
|||
} |
|||
|
|||
handleInstallFirmware = async () => { |
|||
try { |
|||
const { latestFirmware } = this.state |
|||
console.log(latestFirmware) |
|||
const { |
|||
device: { path: devicePath }, |
|||
} = this.props |
|||
await runJob({ |
|||
channel: 'manager', |
|||
job: 'installFirmware', |
|||
successResponse: 'device.firmwareInstalled', |
|||
errorResponse: 'device.firmwareInstallError', |
|||
data: { |
|||
devicePath, |
|||
firmware: latestFirmware, |
|||
}, |
|||
}) |
|||
} catch (err) { |
|||
console.log(err) |
|||
} |
|||
} |
|||
|
|||
render() { |
|||
const { t, ...props } = this.props |
|||
const { latestFirmware } = this.state |
|||
|
|||
if (!latestFirmware) { |
|||
return null |
|||
} |
|||
|
|||
return ( |
|||
<Box flow={4} {...props}> |
|||
<Box color="dark" ff="Museo Sans" fontSize={6}> |
|||
{t('manager:firmwareUpdate')} |
|||
</Box> |
|||
<Card flow={2} {...props}> |
|||
<Box horizontal align="center" flow={2}> |
|||
<Box ff="Museo Sans">{`Latest firmware: ${latestFirmware.name}`}</Box> |
|||
<Button outline onClick={this.fetchLatestFirmware}> |
|||
{'Fetch'} |
|||
</Button> |
|||
</Box> |
|||
<Box |
|||
fontSize={3} |
|||
style={{ whiteSpace: 'pre' }} |
|||
dangerouslySetInnerHTML={{ __html: latestFirmware.notes }} |
|||
/> |
|||
</Card> |
|||
</Box> |
|||
) |
|||
} |
|||
} |
|||
|
|||
export default FirmwareUpdate |
@ -0,0 +1,45 @@ |
|||
// @flow
|
|||
import axios from 'axios' |
|||
import CommNodeHid from '@ledgerhq/hw-transport-node-hid' |
|||
import isEmpty from 'lodash/isEmpty' |
|||
|
|||
import type { IPCSend } from 'types/electron' |
|||
|
|||
import { getFirmwareInfo } from './helpers' |
|||
|
|||
const { API_BASE_URL } = process.env |
|||
|
|||
export default async (send: IPCSend, data: any) => { |
|||
try { |
|||
const transport = await CommNodeHid.open(data.devicePath) |
|||
const infos = await getFirmwareInfo(transport) |
|||
|
|||
// Get device infos from targetId
|
|||
const { data: deviceVersion } = await axios.get( |
|||
`${API_BASE_URL}/device_versions_target_id/${infos.targetId}`, |
|||
) |
|||
|
|||
// Get firmware infos with firmware name and device version
|
|||
const { data: seFirmwareVersion } = await axios.post(`${API_BASE_URL}/firmware_versions_name`, { |
|||
device_version: deviceVersion.id, |
|||
se_firmware_name: infos.version, |
|||
}) |
|||
|
|||
// Fetch next possible firmware
|
|||
const { data: serverData } = await axios.post(`${API_BASE_URL}/get_latest_firmware`, { |
|||
current_se_firmware_version: seFirmwareVersion.id, |
|||
device_version: deviceVersion.id, |
|||
providers: [1], |
|||
}) |
|||
|
|||
const { se_firmware_version } = serverData |
|||
|
|||
if (!isEmpty(se_firmware_version)) { |
|||
send('manager.getLatestFirmwareForDeviceSuccess', se_firmware_version) |
|||
} else { |
|||
send('manager.getLatestFirmwareForDeviceError', { name: 'yolo', notes: 'fake' }) |
|||
} |
|||
} catch (error) { |
|||
send('manager.getLatestFirmwareForDeviceError', { name: 'yolo', notes: 'fake', error }) |
|||
} |
|||
} |
@ -0,0 +1,17 @@ |
|||
// @flow
|
|||
|
|||
import type { IPCSend } from 'types/electron' |
|||
|
|||
export default async (send: IPCSend, data: any) => { |
|||
/** |
|||
* 1 CREATE TRANSPORT |
|||
* 2 GETFIRMWARE INFOS |
|||
* 3 SEND |
|||
*/ |
|||
console.log(data) |
|||
} |
|||
// createTransportHandler(send, {
|
|||
// action: installFirmware,
|
|||
// successResponse: 'manager.appInstalled',
|
|||
// errorResponse: 'manager.appInstallError',
|
|||
// })(data)
|
Loading…
Reference in new issue