Esempio n. 1
0
function wireRelay_plugin_setting_page()
{
    global $_, $myUser, $conf;
    if (isset($_['section']) && $_['section'] == 'wireRelay') {
        if (!$myUser) {
            throw new Exception('Vous devez être connecté pour effectuer cette action');
        }
        $wireRelayManager = new WireRelay();
        $wireRelays = $wireRelayManager->populate();
        $roomManager = new Room();
        $rooms = $roomManager->populate();
        $selected = new WireRelay();
        $selected->pulse = 0;
        $selected->icon = 'fa fa-flash';
        //Si on est en mode modification
        if (isset($_['id'])) {
            $selected = $wireRelayManager->getById($_['id']);
        }
        $icons = array('fa fa-lightbulb-o', 'fa fa-power-off', 'fa fa-flash', 'fa fa-gears', 'fa fa-align-justify', 'fa fa-adjust', 'fa fa-arrow-circle-o-right', 'fa fa-desktop', 'fa fa-music', 'fa fa-bell-o', 'fa fa-beer', 'fa fa-bullseye', 'fa fa-automobile', 'fa fa-book', 'fa fa-bomb', 'fa fa-clock-o', 'fa fa-cutlery', 'fa fa-microphone', 'fa fa-tint');
        ?>

		<div class="span9 userBloc">

			<h1>Relais</h1>
			<p>Gestion des relais filaires</p>  

			<fieldset>
			    <legend>Ajouter/Modifier un relais filaire</legend>

			    <div class="left">

				    <label for="nameWireRelay">Nom</label>
				    <input type="hidden" id="id" value="<?php 
        echo $selected->id;
        ?>
">
				    <input type="text" id="nameWireRelay" value="<?php 
        echo $selected->name;
        ?>
" placeholder="Lumiere Canapé…"/>
				    
				    <label for="descriptionWireRelay">Description</label>
				    <input type="text"  value="<?php 
        echo $selected->description;
        ?>
" id="descriptionWireRelay" placeholder="Relais sous le canapé…" />

					<label for="iconWireRelay">Icone</label>
				    <input type="hidden"  value="<?php 
        echo $selected->icon;
        ?>
" id="iconWireRelay"  />
					
					<div>
						<div style='margin:5px;'>
						<?php 
        foreach ($icons as $i => $icon) {
            if ($i % 6 == 0) {
                echo '</div><div style="margin:5px;">';
            }
            ?>
							<i style="width:25px;" onclick="plugin_wirerelay_set_icon(this,'<?php 
            echo $icon;
            ?>
');" class="<?php 
            echo $icon;
            ?>
 btn <?php 
            echo $selected->icon == $icon ? 'btn-success' : '';
            ?>
"></i>
						<?php 
        }
        ?>
 
						</div>
					</div>

				    <label for="onWireRelay">Commande vocale "ON" associée</label>
				    <?php 
        echo $conf->get('VOCAL_ENTITY_NAME');
        ?>
, <input type="text" id="onWireRelay" value="<?php 
        echo $selected->oncommand;
        ?>
" placeholder="Allume la lumière, Ouvre le volet…"/>
				   
				    
				    <label for="offWireRelay">Commande vocale "OFF" associée</label>
				    <?php 
        echo $conf->get('VOCAL_ENTITY_NAME');
        ?>
, <input type="text" id="offWireRelay" value="<?php 
        echo $selected->offcommand;
        ?>
" placeholder="Eteinds la lumière, Ferme le volet…"/>
				    
				    
				    <label for="pinWireRelay">Pin GPIO (Numéro Wiring PI) relié au relais</label>
				    <input type="number" value="<?php 
        echo $selected->pin;
        ?>
" id="pinWireRelay" placeholder="0,1,2…" />
				    
				    <label for="roomWireRelay">Pièce de la maison</label>
				    <select id="roomWireRelay">
				    	<?php 
        foreach ($rooms as $room) {
            ?>
				    	<option <?php 
            if ($selected->room == $room->getId()) {
                echo "selected";
            }
            ?>
 value="<?php 
            echo $room->getId();
            ?>
"><?php 
            echo $room->getName();
            ?>
</option>
				    	<?php 
        }
        ?>
				    </select>
				   <label for="pinWireRelay">Mode impulsion (micro secondes)</label>
				   <input type="number" value="<?php 
        echo $selected->pulse;
        ?>
" id="pulseWireRelay" placeholder="0" />
				   <small>laisser à zéro pour désactiver le mode impulsion</small>
				</div>

	  			<div class="clear"></div>
			    <br/><button onclick="plugin_wirerelay_save(this)" class="btn">Enregistrer</button>
		  	</fieldset>
			<br/>


			<fieldset>
				<legend>Consulter les relais filaires existants</legend>
				<table class="table table-striped table-bordered table-hover">
				    <thead>
					    <tr>
					    	<th>Nom</th>
						    <th>Description</th>
						    <th>Pin GPIO</th>
						    <th>Pièce</th>
						    <th colspan="2">Impulsion</th>
						    
					    </tr>
				    </thead>
			    
			    	<?php 
        foreach ($wireRelays as $wireRelay) {
            $room = $roomManager->load(array('id' => $wireRelay->room));
            ?>
					<tr>
				    	<td><?php 
            echo $wireRelay->name;
            ?>
</td>
					    <td><?php 
            echo $wireRelay->description;
            ?>
</td>
					    <td><?php 
            echo $wireRelay->pin;
            ?>
</td>
					    <td><?php 
            echo $room->getName();
            ?>
</td>
					    <td><?php 
            echo $wireRelay->pulse;
            ?>
</td>
					    <td>
					    	<a class="btn" href="setting.php?section=wireRelay&id=<?php 
            echo $wireRelay->id;
            ?>
"><i class="fa fa-pencil"></i></a>
					    	<div class="btn" onclick="plugin_wirerelay_delete(<?php 
            echo $wireRelay->id;
            ?>
,this);"><i class="fa fa-times"></i></div>
					    </td>
					    </td>
			    	</tr>
			    <?php 
        }
        ?>
			    </table>
			</fieldset>
		</div>

<?php 
    }
}
function wireRelay_action_wireRelay()
{
    global $_, $conf, $myUser;
    switch ($_['action']) {
        case 'wireRelay_delete_wireRelay':
            if ($myUser->can('relais filaire', 'd')) {
                $wireRelayManager = new WireRelay();
                $wireRelayManager->delete(array('id' => $_['id']));
            }
            header('location:setting.php?section=wireRelay');
            break;
        case 'wireRelay_plugin_setting':
            $conf->put('plugin_wireRelay_emitter_pin', $_['emiterPin']);
            $conf->put('plugin_wireRelay_emitter_code', $_['emiterCode']);
            header('location: setting.php?section=preference&block=wireRelay');
            break;
        case 'wireRelay_add_wireRelay':
            if ($myUser->can('relais filaire', $_['id'] != '' ? 'u' : 'c')) {
                $wireRelayManager = new WireRelay();
                $wireRelay = $_['id'] != '' ? $wireRelayManager->getById($_['id']) : new WireRelay();
                $wireRelay->setName($_['nameWireRelay']);
                $wireRelay->setDescription($_['descriptionWireRelay']);
                $wireRelay->setPin($_['pinWireRelay']);
                $wireRelay->setRoom($_['roomWireRelay']);
                $wireRelay->setPulse($_['pulseWireRelay']);
                $wireRelay->save();
            }
            header('location:setting.php?section=wireRelay');
            break;
        case 'wireRelay_change_state':
            global $_, $myUser;
            if ($myUser->can('relais filaire', 'u')) {
                $wireRelay = new WireRelay();
                $wireRelay = $wireRelay->getById($_['engine']);
                //TODO - remplacer par Gpio::mode($wireRelay->getPin(),'out');
                $cmd = '/usr/local/bin/gpio mode ' . $wireRelay->getPin() . ' out';
                Functions::log('Launch system command : ' . $cmd);
                system($cmd, $out);
                if ($wireRelay->getPulse() == 0) {
                    //TODO - remplacer par Gpio::write($wireRelay->getPin(),$_['state']);
                    $cmd = '/usr/local/bin/gpio write ' . $wireRelay->getPin() . ' ' . $_['state'];
                    Functions::log('Launch system command : ' . $cmd);
                    system($cmd, $out);
                } else {
                    //TODO - remplacer par Gpio::write($wireRelay->getPin(),1);
                    $cmd = '/usr/local/bin/gpio write ' . $wireRelay->getPin() . ' 1';
                    system($cmd, $out);
                    usleep($wireRelay->getPulse());
                    //TODO - remplacer par Gpio::write($wireRelay->getPin(),0);
                    $cmd = '/usr/local/bin/gpio write ' . $wireRelay->getPin() . ' 0';
                    Functions::log('Launch system command : ' . $cmd);
                    system($cmd, $out);
                }
                //TODO change bdd state
                if (!isset($_['webservice'])) {
                    header('location:index.php?module=room&id=' . $wireRelay->getRoom());
                } else {
                    $affirmations = array('A vos ordres!', 'Bien!', 'Oui commandant!', 'Avec plaisir!', 'J\'aime vous obéir!', 'Avec plaisir!', 'Certainement!', 'Je fais ça sans tarder!', 'Avec plaisir!', 'Oui chef!');
                    $affirmation = $affirmations[rand(0, count($affirmations) - 1)];
                    $response = array('responses' => array(array('type' => 'talk', 'sentence' => $affirmation)));
                    $json = json_encode($response);
                    echo $json == '[]' ? '{}' : $json;
                }
            } else {
                $response = array('responses' => array(array('type' => 'talk', 'sentence' => 'Je ne vous connais pas, je refuse de faire ça!')));
                echo json_encode($response);
            }
            break;
    }
}