|
|
|
|
@@ -54,33 +54,33 @@ import com.example.hpostesting.data.model.updates.DeviceUpdateRequest
|
|
|
|
|
import com.example.hpostesting.encryption.Encryption
|
|
|
|
|
import com.example.hpostesting.presentation.KitScanActivity
|
|
|
|
|
import com.example.hpostesting.presentation.adapter.OfflineUserListAdapter
|
|
|
|
|
import com.example.hpostesting.presentation.adapter.UserListAdapter
|
|
|
|
|
import com.example.hpostesting.presentation.assurance.AssuranceControlsActivity
|
|
|
|
|
import com.example.hpostesting.presentation.hemocube.HemoCubeViewModel
|
|
|
|
|
import com.example.hpostesting.presentation.testRight.TestRightViewModel
|
|
|
|
|
import com.example.hpostesting.presentation.utils.DeviceCommunicationHandler
|
|
|
|
|
import com.example.hpostesting.presentation.utils.UsbServiceListener
|
|
|
|
|
import com.example.hpostesting.util.Result
|
|
|
|
|
import com.firebase.ui.firestore.FirestoreRecyclerOptions
|
|
|
|
|
import com.google.firebase.crashlytics.ktx.crashlytics
|
|
|
|
|
import com.google.firebase.firestore.Query
|
|
|
|
|
import com.google.firebase.firestore.ktx.firestore
|
|
|
|
|
import com.google.firebase.ktx.Firebase
|
|
|
|
|
import com.google.firebase.perf.ktx.performance
|
|
|
|
|
import dagger.hilt.android.AndroidEntryPoint
|
|
|
|
|
import `in`.sminnovations.hpostesting.R
|
|
|
|
|
import `in`.sminnovations.hpostesting.databinding.FragmentHomeBinding
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
import kotlinx.coroutines.GlobalScope
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
import okhttp3.ResponseBody
|
|
|
|
|
import org.json.JSONObject
|
|
|
|
|
import java.io.BufferedOutputStream
|
|
|
|
|
import java.io.File
|
|
|
|
|
import java.io.FileInputStream
|
|
|
|
|
import java.io.FileOutputStream
|
|
|
|
|
import java.net.HttpURLConnection
|
|
|
|
|
import java.net.URL
|
|
|
|
|
import java.nio.charset.Charset
|
|
|
|
|
import java.text.SimpleDateFormat
|
|
|
|
|
import java.util.Calendar
|
|
|
|
|
import java.util.Date
|
|
|
|
|
import java.util.Locale
|
|
|
|
|
import java.util.Scanner
|
|
|
|
|
import java.util.zip.ZipEntry
|
|
|
|
|
import java.util.zip.ZipInputStream
|
|
|
|
|
import kotlin.properties.Delegates
|
|
|
|
|
@@ -251,6 +251,7 @@ class HomeFragment : Fragment() {
|
|
|
|
|
// loadUserData()
|
|
|
|
|
//setSearch()
|
|
|
|
|
checkForLocalDBData()
|
|
|
|
|
|
|
|
|
|
if (Constants.MOLBIO_INTEGRATION) {
|
|
|
|
|
checkForTokenAndUpdate()
|
|
|
|
|
}
|
|
|
|
|
@@ -354,7 +355,9 @@ class HomeFragment : Fragment() {
|
|
|
|
|
Log.d("istoken", isTokenAvailable.toString())
|
|
|
|
|
if (!isTokenAvailable) {
|
|
|
|
|
Log.d("istoken1", isTokenAvailable.toString())
|
|
|
|
|
hemoCubeViewModel.login(createLoginRequestData(userID, password))
|
|
|
|
|
callLogin(userID, password)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//isTokenAvailable = true
|
|
|
|
|
|
|
|
|
|
@@ -362,7 +365,8 @@ class HomeFragment : Fragment() {
|
|
|
|
|
Log.d("istoken7", isTokenAvailable.toString())
|
|
|
|
|
if (isTokenExpired(accessToken)) {
|
|
|
|
|
Log.d("istoken8", isTokenAvailable.toString())
|
|
|
|
|
hemoCubeViewModel.login(createLoginRequestData(userID, password))
|
|
|
|
|
callLogin(userID, password)
|
|
|
|
|
// hemoCubeViewModel.login(createLoginRequestData(userID, password))
|
|
|
|
|
//hemoCubeViewModel.startPeriodicCheckUpdate()
|
|
|
|
|
}else{
|
|
|
|
|
hemoCubeViewModel.checkUpdate(createCheckUpdateRequestData())
|
|
|
|
|
@@ -380,14 +384,17 @@ class HomeFragment : Fragment() {
|
|
|
|
|
val credentials = decryptedMessage.split("\n")
|
|
|
|
|
userID = credentials[0]
|
|
|
|
|
password = credentials[1]
|
|
|
|
|
deviceId = credentials[0]
|
|
|
|
|
// Toast.makeText(context, "DECRYPTED: $credentials", Toast.LENGTH_SHORT).show()
|
|
|
|
|
if (!isTokenAvailable) {
|
|
|
|
|
hemoCubeViewModel.login(createLoginRequestData(userID, password))
|
|
|
|
|
callLogin(userID, password)
|
|
|
|
|
//hemoCubeViewModel.login(createLoginRequestData(userID, password))
|
|
|
|
|
}
|
|
|
|
|
else if (isTokenAvailable) {
|
|
|
|
|
//hemoCubeViewModel.startPeriodicCheckUpdate()
|
|
|
|
|
if (isTokenExpired(accessToken)) {
|
|
|
|
|
hemoCubeViewModel.login(createLoginRequestData(userID, password))
|
|
|
|
|
callLogin(userID, password)
|
|
|
|
|
// hemoCubeViewModel.login(createLoginRequestData(userID, password))
|
|
|
|
|
}else{
|
|
|
|
|
hemoCubeViewModel.checkUpdate(createCheckUpdateRequestData())
|
|
|
|
|
}
|
|
|
|
|
@@ -621,6 +628,19 @@ class HomeFragment : Fragment() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun callLogin(userID: String, password: String) {
|
|
|
|
|
if(DataHolder.ipAddress == "0.0"){
|
|
|
|
|
getPublicIpAddr { ipAddress ->
|
|
|
|
|
if(ipAddress != "fail"){
|
|
|
|
|
DataHolder.ipAddress = ipAddress
|
|
|
|
|
}
|
|
|
|
|
hemoCubeViewModel.login(createLoginRequestData(userID, password))
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
hemoCubeViewModel.login(createLoginRequestData(userID, password))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun isTokenExpired(token: String): Boolean {
|
|
|
|
|
if (token.isNotEmpty()) {
|
|
|
|
|
@@ -654,13 +674,14 @@ class HomeFragment : Fragment() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun createLoginRequestData(userID: String, password: String): LoginRequest {
|
|
|
|
|
|
|
|
|
|
val pInfo = requireActivity().packageManager.getPackageInfo(
|
|
|
|
|
requireActivity().packageName, 0
|
|
|
|
|
)
|
|
|
|
|
val version = pInfo.versionName
|
|
|
|
|
var labname = sharedPreference.getString(Constants.LABNAME,"")
|
|
|
|
|
return LoginRequest(
|
|
|
|
|
password = password, serialNumber = userID, username = userID, version = version, lab = labname,location = DataHolder.location.toString()
|
|
|
|
|
password = password, serialNumber = userID, username = userID, version = version, lab = labname,location = DataHolder.ipAddress.toString()
|
|
|
|
|
)
|
|
|
|
|
}//latitude = DataHolder.location!!.latitude.toString(), longitude = DataHolder.location!!.longitude.toString(), location = DataHolder.location.toString()
|
|
|
|
|
|
|
|
|
|
@@ -676,7 +697,7 @@ class HomeFragment : Fragment() {
|
|
|
|
|
|
|
|
|
|
private fun createDeviceUpdateRequestData(): DeviceUpdateRequest {
|
|
|
|
|
return DeviceUpdateRequest(
|
|
|
|
|
serial_no = sharedPreference.getString(Constants.DEVICE_ID, "")
|
|
|
|
|
serial_no = deviceId
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -796,7 +817,8 @@ class HomeFragment : Fragment() {
|
|
|
|
|
}
|
|
|
|
|
if (!isTokenAvailable) {
|
|
|
|
|
Log.d("istoken0", isTokenAvailable.toString())
|
|
|
|
|
hemoCubeViewModel.login(createLoginRequestData(username, password))
|
|
|
|
|
callLogin(username, password)
|
|
|
|
|
//hemoCubeViewModel.login(createLoginRequestData(username, password))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} ?: Log.e("fetchDeviceCredentials", "Failed to parse device data.")
|
|
|
|
|
@@ -1487,6 +1509,33 @@ class HomeFragment : Fragment() {
|
|
|
|
|
super.onDestroy()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
private fun getPublicIpAddr(callback: (String) -> Unit) {
|
|
|
|
|
try {
|
|
|
|
|
GlobalScope.launch(Dispatchers.IO) {
|
|
|
|
|
val url = URL("https://api.ipify.org")
|
|
|
|
|
val conn = url.openConnection() as HttpURLConnection
|
|
|
|
|
try {
|
|
|
|
|
conn.connect()
|
|
|
|
|
if (conn.responseCode == HttpURLConnection.HTTP_OK) {
|
|
|
|
|
val scanner = Scanner(conn.inputStream)
|
|
|
|
|
scanner.useDelimiter("\\A")
|
|
|
|
|
if (scanner.hasNext()) {
|
|
|
|
|
val ipAddress = scanner.next()
|
|
|
|
|
Log.d("ipaddress",DataHolder.ipAddress)
|
|
|
|
|
callback(ipAddress)
|
|
|
|
|
}else{
|
|
|
|
|
callback("fail")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
conn.disconnect()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}catch (e: Exception){
|
|
|
|
|
Log.e("home",e.toString())
|
|
|
|
|
callback("fail")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|