Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
fr:liste_script [2016/10/14 11:57]
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 299: Line 329:
 //By mifrey// //By mifrey//
  
-**But :** Script permettant de simuler une présence en allumant/éteignant des lampes dans une certaine séquence et avec des durées aléatoires.+**But :** Script permettant de simuler une présence en allumant/éteignant des lampes dans une certaine séquence (par exemple on sort du salon, on va dans la salle de bain puis dans la chambre) avec des durées aléatoires.
  
 **Règle :** Le script doit être mis dans les conditions d'une règle et être déclenché par le changement des IOs "Enable", "Evening" et "Timer". Rien dans les actions. **Règle :** Le script doit être mis dans les conditions d'une règle et être déclenché par le changement des IOs "Enable", "Evening" et "Timer". Rien dans les actions.
Line 308: Line 338:
  
 Pour tester la séquence sans devoir attendre le couché du soleil, il faut mettre la variable %%"script_test_enable"%% à true puis activer le simulateur avec l'IO "Enable". La séquence se déroulera avec un changement d'état toutes les 5 secondes. Pour tester la séquence sans devoir attendre le couché du soleil, il faut mettre la variable %%"script_test_enable"%% à true puis activer le simulateur avec l'IO "Enable". La séquence se déroulera avec un changement d'état toutes les 5 secondes.
 +
 +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 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: State=0
 +Oct 14 12:01:59 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: End
 +Oct 14 12:02:02 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: Start
 +Oct 14 12:02:02 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: State=1
 +Oct 14 12:02:02 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: Set timer = 0:0:16:803
 +Oct 14 12:02:02 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: End
 +Oct 14 12:02:07 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: Start
 +Oct 14 12:02:07 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: State=2
 +Oct 14 12:02:07 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: Set timer = 0:12:37:894
 +Oct 14 12:02:07 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: End
 +Oct 14 12:02:12 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: Start
 +Oct 14 12:02:12 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: State=3
 +Oct 14 12:02:12 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: Set timer = 0:19:10:782
 +Oct 14 12:02:12 ... LuaPrint: SCRIPT_CONDITIONS_PRESENCE_SIMULATOR: End
 +...
 +</code>