Skip to content

Commit

Permalink
subscription info
Browse files Browse the repository at this point in the history
  • Loading branch information
bassmaster187 committed Dec 11, 2024
1 parent e690fd8 commit f5b2be9
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 6 deletions.
43 changes: 40 additions & 3 deletions TeslaLogger/www/admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@
require_once("redirect.php");
require_once("language.php");
require_once("tools.php");
session_start();

if (session_status() == PHP_SESSION_NONE) {
session_start();
}

include "menu.php";
global $display_name;
global $carNeedFleetAPI;
global $carVIN;
global $carNeedSubscription;
global $fleetapiinfo;

$carNeedSubscription = false;
$carid = GetDefaultCarId();
if (isset($_REQUEST["carid"]))
{
Expand Down Expand Up @@ -425,14 +436,12 @@ function updateSMT(jsonData)
}

<?php
require_once("info.php");
?>
</script>

</head>
<body>
<?php
include "menu.php";
echo(menu("Teslalogger"));
?>

Expand All @@ -455,6 +464,31 @@ function updateSMT(jsonData)
<img id="unlocked"class="caricons" src="img/unlocked.png" title="Unlocked">
</td>
</thead>
<?php
if ($carNeedFleetAPI)
echo("<tr><td><font color='red'><b>".get_text("FleetAPI")."</b></font></td><td><a href='password_fleet.php?id=$carid&vin=$carVIN'>".get_text("FleetAPIRequired")." ⚠️</a></td></tr>");
else if ($carNeedSubscription)
{
?>
<!-- car need subscription -->
<tr id="subscriptioninfo" style='display: none;'><td><font color='red'><b><?php t("Subscription") ?></b></font></td><td><a href='https://buy.stripe.com/9AQaHNdU33k29Vu144?client_reference_id=<?=$carVIN?>'><?php t("SubscriptionRequired") ?> ⚠️</a></td></tr>
<script>
$(document).ready(function(){
$.ajax({
url: "subscription-check.php?vin=<?=$carVIN?>",
}).done(function(data) {
if (data == "No subscription") {
$("#subscriptioninfo").show();
}
}).fail(function(jqXHR, textStatus, errorThrown) {
console.error("Error: " + textStatus, errorThrown);
});
});
</script>
<?php
}

?>
<tr><td width="130px"><b><span id="car_statusLabel"></span></b></td><td width="180px"><span id="car_status"></span></td></tr>
<tr id='CellTempRow'><td><b><?php t("Cell Temp"); ?>:</b></td><td><span id="CellTemp"></span></td></tr>
<tr id='BMSMaxChargeRow'><td><b><?php t("Max Charge"); ?>:</b></td><td><span id="BMSMaxCharge"></span></td></tr>
Expand Down Expand Up @@ -546,5 +580,8 @@ function getZoomLevel()

?>
</div>
<script>
<?php require_once("info.php"); ?>
</script>
</body>
</html>
14 changes: 12 additions & 2 deletions TeslaLogger/www/admin/info.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@
function ShowInfo()
{
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
global $fleetapiinfo;

$fileinfofleetapi = "/tmp/fleetapiinfo".date("Y-m-d").".txt";
$prefix = "/etc/teslalogger/";
if (isDocker())
$prefix = "/tmp/";

if (!file_exists($fileinfofleetapi))

echo ("\r\n<!-- fleetapiinfo: show: ".$_SESSION["fleetapiinfo"]." -->\r\n");

if ($fleetapiinfo === true
//&& !file_exists($fileinfofleetapi)
)
{
file_put_contents($fileinfofleetapi, '');
$tinfo = get_text("INFO_FLEETAPI");
$tinfo = get_text("INFO_FLEETAPI"). "<br>FleetAPI: " . $_SESSION["fleetapiinfo"];
$tinfo=str_replace("{LINK1}", "<a href='https://developer.tesla.com/docs/fleet-api/announcements#2024-11-27-pay-per-use-pricing' target='_blank'>Tesla Pay per use pricing</a>", $tinfo);
$tinfo=str_replace("{LINK2}", "<a href='https://digitalassets.tesla.com/tesla-contents/image/upload/Fleet-API-Agreement-EN.pdf' target='_blank'>Fleet API Agreement</a>", $tinfo);
?>
Expand Down
47 changes: 46 additions & 1 deletion TeslaLogger/www/admin/menu.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?PHP
if (session_status() == PHP_SESSION_NONE) {
session_start();
}

require_once("language.php");
require_once("tools.php");
function full_path()
Expand All @@ -22,6 +26,10 @@ function menu($title, $prefix = "")
$car = "";
$tasker_token = "";
global $display_name;
global $carNeedFleetAPI;
global $carNeedSubscription;
global $carVIN;
global $fleetapiinfo;

$current_carid = $_SESSION["carid"];
if (!isset($current_carid))
Expand All @@ -34,17 +42,54 @@ function menu($title, $prefix = "")
$jcars = json_decode($allcars);
if ($jcars !== null)
{
$fleetapiinfo = false;
foreach ($jcars as $k => $v) {
if ($v->{"id"} == $current_carid)
{
$display_name = $v->{"display_name"};
$tasker_token = $v->{"tasker_hash"};
$car = $v->{"model_name"};
$carVIN = $v->{"vin"};

if (strlen($display_name) == 0)
$display_name = "Car ".$v->{"id"};
}
}

$carid = $v->{"id"};

if ($v->{"inactive"} == 0) // info will be shown only if the car is active
{
$cartype = $v->{"car_type"};
$NeedSubscription = !($cartype === "models" || $cartype === "modelx" || $cartype === "models2");

if ($v->{"fleetAPI"} == 0)
{
if ($NeedSubscription) // old Model S/X doesn't need a subscription
{
echo ("<!-- fleetapiinfo: cartype: $cartype - ID: $carid - Name: $car -->\r\n");
$fleetapiinfo = true;
if ($v->{"id"} == $current_carid)
{
$carNeedFleetAPI = true;
$carNeedSubscription = true;
echo("<!-- car need subscription true 2-->");
}
}
else
$carNeedSubscription = false;
}
else
{
if ($v->{"id"} == $current_carid)
{
$carNeedSubscription = true;
echo("<!-- car need subscription true 3-->");
}
}
}
else
echo ("<!-- fleetapiinfo: car inactive: ID: $carid - Name: $car -->");
}
}

$ref = "?token=" . $tasker_token . "&ref=" . full_path()."&car=".$car;
Expand Down
32 changes: 32 additions & 0 deletions TeslaLogger/www/admin/subscription-check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
$vin = $_GET['vin'];

if (empty($vin)) {
echo "VIN is empty";
error_log("VIN is empty", 0);
exit;
}

if (strlen($vin) < 17) {
echo "VIN is not at least 17 characters long";
error_log("VIN is not at least 17 characters long", 0);
exit;
}

$cache_file = "/tmp/subscription_cache_$vin.json";
$cache_time = 60; // 1 minutes in seconds

if (file_exists($cache_file) && (time() - filemtime($cache_file)) < $cache_time) {
// Use cached response
$subscription = file_get_contents($cache_file);
error_log("Using cached response for VIN: $vin", 0);

} else {
// Fetch new response and cache it
$subscription = file_get_contents("https://teslalogger.de/stripe/subscription-check.php?vin=$vin");
file_put_contents($cache_file, $subscription);
error_log("Fetched new response and cached it for VIN: $vin", 0);
}

echo $subscription;
?>

0 comments on commit f5b2be9

Please sign in to comment.