Register alpha.0 B4A prototype

This commit is contained in:
2026-06-12 15:11:26 +02:00
commit c97a49cad7
12 changed files with 768 additions and 0 deletions

470
swarm.b4a Normal file
View File

@@ -0,0 +1,470 @@
Build1=Default,anywheresoftware.b4a.swarm
File1=drp.bal
File2=swarm.bal
FileGroup1=Default Group
FileGroup2=Default Group
Group=Default Group
Library1=core
Library2=phone
Library3=reflection
Library4=tts
Library5=json
Library6=okhttputils2
ManifestCode='This code will be applied to the manifest file during compilation.~\n~'You do not need to modify it in most cases.~\n~'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136~\n~AddManifestText(~\n~<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="28"/>~\n~<supports-screens android:largeScreens="true" ~\n~ android:normalScreens="true" ~\n~ android:smallScreens="true" ~\n~ android:anyDensity="true"/>)~\n~SetApplicationAttribute(android:icon, "@drawable/icon")~\n~SetApplicationAttribute(android:label, "$LABEL$")~\n~CreateResourceFromFile(Macro, Themes.LightTheme)~\n~'End of default text.~\n~
Module1=droplist
NumberOfFiles=2
NumberOfLibraries=6
NumberOfModules=1
Version=13
@EndOfDesignText@
#Region Module Attributes
#FullScreen: True
#IncludeTitle: False
#ApplicationLabel: Swarm
#VersionCode: 1
#VersionName: 1.0
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
'#AdditionalJar: jtds-1.3.1.jar
'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim VRStd As VoiceRecognition
Dim TTSStd As TTS
Dim VRImp As VoiceRecognition
Dim TTSImp As TTS
Private Const API_ENDPOINT As String = "https://example.com/api/processes"
Private Const EVENT_TYPE_STANDARD As String = "standard"
Private Const EVENT_TYPE_UNEXPECTED As String = "unexpected"
Private CurrentProcessId As String
Private ProcessStartTime As Long
Private ProcessStartedAt As String
Private Events As List
Private ProcessActive As Boolean
Private crono As Timer
crono.Initialize("CronoClock", 1000)
End Sub
Sub CronoClock_Tick
'Handle tick events
tempoTrascorso = tempoTrascorso +1
TimeElapsed.Text= "Secondi trascorsi " & tempoTrascorso & " sec"
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim pnl1 As Panel
Dim dlist As droplist
Dim tempoTrascorso As Long
Private BtnRecImp As Button
Private BtnRecStd As Button
Private BtnSend As Button
Private InputImp As EditText
Private InputStd As EditText
Private LblEvnt As Label
Private StartStopTog As ToggleButton
Private TimeElapsed As Label
Private pressed As Long
Private IgnoreToggleEvent As Boolean
Private EventList As ListView
Private SelectedEventIndex As Int
Private RerecordEventIndex As Int
tempoTrascorso=0
SelectedEventIndex = -1
RerecordEventIndex = -1
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
VRStd.Initialize("VRStd")
TTSStd.Initialize("TTSStd")
VRImp.Initialize("VRImp")
TTSImp.Initialize("TTSImp")
Events.Initialize
End If
Activity.LoadLayout("swarm")
dlist.Initialize(pnl1, "Operatore:", "DropSelect")
dlist.dlItems.Add("Beppe")
dlist.dlItems.Add("George")
dlist.dlItems.Add("Cristina")
dlist.dlItems.Add("Erica")
dlist.dlItems.Add("Alfonso")
' If VR.IsSupported Then
' ToastMessageShow("Voice recognition is supported.", False)
' Else
' ToastMessageShow("Voice recognition is not supported.", True)
' End If
BtnRecImp.SetTextColorAnimated(0, Colors.Black)
BtnRecImp.Text="Rec imp"
BtnRecStd.SetTextColorAnimated(0, Colors.Black)
BtnRecStd.Text="Rec std"
BtnSend.SetTextColorAnimated(0, Colors.Black)
BtnSend.Text="Invia"
SetupEventList
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub DropSelect
Log("** You Selected " & dlist.selVal & ", at " & dlist.selPos & " **")
End Sub
Private Sub StartStopTog_CheckedChange(Checked As Boolean)
If IgnoreToggleEvent Then Return
Log("** You Selected: " & StartStopTog.Checked)
If StartStopTog.Checked Then
Log("** Start counting " & StartStopTog.Checked)
StartProcess
Else
Log("** Stop counting " & StartStopTog.Checked)
StopProcess(False)
End If
'Dim pressedTime As Long = DateTime.Now - pressed
'TimeElapsed.Text = pressedTime & "s"
End Sub
Private Sub BtnRecStd_Click
If ProcessActive = False Then
ToastMessageShow("Avvia prima il processo.", False)
Return
End If
VRStd.Prompt = "Fai il tuo sproloquio per un evento standard!"
VRStd.Listen 'calls the voice recognition external activity
End Sub
Private Sub BtnRecImp_Click
If ProcessActive = False Then
ToastMessageShow("Avvia prima il processo.", False)
Return
End If
VRImp.Prompt = "Descrivi l'evento imprevisto!"
VRImp.Listen 'calls the voice recognition external activity
End Sub
Sub VRStd_Result (Success As Boolean, Texts As List)
If Success = False Then
RerecordEventIndex = -1
Return
End If
If Texts.IsInitialized = False Then
RerecordEventIndex = -1
Return
End If
If Texts.Size = 0 Then
RerecordEventIndex = -1
Return
End If
Dim description As String = Texts.Get(0)
Log("** You said: " & description)
InputStd.Text=description
TTSStd.Speak(description, True)
SaveVoiceDescription(EVENT_TYPE_STANDARD, description)
End Sub
Sub VRImp_Result (Success As Boolean, Texts As List)
If Success = False Then
RerecordEventIndex = -1
Return
End If
If Texts.IsInitialized = False Then
RerecordEventIndex = -1
Return
End If
If Texts.Size = 0 Then
RerecordEventIndex = -1
Return
End If
Dim description As String = Texts.Get(0)
Log("** You said: " & description)
InputImp.Text=description
TTSImp.Speak(description, True)
SaveVoiceDescription(EVENT_TYPE_UNEXPECTED, description)
End Sub
Private Sub BtnSend_Click
Log("** You clicked: " & " INVIATO" )
SendProcess
End Sub
Private Sub StartProcess
If dlist.selPos < 0 Then
ToastMessageShow("Seleziona prima l'operatore.", True)
IgnoreToggleEvent = True
StartStopTog.Checked = False
IgnoreToggleEvent = False
Return
End If
CurrentProcessId = "SWARM-" & DateTime.Now
ProcessStartTime = DateTime.Now
ProcessStartedAt = FormatApiDate(ProcessStartTime)
tempoTrascorso = 0
Events.Initialize
SelectedEventIndex = -1
RerecordEventIndex = -1
ProcessActive = True
crono.Enabled = True
TimeElapsed.Text= "Secondi trascorsi 0 sec"
LblEvnt.Text = "Processo avviato"
RefreshEventList
End Sub
Private Sub StopProcess(ClearData As Boolean)
crono.Enabled = False
ProcessActive = False
If ClearData Then
CurrentProcessId = ""
ProcessStartTime = 0
ProcessStartedAt = ""
tempoTrascorso = 0
Events.Initialize
SelectedEventIndex = -1
RerecordEventIndex = -1
TimeElapsed.Text= " 0"
LblEvnt.Text = ""
RefreshEventList
Else
LblEvnt.Text = "Processo fermato - eventi: " & Events.Size
End If
End Sub
Private Sub SaveVoiceDescription(EventType As String, Description As String)
If RerecordEventIndex >= 0 Then
UpdateEventDescription(RerecordEventIndex, Description)
RerecordEventIndex = -1
Else
RegisterEvent(EventType, Description)
End If
End Sub
Private Sub RegisterEvent(EventType As String, Description As String)
If Events.IsInitialized = False Then Events.Initialize
Dim evt As Map
evt.Initialize
evt.Put("sequence", Events.Size + 1)
evt.Put("type", EventType)
evt.Put("description", Description)
evt.Put("elapsedSeconds", tempoTrascorso)
evt.Put("timestamp", FormatApiDate(DateTime.Now))
Events.Add(evt)
SelectedEventIndex = -1
LblEvnt.Text = "Eventi registrati: " & Events.Size
RefreshEventList
End Sub
Private Sub UpdateEventDescription(Index As Int, Description As String)
If Events.IsInitialized = False Then Return
If Index < 0 Or Index >= Events.Size Then Return
Dim evt As Map = Events.Get(Index)
evt.Put("description", Description)
evt.Put("updatedAt", FormatApiDate(DateTime.Now))
Events.Set(Index, evt)
SelectedEventIndex = -1
LblEvnt.Text = "Record aggiornato: " & (Index + 1)
RefreshEventList
End Sub
Private Sub SendProcess
If CurrentProcessId = "" Then
ToastMessageShow("Nessun processo da inviare.", True)
Return
End If
If Events.IsInitialized = False Then
ToastMessageShow("Registra almeno un evento prima di inviare.", True)
Return
End If
If Events.Size = 0 Then
ToastMessageShow("Registra almeno un evento prima di inviare.", True)
Return
End If
crono.Enabled = False
ProcessActive = False
Dim payload As Map
payload.Initialize
payload.Put("processId", CurrentProcessId)
payload.Put("operator", dlist.selVal)
payload.Put("startedAt", ProcessStartedAt)
payload.Put("endedAt", FormatApiDate(DateTime.Now))
payload.Put("durationSeconds", tempoTrascorso)
payload.Put("events", Events)
Dim json As JSONGenerator
json.Initialize(payload)
Dim job As HttpJob
job.Initialize("SendProcess", Me)
job.PostString(API_ENDPOINT, json.ToString)
job.GetRequest.SetContentType("application/json")
LblEvnt.Text = "Invio in corso..."
End Sub
Sub JobDone(Job As HttpJob)
If Job.JobName = "SendProcess" Then
If Job.Success Then
Log("** API response: " & Job.GetString)
ToastMessageShow("Processo inviato.", False)
IgnoreToggleEvent = True
StartStopTog.Checked = False
IgnoreToggleEvent = False
StopProcess(True)
Else
Log("** API error: " & Job.ErrorMessage)
ToastMessageShow("Errore invio: " & Job.ErrorMessage, True)
LblEvnt.Text = "Invio non riuscito - eventi: " & Events.Size
End If
End If
Job.Release
End Sub
Private Sub FormatApiDate(Ticks As Long) As String
Return DateTime.GetYear(Ticks) & "-" & TwoDigits(DateTime.GetMonth(Ticks)) & "-" & TwoDigits(DateTime.GetDayOfMonth(Ticks)) & "T" & TwoDigits(DateTime.GetHour(Ticks)) & ":" & TwoDigits(DateTime.GetMinute(Ticks)) & ":" & TwoDigits(DateTime.GetSecond(Ticks))
End Sub
Private Sub TwoDigits(Value As Int) As String
If Value < 10 Then Return "0" & Value
Return "" & Value
End Sub
Private Sub SetupEventList
EventList.Initialize("EventList")
Activity.AddView(EventList, 8dip, Activity.Height - 170dip, Activity.Width - 16dip, 160dip)
EventList.SingleLineLayout.Label.TextSize = 14
EventList.SingleLineLayout.Label.TextColor = Colors.Black
EventList.SingleLineLayout.ItemHeight = 48dip
RefreshEventList
End Sub
Private Sub RefreshEventList
If EventList.IsInitialized = False Then Return
EventList.Clear
If Events.IsInitialized = False Then Return
For i = 0 To Events.Size - 1
Dim evt As Map = Events.Get(i)
Dim description As String = evt.Get("description")
If description.Length > 45 Then description = description.SubString2(0, 45) & "..."
EventList.AddSingleLine((i + 1) & ". [" & evt.Get("elapsedSeconds") & "s] " & evt.Get("type") & ": " & description)
Next
End Sub
Private Sub EventList_ItemClick (Position As Int, Value As Object)
If Events.IsInitialized = False Then Return
If Position < 0 Or Position >= Events.Size Then Return
SelectedEventIndex = Position
RerecordEventIndex = Position
Dim evt As Map = Events.Get(Position)
Dim eventType As String = evt.Get("type")
LblEvnt.Text = "Riregistra record " & (Position + 1)
If eventType = EVENT_TYPE_UNEXPECTED Then
VRImp.Prompt = "Riregistra la descrizione dell'evento imprevisto."
VRImp.Listen
Else
VRStd.Prompt = "Riregistra la descrizione dell'evento standard."
VRStd.Listen
End If
End Sub
Sub GetAllTexts (Act As Activity)
Dim lbl As Label
Dim txt As EditText
For i = 0 To Act.NumberOfViews - 1
If Act.GetView(i) Is Label Then
lbl = Act.GetView(i)
Log(lbl.Text)
End If
' If Activity.GetView(i) Is EditText Then
' txt= Activity.GetView(i)
' Log(lbl.Text)
' End If
If Act.GetView(i) Is Panel Then
Dim pan As Panel
pan = Act.GetView(i)
'lbl = Act.GetView(i)
'GetAllTexts(pan)
For j = 0 To pan.NumberOfViews - 1
Dim lbl As Label
Dim txt As EditText
If pan.GetView(j) Is Label Then
lbl = pan.GetView(j)
Log(lbl.Text)
End If
Next
End If
Next
End Sub