User Tools

Site Tools


doc:lua_make-your-own-scripts

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
doc:lua_make-your-own-scripts [2014/10/13 18:22] admindoc:lua_make-your-own-scripts [2019/04/08 18:43] (current) – [The IO Commands] admin
Line 136: Line 136:
  
  
-==== The Communication  Commands ====+==== The Communication Commands ====
  
  
Line 186: Line 186:
  
  
-=== ioInput(file name ,file_extension,message) ===+=== ioInput(file_name ,file_extension,message) ===
  
  
Line 192: Line 192:
  
  
-^  filename  ^  message  ^  Effect   | +^  filename  ^  message  ^  Effect 
-|/unix-path/.. |any message |Open a file Open dialog with preselected path (and file). At success returns the file path, otherwise null |  +|/unix-path/.. |any message |Open a file Open dialog with preselected path (and file). At success returns the file path, otherwise null | 
-|/unix-path/.. |'direct' |Open the given file without Dialog, At success returns the file path, otherwise null  | +|/unix-path/.. |'direct' |Open the given file without Dialog, At success returns the file path, otherwise null  \\ actual either complete path are supported, relative paths or filenames only. Filenames and relative paths are relative to the actual script directory 
-|URL like ''HTTP:<nowiki>//</nowiki>''.. |'html' |Reads the URL as input.At  success returns the URL, otherwise null |+|URL like ''HTTP:<nowiki>//</nowiki>''.. |'html' |Reads the URL as input.At  success returns the URL, otherwise null | 
-|URL like ''HTTP:<nowiki>//</nowiki>''.. |a lua table |JSON-RFC call: If the parameter message is a lua table, it's translated into a JSON string and send as POST to the given URL. After evaluation, the server returns a JSON string converted into a lua table and returned by the function |  |+|URL like ''HTTP:<nowiki>//</nowiki>''.. or ''file:<nowiki>//</nowiki>''..|'json' |JSON-RFC call: The parameter ''file_extension'' can be a lua table or a JSON string, in case of a lua table, it's translated into a JSON string and send as POST to the given URL. In case of a file:<nowiki>//</nowiki>- URL, the local file is read as a JSON file |
  
 +
 +After opening the file with IOInput(), it can be read with ioRead()
  
 === ioRead("%%*%%line") === === ioRead("%%*%%line") ===
  
  
-Read one line of the input file ending with either %%\%%r%%\%%n or %%\%%n as string without the EOL. In case of input EOF or read errors the function returns nil +Read one line of the input file ending with either<nowiki> \</nowiki>r<nowiki>\</nowiki>n or<nowiki> \</nowiki>n as string without the EOL. In case of input EOF or read errors the function returns nil. By this the whole file can be read line per line
- +
 === ioRead ("%%*%%all") === === ioRead ("%%*%%all") ===
  
Line 211: Line 211:
  
  
-=== ioRead (count) ===+=== ioRead ("%%*%%json") ===
  
  
-Read count number of bytes as string+Here it's assumed that the input file consists of a JSON string containing data. This string is translated into a lua table structure and returned
  
  
-=== ioOutput(URL[,append[,message]]) ===+=== ioRead ("%%*%%sha256") ===
  
  
-Try to opens ''URL'' as outputA ''SCRIPT:<nowiki>//</nowiki>'' URL is readonly and so not allowed as output. If append is TRUE, the output is appended to an already existing file. If the given file path is empty or just a directory, a file dialog opens. If given, the message is used in the dialog as title +This mainly for testing purposesIt returns the sha256 checksum of that file
-=== ioWrite (String) ===+
  
  
-Attaches the string content to the output file+\\
  
 +==== The Buffer Commands ====
  
  
-==== loadbuffer ==== +FIXME this whole Buffer section is not implemented and actual just here as a reference for discussion
-  loadbuffer(start: longint; VAR filelen: longint;  VAR filename: String; VAR err: integer)+
  
-Reads the file "filename" into the telegram buffer starting at position "start" by reading "filelen" number of bytes 
  
-The number of bytes read will be returned in "len".+=== loadbuffer === 
 +  len, newfilename= loadbuffer(start , filelen , file_name , file_extension , message) 
 + 
 +Reads the file "file_name" into the telegram buffer starting at position "start" by reading "filelen" number of bytes. 
 + 
 +The data source can be defined as explained for the ioInput command. 
 + 
 +The number of bytes read will be returned in "len", a negative value means a load error
  
 The following conditions apply: The following conditions apply:
-  * If the filename is "" (empty), then a file selection window will be opened, from which a file can be selected. The selected filename will be returned in "filename"+  * The selected filename will be returned in "filename"
-  * If len is 0, the whole file will be read. If the length exceeds the telegram length, an error will be raised.+  * If filename is 0, the whole file will be read. If the length exceeds the telegram length, an error will be raised.
  
  
  
  
-==== savebuffer ==== +=== savebuffer === 
-  savebuffer(start: longint; VAR filelen: longint;  VAR filename: String; VAR err: integer)+  len, newfilename = savebuffer(start filelen , file_name , file_extension , message)
  
 Writes the telegram buffer starting at position "start" by reading "filelen" number of bytes into the file "filename" Writes the telegram buffer starting at position "start" by reading "filelen" number of bytes into the file "filename"
  
-The number of bytes written will be returned in "len".+The number of bytes written will be returned in "len", a negative value means a load error
  
 The following conditions apply: The following conditions apply:
-  * If the filename is "" (empty), then a file selection window will be opened, from which a file can be selected. The selected file name will be returned in "filename".+  * The selected file name will be returned in "filename".
   * If len is 0, the whole telegram buffer will be written.    * If len is 0, the whole telegram buffer will be written. 
  
-==== setbuffer ==== +=== setbuffer === 
-  setBuffer(bufferNr : byte; newSize: longint)+  setBuffer(bufferNr newSize )
  
-Changes the actual buffer used to buffer number "buffernr"SKDS supports 10 buffers, counted from 0 to 9. The startup buffer is nr. 0.+Changes the actual buffer used to buffer number "buffernr"OOBD supports 10 buffers, counted from 0 to 9. The startup buffer is nr. 0.
  
-If newsize is <> 0, the old buffer is deleted and new memory with size newmem is allocated. The maximal memsize is 2^31 = 2147483648 bytes+If newsize is <> 0, the old buffer is deleted and new memory with size newmem is allocated
  
  
-==== copyBuffer ==== +=== copyBuffer === 
-  copyBuffer(bufferNr : byte)+  copyBuffer(bufferNr )
  
-Copies the content of buffer nr. "bufferNr" into the actual buffer.+Copies the content of buffer  "bufferNr" into the actual buffer.
  
  
  
  
-==== BlitBuffer ==== +=== BlitBuffer === 
-  BlitBuffer(frombuffer: byte; startpos: longint; topos: longint; blocklen: longint )+  BlitBuffer(frombuffer startpos topos blocklen  )
  
 Copies a memory block  from the buffer "frombuffer" starting at position "startpos" to the actual buffer to position "topos" with the length of "blocklen" bytes. Copies a memory block  from the buffer "frombuffer" starting at position "startpos" to the actual buffer to position "topos" with the length of "blocklen" bytes.
Line 276: Line 281:
 In case the buffer len needs to be bigger, the buffer len is increased accourdingly. In case the buffer len needs to be bigger, the buffer len is increased accourdingly.
  
-==== SetBufferLen ====+=== SetBufferLen ===
  
  
-  SetBufferLen(VAR newSize: longint)+ newSize= SetBufferLen( newSize)
  
 All other buffer commands can increase the 'len' of a buffer, but none of them can make a buffer shorter, except SetBufferLen.  All other buffer commands can increase the 'len' of a buffer, but none of them can make a buffer shorter, except SetBufferLen. 
Line 285: Line 290:
 SetBufferLen sets the 'len' of the current Buffer to 'newSize'. SetBufferLen sets the 'len' of the current Buffer to 'newSize'.
  
-The success of the SetBufferLen - operation is returned in the variable newSize itself as follows:+The success of the SetBufferLen - operation is returned as function result as follows:
  
-^  input value of newSize  ^  return value of newSize  ^+^  input value of newSize  ^  return value   ^
 |  < = 0                    | available size of the buffer in bytes. This can be used to read the real allocated memory size of that buffer  | |  < = 0                    | available size of the buffer in bytes. This can be used to read the real allocated memory size of that buffer  |
 |  1.. available size      | new available size (= requested size)  | |  1.. available size      | new available size (= requested size)  |
Line 295: Line 300:
  
  
-====  Miscellaneous  Commands ====+=== SendBuffer === 
 +Sends the actual buffer
  
-=== serDisplayWrite(String) === 
  
  
-Writes //String// to the build in output console.+==== WriteString Command Syntax ==== 
 + 
 +As default the command  
 + 
 +  serDisplayWrite(String) 
 + 
 + 
 +writes //String// to the build in output console. But with an optional secound parameter as command this behavior can be changed: 
 + 
 +  serDisplayWrite(parameter, command) 
 + 
 +The different commands have the following effects: 
 + 
 +^ parameter  ^ command  ^ Function 
 +| buffername  | setbuffer  | redirects the following DisplayWrite()'s output into buffer "bufferName". If the buffer does not exists, it's automatically generated \\ The default output window buffername, which is also active at start, is //display// To write some output there after using some other buffers before, set buffer back to //display// | 
 +| -           | clear      | clears actual buffer content | 
 +| -           | clearall   | clears all buffers. Senseful at start of scripts, if wanted, as the buffers contain their contents between scripts runs | 
 +| filename    | save       | saves actual buffer to filename without further asking 
 +| filename    | saveas       | saves actual buffer to filename by let the user first confirm the filename 
 +| filename    | append       | appends actual buffer to filename without further asking 
 +| filename    | appendas       | appends actual buffer to filename by let the user first confirm the filename 
 + 
 + 
 + 
 + 
 +==== Miscellaneous  Commands ==== 
  
 === dbLookup(db-File , searchstring) === === dbLookup(db-File , searchstring) ===
  
-Searches in the //db-file// for all entries with index //searchstring//. The //db-file// needs to be in the same directory as the Lua- script itself. The //db-file// itself is made by [[tools_oodbcreate|oodbCreate]].+ 
 +Searches in the //db-file// for all entries with index //searchstring//. The //db-file// needs to be in the same directory as the Lua- script itself. The //db-file// itself is made by [[:doc:tools_oodbcreate|oodbCreate]]. 
  
 dbLookup() returns a Lua table dbLookup() returns a Lua table
-   + 
-  myTable = dbLookup("dtc.oodb", "0815")+ 
 +<code> 
 +myTable = dbLookup("dtc.oodb", "0815") 
 +</code> 
  
 //myTable.len// tells the success status: //myTable.len// tells the success status:
-  * if //myTable.len// < 0 then an error has occured 
-  * if //myTable.len// = 0 then //searchstring// has not been found 
-  * if //myTable.len// > 0 then //myTable.len// tells the number of entries found 
  
-When something has been found, than //myTable// contains two sections, //header// and //data//.+ 
 +    * if //myTable.len//  < 0 then an error has occured 
 +    * if //myTable.len//  = 0 then //searchstring//  has not been found 
 +    * if //myTable.len//  > 0 then //myTable.len//  tells the number of entries found 
 + 
 + 
 +When something has been found, than //myTable//  contains two sections, //header//  and //data//. 
  
 The header section is needed in case you don't know in which column your wanted result is stored; you can identify the column by its column header name instead: The header section is needed in case you don't know in which column your wanted result is stored; you can identify the column by its column header name instead:
  
-   col= myTable.header["DTC-Text"] 
-   print (col) 
-   2 
-    
- //myTable.header.size// tells the number of colums in total **without** the first index column, which is always surpressed. 
  
 +<code>
 + col= myTable.header["DTC-Text"]
 + print (col)
 + 2
 +</code>
  
-The //data// section then contains the found data itself, arranged as a two dimensional array, sorted by rows and columns.  
  
-    result=myTable.data[row][column] +//myTable.header.size//  tells the number of colums in total **without**  the first index column, which is always surpressed. 
-   + 
-**ATTENTION**: Although the row and column indexes are expressed as numbers (1,2,3,4..), they are internally represented as string values ("1","2","3","4"...), so to read the result correctly, numeric row and column counters need to be converted to strings first to address the array correctly+ 
 +The //data//  section then contains the found data itself, arranged as a two dimensional array, sorted by rows and columns. 
 + 
 + 
 +<code> 
 +  result=myTable.data[row][column] 
 +</code> 
 + 
 + 
 +**ATTENTION**: Although the row and column indexes are expressed as numbers (1,2,3,4..), they are internally represented as string values (1,2,3,4”…), so to read the result correctly, numeric row and column counters need to be converted to strings first to address the array correctly 
 + 
 + 
 +<code> 
 +  column=3 
 +  row=2 
 +  result=myTable.data[tostring(row)][tostring(column)]) 
 +</code> 
 + 
 + 
 +Here after all a piece of sample code
  
-    column=3 
-    row=2 
-    result=myTable.data[tostring(row)][tostring(column)]) 
-     
-     
- Here after all a piece of sample code 
  
 <code lua> <code lua>
 myTable= dbLookupCall("dtc.oodb","005") myTable= dbLookupCall("dtc.oodb","005")
 + 
 print ("header") print ("header")
 for k,v in pairs (myTable.header) do for k,v in pairs (myTable.header) do
     print (k,"=",v)     print (k,"=",v)
 end end
 + 
 nrOfColumns = myTable.header.size nrOfColumns = myTable.header.size
 nrOfRows = myTable.len nrOfRows = myTable.len
 + 
 print ("Rows x Columns:" ,nrOfRows, nrOfColumns) print ("Rows x Columns:" ,nrOfRows, nrOfColumns)
- +  
 + 
 for row = 1 , nrOfRows , 1 do for row = 1 , nrOfRows , 1 do
   for column = 1 , nrOfColumns, 1 do    for column = 1 , nrOfColumns, 1 do 
-  + 
    print (cy, cx, myTable.data[tostring(row)][tostring(column)])    print (cy, cx, myTable.data[tostring(row)][tostring(column)])
   end   end
 end end
 </code> </code>
 +
  
 === openXCVehicleData(lua table) === === openXCVehicleData(lua table) ===
  
-OOBD can work as VehicleDatasource for [[http://openxcplatform.com/|openXC]], which means OOBD can send datasets to the openXC system (which needs to be installed on the same android device too, obviously). + 
 +OOBD can work as VehicleDatasource for [[http://openxcplatform.com/|openXC]], which means OOBD can send datasets to the openXC system (which needs to be installed on the same android device too, obviously). 
  
 To do so, a lua table is filled with the right indentifiers and correct formated values accourding to the [[https://github.com/openxc/openxc-message-format|OpenXC Message Format Specification]], one value per call. To do so, a lua table is filled with the right indentifiers and correct formated values accourding to the [[https://github.com/openxc/openxc-message-format|OpenXC Message Format Specification]], one value per call.
 +
  
 With that table openXCVehicleData() is called and the data are been transferred to the openXC backbone task for further handling. With that table openXCVehicleData() is called and the data are been transferred to the openXC backbone task for further handling.
  
-  openXCVehicleData({timestamp= 1332794087.675514, name= "longitude", value= -83.237427}) + 
-  +<code> 
 +openXCVehicleData({timestamp= 1332794087.675514, name= "longitude", value= -83.237427}) 
 +</code> 
 + 
 So everything which is understood by openXC can be generated out of a OOBD lua script. So everything which is understood by openXC can be generated out of a OOBD lua script.
  
doc/lua_make-your-own-scripts.1413217340.txt.gz · Last modified: 2014/10/13 18:22 by admin