« Spaceapi » : différence entre les versions

De Breizh-Entropy
Aller à la navigation Aller à la recherche
(création de la page)
 
(ajout du code du bot)
Ligne 8 : Ligne 8 :


C'est justement l'application Android qui [[user:petrus|m'a]]  donné envie d'implémenter ça.
C'est justement l'application Android qui [[user:petrus|m'a]]  donné envie d'implémenter ça.
== Comment ==
Le json est situé [http://breizh-entropy.org/spaceapi.json ici]]. Sur la machine il est là :
  $ ls -l /var/www/breizh-entropy.org/spaceapi.json
  lrwxrwxrwx 1 root root 38 avril 26 16:39 /var/www/breizh-entropy.org/spaceapi.json -> /home/petrus/public_html/spaceapi.json
Il est facile de modifier le json en utilisant [http://stedolan.github.io/jq/ jq] :
  $ cat /home/petrus/public_html/spaceapi.json | jq '.state.open'
  false
Pour ouvrir le lab :
  $ cat /home/petrus/public_html/spaceapi.json | jq '.state.open=true' > /tmp/spaceapi.json; mv /tmp/spaceapi.json /home/petrus/public_html/spaceapi.json
Pour fermer le lab :
  $ cat /home/petrus/public_html/spaceapi.json | jq '.state.open=false' > /tmp/spaceapi.json; mv /tmp/spaceapi.json /home/petrus/public_html/spaceapi.json
== Le bot irc ==
argos est le bot qui permet de changer l'état (ouvert ou fermé) du lab :
  <petrus> !open
  <argos> Lab is open!
  <petrus> !closed
  <argos> On ferme :(
  <petrus> !status
  <argos> Lab is closed :(
argos est un eggdrop actuellement et tourne sur [[user:petrus|ma]] machine, mais pourrait être installé sur la vm du hackerspace voire recodé simplement en utilisant supybot.
Le script eggdrop est très simple et communique avec le serveur en ssh, en utilisant les scripts lab_open et lab_close montrés plus haut :
  ################
  # spaceapi.tcl #
  ################
 
  bind pub - "!open" spaceapi:open
  bind pub - "!closed" spaceapi:closed
  bind pub - "!close" spaceapi:closed
  bind pub - "!status" spaceapi:status
  bind msg - "!open" spaceapi:open
  bind msg - "!closed" spaceapi:closed
  bind msg - "!close" spaceapi:closed
  bind msg - "!status" spaceapi:status
 
  proc spaceapi:open {nick uhost handle chan text} {
    exec lab_open
    set msg "\00312Lab is \0033open!"
    putnow "PRIVMSG $chan :$msg"
  }
 
  proc spaceapi:closed {nick uhost handle chan text} {
    exec lab_closed
    set msg "\00312On \0033ferme :("
    putnow "PRIVMSG $chan :$msg"
  }
 
  proc spaceapi:status {nick uhost handle chan text} {
    set state [exec curl -s http://breizh-entropy.org/spaceapi.json | jq ".state.open"]
    switch $state {
      "true" {
        set msg "\00312Lab is \0033open!"
      }
      "false" {
        set msg "\00312Lab is \0033closed :("
      }
      default {
        set msg "ça a po marché :("
      }
    }
    putnow "PRIVMSG $chan :$msg"
  }
 
  putlog "\003spaceapi.tcl V0.2"

Version du 27 avril 2014 à 12:02

Qu'est ce que c'est ?

D'après le site oueb : The purpose of the Space API is to define a unified specification across the hackerspaces that can be used to expose information to web apps or any other application.

À partir de là un certain nombre d'application a été développé, pour avoir des statistiques, un plugin firefox ou même une application android :

Myhackerspace.png

C'est justement l'application Android qui m'a donné envie d'implémenter ça.

Comment

Le json est situé ici]. Sur la machine il est là :

 $ ls -l /var/www/breizh-entropy.org/spaceapi.json
 lrwxrwxrwx 1 root root 38 avril 26 16:39 /var/www/breizh-entropy.org/spaceapi.json -> /home/petrus/public_html/spaceapi.json

Il est facile de modifier le json en utilisant jq :

 $ cat /home/petrus/public_html/spaceapi.json | jq '.state.open'
 false

Pour ouvrir le lab :

 $ cat /home/petrus/public_html/spaceapi.json | jq '.state.open=true' > /tmp/spaceapi.json; mv /tmp/spaceapi.json /home/petrus/public_html/spaceapi.json

Pour fermer le lab :

 $ cat /home/petrus/public_html/spaceapi.json | jq '.state.open=false' > /tmp/spaceapi.json; mv /tmp/spaceapi.json /home/petrus/public_html/spaceapi.json

Le bot irc

argos est le bot qui permet de changer l'état (ouvert ou fermé) du lab :

 <petrus> !open
 <argos> Lab is open!
 <petrus> !closed
 <argos> On ferme :(
 <petrus> !status
 <argos> Lab is closed :(

argos est un eggdrop actuellement et tourne sur ma machine, mais pourrait être installé sur la vm du hackerspace voire recodé simplement en utilisant supybot.

Le script eggdrop est très simple et communique avec le serveur en ssh, en utilisant les scripts lab_open et lab_close montrés plus haut :

 ################
 # spaceapi.tcl #
 ################
 
 bind pub - "!open" spaceapi:open
 bind pub - "!closed" spaceapi:closed
 bind pub - "!close" spaceapi:closed
 bind pub - "!status" spaceapi:status
 bind msg - "!open" spaceapi:open
 bind msg - "!closed" spaceapi:closed
 bind msg - "!close" spaceapi:closed
 bind msg - "!status" spaceapi:status
 
 proc spaceapi:open {nick uhost handle chan text} {
   exec lab_open
   set msg "\00312Lab is \0033open!"
   putnow "PRIVMSG $chan :$msg"
 }
 
 proc spaceapi:closed {nick uhost handle chan text} {
   exec lab_closed
   set msg "\00312On \0033ferme :("
   putnow "PRIVMSG $chan :$msg"
 }
 
 proc spaceapi:status {nick uhost handle chan text} {
   set state [exec curl -s http://breizh-entropy.org/spaceapi.json | jq ".state.open"]
   switch $state {
     "true" {
       set msg "\00312Lab is \0033open!"
     }
     "false" {
       set msg "\00312Lab is \0033closed :("
     }
     default {
       set msg "ça a po marché :("
     }
   }
   putnow "PRIVMSG $chan :$msg"
 }
 
 putlog "\003spaceapi.tcl V0.2"