You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
171 lines
5.5 KiB
171 lines
5.5 KiB
package haus.nightmare.GlassTesla; |
|
|
|
import android.app.Service; |
|
import android.content.Intent; |
|
import android.content.SharedPreferences; |
|
import android.os.Binder; |
|
import android.os.Handler; |
|
import android.os.IBinder; |
|
import android.preference.PreferenceManager; |
|
import android.util.Log; |
|
import com.google.gson.Gson; |
|
import org.conscrypt.Conscrypt; |
|
import java.net.URL; |
|
import java.security.Security; |
|
import javax.net.ssl.SSLException; |
|
import okhttp3.OkHttpClient; |
|
import okhttp3.Request; |
|
import okhttp3.RequestBody; |
|
import okhttp3.Response; |
|
|
|
public class SyncTeslaService extends Service { |
|
int startMode; // indicates how to behave if the service is killed |
|
IBinder binder = new SyncTeslaBinder(); // interface for clients that bind |
|
boolean allowRebind; // indicates whether onRebind should be used |
|
private TeslaResponse vehicleData; |
|
private SharedPreferences prefs; |
|
|
|
|
|
public class SyncTeslaBinder extends Binder { |
|
SyncTeslaService getService() { |
|
return SyncTeslaService.this; |
|
} |
|
} |
|
|
|
public TeslaResponse getVehicleData() { |
|
return vehicleData; |
|
} |
|
|
|
@Override |
|
public void onCreate() { |
|
prefs = PreferenceManager.getDefaultSharedPreferences(this); |
|
} |
|
|
|
@Override |
|
public int onStartCommand(Intent intent, int flags, int startId) { |
|
// The service is starting, due to a call to startService() |
|
|
|
new Thread(new Runnable(){ |
|
public void run() { |
|
// TODO Auto-generated method stub |
|
while(true) |
|
{ |
|
try { |
|
Thread.sleep(2 * 60000); |
|
loadVehicleData(); |
|
|
|
} catch (InterruptedException e) { |
|
e.printStackTrace(); |
|
} |
|
|
|
} |
|
|
|
} |
|
}).start(); |
|
|
|
return startMode; |
|
} |
|
public void loadVehicleData(String vehicleId) { |
|
String json = getResponseFromJsonURL("https://owner-api.teslamotors.com/api/1/vehicles/" |
|
+ vehicleId +"/vehicle_data"); |
|
Gson gson = new Gson(); |
|
ResponseRoot root = gson.fromJson(json, ResponseRoot.class); |
|
vehicleData = root.response; |
|
} |
|
public void loadVehicleData() { |
|
loadVehicleData(prefs.getString("vehicle_id", "")); |
|
} |
|
|
|
public void updateSharedPrefs(SharedPreferences p) { |
|
SharedPreferences.Editor e = prefs.edit(); |
|
prefs = p; |
|
e.putString("bearer_token", p.getString("bearer_token", "")); |
|
e.putString("vehicle_id", p.getString("vehicle_id", "")); |
|
e.apply(); |
|
|
|
} |
|
|
|
public boolean postCommand(String command) { |
|
try { |
|
String token = prefs.getString("bearer_token", ""); |
|
String vehicleId = prefs.getString("vehicle_id", ""); |
|
URL object = new URL("https://owner-api.teslamotors.com/api/1/vehicles/" |
|
+ vehicleId +"/command/" + command); |
|
OkHttpClient client = new OkHttpClient(); |
|
Request request = new Request.Builder() |
|
.url(object) |
|
.method("POST", RequestBody.create(null, new byte[]{})) |
|
.addHeader("Authorization", "Bearer " + token) |
|
.addHeader("User-Agent", "Tesla-GLASS") |
|
.addHeader("Content-Type", "application/json") |
|
.build(); |
|
|
|
|
|
Response response = client.newCall(request).execute(); |
|
if (response.code() == 408) { |
|
postCommand("wake_up"); |
|
} |
|
return response.code() == 200; |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
return false; |
|
} |
|
|
|
public String getResponseFromJsonURL(String url) { |
|
String token = prefs.getString("bearer_token", ""); |
|
|
|
String jsonResponse = null; |
|
if (url.length() > 0) { |
|
try { |
|
Security.insertProviderAt(Conscrypt.newProvider(), 1); |
|
/************** For getting response from HTTP URL start ***************/ |
|
URL object = new URL(url); |
|
|
|
OkHttpClient client = new OkHttpClient(); |
|
Request request = new Request.Builder() |
|
.url(object) |
|
.addHeader("Authorization", "Bearer " + token) |
|
.addHeader("User-Agent", "Tesla-GLASS") |
|
.addHeader("Content-Type", "application/json") |
|
.build(); |
|
|
|
Response response = client.newCall(request).execute(); |
|
|
|
int responseCode = response.code(); |
|
|
|
if (responseCode == 200) { |
|
return response.body().string(); |
|
|
|
} else if (responseCode == 408) { |
|
postCommand("wake_up"); |
|
} |
|
} catch (SSLException e) { |
|
Log.e("DataSSL", "SSL Exception getting json:", e); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
} |
|
return jsonResponse; |
|
} |
|
|
|
@Override |
|
public IBinder onBind(Intent intent) { |
|
// A client is binding to the service with bindService() |
|
return binder; |
|
} |
|
@Override |
|
public boolean onUnbind(Intent intent) { |
|
// All clients have unbound with unbindService() |
|
return allowRebind; |
|
} |
|
@Override |
|
public void onRebind(Intent intent) { |
|
// A client is binding to the service with bindService(), |
|
// after onUnbind() has already been called |
|
} |
|
@Override |
|
public void onDestroy() { |
|
// The service is no longer used and is being destroyed |
|
} |
|
} |