Read/write file to LM2 connected USB flash
Example: Read/write file to LM2 connected USB flash
Init script
USB storage will be mounted to /mnt/usb directory.
Note! First found USB storage device will be mounted on system start. Hot plugging is not supported by this script.
Usage
- -- create usb mount directory
- os.execute('mkdir -p /mnt/usb')
-
- -- find first matching usb storage device
- dev = io.readproc('ls /dev/sd*1 2>/dev/null'):match('/dev/sd%l1')
-
- -- found it, mount
- if dev then
- os.execute('mount ' .. dev .. ' /mnt/usb')
- alert('[usb-mount] mounted %s', dev)
- -- nothing found, local flash will be used
- else
- alert('[usb-mount] no device found')
- end
Read/Write
You can use standard Lua io library functions as well as LM specific helpers for reading/writing whole files at once:
- Reads whole file at once. Return file contents as a string on success or nil on error.
- io.readfile (file)
- Writes given data to a file. Data can be either a value convertible to string or a table of such values. When data is a table then each table item is terminated by a new line character. Return boolean as write result when file can be open for writing or nil when file cannot be accessed.
- io.writefile (file, data)
- value = knxdatatype.decode(event.datahex, dt.bool)
- data = string.format('%s value is %s', os.date('%c'), tostring(value))
- -- write to the end of log file preserving all previous data
- file = io.open('/mnt/usb/log.txt', 'a+')
- file:write(data .. '\r\n')
- file:close()
- for line in io.lines('/mnt/usb/config.txt') do
- -- split line by '=' sing
- items = line:split('=')
- -- two items, line seems to be valid
- if #items == 2 then
- key = items[ 1 ]:trim()
- value = items[ 2 ]:trim()
- alert('[config] %s = %s', key, value)
- end
- end
Examples
Write event status to log file
Produced result:
Mon Jan 3 05:25:13 2011 value is false
Mon Jan 3 05:25:14 2011 value is true
Mon Jan 3 05:25:32 2011 value is false
Mon Jan 3 05:25:33 2011 value is true
Read data from file (config in format key=value)