Browse Source

fetch last firmware from api

master
Valentin D. Pinkman 7 years ago
parent
commit
8a8cb4fcdf
No known key found for this signature in database GPG Key ID: E7D110669FFB8D3E
  1. 3
      README.md
  2. 27
      src/components/ManagerPage/AppsList.js
  3. 114
      src/components/ManagerPage/FirmwareUpdate.js
  4. 8
      src/components/ManagerPage/index.js
  5. 45
      src/internals/manager/getLatestFirmwareForDevice.js
  6. 1
      src/internals/manager/index.js
  7. 17
      src/internals/manager/installFirmware.js

3
README.md

@ -37,6 +37,9 @@ yarn
# Where errors will be tracked (you may not want to edit this line)
# SENTRY_URL=
# api base url
API_BASE_URL=http://...
# OPTIONAL ENV VARIABLES
# ----------------------

27
src/components/ManagerPage/AppsList.js

@ -64,7 +64,7 @@ class AppsList extends PureComponent<Props, State> {
}
componentDidMount() {
this.fetchList()
this.fetchAppList()
}
componentWillUnmount() {
@ -73,8 +73,7 @@ class AppsList extends PureComponent<Props, State> {
_unmounted = false
async fetchList() {
console.log(`fetching app list`)
async fetchAppList() {
const appsList =
CACHED_APPS ||
(await runJob({
@ -98,23 +97,23 @@ class AppsList extends PureComponent<Props, State> {
device: { path: devicePath },
} = this.props
const data = { appParams, devicePath }
await runJob({ channel: 'usb', job, successResponse, errorResponse, data })
await runJob({ channel: 'manager', job, successResponse, errorResponse, data })
this.setState({ status: 'success' })
} catch (err) {
this.setState({ status: 'error', error: err.message })
}
}
handleInstall = this.createDeviceJobHandler({
job: 'manager.installApp',
successResponse: 'device.appInstalled',
errorResponse: 'device.appInstallError',
handleInstallApp = this.createDeviceJobHandler({
job: 'installApp',
successResponse: 'manager.appInstalled',
errorResponse: 'manager.appInstallError',
})
handleUninstall = this.createDeviceJobHandler({
job: 'manager.uninstallApp',
successResponse: 'device.appUninstalled',
errorResponse: 'device.appUninstallError',
handleUninstallApp = this.createDeviceJobHandler({
job: 'uninstallApp',
successResponse: 'manager.appUninstalled',
errorResponse: 'manager.appUninstallError',
})
handleCloseModal = () => this.setState({ status: 'idle' })
@ -129,8 +128,8 @@ class AppsList extends PureComponent<Props, State> {
name={c.name}
version={`Version ${c.version}`}
icon={ICONS_FALLBACK[c.icon] || c.icon}
onInstall={this.handleInstall(c)}
onUninstall={this.handleUninstall(c)}
onInstall={this.handleInstallApp(c)}
onUninstall={this.handleUninstallApp(c)}
/>
))}
<Modal

114
src/components/ManagerPage/FirmwareUpdate.js

@ -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

8
src/components/ManagerPage/index.js

@ -13,6 +13,7 @@ import Pills from 'components/base/Pills'
import AppsList from './AppsList'
import DeviceInfos from './DeviceInfos'
import FirmwareUpdate from './FirmwareUpdate'
const mapStateToProps = state => ({
device: getCurrentDevice(state),
@ -62,7 +63,12 @@ class ManagerPage extends PureComponent<Props, State> {
return (
<Fragment>
<Pills items={tabs} activeKey={currentTab} onChange={this.handleTabChange} mb={6} />
{currentTab === 'apps' && <AppsList device={device} />}
{currentTab === 'apps' && (
<Fragment>
<FirmwareUpdate t={t} device={device} mb={4} />
<AppsList device={device} />
</Fragment>
)}
{currentTab === 'device' && <DeviceInfos device={device} />}
</Fragment>
)

45
src/internals/manager/getLatestFirmwareForDevice.js

@ -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 })
}
}

1
src/internals/manager/index.js

@ -20,3 +20,4 @@ export { default as getMemInfos } from './getMemInfos'
export { default as installApp } from './installApp'
export { default as listApps } from './listApps'
export { default as uninstallApp } from './uninstallApp'
export { default as getLatestFirmwareForDevice } from './getLatestFirmwareForDevice'

17
src/internals/manager/installFirmware.js

@ -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…
Cancel
Save