B4J Question Classes, const and code modules

Pablocao

Member
Hello people!
Im working on a project which have no classes, just code modules, and it is constanly reciving updates for changes in firmwares of the devices i connect to manage its parameters and configuration via commands in a data stream (serial com via teraterm).
My question is if it is a good idea to make an Object call "Device" that have its commands defined in const variables with getters, setters and other kind of functions.

Here is a example code as it is "working" when sending commands :

Main Module.
This function sends the commands
Main:
Public Sub comSends (s As String)
    s = $"${s}${Chr(13)}"$
    If portOpen Then
        astream.Write(s.GetBytes("UTF8"))
    End If
    reply = ""
End Sub

'-----..............'

maq Module.
this module is a STATE machine, where you send these commands, e.x:

maq:
Main.comSends("/usrdata/bin/**** ***** get_firmware_version")

This is used way many times and in different stages so im trying to make the code more readable and easier to update when the commands change or anything in the code you have to modify is easier to localize, but i dunno if it worst performance and if this is the way to do it:

Ive created a class call Device which looks, for now, like this, and its growing more:

Device:
'Class Device module
Sub Class_Globals
    Private fx As JFX
  
    '--------------------------------------- Device Commands ---------------------------------------'
    '------------ Getters ------------'
    Private const gIMEI As String = "/usardata/bin/zbox modem get_imei"                                                            '-- get IMEI
    Private const gModel As String = "zbox -v"                                                                                                      '-- get Modelo
    Private const gFirmware As String = "/usrdata/bin/zbox registry get_firmware_version"                                            '-- get Firmware
    Private const gSerialNumber As String = "/usrdata/bin/zbox registry get_device_serial_number"                                    '-- get Serial Number
    Private const gAPN As String = "zbox wuci wan.@sim[0].apn"                                                                        '-- get APN
    Private const gAPNUser As String = "zbox wuci wan.@sim[0].user"                                                                    '-- get APN User
    Private const gAPNPassword As String = "zbox wuci wan.@sim[0].password"                                                            '-- get APN Password
    Private const gAtCREG As String = "zbox modem sendat AT+CREG?"                                                                    '-- get AT+CREG
    Private const gAtQENG As String = "zbox modem sendat 'AT+QENG='servingcell"                                                        '-- get AT+QENG
    Private const gAtQCCID As String = "zbox modem sendat AT+QCCID"                                                                    '-- get AT+QCCID
    Private const gIpSim As String = "ifconfig rmnet_data0 | grep 'inet addr'"                                                        '-- get IP Sim
  
    '------------ Setters ------------'
    Private const sSerialNumber As String = "/usrdata/bin/zbox registry set_device_serial_number"                            '-- set Serial Number
    Private const sUNESA As String = "/usrdata/bin/zbox wuci /usrdata/parameters registry.@device[0].serial_number_unesa="    '-- set UNESA
    Private const sAPN As String = "zbox wuci wan.@sim[0].apn="                                                                '-- set APN
    Private const sAPNUser As String = "zbox wuci wan.@sim[0].user="                                                            '-- set APN User
    Private const sAPNPassword As String = "zbox wuci wan.@sim[0].password="                                                    '-- set APN Password
    Private const sBand As String = "zbox wuci wan.@sim[0].band="                                                            '-- set Band
    Private const sAuth As String = "zbox wuci wan.@sim[0].auth="                                                            '-- set Auth
    Private const sBaudRate As String = "zbox wuci wan.@sim[0].baudrate="                                                    '-- set BaudRate
    Private const sParity As Stringr = "zbox wuci wan.@sim[0].parity="                                                        '-- set Parity
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
  
End Sub

Public Sub GetIMEI As Int
    Return gIMEI
End Sub


So when i send the command it would looks like this:

maq:
Dim dev As Device
dev.Initialize
Main.comSends(dev.GetIMEI)

And if i need to change the command just changing the constant in its class would change it anywhere in the code, BUT im confuse because usually a "getter" from an object would give you the value.

Can anyone tell me if this worth? or should i just make a Code Module for this case? or add in the function "GetIMEI" the function "comSends" so get the reply from the device and store the result this way? Hope you can give me some feedback and that is clear my confusion.

Thank you!
 
Last edited:

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
There is a small bug in your code.

gIMEI is a string yet GetIMEI returns an int.


B4X:
Public Sub GetIMEI As String ' Changed from int
    Return gIMEI
End Sub
 
Upvote 0

Pablocao

Member
i think it is not clear my question in this topic, my english is... you know.
When i use GetIMEI, it is not trully giving me the "IMEI from the object device", it is sending the command which will give the IMEI after sending it via the function "comSends" and then the streamdata is listenning and waiting the reply. This is the function which give you the reply (which btw, can be wrong or corrapted):


B4X:
Private Sub AStream_NewData (b() As Byte)
    Dim bc As ByteConverter
    If Not(comBinary) Then
        Dim s As String = BytesToString(b, 0, b.Length, "UTF8")
        reply = reply & s
        syslogAppend(s)
    Else
        syslogAppend("RX: " & bc.HexFromBytes(b) & CRLF)
    End If
End Sub

and thats what im trying to figure out, is it ok as im doing it, or should i add some logic to GetIMEI so it will give me the reply after sending the command and storage it in a variable from the object? example:

B4X:
Sub Class_Globals
    Private fx As JFX
    
    Private deviceIMEI As String
    '-------------------------------------- Comandos para AxomQ4 --------------------------------------'
    
    '------------ Getters ------------'
    Private const gIMEI As String = "/usardata/bin/zbox modem get_imei"       
    '......'

End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    
End Sub

Public Sub GetIMEI As String
    Main.comSends(gIMEI)
    Return Main.reply
End Sub

Calling "Main.comSends(gIMEI)" and "Main.reply" seems wrong for me... as comSends doesnt return a string, and main.reply depends of a given milliseconds from "Sub AStream_NewData" to give a value to "reply"
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
I think I would do thins using callbacks.


B4X:
public sub setCallback(cb as object, evt as string)
callback = cb
event = evt
end sub

Public Sub comSends (s As String)
    s = $"${s}${Chr(13)}"$
    If portOpen Then
        astream.Write(s.GetBytes("UTF8"))
    End If
    reply = ""
End Sub


Private Sub AStream_NewData (b() As Byte)
    Dim bc As ByteConverter
    If Not(comBinary) Then
        Dim s As String = BytesToString(b, 0, b.Length, "UTF8")
        reply = reply & s
        syslogAppend(s)
    callsubdelayed(callback,event,s)
    Else
        syslogAppend("RX: " & bc.HexFromBytes(b) & CRLF)
    End If
End Sub

then

B4X:
Dim dev As Device
dev.Initialize
dev.setcallback(me,"data_received")
Main.comSends(dev.GetIMEI)

....

sub data_received(s as string)
Log($"Get data : ${s}"$)
end sub
 
Upvote 0
Top