Commit c9f6f334 authored by Kylian ou Nathan's avatar Kylian ou Nathan
Browse files

Controlleur + Modele Camera

parent 3e93ea27
......@@ -7,6 +7,7 @@ const axios = require('axios');
const readline = require('readline');
const raspivid = require('raspivid');
var CAMERA = require('raspberry-pi-camera-native')
/**
* TODO : DEBUG a replacer par une interface gpio
......@@ -17,7 +18,6 @@ const rl = readline.createInterface({
});
var STREAMING = []
var CAMERA = require('raspberry-pi-camera-native')
/**
* ? FN GET qui renvoit le stream vidéo en MJPEG
......
const express = require('express')
const app = express()
const fs = require("fs");
const http = require('http');
const FormData = require('form-data');
const axios = require('axios');
const readline = require('readline');
const raspivid = require('raspivid');
/**
* TODO : DEBUG a replacer par une interface gpio
*/
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var STREAMING = []
const SplitFrames = require('split-frames')
const JPEG_START = Buffer.from('\xff\xd8', 'binary')
const JPEG_END = Buffer.from('\xff\xd9', 'binary')
/**
* ? FN GET qui renvoit le stream vidéo en MJPEG
*/
app.get('/stream', function(req, res) {
const cameraOptions = {
width: 720,
height: 480,
fps: 16,
encoding: 'JPEG',
quality: 7
};
var video = raspivid({
width: 1280,
height: 720,
framerate: 16,
codec: 'MJPEG'
});
video.pipe(new SplitFrames({
startWith: JPEG_START,
endWith: JPEG_END
}));
video.on("end", r => {})
video.on('data', function(frame) {
...
})
req.on('close', () => {
console.log('\nConnection terminée : ' + req.hostname);
STREAMING.pop()
});
});
var httpServer = http.createServer(app);
httpServer.listen(8080);
/**
* ? Méthode permettant de filmer pendant 1 minutes
*/
function record() {
if (STREAMING.length > 0) {
try {
CAMERA.stop()
} catch (err) {}
STREAMING = []
}
console.log("DEBUT ENREGISTREMENT")
var file = fs.createWriteStream(`${__dirname}/capture/${new Date().getTime()}.h264`);
var video = raspivid({
width: 1280,
height: 720,
framerate: 30,
timeout: 5 * 1000,
'codec': 'MJPEG'
});
video.pipe(file);
video.on("end", r => {
console.log("FIN ENREGISTREMENT")
})
}
/**
* ? Méthode permettant d'avertir le serveur de l'intrusion
*/
function intrusion() {
var formData = new FormData()
formData.append("type", "intrusion")
axios.post("http://lockhome.nrocher.fr/api", formData).then(res => {
console.log(res.data)
}).catch(err => {
console.error(err)
})
}
/**
* TODO : DEBUG : Doit être réplacé par un évenement d'un port GPIO (module de détection de mouvement)
*/
rl.on("line", input => {
switch (input) {
case "intrusion":
record()
intrusion()
break;
}
});
\ No newline at end of file
......@@ -4,30 +4,104 @@ class Camera extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model("CameraModele");
$this->load->helper("url_helper");
}
public function list(){
$this->load->view('camera/list');
//VERIFICATION QUE L'UTILISATEUR EST CONNECTE
if (!$this->session->has_userdata("email")) {
redirect('/connexion', 'refresh');
exit();
}
$cameras = $this->CameraModele->getAll();
if ($cameras == false) {
$data = array("error" => "Vous n'avez pas ajouté de camera pour le moment.");
} else {
$data = array("camera" => $cameras);
}
$this->load->view('camera/list', $data);
}
public function edit($id = "new"){
public function edit($id = "new")
{
//VERIFICATION QUE L'UTILISATEUR EST CONNECTE
if (!$this->session->has_userdata("email")) {
redirect('/connexion', 'refresh');
exit();
}
$data = array();
if ($this->input->server('REQUEST_METHOD') == 'POST') {
//REQUETE HTTP POST
$nom = $this->input->post('nom');
$ip = $this->input->post('ip');
if ($nom != "" and $ip != "" ) {
// On met a jour le capteur
$this->CameraModele->edit($id, $nom, $ip);
redirect("/camera", "refresh");
} else {
$data["id"] = $id;
$data["new"] = $id == "new";
$data["nom"] = $nom;
$data["ip"] = $ip;
$data["new"] = true;
$this->load->view('camera/edit', $data);
}
$this->load->view('camera/edit', $data);
} else {
//REQUETE HTTP GET OU AUTRE
if ($id != "new") {
$camera = $this->CameraModele->get($id);
if ($camera !== false) {
$data["id"] = $camera->id;
$data["nom"] = $camera->nom;
$data["ip"] = $camera->ip;
$data["new"] = false;
} else {
redirect("/camera", "refresh");
}
} else {
$data["new"] = true;
}
$this->load->view('camera/edit', $data);
}
}
public function view($id){
$this->load->view('camera/view');
}
public function delete($id){
$this->load->view('camera/view');
}
}
public function delete($id)
{
//VERIFICATION QUE L'UTILISATEUR EST CONNECTE
if (!$this->session->has_userdata("email")) {
redirect('/connexion', 'refresh');
exit();
}
$this->CapteurModele->delete($id);
redirect("/camera", "refresh");
?>
\ No newline at end of file
}
}
......@@ -43,13 +43,11 @@ class Capteur extends CI_Controller
if ($this->input->server('REQUEST_METHOD') == 'POST') {
//REQUETE HTTP POST
if ($this->input->post('nom') != null and
$this->input->post('ip') != null and
$this->input->post('type') != null
) {
$nom = $this->input->post('nom');
$ip = $this->input->post('ip');
$type = $this->input->post('type');
$nom = $this->input->post('nom');
$ip = $this->input->post('ip');
$type = $this->input->post('type');
if ($nom != null and $ip != null and $type != null) {
// On met a jour le capteur
$this->CapteurModele->edit($id, $nom, $ip, $type);
......@@ -60,9 +58,9 @@ class Capteur extends CI_Controller
$data["id"] = $id;
$data["new"] = $id == "new";
$data["nom"] = $this->input->post('nom');
$data["ip"] = $this->input->post('ip');
$data["type"] = $this->input->post('type');
$data["nom"] = $nom;
$data["ip"] = $ip;
$data["type"] = $type;
$this->load->view('capteur/edit', $data);
}
......
<?php
class CameraModele extends CI_Model
{
public function __construct()
{
$this->load->database();
}
// METHODE PERMETTANT DE RECUPERER TOUTE LES CAMERAS
public function getAll()
{
$sql = "SELECT * from Camera";
$query = $this->db->query($sql);
if ($query->num_rows() == 0) {
return false;
} else {
return $query->result();
}
}
// METHODE PERMETTANT DE RECUPERER UNE CAMERA
public function get($id)
{
$sql = "SELECT * from Camera WHERE id = ?";
$query = $this->db->query($sql, array($id));
if ($query->num_rows() == 0) {
return false;
} else {
return $query->result()[0];
}
}
// METHODE PERMETTANT D'EDITER UNE CAMERA
public function edit($id, $nom, $ip)
{
$data = array();
$data["nom"] = $nom;
$data["ip"] = $ip;
if ($id == "new") {
$this->db->insert('Camera', $data);
} else {
$this->db->update('Camera', $data, array("id" => $id));
}
if ($this->db->affected_rows() > 0) {
return true;
} else {
return false;
}
}
// METHODE PERMETTANT DE SUPPRIMER UNE CAMERA
public function delete($id)
{
$this->db->delete('Camera', array("id" => $id));
if ($this->db->affected_rows() > 0) {
return true;
} else {
return false;
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment