1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
| package sonnette_server;
import OdroidGPIOControl.Pin;
//** Classe qui gère les commandes envoyées par le serveur vers le / les clients **
class Commandes implements Runnable{
Serv _myServ; // pour utilisation des méthodes de la classe principale
Thread _t; // contiendra le thread
Mylog mylog = new Mylog(this.getClass().getName());
boolean valide;//thread principal
long lastpingTime;//permet de savoir de quand remonte le dernier ping.
long maxpingtimesecondes=10;
//gestion btt sonnette
Pin LED,BTT;
int _Blinkingtime=2000;//duree allumage LED
int _WaitClient=1000;//on attend la connexion du client x _WaitClientCount
int _WaitClientCount=5;//nombre de tentative d'envois tous les _WaitClient
boolean triggerbutton;//trigger sur le bouton
Commandes(Serv myServ){
_myServ=myServ;
Initialisation();
//demarrage Thread
_t = new Thread(this); // instanciation du thread
_t.start(); // demarrage du thread, la fonction run() est ici lancée
}
/*************** Initialisation GPIO ***************/
private void Initialisation(){
mylog.mlog("info", "Initialisation GPIOThread");
LED = new Pin(27);
BTT= new Pin(26);
LED.SetOUT();
BTT.SetIN();
LED.SetLOW();
valide=true;
triggerbutton=true;
lastpingTime=System.nanoTime();
}
/**************** Thread principal ****************/
//** Methode du serveur qui envoie les commandes aux clients **
public void run(){
mylog.mlog("info", "Demarrage Thread Commandes");
//boucle infinie
while (valide){
//on attend le relachement du bouton
if(!triggerbutton) {
mylog.mlog("info", "On attend que le bouton soit relaché");
while(!triggerbutton){
if(BoutonReleased()) triggerbutton=true;
}
}
//on a pressé le bouton un certain temps
if (BoutonPressed()){
triggerbutton=false;
mylog.mlog("info", "Button pressed");
//nouveau Thread d'allumage des leds
blinking();
//boucle de X secondes pour attendre un client
int tmpcount=0;
while(tmpcount<_WaitClientCount && _myServ.getNbClients()<=0){
try{Thread.sleep(_WaitClient);}catch (Exception e) {mylog.mlog("info",e.getMessage());}
tmpcount++;
mylog.mlog("info", "boucle attente du client");
}
//Avertissement des clients
if(_myServ.getNbClients()>0) AvertissementClients();
else mylog.mlog("warning", "Il n'y a aucun client à avertir");
}
//on envoi un ping a tous les clients. permet de supprimer les connexions perdues.
if(_myServ.getNbClients()>0) CheckClients();
//Pause
try{Thread.sleep(10);}catch (Exception e) {mylog.mlog("info",e.getMessage());}
}
mylog.mlog("info", "Fin Thread Commandes");
}
//methode pour prevenir les clients
private void AvertissementClients() {
mylog.mlog("info","avertissement ringing");
_myServ.sendAll("RINGING");
}
//methode pour vérifier que le client est toujours connecté
private void CheckClients() {
//on recup le temps courant
long endTime = System.nanoTime();
long duration = (endTime - lastpingTime);
//en secondes
double seconds = (double) duration / 1000000000.0;
if(seconds<maxpingtimesecondes) return;
else{
lastpingTime=endTime;
//mylog.mlog("info","pinging client");
_myServ.sendAll("PING OK");
}
}
/************ Gestion des GPIO ************/
//on controle si le bouton a été pressé
private boolean BoutonPressed() {
//normalement le bouton = 1 et 0 si pressé
if(BTT.GetValue()==0){
//on teste la valeur plusieur fois pour éviter le rebond
for(int i=0;i<5;i++){
//quand ça repasse a 1 c'est pas bon
if(BTT.GetValue()==1) {
mylog.mlog("info", "bouton relaché trop tôt");
return false;
}
try{
Thread.sleep(1);
}catch (Exception e) {
System.out.println(e.getMessage());
}
}
//si on arrive jusque là c'est que le btt =0 suffisament longtemps
return true;
}
return false;
}
private boolean BoutonReleased(){
//bouton relaché
if(BTT.GetValue()==1) {
//on teste la valeur plusieur fois pour éviter le rebond
for(int i=0;i<5;i++){
//quand ça repasse a 0 c'est pas bon
if(BTT.GetValue()==0) {
mylog.mlog("info", "bouton relaché");
return false;
}
try{
Thread.sleep(1);
}catch (Exception e) {
System.out.println(e.getMessage());
}
}
//si on arrive jusque là c'est que le btt = 0 suffisament longtemps
return true;
}
return false;
}
//Thread d'allumage de la led
private void blinking() {
Thread Tblinking = new Thread("Blinking") {
public void run() {
mylog.mlog("info", "Demarrage Thread blinking");
LED.SetHIGH();
try{
Thread.sleep(_Blinkingtime);
}catch (Exception e) {
System.out.println(e.getMessage());
}
LED.SetLOW();
mylog.mlog("info", "Fin thread blinking");
}
};
Tblinking.start();
}
} |
Partager