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

Feat/multiple vehicles #38

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 18 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,17 @@ function createWindow() {

// get tesla Data
let authToken
const getTeslaData = async () => {
const getTeslaData = async (vehicleIdx) => {
authToken = store.get('authToken')
if (typeof vehicleIdx !== 'number') vehicleIdx = store.get('vehicleIdx') || 0

if (mainWindow.isVisible() && authToken) {
try {
let vehicle
try {
vehicle = await tesla.vehicle(authToken)
const vehicles = await tesla.vehicles(authToken)
vehicleIdx = (vehicleIdx % vehicles.length) || 0
vehicle = vehicles[vehicleIdx]
} catch (error) {
log.error(error)
store.delete('authToken')
Expand All @@ -159,8 +162,10 @@ function createWindow() {
}

store.set('vehicleId', vehicle.vehicleID)
store.set('vehicleIdx', vehicleIdx)
if (vehicle.state !== 'online') {
mainWindow.webContents.send('tesla-data', {
vehicleIdx,
...vehicle
})
await tesla.wakeUp(authToken, vehicle.vehicleID)
Expand All @@ -172,6 +177,7 @@ function createWindow() {
)
mainWindow.webContents.send('tesla-data', {
model: vehicle.model,
vehicleIdx,
...vehicleData
})
mainWindow.webContents.send('tesla-data-error', false)
Expand Down Expand Up @@ -208,6 +214,10 @@ function createWindow() {
startLogin(false, loginEmailPw)
})

ipcMain.on('switch-vehicle', async (event, vehicleIdx) => {
getTeslaData(vehicleIdx)
})

// wait helper function
async function wait(ms) {
return new Promise(resolve => {
Expand All @@ -219,7 +229,7 @@ function createWindow() {
mainWindow.webContents.send('action-loading', action)
try {
if (action === 'door-unlock') {
await tesla.unLockDoor(store.get('authToken'), store.get('vehicleId'))
await tesla.unlockDoor(store.get('authToken'), store.get('vehicleId'))
} else {
await tesla.lockDoor(store.get('authToken'), store.get('vehicleId'))
}
Expand Down Expand Up @@ -359,12 +369,16 @@ function createWindow() {
}

let firstshow = true
let loading = false // lock to ensure we don't load more than we ought to
mainWindow.on('show', async () => {
if (firstshow) {
firstshow = false
return
}
getTeslaData()
if (loading) return
loading = true
await getTeslaData()
loading = false
})
})

Expand Down
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
"directories": {
"output": "packaged"
},
"publish": [{
"provider": "github",
"owner": "geraldoramos",
"repo": "nikola"
}],
"publish": [
{
"provider": "github",
"owner": "geraldoramos",
"repo": "nikola"
}
],
"appId": "com.github.geraldoramos.nikola",
"mac": {
"category": "public.app-category.utilities",
Expand Down Expand Up @@ -94,6 +96,6 @@
"electron-updater": "^4.0.6",
"google-maps-react": "^2.0.2",
"react-tooltip": "^3.10.0",
"teslajs": "^4.3.9"
"teslajs": "^4.7.4"
}
}
}
21 changes: 21 additions & 0 deletions src/assets/css/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ body {
border-top-right-radius: var(--window-border-radius);
}

.car-picker > svg {
position: absolute;
top: 5px;
}

.car-picker > svg:hover {
color: #bbbbff;
}

.car-picker > svg:active {
color: #ffffbb;
}

.car-picker .car-left {
left: 2px;
}

.car-picker .car-right {
right: 2px;
}

.window-content {
background-color: var(--background-color);
}
Expand Down
33 changes: 32 additions & 1 deletion src/components/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import React, { Component } from 'react'
const { ipcRenderer, remote } = window.require('electron')
import batteryLevelIcon from './helpers/battery-level-icon'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faWifi } from '@fortawesome/free-solid-svg-icons'
import {
faWifi,
faChevronCircleLeft,
faChevronCircleRight
} from '@fortawesome/free-solid-svg-icons'
import App from './App'
import Login from './Login'

Expand All @@ -21,6 +25,8 @@ class Home extends React.Component {
online: true
}
this.alertOnlineStatus = this.alertOnlineStatus.bind(this)
this.handleVehicleIdxDec = this.handleVehicleIdxDec.bind(this)
this.handleVehicleIdxInc = this.handleVehicleIdxInc.bind(this)
}

alertOnlineStatus() {
Expand Down Expand Up @@ -106,6 +112,7 @@ class Home extends React.Component {
? store.gui_settings.gui_charge_rate_units
: null
},
vehicleIdx: store.vehicleIdx,
status: {
driverTempSetting: store.climate_state
? store.climate_state.driver_temp_setting
Expand Down Expand Up @@ -163,6 +170,14 @@ class Home extends React.Component {
window.removeEventListener('offline', this.alertOnlineStatus)
}

handleVehicleIdxDec() {
ipcRenderer.send('switch-vehicle', this.state.vehicleIdx - 1)
}

handleVehicleIdxInc() {
ipcRenderer.send('switch-vehicle', this.state.vehicleIdx + 1)
}

render() {
if (!this.state.online) {
return (
Expand Down Expand Up @@ -209,6 +224,22 @@ class Home extends React.Component {
<header className="toolbar toolbar-header" />
<div className="window-content">
<div className="pane">
<div className="car-picker">
<FontAwesomeIcon
icon={faChevronCircleLeft}
className="car-left"
size="2x"
color="#ffffff"
onClick={this.handleVehicleIdxDec}
/>
<FontAwesomeIcon
icon={faChevronCircleRight}
className="car-right"
size="2x"
color="#ffffff"
onClick={this.handleVehicleIdxInc}
/>
</div>
<App {...this.state} />
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Login extends React.Component {
onChange={this.handleChange}
className="form-control"
placeholder="Enter email"
autofocus={true}
/>
</div>
<div className="form-group">
Expand Down
31 changes: 13 additions & 18 deletions src/components/helpers/battery-level-icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,18 @@ import {
faBatteryThreeQuarters
} from '@fortawesome/free-solid-svg-icons'

export default level => {
let battery
switch (true) {
case 0 <= level && level <= 9:
battery = { type: faBatteryEmpty, color: '#cc0001' }
break
case 10 <= level && level <= 39:
battery = { type: faBatteryQuarter, color: '#cc0001' }
break
case 40 <= level && level <= 69:
battery = { type: faBatteryHalf, color: '#1BC47D' }
break
case 70 <= level && level <= 90:
battery = { type: faBatteryThreeQuarters, color: '#1BC47D' }
break
default:
battery = { type: faBatteryFull, color: '#1BC47D' }
const levels = {
10: { type: faBatteryEmpty, color: '#cc0001' },
40: { type: faBatteryQuarter, color: '#cc0001' },
70: { type: faBatteryHalf, color: '#1BC47D' },
90: { type: faBatteryThreeQuarters, color: '#1BC47D' },
100: { type: faBatteryFull, color: '#1BC47D' },
}

export default function batteryLevelIcon(thisLevel) {
for (const levelThresh of Object.keys(levels)) {
if (thisLevel <= Number(levelThresh)) return levels[levelThresh]
}
return battery
// Over 100?
return levels[100]
}
Loading