Differences

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

Link to this comparison view

Both sides previous revision Previous revision
fr:liste_script [2016/10/14 12:32]
mifrey
fr:liste_script [2016/11/11 00:25] (current)
mifrey Correction script panique
Line 225: Line 225:
 **Précisions :** Le script doit être mis dans les conditions d'une règle et être déclenché par tous les poussoirs voulus (je les ai mis tous chez moi).  Il retourne true en cas de situation panique. Dans l'exemple, il retourne true si les poussoirs sont pressés 6 fois (12 changements d'etat) dans les 2 secondes (à définir dans le timer). Les actions sont à dénifir dans la règle, par exemple allumer toute les lumières, envoyer un mail ou déclencher l'alarme, etc. **Précisions :** Le script doit être mis dans les conditions d'une règle et être déclenché par tous les poussoirs voulus (je les ai mis tous chez moi).  Il retourne true en cas de situation panique. Dans l'exemple, il retourne true si les poussoirs sont pressés 6 fois (12 changements d'etat) dans les 2 secondes (à définir dans le timer). Les actions sont à dénifir dans la règle, par exemple allumer toute les lumières, envoyer un mail ou déclencher l'alarme, etc.
  
 +Afin de d'analyser si le script a bien fonctionné, on peut analyser les logs de Calaos-Server :
 +<code>
 +root@raspberrypi:~# journalctl -lu calaos-server --since 00:16 --no-pager | grep PANIC
 +Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: Start
 +Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: timer = true
 +Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: counter = 0
 +Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: Max time exceeded, restart timer and counter.
 +Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: Set timer = start
 +Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: Set counter = 1
 +Nov 11 00:16:34 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: End
 +Nov 11 00:16:35 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: Start
 +Nov 11 00:16:35 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: timer = false
 +Nov 11 00:16:35 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: counter = 1
 +Nov 11 00:16:35 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: Max time not exceeded, count up. Set counter = 2
 +Nov 11 00:16:35 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: Counts not reached, wait for the next count.
 +Nov 11 00:16:35 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: end
 +Nov 11 00:16:46 ... LuaPrint: SCRIPT_CONDITIONS_PANIC: Start
 +
 +...
 +</code>
  
 __A créer dans CALAOS INSTALLER :__ __A créer dans CALAOS INSTALLER :__
Line 235: Line 255:
 <code lua> <code lua>
 -- Detect a panic situation in which switches are pressed several times within a short time. -- Detect a panic situation in which switches are pressed several times within a short time.
--- The script assumes to be called each time the state of a switch changes. +-- The script shall be called each time the state of a switch changes. 
 + 
 --if true then return false end -- Uncomment to disable the script --if true then return false end -- Uncomment to disable the script
 + 
 -- Script start -- Script start
 local script_name = "SCRIPT_CONDITIONS_PANIC" local script_name = "SCRIPT_CONDITIONS_PANIC"
-print(script_name .. ": start") +print(script_name .. ": Start") 
- +  
--- IO id+-- IOs id
 local timer_id = "input_58" -- InputTimer type local timer_id = "input_58" -- InputTimer type
 local counter_id = "intern_0" -- InternalInt type local counter_id = "intern_0" -- InternalInt type
 + 
 -- Other variables to define -- Other variables to define
 local panic_count = 12 -- Number of times the state of a switch must change within a specific time (typically 2 seconds) to trigger a panic situation local panic_count = 12 -- Number of times the state of a switch must change within a specific time (typically 2 seconds) to trigger a panic situation
 + 
 +-- Read IOs
 +local timer = calaos:getInputValue(timer_id) -- ATTENTION: getInputValue(InputTimer type) returns a string type.  Timer is false when timer starts or stops, true when timer is done.
 +local counter = calaos:getInputValue(counter_id)
  
--- Print timer and counter value at script start +print(script_name .. ": timer = " .. tostring(timer)) 
-print(script_name .. ": timer = " .. tostring(calaos:getInputValue(timer_id))) +print(script_name .. ": counter = " .. tostring(counter))
-print(script_name .. ": counter = " .. tostring(calaos:getInputValue(counter_id)))+
  
--- Get timer status: Timer is false when timer starts or stopstrue when timer is done+  
-local timer = calaos:getInputValue(timer_id) -- ATTENTIONgetInputValue(InputTimer typereturns a string type+-- Max time exceeded or first count 
 +if timer == "true" or counter == 0 then 
 +    print(script_name .. ": Max time exceededrestart timer and counter.") 
 + -- Restart timer 
 + timer = "start" 
 + calaos:setOutputValue(timer_id, timer) 
 + print(script_name .. ": Set timer = " .. tostring(timer)) 
 + -- First count 
 + counter = 1; 
 +    calaos:setOutputValue(counter_id, counter) 
 + print(script_name .. ": Set counter = " .. tostring(counter)) 
 +    print(script_name .. ": End"
 +    return false 
 +-- Max time not exceeded 
 +else 
 + -- Count up 
 + counter = counter + 1 
 + calaos:setOutputValue(counter_id, counter) 
 + print(script_name .. ": Max time not exceeded, count up. Set counter = " .. tostring(counter))
  
--- Timer is donereset timer and counter+ -- Counts not reachedwait for the next count
-if timer == "true" then + if counter < panic_count then 
-    print(script_name .. ": Timer is donereset timer and counter.") + print(script_name .. ": Counts not reachedwait for the next count.") 
-    calaos:setOutputValue(timer_id, "start") + print(script_name .. ": end") 
-    calaos:setOutputValue(counter_id1+ return false 
-    print(script_name .. ": timer = " .. tostring(calaos:getInputValue(timer_id))+ -- Counts reached, panic mode.     
-    print(script_name .. ": counter = " .. tostring(calaos:getInputValue(counter_id))) + else 
-    print(script_name .. ": end"+ print(script_name .. ": Counts reached, panic mode."
-    return false+ -- Stop timer 
 + timer = "stop" 
 + calaos:setOutputValue(timer_idtimer
 + print(script_name .. ": Set timer = " .. tostring(timer)) 
 + -- Reset counter 
 + counter = 0; 
 + calaos:setOutputValue(counter_id, counter
 + print(script_name .. ": Set counter = " .. tostring(counter)) 
 + print(script_name .. ": end"
 + return true 
 + end
 end end
  
--- Increment the counter 
-local counter = calaos:getInputValue(counter_id) + 1 
-calaos:setOutputValue(counter_id, counter) 
- 
--- Timer is not done and counts not reached, continues to count. 
-if counter < panic_count then 
-    print(script_name .. ": Timer is not done and counts not reached, continue.") 
-    print(script_name .. ": timer = " .. tostring(calaos:getInputValue(timer_id))) 
-    print(script_name .. ": counter = " .. tostring(calaos:getInputValue(counter_id))) 
-    print(script_name .. ": end") 
-    return false 
--- Timer is not done and counts reached, panic mode.     
-else 
-    print(script_name .. ": Timer is not done and counts reached, panic mode.") 
-    print(script_name .. ": timer = " .. tostring(calaos:getInputValue(timer_id))) 
-    print(script_name .. ": counter = " .. tostring(calaos:getInputValue(counter_id))) 
-    calaos:setOutputValue(timer_id, "stop") 
-    calaos:setOutputValue(counter_id, 0) 
-    print(script_name .. ": end") 
-    return true 
-end 
  
 print(script_name .. ": Oups, should never be there...") print(script_name .. ": Oups, should never be there...")
Line 311: Line 341:
 Afin de d'analyser si le script a bien fonctionné, on peut analyser les logs de Calaos-Server : Afin de d'analyser si le script a bien fonctionné, on peut analyser les logs de Calaos-Server :
 <code> <code>
-root@raspberrypi:~# journalctl -lu calaos-server --no-pager | grep SCRIPT_CONDITIONS_PRESENCE_SIMULATOR+root@raspberrypi:~# journalctl -lu calaos-server --since 12:00 --no-pager | grep PRESENCE_SIMULATOR
 Oct 14 12:01:59 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: Start Oct 14 12:01:59 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: Start
 Oct 14 12:01:59 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: State=0 Oct 14 12:01:59 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: State=0