﻿Type=Service
Version=1.90
StartAtBoot=False
@EndOfDesignText@
'Service module
Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.
Dim timerservice As Timer
Dim MainRequestID3 As Int :MainRequestID3 = 5
Dim Parser4 As SaxParser
Dim tiempos As List 
Dim seconds1 As String 
Dim setflag As Boolean :setflag = False 
Dim countdown As Timer 
Dim minuteslist As List 

End Sub




Sub Service_Create
timerservice.Initialize("TimerService", 30000)
Parser4.Initialize 
Schedule.times.Clear 
tiempos.Initialize 
minuteslist.Initialize 
setflag = False
End Sub

Sub Service_Start (StartingIntent As Intent)
timerservice.Enabled = True 
End Sub

Sub Service_Destroy

End Sub

Sub TimerService_Tick
StartServiceAt("", DateTime.Now, True)
Schedule.hc3.Execute(Schedule.req3, 2)
Log("Starting Service")
End Sub

Sub hc3_ResponseSuccess(Response As HttpResponse, TaskId As Int)
If TaskId = MainRequestID3 Then
    Response.GetAsynchronously("GetNotify",	File.OpenOutput(File.DirDefaultExternal, "GetNotify.xml", False), True, MainRequestID3)
	Else
	 Response.GetAsynchronously("GetNotify",	File.OpenOutput(File.DirDefaultExternal, "GetNotify1.xml", False), True, TaskId)
	End If
	

End Sub

Sub hc3_ResponseError(Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)

    If Response <> Null Then

       Msgbox("Error: " & Response.GetString("UTF8"), "Connection Error")

							Response.Release
 
    End If
	
End Sub

Sub GetNotify_StreamFinish(Success As Boolean, TaskId As Int)
	If Success = False Then
		
							Msgbox(LastException.Message, "Error")
       Return
	
	   End If
	   
	   Dim in As InputStream
	   Dim minutes As String
	   Dim decimal As Int 
	   Dim seconds As String 
	   Dim intervaltime As Int 
	   	Dim difference As Int 
				Dim difference1 As Int
				Dim totaldifference As Int
				Dim multiple As Int 
				Dim base As Int :base = 60
	   
	   Dim schedulelist As Int 

				in = File.OpenInput(File.DirDefaultExternal, "GetNotify1.xml")
	   			Parser4.Parse(in, "Parser4")
				in.Close
				If Schedule.flag = True Then
				For i = 0 To tiempos.Size - 1
				minutes = tiempos.Get(i) / 60
				seconds = tiempos.Get(i) Mod 60
				decimal = minutes.IndexOf(".")
				If decimal > -1 AND seconds1 > 60 Then
				minutes = minutes.SubString2(0, decimal)
				minuteslist.Add(minutes)
				Else
				minutes = tiempos.Get(i) / 60
				seconds = tiempos.Get(i) Mod 60
				minuteslist.Add(minutes)
				End If

				Next
				
				
				
				
				Log(minuteslist.Get(Schedule.itemhold))
				intervaltime = Schedule.itempicked 
				multiple = intervaltime * 60
				
				If Schedule.itemhold < 0 Then
				Schedule.hc3.Execute(Schedule.req3, 2)
				Else
				If setflag = False Then  'if setflag false initialize countdown timer
				difference = minuteslist.Get(Schedule.itemhold)
				difference1 = Schedule.itempicked
				totaldifference = difference - difference1
				countdown.Initialize("countdown", multiple * 1000)
				countdown.Enabled = True 
				setflag = True 'set setflag to true so that the countdowntimer won't run everytime only when needed.
				End If
				End If
				Log("interval " & Schedule.itempicked)
				Schedule.flag = False
				tiempos.Clear		
	End If
	   
    
End Sub


Sub countdown_tick
Log("countdown initialized")
		CallSub(Schedule, "NotifyArrival")
		StopService("")
countdown.Enabled = False
End Sub

Sub Parser4_StartElement(Uri As String, Name As String, Attributes As Attributes)

If Name = "predictions" Then
	Schedule.routenumber = (Attributes.GetValue(2))
End If
	


If Name = "direction" Then 'AND Attributes.GetValue(0) = Direction.busdirection Then
Schedule.busdirection1.Add("BusDirection" & "~" & Attributes.GetValue(0))  'Add all directions to busdirection1 list
'Log(Schedule.busdirection1)
Schedule.samedirection = Attributes.GetValue(0)
End If

Dim attributelist As List
attributelist.Initialize
Dim attributesize As Int 
Dim timesize As Int 

If Name = "prediction" Then
attributesize = Attributes.Size 
Schedule.seconds1 = Attributes.GetValue(1)   'add all the times of all directions to the seconds1 list
'Log("attribute 1 " & Attributes.GetValue(1))
seconds1 = Attributes.GetValue(1)
tiempos.Add(Attributes.GetValue(1))
If attributesize = 9 Then
Schedule.VehicleID.Add(Schedule.seconds1 & "~" & Attributes.GetValue(6))
Schedule.TripTag.Add(Attributes.GetValue(8))
'Log("attribute 8 " & Attributes.GetValue(8))
Schedule.flag = True
'setflag = True
Schedule.times.Add(Schedule.samedirection & "~" & Schedule.seconds1 & "~" & Schedule.routenumber)
Else
Schedule.VehicleID.Add(Schedule.seconds1 & "~" & Attributes.GetValue(5))
Schedule.TripTag.Add(Attributes.GetValue(7))
Schedule.flag = True
'setflag = True
Schedule.times.Add(Schedule.samedirection & "~" & Schedule.seconds1 & "~" & Schedule.routenumber)
End If
End If
timesize = Schedule.times.Size 


End Sub

Sub Parser4_EndElement(Uri As String, Name As String, Text As StringBuilder)

End Sub
