Logic Machine Forum
Remote script modification - 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: Remote script modification (/showthread.php?tid=1233)

Pages: 1 2


Remote script modification - Hippolyte - 12.02.2018

Hi everyone,
 
A lot of questions lately Smile but this one may be tricky.
We have to update a script on 300 LMs, please tell me there is a simple way to do this remotely … The script name is the same on every LM.
I know there is no API for this but maybe you have another technical solution in your head !
Thank you very much for your help.

Hippolyte


RE: Remote script modification - admin - 13.02.2018

You can create a custom .lp file that will handle this, to which you must pass name and script get/post variables. Like this: http://LM_IP/user/update.lp?name=test&script=log(event)
For better security you should add some extra password/variable check to this file.

Code:
<?

name = getvar('name')
scriptdata = getvar('script')

if name and scriptdata then
  item = script.get(name)
  
  if item then
    item.script = scriptdata

    db:update('scripting', { script = scriptdata }, { id = item.id })

    -- save script data to file
    script.save(item, true)
    -- reload script/group monitor
    script.reloadsingle(item)

    print('update ok')
  else
    print('script not found')
  end
else
  print('invalid arguments')
end



RE: Remote script modification - Hippolyte - 13.02.2018

Wow thank you for your responsiveness ! I'll try this soon and keep you inform.
Thx !


RE: Remote script modification - Hippolyte - 20.03.2018

Hello, I have an error when doing exactly what you wrote.

"Error in /www/user/update2.lp at line 5: attempt to index global 'script' (a nil value)"

Can you help me with that please ?


RE: Remote script modification - admin - 20.03.2018

Add require('apps') to the next line after <?


RE: Remote script modification - Hippolyte - 20.03.2018

That's what I did. Now I have "Error in /www/user/update.lp at line 10: '=' expected near ' if'"


RE: Remote script modification - admin - 20.03.2018

Post your full code, you probably modified something by accident


RE: Remote script modification - Hippolyte - 21.03.2018

Code:
<?
require('apps')
name = getvar('name')
scriptdata = getvar('script')
if name and scriptdata then
  item = script.get(name)
  if item then
    item.script = scriptdata
    db:update('scripting', { script = scriptdata }, { id = item.id })
    -- save script data to file
    script.save(item, true)
    -- reload script/group monitor
    script.reloadsingle(item)
    print('update ok')
  else
    print('script not found')
  end
else
  print('invalid arguments')
end

Here it is Smile


RE: Remote script modification - buuuudzik - 21.03.2018

Try log variables and check in LM logs what could be the source of problem, like below:

Code:
You can try to log it like this:

<?
require('apps')
name = getvar('name')
scriptdata = getvar('script')

log(1, name, scriptdata)

if name and scriptdata then
 item = script.get(name)
 
  log(2, item)

 if item then
   item.script = scriptdata

   log(3, item.script)

   res = db:update('scripting', { script = scriptdata }, { id = item.id })
    log(4, res)
   -- save script data to file
   script.save(item, true)
    log(5, 'After script.save')
   -- reload script/group monitor
   script.reloadsingle(item)
   print('update ok')
 else
   print('script not found')
 end
else
 print('invalid arguments')
end



RE: Remote script modification - Erwin van der Zwart - 21.03.2018

Hi,

If this is your full sample then you miss ?> at the end ..

BR,

Erwin


RE: Remote script modification - admin - 21.03.2018

Code is fine, ending ?> is optional. Double check .lp file contents on your device.


RE: Remote script modification - Hippolyte - 21.03.2018

(21.03.2018, 12:41)admin Wrote: Code is fine, ending ?> is optional. Double check .lp file contents on your device.

@admin, I literally copy paste the code of my update.lp file on my device.
Are you sure for the http://LM_IP/user/update.lp?name=test&script=log(event) ?

@Erwin I did add the ?> yet nothing, as admin said it might be optional

@buuuudzik I tried your test and still -> Error in /www/user/update2.lp at line 11: '=' expected near ' log'

I wonder if this is not an issue about the copy paste from this website to the update.lp file. Like invisible caracters coming in the code ...


RE: Remote script modification - admin - 21.03.2018

You probably have a non breaking space there, either use another code editor or remove all leading spaces from the code.


RE: Remote script modification - Hippolyte - 22.03.2018

(21.03.2018, 17:14)admin Wrote: You probably have a non breaking space there, either use another code editor or remove all leading spaces from the code.

Whether I put it perfectly like this

Code:
<?
require('apps')
name = getvar('name')
scriptdata = getvar('script')
if name and scriptdata then
 item = script.get(name)
 if item then
   item.script = scriptdata
        db:update('scripting', { script = scriptdata }, { id = item.id })
        script.save(item, true)
        reload script/group monitor
        script.reloadsingle(item)
        print('update ok')
     else
   print('script not found')
 end
else
print('invalid arguments')
end
?>


Or this 

Code:
<?
require('apps')
name = getvar('name')
scriptdata = getvar('script')
if name and scriptdata then
item = script.get(name)
if item then
item.script = scriptdata
db:update('scripting', { script = scriptdata }, { id = item.id })
script.save(item, true)
reload script/group monitor
script.reloadsingle(item)
print('update ok')
else
print('script not found')
end
else
print('invalid arguments')
end
?>


I still have "Error in /www/user/update.lp at line 11: '=' expected near 'script'"
when I type http://192.168.1.230/user/update.lp?name=test&script=log(event) in Chrome


RE: Remote script modification - buuuudzik - 22.03.2018

What you have in "Logs" on Logic Machine when you run this code:

Code:
<?
require('apps')
name = getvar('name')
scriptdata = getvar('script')

log(name, scriptdata) -- THIS CODE LOGS VARIABLES IN LM LOGS

if name and scriptdata then
item = script.get(name)
if item then
item.script = scriptdata
db:update('scripting', { script = scriptdata }, { id = item.id })
script.save(item, true)
-- reload script/group monitor
script.reloadsingle(item)
print('update ok')
else
print('script not found')
end
else
print('invalid arguments')
end
?>



RE: Remote script modification - admin - 22.03.2018

Line 11 is missing comment:
Code:
-- reload script/group monitor



RE: Remote script modification - Hippolyte - 23.03.2018

(22.03.2018, 10:45)admin Wrote: Line 11 is missing comment:
Code:
-- reload script/group monitor

Angry  I feel so bad ! Sorry for wasting your time and thank you very much all.
It works great !

One last quick question : how do you do a return carriage (new line) ?


RE: Remote script modification - Hippolyte - 30.03.2018

Hello, once again thank you for your help.
So it is working really great. Yet I still have this return carriage issue. If I use the http://LM_IP/user/update.lp?name=test&script=100linesscript then the script is written on only one line when I open it on the LM, do you think there is a way to keep the indentation ?
This is for upgrading script on 500 LMs at the same time Big Grin


RE: Remote script modification - admin - 30.03.2018

You need to use POST request, because GET query string is limited to a certain number of characters


RE: Remote script modification - Leo681 - 26.02.2023

there is anyway to create the script first? i mean create a script from another script.

If its posible how i make the diferent scripts types?

thanks