-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensorspresion.php
More file actions
263 lines (184 loc) · 7.71 KB
/
sensorspresion.php
File metadata and controls
263 lines (184 loc) · 7.71 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
session_start();
$logged = $_SESSION['logged'];
if(!$logged){
echo "Ingreso no autorizado";
die();
}else{
$user_name = $_SESSION['users_name'];
$user_id = $_SESSION['users_id'];
$user_password = $_SESSION['users_password'];
include 'conectionDB.php';
}
?>
<!DOCTYPE html>
<html lang="es">
<?php
include ('head.php');
?>
<body>
<div class="app" id="app">
<!-- ############ LAYOUT START-->
<?php
include ('BarraIzquierda.php');
?>
<!-- content -->
<div id="content" class="app-content box-shadow-z0" role="main">
<?php
include ('BarraDerecha.php');
include ('PiePagina.php');
?>
<div ui-view class="app-body" id="view">
<!-- SECCION CENTRAL -->
<div class="padding">
<!-- VALORES EN TIEMPO REAL -->
<div class="row">
<?php
$result = $conn->query("SELECT * FROM `devices` WHERE `id_user`='".$user_id."' AND `status`='1' order by `mqtt` ");
$devices = $result->fetch_all(MYSQLI_ASSOC);
$devices_num = count($devices);
?>
<?php for($i=0;$i<$devices_num;$i++){ ?>
<?php if($devices[$i]['tipo'] == "Sensor Presion" ){ ?>
<div class="col-xs-12 col-sm-4">
<div class="box p-a">
<div class="pull-left m-r">
<span class="w-48 rounded accent" title="<?php echo $devices[$i]['lugar']."->".$devices[$i]['ubicacion'] ?>">
<i class="fa fa-home"></i>
</span>
</div>
<div class="clear">
<h4 class="m-0 text-lg _300"><b id="presion_<?php echo $devices[$i]['nombre'] ?>">--</b><span class="text-sm"> °bar</span></h4>
<small class="text-muted">Presion <?php echo $devices[$i]['nombre'] ?></small>
</div>
</div>
</div>
<?php } ?>
<?php } ?>
</div>
</div>
<!-- ############ PAGE END-->
</div>
</div>
<!-- / -->
</div>
<?php
include ('SelectorTemas.php');
?>
<!-- ############ LAYOUT END-->
<!-- build:js scripts/app.html.js -->
<!-- jQuery -->
<script src="libs/jquery/jquery/dist/jquery.js"></script>
<!-- Bootstrap -->
<script src="libs/jquery/tether/dist/js/tether.min.js"></script>
<script src="libs/jquery/bootstrap/dist/js/bootstrap.js"></script>
<!-- core -->
<script src="libs/jquery/underscore/underscore-min.js"></script>
<script src="libs/jquery/jQuery-Storage-API/jquery.storageapi.min.js"></script>
<!--script src="libs/jquery/PACE/pace.min.js"></script-->
<script src="html/scripts/config.lazyload.js"></script>
<script src="html/scripts/palette.js"></script>
<script src="html/scripts/ui-load.js"></script>
<script src="html/scripts/ui-jp.js"></script>
<script src="html/scripts/ui-include.js"></script>
<script src="html/scripts/ui-device.js"></script>
<script src="html/scripts/ui-form.js"></script>
<script src="html/scripts/ui-nav.js"></script>
<script src="html/scripts/ui-screenfull.js"></script>
<script src="html/scripts/ui-scroll-to.js"></script>
<script src="html/scripts/ui-toggle-class.js"></script>
<script src="html/scripts/app.js"></script>
<!-- ajax -->
<script src="libs/jquery/jquery-pjax/jquery.pjax.js"></script>
<script src="html/scripts/ajax.js"></script>
<?php $tiempo = time(); ?>
<script type="text/javascript" src="linkPage.js?v=<?php echo $tiempo ?>"></script>
<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
<?php $tiempo = time(); ?>
<script type="text/javascript" src="Funciones.js?v=<?php echo $tiempo ?>"></script>
<script type="text/javascript">
if('<?php echo $_SESSION['conection_status_emqx']; ?>' == "ON"){
console.log('NO nos tenemos que conectar el brouker EMQX')
}else{
console.log('Conectando al brouker EMQX...')
console.log('<?php echo "Status Conection:" . $_SESSION['conection_status_emqx']; ?>')
<?php //$_SESSION['conection_status_emqx']="ON"; ?>
}
/*
*******************************************************************************
******************* CONEXION SETUP *************************************
*******************************************************************************
*/
//Esta parte esta funcionado para conexion por mqtt (demora mucho)
var options = {
connectTimeout: 2000,
// Authentication
clientId: '<?php echo "web_" . $user_name . rand(1,999) ; ?>',
username: '<?php echo $user_name; ?>',
password: '<?php echo $user_password; ?>',
keepalive: 60, //tiempo de mensaje interno hacia el brouker para avisar que estamos conectados
clean: false, //iniciamos en una session limpia (es una session no percistente)
}
// WebSocket connect url
var WebSocket_URL = 'wss://dataloggercdc.com:8094/mqtt'
//var client = mqtt.connect(WebSocket_URL, options)
var client = mqtt.connect(WebSocket_URL, options)
client.on('connect', () => {//original
console.log('Conexion exito con brouker')
<?php $_SESSION['conection_status_emqx'] = "ON"; ?>
console.log('<?php echo "Status Conection:" . $_SESSION['conection_status_emqx']; ?>')
top_topic = '<?php echo $user_name; ?>'+'/#';
client.subscribe(top_topic, { qos: 0 }, (error) => {
if (!error) {
console.log('Suscripción exitosa topics ->'+top_topic);
alert ('Suscripción exitosa topics ->'+top_topic);
}else{
console.log('Suscripción fallida!')
alert ('Suscripción fallida topics ->'+top_topic);
}
})
})
client.on('message', (topic, message) => {
let arr_topic = topic.split('/');
var tamaño_topic = arr_topic.length;
var top_topic = arr_topic[tamaño_topic-5];
var seccion_topic = arr_topic[tamaño_topic-4];
var subseccion_topic = arr_topic[tamaño_topic-3];
var tipo_topic = arr_topic[tamaño_topic-2];
var alias_topic = arr_topic[tamaño_topic-1];
console.log('Mensaje recibido: ',topic, ' -> ', message.toString())
if(tamaño_topic == 5){//recibimos un mensaje con longitud/caracteristica valida
console.log('tamaño topic: ', tamaño_topic,'\ntop: ', top_topic,'\nseccion: ', seccion_topic,'\nSub seccion: ', subseccion_topic,'\ntipo: ', tipo_topic,'\nalias: ', alias_topic,'\nvalor: ', message.toString())
<?php
$result = $conn->query("SELECT * FROM `devices` WHERE `id_user` = '$user_id' ");
$devices = $result->fetch_all(MYSQLI_ASSOC);
?>
<?php foreach ($devices as $device ) { ?>
if(topic == '<?php echo $device['mqtt']; ?>'){
if(tipo_topic == "Sensor Presion"){
value_presion_mqtt = message.toString();
$("#presion_"+alias_topic).html(value_presion_mqtt);
}
}
<?php } ?>
}else{
console.log('Topic no Valido, la longitud no es la admitida por nuestro protocolo')
}
})
/*
client.on('reconnect', (error) => {
console.log('Reconectando....:', error)
<?php $_SESSION['conection_status_emqx'] = "OFF"; ?>
console.log('<?php echo $_SESSION['conection_status_emqx']; ?>')
})
*/
///////////////////////////////////////////////////////////////////////////////////
client.on('error', (error) => {
console.log('Connect Error:', error)
$_SESSION['conection_status_emqx'] = "OFF"
})
//*******************************************************************************
</script>
<!-- endbuild -->
</body>
</html>