Logic Machine Forum
Download file real time - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Download file real time (/showthread.php?tid=2998)



Download file real time - Domoticatorino - 19.11.2020

Hi there,
I am doing a script which writes in a CSV value and name of a list of object selected by a tag.

Since the script is launched with a push button, I am wondering if It Is possibile download the file CSV directly on the browser. I mean such as trend log file push button export.

Thanks as Always.

Br.


RE: Download file real time - admin - 20.11.2020

You can use .lp file to generate a CSV that will be sent to the user.
First you need to attach an event to a button that will open .lp in a new window:
Code:
$(function() {
  // additional class must be set to csv
  $('.csv').on('vclick', function() {
    window.open('/user/csv.lp');
  });
});

Use this as a starting point for csv.lp:
Code:
<?
-- set headers for csv file download
setdlheader('text/csv; charset=utf-8', 'report', 'csv')
-- generate csv here
csv = '...'
-- output to the user
print(csv)



RE: Download file real time - Domoticatorino - 20.11.2020

Thank you Admin.

Can I do that in Mosaico as well?

Br


RE: Download file real time - Daniel - 20.11.2020

Mosaic does not have custom js but maybe you could do something in the html widget.


RE: Download file real time - Domoticatorino - 24.11.2020

(20.11.2020, 06:57)admin Wrote: You can use .lp file to generate a CSV that will be sent to the user.
First you need to attach an event to a button that will open .lp in a new window:
Code:
$(function() {
  // additional class must be set to csv
  $('.csv').on('vclick', function() {
    window.open('/user/csv.lp');
  });
});

Use this as a starting point for csv.lp:
Code:
<?
-- set headers for csv file download
setdlheader('text/csv; charset=utf-8', 'report', 'csv')
-- generate csv here
csv = '...'
-- output to the user
print(csv)

Hi admin,
sorry but do not understand program language above. If I use <? LM inform me "Lua syntax error at line 1: unexpected symbol near '<'".

Thanks.


RE: Download file real time - admin - 24.11.2020

.lp file is not a script, it's a Lua file that only the web server can run. It should be uploaded via FTP to the user directory using apps username. If the file is named csv.lp then the URL will be http://LM_IP/user/csv.lp


RE: Download file real time - Domoticatorino - 24.11.2020

(24.11.2020, 07:34)admin Wrote: .lp file is not a script, it's a Lua file that only the web server can run. It should be uploaded via FTP to the user directory using apps username. If the file is named csv.lp then the URL will be http://LM_IP/user/csv.lp

Code:
<?
-- set headers for csv file download
setdlheader('text/csv; charset=utf-8', 'report', 'csv')
-- generate csv here
value=grp.tag("consumption")

filename = string.format('consumption.csv', os.date('%d-%m-%Y-%H-%M'))

a={}

for index, id in ipairs(value) do
  name=id["name"]
 
  meter=id["data"]
 
  consumption=name .."," .. meter
 
  a[index]=consumi
  log(a)
end

csv = a
result, err = io.writefile (filename, csv)

-- output to the user
print(csv)

I generate file as above without success. Is it correct the procedure? Thanks.


RE: Download file real time - admin - 24.11.2020

Try this:
Code:
<?
require('apps')
-- set headers for csv file download
setdlheader('text/csv; charset=utf-8', 'Consumption', 'csv')
-- generate csv here
value=grp.tag("consumption")

a={}

for index, id in ipairs(value) do
  name=id["name"]
  meter=id["data"]
  consumption=name .."," .. meter
  a[index]=consumi
end

print(a)



RE: Download file real time - Domoticatorino - 24.11.2020

(24.11.2020, 09:16)admin Wrote: Try this:
Code:
<?
require('apps')
-- set headers for csv file download
setdlheader('text/csv; charset=utf-8', 'Consumption', 'csv')
-- generate csv here
value=grp.tag("consumption")

a={}

for index, id in ipairs(value) do
  name=id["name"]
  meter=id["data"]
  consumption=name .."," .. meter
  a[index]=consumi
end

print(a)

Thank you very much Admin, but unfortunately when I launch the script on the visu I receive response from http://192.168.1.10/user/csv.lp that it is not possible reach the web site. The script is correct and I cannot identify the problem.

Thanks.
BR


RE: Download file real time - admin - 24.11.2020

There were some errors in the script, this should work:
Code:
<?
require('apps')
setdlheader('text/csv; charset=utf-8', 'Consumption', 'csv')

csv = {}
objects = grp.tag('consumption')

for _, obj in ipairs(objects) do
  csv[ #csv + 1 ] = obj.name .. ',' .. tostring(obj.value)
end

print(table.concat(csv, '\n'))



RE: Download file real time - Domoticatorino - 24.11.2020

(24.11.2020, 09:56)admin Wrote: There were some errors in the script, this should work:
Code:
<?
require('apps')
setdlheader('text/csv; charset=utf-8', 'Consumption', 'csv')

csv = {}
objects = grp.tag('consumption')

for _, obj in ipairs(objects) do
  csv[ #csv + 1 ] = obj.name .. ',' .. tostring(obj.value)
end

print(table.concat(csv, '\n'))

FANTASTIC ADMIN!! THANK YOU VERY MUCH.

I would like only to clerify a thing. 

string.gsub(tostring(obj.value), ".", ",")

With this above I would like to replace dot with comma in the number. I do not know why but he replace all the number with ",". Hence 23.88 became ,,,,,

What is the reason?

Thanks.


RE: Download file real time - admin - 24.11.2020

dot is a special symbol that is treated as "any character" by the gsub. You need to escape it like this:
Code:
value = string.gsub(tostring(obj.value), "%.", ",")
csv[ #csv + 1 ] = obj.name .. ';' .. value
Since you are using comma as a decimal separator you should use ; instead of comma as csv field separator.


RE: Download file real time - Domoticatorino - 24.11.2020

(24.11.2020, 10:33)admin Wrote: dot is a special symbol that is treated as "any character" by the gsub. You need to escape it like this:
Code:
value = string.gsub(tostring(obj.value), "%.", ",")
csv[ #csv + 1 ] = obj.name .. ';' .. value
Since you are using comma as a decimal separator you should use ; instead of comma as csv field separator.

Yes I know. I already use ";" in fact the format of the file in excel as well is perfect. Great Job. Thanks.

Now I work to download file from Mosaic as well.

Thanks.