Skip to content

Commit

Permalink
BLE local control: Stability updates - use file instead of pipe to av…
Browse files Browse the repository at this point in the history
…oid blocking, add timeout for commands
  • Loading branch information
ngardiner committed Dec 18, 2024
1 parent 536e451 commit 87f9d0e
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions lib/TWCManager/Vehicle/TeslaBLE.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
import logging
import os
import subprocess
from threading import Timer
import time

logger = logging.getLogger("\U0001F697 TeslaBLE")


class TeslaBLE:
binaryPath = "/home/twcmanager/gobin/tesla-control"
commandTimeout = 10
config = None
master = None
pipe = None
pipeName = "/tmp/ble_pipe"
pipeName = "/tmp/ble_data"

def __init__(self, master):
self.master = master
Expand Down Expand Up @@ -49,6 +51,7 @@ def parseCommandOutput(self, output):
return success

def peerWithVehicle(self, vin):
self.sendPublicKey(vin)
result = subprocess.run(
[
self.binaryPath,
Expand All @@ -63,14 +66,15 @@ def peerWithVehicle(self, vin):
],
stdout=subprocess.PIPE,
)
self.sendPublicKey(vin)
self.closeFile()
return self.parseCommandOutput(ret)

def pingVehicle(self, vin):
ret = self.sendCommand(vin, "ping")
return self.parseCommandOutput(ret)

def sendCommand(self, vin, command, args=None):
self.sendPrivateKey(vin)
command_string = [
self.binaryPath,
"-debug",
Expand All @@ -84,13 +88,20 @@ def sendCommand(self, vin, command, args=None):
if args:
command_string.append(args)

result = subprocess.run(
result = subprocess.Popen(
command_string,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
self.sendPrivateKey(vin)
return result.stderr.decode("utf-8")
timer = Timer(self.commandTimeout, result.kill)
try:
timer.start()
stdout, stderr = result.communicate()
finally:
timer.cancel()

self.closeFile()
return stderr.decode("utf-8")

def setChargeRate(self, charge_rate, vehicle=None, set_again=False):
ret = self.sendCommand(vehicle, "charging-set-amps", charge_rate)
Expand Down Expand Up @@ -155,36 +166,25 @@ def scanForVehicles(self):
def wakeVehicle(self, vin):
self.sendCommand(vin, "wake")

def closeFIFO(self):
os.close(self.pipe)

def openFIFO(self):
# Open FIFO pipe for passing data to tesla-control
try:
os.mkfifo(self.pipeName)
except FileExistsError:
pass
except OSError as oe:
if oe.errno != errno.EEXIST:
raise
def closeFile(self):
self.pipe.close()
os.unlink(self.pipeName)

self.pipe = os.open(self.pipeName, os.O_WRONLY)
def openFile(self):
# Open output file for passing data to tesla-control
self.pipe = open(self.pipeName, "wb", 0)

def sendPublicKey(self, vin):
self.openFIFO()
os.write(
self.pipe,
self.openFile()
self.pipe.write(
base64.b64decode(self.master.settings["Vehicles"][vin]["pubKeyPEM"]),
)
self.closeFIFO()

def sendPrivateKey(self, vin):
self.openFIFO()
os.write(
self.pipe,
self.openFile()
self.pipe.write(
base64.b64decode(self.master.settings["Vehicles"][vin]["privKey"]),
)
self.closeFIFO()

def updateSettings(self):
# Called by TWCMaster when settings are read/updated
Expand Down

0 comments on commit 87f9d0e

Please sign in to comment.