Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private NativeMe As JavaObject ' used for vehicle / engine data...
Public mympp,ECMmap,tECMmap,pgnmp, EventMap As Map
Private Mastlst, MastEvent, MastEventNum As List
Public ecmtimer As Timer
Private inEvent As Boolean = False
Public sql6, sql As SQL
Private ignstatus, tempmap As String
Private inc As Long = 0
Private rinc As Long = 0
Private mLst As List
Private lastcon, newcon As Long
Public rectick As Int = 1000
Public ecmmastrec, currmastrec As Long
Private noadd As Boolean = False
Public CurrFileName As String = ""
Private TempFileName As String = ""
Public fWriter As TextWriter
Private outstrm As OutputStream
Private MyDir As String
Public ignore1708 As Boolean = True
Public ignore1939 As Boolean = False
' operator constants... (= , <>, >= , <=, On, Off, AND, OR, ...)
Private const EQL As Int = 0
Private const NEQL As Int = 1
Private const GTEQL As Int = 2
Private const LTEQL As Int = 3
Private const LTHAN As Int = 4
Private const GTHAN As Int = 5
Private const CON As Int = -1
Private const COFF As Int = -2
Private const CAND As Int = 1
Private const COR As Int = 2
Type zonemast (mastpk As String, tmap As Map)
End Sub
Sub Service_Create
mympp.Initialize
ECMmap.Initialize
tECMmap.Initialize
pgnmp.Initialize
mLst.Initialize
tempmap = ""
Mastlst.Initialize
MastEvent.Initialize
MyDir = File.DirDefaultExternal ' & "/ecmfiles/"
File.MakeDir(MyDir,"ecmfiles")
ecmtimer.Initialize("tim",rectick)
NativeMe.InitializeContext
C_DB
Log(" ...Is 1939 ignored?: "&ignore1939)
SetProtocol
UpdatePGNmap
BuildMastEventList
End Sub
Sub Service_Start(StartingIntent As Intent)
Log(" Ecm Service Started")
ecmtimer.Enabled = True
End Sub
' build a structure to use for event evaluation
'
Public Sub BuildMastEventList
EventMap.Initialize
Mastlst.Initialize
MastEvent.Initialize
MastEventNum.Initialize
DefCM.SQL1.ExecNonQuery("DROP TABLE IF EXISTS evtviols")
DefCM.SQL1.ExecNonQuery("CREATE TABLE IF NOT EXISTS evtviols ( mpk INTEGER, sdate INTEGER, edate INTEGER, evtviol TEXT, evtzone INTEGER, evttype INTEGER, drvid INTEGER, trknum TEXT, sodo TEXT, eodo TEXT, ival1 INTEGER, ival2 INTEGER, sval1 TEXT, sval2 TEXT, dval1 REAL, dval2 REAL, compid INTEGER, sent INTEGER, lat REAL, lon REAL, location TEXT )")
Try
Dim c, mst As Cursor
mst = DefCM.SQL1.ExecQuery("Select * from zonemast where stype = 5") ' type 5 are 'these' special events... type 1 and 2 are Geo-Zones
For i = 0 To mst.RowCount - 1
mst.Position = i
Dim mast As Map
mast.Initialize
Dim zm As zonemast ' create new Type
zm.Initialize
Dim mstid As Int = mst.GetInt("mastpk")
Dim mstevt As Map
mstevt.Initialize
For x = 0 To mst.ColumnCount -1
mstevt.Put( mst.GetColumnName(x), mst.GetString2(x) ) ' add data from table to a map for faster processing - avoid SQL each time...
Next
zm.mastpk = mstid 'set the types values - adding a map here
zm.tmap = mstevt
MastEvent.Add(zm) ' add these types to a list
MastEventNum.Add(mstid) ' add the mastID to a list for quick lookups ( Could I use the zm Type for this?)
Dim m As Map
m.Initialize
' I reuse a table already constructed for a different purpose
c = DefCM.SQL1.ExecQuery("Select * from zonedet where mastpk = "&mstid)
For j = 0 To c.RowCount - 1
c.Position = j
Dim dm As Map
dm.Initialize
dm.Put("pid", c.GetInt("lat"))
dm.Put("op", c.GetInt("lon"))
dm.Put("val", c.GetInt("s1"))
dm.Put("more",c.GetInt("s2"))
m.Put(c.GetInt("pk"), dm)
Next
c.Close
mast.Put(mstid,m)
Mastlst.Add(mast)
Next
mst.Close
' just for testing.... to be removed....
For i = 0 To MastEvent.Size-1
Dim zm As zonemast
zm.Initialize
zm = MastEvent.Get(i)
Log(" mastevent list: "&MastEventNum.Get(i)&CRLF&zm)
Next
Catch
Log(" blew up in BuildMastEventList "&LastException.Message)
End Try
End Sub
' this runs in a timer each second....
Private Sub ProcViols
Dim st, et, ms As Long
st = DateTime.Now
Dim res As Boolean = True
Try
For i = 0 To Mastlst.Size-1
Dim mstmap As Map ' create the master map from list of maps
mstmap.Initialize
mstmap = Mastlst.Get(i) ' get the current map from the list
For Each key As String In mstmap.Keys
Dim mastpk As String = key ' set the current mastpk (should be only 1)
Next
For j = 0 To mstmap.Size -1
res = True
For Each v As Map In mstmap.Values ' get a map from each master
For Each x As Map In v.Values ' get the map and process the values
'Log( "vars - PID: "&x.Get("pid")&" Operator: "&x.Get("op")&" Value: "&x.Get("val")&" And/Or: "& x.Get("more") )
Dim bd As Boolean = ProcessCurrPID(x.Get("pid") , x.Get("op"), x.Get("val"), x.Get("more") ) ' process each event
If bd = False Then
res = False ' if a condition fails - ALL params fail...
End If
Next
If res Then ' if ALL conditions passed
If EventMap.ContainsKey(mastpk) Then
' then the event was started already - need to check for end of this event
' res was inited - still in event... do nothing
Else
SaveEventStart(mastpk)
EventMap.Put(mastpk, res)
' build our viol record...
End If
Else ' when false
If EventMap.ContainsKey(mastpk) Then ' does the set contain this record when false?
UpdateEvent(mastpk) ' if so, update the event a write a record
End If
End If
Next
Next
Next
Catch
Log(" blew up in ProcViols: "&LastException.Message)
End Try
et = DateTime.Now
ms = et - st
Log(" ---------------- Time: (ms): "&ms) ' how long does the process take? generally 2 to 60 ms
End Sub
' determine if the passed pid meets the requirements ( ie: is this true? 61 >= 60 )
'
Private Sub ProcessCurrPID(pid As String, opr As Int, pidval As Double, more As Int) As Boolean
Dim ret As Boolean = False
Dim thispidval As Double
Try
Select pid
Case 2 ' this is a speed pid - determine if value is metric or US
Dim tpi As Double = ECMmap.Getdefault("2",0)
If DefCM.metric = False Then ' speed in MPH
thispidval = NumberFormat2(tpi * 0.62137,1,1,1,False)
' Log(" us miles pid: "&pid&" pidval value: "&NumberFormat2( thispidval,1,1,1,False) )
Else
thispidval = tpi
' Log(" KMS pid: "&pid&" pidval value: "& NumberFormat2( thispidval,1,1,1,False) )
End If
Case 16 ' this is a brake pid. Determine if it is ON - and change the value to numeric for processing
Dim tpid As String = ECMmap.Getdefault("16",-2)
If tpid.ToLowerCase = "on" Then
thispidval = CON
Else
thispidval = COFF
End If
Case 99 ' this is the ignition switch - change to numeric value
Dim tpidx As String = ECMmap.Getdefault("99",0)
If tpidx.ToLowerCase = "on" Then
thispidval = CON
Else
thispidval = COFF
End If
Case Else ' the default for all other pids - values are numeric...
thispidval = ECMmap.GetDefault(pid, -100)
End Select
' Log(" What is THIS pid "&pid& " and pidval: "& NumberFormat2( thispidval,1,1,1,False))
Select opr
Case EQL ' see constants
If thispidval = pidval Then ' equal to...
ret = True
End If
' Log(pid&" operator was = "&thispidval&" - "&pidval)
Case NEQL
If thispidval <> pidval Then ' NOT equal to...
ret = True
End If
' Log(pid&" operator was not <> "&thispidval&" - "&pidval)
Case GTEQL
If thispidval >= pidval Then
ret = True
End If
' Log(pid&" operator >= " &thispidval&" - "&pidval)
Case LTEQL
If thispidval <= pidval Then
ret = True
End If
' Log(pid&" operator was <= "&thispidval&" - "&pidval)
Case LTHAN
If thispidval < pidval Then
ret = True
End If
' Log(pid&" operator was < "&thispidval&" - "&pidval)
Case GTHAN
If thispidval > pidval Then
ret = True
End If
' Log(pid&" operator was > "&thispidval&" - "&pidval)
End Select
Catch
Log(" blew up in ProcessCurrPID "&LastException.Message)
End Try
Return ret
End Sub
Private Sub UpdateEvent(mpk As String)
Try
Dim zm As zonemast
zm.Initialize
Dim idx As Int
For i = 0 To MastEventNum.Size-1
If (mpk = MastEventNum.Get(i)) Then
idx = i
Exit
End If
Next
' idx = MastEventNum.IndexOf(idx) ' tried indexof - but it always returned -1
Log(" What is idx: "&idx&" Mastnum: "&MastEventNum& " mpk: "&idx)
zm = MastEvent.Get(idx) ' get the Type at the index
Dim m As Map
m.Initialize
m = zm.tmap ' get the map stored in type zm
Log(" What is zm type: "&zm)
Dim grace As Int = m.GetDefault("s1",0)
'Dim summ As Int = m.GetDefault("s3",0) ' to use later...
Dim c As Cursor
c = DefCM.SQL1.ExecQuery("Select * from evtviols where mpk = "&mpk)
If c.RowCount > 0 Then
c.Position = 0
Dim sd As Long = c.GetLong("sdate")
Dim grc As Long = (DateTime.Now - sd) / 1000
If grc > grace Then ' record an event is duration exceeds grace period allowed....
LogColor(" Recording violation "&mpk&" Time: "&grc&" Grace: "&grace, Colors.Blue)
Dim tmap As Map
tmap.Initialize
tmap.Put("sdate",c.GetLong("sdate"))
tmap.Put("edate",DateTime.Now)
tmap.Put("vspeed",grc)
tmap.Put("zone",mpk)
tmap.Put("event",5)
tmap.Put("trknum",DefCM.DefTrkNum)
tmap.Put("compid",DefCM.Company_id)
tmap.Put("drvid",DefCM.CDrv)
tmap.Put("sodo",c.GetString("sodo"))
tmap.Put("eodo",NumberFormat2(GPSServMod.ECMOdom,1,1,1,False))
tmap.Put("sourceid",DefCM.ShiftID)
tmap.Put("swver",DefCM.swver)
tmap.Put("comment","")
CallSubDelayed2(LogServmod,"RecordTheseEvents",tmap) ' make a record for reporting...
DefCM.SQL1.ExecNonQuery("DELETE FROM evtviols where mpk = "&mpk) ' allow a new record to start...
Else
LogColor(" NOT recording viol "&mpk&" Not Exceed Grace: "&grc, Colors.Red)
End If
Else
LogColor(" NOT recording viol "&mpk&" Not Found: ", Colors.Red)
End If
c.Close
Catch
Log(" blew up in UpdateEvent "&LastException.Message)
End Try
EventMap.Remove(mpk) ' remove the item from the map
End Sub
Private Sub SaveEventStart(mpk As String)
DefCM.SQL1.ExecNonQuery2("INSERT INTO evtviols (mpk,sdate,edate,evtviol,evtzone,evttype,drvid,trknum,sodo,eodo,compid,sent,lat,lon,location) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", _
Array As Object(mpk, DateTime.Now, 0, 0, mpk , 5, DefCM.CDrv,DefCM.DefTrkNum, NumberFormat2( GPSServMod.ECMOdom,1,1,1,False) , 0,0, 0, GPSServMod.gLoc.fLatitude, GPSServMod.gLoc.fLongitude, DefCM.CurrLoc) )
' just for testing... to be removed....
Dim c As Cursor
c = DefCM.SQL1.ExecQuery("Select * from evtviols")
If c.RowCount > 0 Then
For i = 0 To c.RowCount -1
c.Position = i
Log(" viol recs: "&c.GetString("mpk")&" "&c.GetString("sdate"))
Next
End If
c.Close
End Sub