﻿B4A=true
Group=ATLAS CM\CLASS
ModulesStructureVersion=1
Type=Class
Version=10.5
@EndOfDesignText@
Sub Class_Globals
	
	Private mScan			As MLScan
	Private t1 				As CMSTAT
	Private ser 			As B4XSerializator
	Private su 				As StringUtils
	Private STATUS_EVENT 	As Int = 4
	Private cC 				As cls_cache
	Private bu				As cls_battery
	Private batterystatus(11) As Int
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
	
	mScan.updateWifiList
	cC.Initialize
	bu.Initialize
	batterystatus = bu.BatteryInformation
	
End Sub

Sub CreateCMSTAT () As CMSTAT

	Dim p As Phone
	Dim AHL As AHLocale
	AHL.Initialize
	
	t1.Initialize
	t1.TimeStamp = DateTime.now
	t1.imei = Starter.appset.IMEI
	t1.app_version = Starter.appset.version
	t1.app_lic_code = ""
	t1.os_version = p.SdkVersion
	t1.device_manufacturer = p.Manufacturer
	t1.device_model = p.Model
	t1.device_product = p.Product
	t1.device_charging = IIf(Starter.appset.charging,1,0)
	t1.device_bat_lvl = batterystatus(0)
	t1.mobile_signal = Starter.getSignalLevel
	t1.device_type = 1

	t1.mcc = AHL.Country
	t1.mnc = p.GetSimOperator
	t1.mnn = p.GetNetworkOperatorName
	
	' // GPS
	t1.gps_fix = Starter.appset.locdata.flp.Time 'DateTime.now
	t1.gps_lon = Starter.appset.locdata.flp.Longitude
	t1.gps_lat = Starter.appset.locdata.flp.Latitude
	t1.gps_speed = Starter.appset.locdata.flp.Speed
	' // beacon
	t1.beacon_fix = DateTime.now
	t1.beacon_id =  ""
	t1.beacon_rssi =  0
	
	' // data connection type	
	t1.wifi_fix = DateTime.now
	Try 
		t1.wifi_activenetworktype = svc_service.mlWifi.ActiveNetworkType
		t1.wifi_activenetworktypename = svc_service.mlWifi.ActiveNetworkTypeName
		t1.wifi_signal_db =  svc_service.mlWifi.WifiSignal
		t1.wifi_signal_percent = svc_service.mlWifi.WifiSignalPct
		t1.wifi_ssid = svc_service.mlWifi.WifiSSID
		' // bssid
		t1.wifi_bssid= svc_service.mlWifi.WifiBSSID.ToUpperCase
		t1.wifi_ip = svc_service.mlWifi.WifiIpAddress
		t1.wifi_apdistance = svc_service.mlWifi.WifiAPDistance
	Catch
		mod_functions.writelog($"cls_status_events::wifi,error - ${LastException.Message}"$)
	End Try
	mod_functions.set_premissions_result
	t1.permissions = Starter.appset.permissions
	t1.report_interval = Starter.appset.sm.sm_message_freq
	
	' // active State - default on line
	t1.active_state = mod_core_consts.ACTIVITY_STATE_ONLINE
	t1.active_state_date = DateTime.now
	' // active State - log off until
	If Starter.appset.LogoffUntil > 0 Then
		t1.active_state = mod_core_consts.ACTIVITY_STATE_LOGOFFUNTIL
		t1.active_state_date = Starter.appset.LogoffUntil
	End If
	
	Return t1

End Sub

Sub write_cached_event(e As CMSTAT)
	
	ser.ConvertObjectToBytesAsync(e, "ser")
	Wait For (ser) ser_ObjectToBytes (Success As Boolean, Bytes() As Byte)
	If Success Then
		Dim encData64 As Object = su.EncodeBase64(Bytes)
		cC.write(STATUS_EVENT,encData64,e.TimeStamp)
	End If
	
End Sub

Sub remove_cached_event(pID As Long)

	cC.remove_by_id(STATUS_EVENT, pID)
	
End Sub

Sub get_cached_event() As CMSTAT

	Dim rs As ResultSet = Starter.SQLDB.ExecQuery($"SELECT * FROM cache WHERE cache_type = ${STATUS_EVENT}												
												ORDER BY cache_id ASC "$)
	Dim res As CMSTAT
	
	If rs.NextRow Then
		Dim serData() As Byte
		' // decode the status report
		serData = su.DecodeBase64(rs.GetString("cache_data"))
		res = ser.ConvertBytesToObject(serData)	
	End If
	' // return result
	Return res

End Sub