Commit 840969e4 authored by Kylian ou Nathan's avatar Kylian ou Nathan
Browse files

Lot of thing

parent 12a3c766
......@@ -7,7 +7,6 @@ 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,75 +16,14 @@ const rl = readline.createInterface({
output: process.stdout
});
var STREAMING = []
/**
* ? 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
};
CAMERA.start(cameraOptions);
STREAMING.push(true)
res.writeHead(200, {
'Cache-Control': 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0',
Pragma: 'no-cache',
Connection: 'close',
'Content-Type': 'multipart/x-mixed-replace; boundary=--myboundary'
});
console.log("\nNouvelle connection au flux vidéo : " + req.hostname)
let isReady = true;
let frameHandler = (frameData) => {
try {
if (!isReady) {
return;
}
isReady = false;
res.write('--myboundary\nContent-Type: image/jpg\nContent-length: ${frameData.length}\n\n');
res.write(frameData, function() {
isReady = true;
});
} catch (ex) {}
}
let frameEmitter = CAMERA.on('frame', frameHandler);
req.on('close', () => {
frameEmitter.removeListener('frame', frameHandler);
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`);
......
......@@ -74,10 +74,10 @@ $route['profil/edit/(:any)'] = 'Profil/edit/$1';
$route['profil'] = 'Profil/list';
$route['resume/show'] = 'Resume/show';
$route['resume/list'] = 'Resume/list';
$route['resume'] = 'Resume/list';
$route['api/(:any)/(:any)'] = 'API/init';
$route['api/(:any)'] = 'API/init';
$route['api'] = 'API/init';
$route['404_override'] = '';
......
......@@ -3,14 +3,88 @@ class API extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model("LogCapteurModele");
$this->load->model("LogEmailModele");
$this->load->model("AuthentificationModele");
$this->load->model("CapteurModele");
$this->load->model("ProfilModele");
$this->load->helper("url_helper");
}
public function init($capteur = "", $type = "") {
echo "POST :<br>";
var_dump($_POST);
public function init() {
$ip = $this->uri->segment(2);
$type = $this->uri->segment(3);
$capteur = $this->CapteurModele->getWithIP($ip);
if($capteur === false){
echo "Capteur inexistant";
} else {
$actionDisponible = array("intrusion", "batterie");
if(in_array($type, $actionDisponible)) {
$profil = $this->ProfilModele->getActive();
$debut = explode(":", $profil->debut);
$fin = explode(":", $profil->fin);
$now = mktime(date("H"), date("i"), 0, 0,0,0);
$debut = mktime($debut[0], $debut[1], 0, 0,0,0);
$fin = mktime($fin[0], $fin[1], 0, 0,0,0);
if( ($now >= $debut AND $now <= $fin) OR ($profil->debut == $profil->fin) ) {
if($this->LogCapteurModele->add($capteur->id, $type)){
echo "ok";
$email_type = $type === "intrusion" ? $type : "warning";
$mail_send = $this->LogEmailModele->getAllDay();
if(!in_array($email_type, array_column($mail_send["raw"], "type"))){
$this->send_email($email_type);
}
} else {
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
echo "Erreur lors de l'enregistrement de l'évènement.";
}
} else {
echo "ok";
}
} else {
echo "Action inexistante";
}
}
}
private function send_email($type){
$users = $this->AuthentificationModele->users_accepting_notification();
$users_id = array_column($users, "id");
$users_email = array_column($users, "email");
$to = implode(",", $users_email);
$subject = $type === "intrusion" ? "[LOCKHOME] Intrusion dans votre maison" : "[LOCKHOME] Notification a lire concernant votre système de sécurité";
$message = file_get_contents("./application/email/$type.html");
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: lockhome@lockhome.nrocher.fr' . "\r\n";
$headers .= 'Reply-To: lockhome@lockhome.nrocher.fr' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)) {
echo "<br>Email accepted by server";
$this->LogEmailModele->add($users_id, $type);
} else {
echo "<br>Error: Email not accepted by server";
}
}
}
......
<?php
class News extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model("news_model");
$this->load->helper("url_helper");
}
public function index(){
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($slug = NULL) {
$data["news_item"] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
public function create() {
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
} else {
$this->news_model->set_news();
$this->load->view('news/success');
}
}
}
\ No newline at end of file
<?php
class Pages extends CI_Controller {
public function view($page = "home") {
if( !file_exists(APPPATH.'views/pages/'.$page.'.php')) {
show_404();
}
$data["title"]= ucfirst($page);
$this->load->view("templates/header", $data);
$this->load->view("pages/".$page, $data);
$this->load->view("templates/footer", $data);
}
}
\ No newline at end of file
......@@ -3,26 +3,60 @@ class Resume extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model("ResumeModele");
$this->load->model("LogCapteurModele");
$this->load->model("LogEmailModele");
$this->load->helper("url_helper");
}
public function show(){
$this->load->view('resume/show');
public function list(){
//VERIFICATION QUE L'UTILISATEUR EST CONNECTE
if (!$this->session->has_userdata("email")) {
redirect('/connexion', 'refresh');
exit();
}
$data = array("intrusion" => $this->LogCapteurModele->getAll());
$this->load->view('resume/list', $data);
}
/*
public function show($actions){
foreach($actions as $action){
array_push($data, $this->ResumeModele->getByAction($action));
public function show(){
//VERIFICATION QUE L'UTILISATEUR EST CONNECTE
if (!$this->session->has_userdata("email")) {
redirect('/connexion', 'refresh');
exit();
}
$this->load->view('resume/show', $data);
}*/
$date = $this->uri->segment(3) . "/" . $this->uri->segment(4) . "/" . $this->uri->segment(5);
}
$date_parsed = date_parse_from_format("d/m/Y", $date);
if($date_parsed["error_count"]>0){
echo "Date invalide";
} else {
$date_parsed = DateTime::createFromFormat("d/m/Y", $date);
$data = array("date" => $date);
$capteur_log = $this->LogCapteurModele->getAllDay($date_parsed);
$mail_log = $this->LogEmailModele->getAllDay($date_parsed);
unset($mail_log["raw"]);
$data["list"] = array_merge_recursive($capteur_log, $mail_log);
foreach($data["list"] as $groupe=>$d){
usort($data["list"][$groupe], function($a1, $a2) {
return $a1->date->getTimestamp() - $a2->date->getTimestamp();
});
}
$this->load->view('resume/show', $data);
}
}
}
?>
\ No newline at end of file
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Simple Transactional Email</title>
<style>
/* -------------------------------------
GLOBAL RESETS
------------------------------------- */
/*All the styling goes here*/
img {
border: none;
-ms-interpolation-mode: bicubic;
max-width: 100%;
}
body {
background-color: #f6f6f6;
font-family: sans-serif;
-webkit-font-smoothing: antialiased;
font-size: 14px;
line-height: 1.4;
margin: 0;
padding: 0;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
}
table {
border-collapse: separate;
mso-table-lspace: 0pt;
mso-table-rspace: 0pt;
width: 100%;
}
table td {
font-family: sans-serif;
font-size: 14px;
vertical-align: top;
}
/* -------------------------------------
BODY & CONTAINER
------------------------------------- */
.body {
background-color: #f6f6f6;
width: 100%;
}
/* Set a max-width, and make it display as block so it will automatically stretch to that width, but will also shrink down on a phone or something */
.container {
display: block;
margin: 0 auto !important;
/* makes it centered */
max-width: 580px;
padding: 10px;
width: 580px;
}
/* This should also be a block element, so that it will fill 100% of the .container */
.content {
box-sizing: border-box;
display: block;
margin: 0 auto;
max-width: 580px;
padding: 10px;
}
/* -------------------------------------
HEADER, FOOTER, MAIN
------------------------------------- */
.main {
background: #ffffff;
border-radius: 3px;
width: 100%;
}
.wrapper {
box-sizing: border-box;
padding: 20px;
}
.content-block {
padding-bottom: 10px;
padding-top: 10px;
}
.footer {
clear: both;
margin-top: 10px;
text-align: center;
width: 100%;
}
.footer td,
.footer p,
.footer span,
.footer a {
color: #999999;
font-size: 12px;
text-align: center;
}
/* -------------------------------------
TYPOGRAPHY
------------------------------------- */
h1,
h2,
h3,
h4 {
color: #000000;
font-family: sans-serif;
font-weight: 400;
line-height: 1.4;
margin: 0;
margin-bottom: 30px;
}
h1 {
font-size: 35px;
font-weight: 300;
text-align: center;
text-transform: capitalize;
}
p,
ul,
ol {
font-family: sans-serif;
font-size: 14px;
font-weight: normal;
margin: 0;
margin-bottom: 15px;
}
p li,
ul li,
ol li {
list-style-position: inside;
margin-left: 5px;
}
a {
color: #3498db;
text-decoration: underline;
}
/* -------------------------------------
BUTTONS
------------------------------------- */
.btn {
box-sizing: border-box;
width: 100%;
}
.btn>tbody>tr>td {
padding-bottom: 15px;
}
.btn table {
width: auto;
}
.btn table td {
background-color: #ffffff;
border-radius: 5px;
text-align: center;
}
.btn a {
background-color: #ffffff;
border: solid 1px #3498db;
border-radius: 5px;
box-sizing: border-box;
color: #3498db;
cursor: pointer;
display: inline-block;
font-size: 14px;
font-weight: bold;
margin: 0;
padding: 12px 25px;
text-decoration: none;
text-transform: capitalize;
}
.btn-primary table td {
background-color: #3498db;
}
.btn-primary a {
background-color: #3498db;
border-color: #3498db;
color: #ffffff;
}
/* -------------------------------------
OTHER STYLES THAT MIGHT BE USEFUL
------------------------------------- */
.last {
margin-bottom: 0;
}
.first {
margin-top: 0;
}
.align-center {
text-align: center;
}
.align-right {
text-align: right;
}
.align-left {
text-align: left;
}
.clear {
clear: both;
}
.mt0 {
margin-top: 0;
}
.mb0 {
margin-bottom: 0;
}
.preheader {
color: transparent;
display: none;
height: 0;
max-height: 0;
max-width: 0;
opacity: 0;
overflow: hidden;
mso-hide: all;
visibility: hidden;
width: 0;
}
.powered-by a {
text-decoration: none;
}
hr {
border: 0;
border-bottom: 1px solid #f6f6f6;
margin: 20px 0;
}
/* -------------------------------------
RESPONSIVE AND MOBILE FRIENDLY STYLES
------------------------------------- */
@media only screen and (max-width: 620px) {
table[class=body] h1 {
font-size: 28px !important;
margin-bottom: 10px !important;
}
table[class=body] p,
table[class=body] ul,
table[class=body] ol,
table[class=body] td,
table[class=body] span,
table[class=body] a {
font-size: 16px !important;
}
table[class=body] .wrapper,
table[class=body] .article {
padding: 10px !important;
}
table[class=body] .content {
padding: 0 !important;