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
|
function get_measure($station, $start, $end, $type_ids_array, $connect, $debug){
if (count($type_ids_array)>1) {
$escaped_type_ids_array = array_map(array($connect, 'real_escape_string'), $type_ids_array);
$type_ids_list = implode( ',', $escaped_type_ids_array ); // transform th array into a string
}else if(count($type_ids_array)==1){
$type_ids_list = $type_ids_array[0];
}
else{
$type_ids_list = null;
}
if(empty($type_ids_list)){
$clauseTypes = "";
}else{
$clauseTypes = " AND stype.id_sensor_type IN (" . $type_ids_list . ") ";
}
$clauseTime=' AND c.ttn_m_time >= "'.$start.'" AND c.ttn_m_time <= "'.$end.'"';
$sql_select ='SELECT fi.field_name, st.id_station, st.station_name, st.station_archive, st.station_longname, st.stations_types_id_stations_type, se.sensor_name, se.sensor_longname, se.chart_pointStyle_id_chart_pointStyle, se.chart_pointRadius, se.chart_fill, se.chart_showLine, se.chart_borderWidth, se.chart_pointHoverRadius, se.id_sensor, stype.measure_unit, stype.id_sensor_type, stype.sensor_type_name, stype.sensor_type_awe, stype.sensor_type_longname, m.value, c.id_collection, c.collection_created, c.ttn_m_time,
cs.chart_style_value, cp.chart_pointStyle_value,
col_bg.color_name as bg_color_name, col_bg.color_value as bg_color_value,
col_bd.color_name as bd_color_name, col_bd.color_value as bd_color_value
FROM fields AS fi
RIGHT JOIN stations AS st ON fi.id_field = st.fields_id_field
INNER JOIN sensors AS se ON st.id_station = se.stations_id_station
INNER JOIN sensor_types AS stype ON se.sensor_types_id_sensor_type = stype.id_sensor_type
INNER JOIN measures AS m ON se.id_sensor = m.sensors_id_sensor
INNER JOIN collections AS c ON m.collections_id_collection = c.id_collection
INNER JOIN chart_backgroundColor AS cbgc on cbgc.id_chart_backgroundColor=se.chart_backgroundColor_id_chart_backgroundColor
INNER JOIN colors AS col_bg on cbgc.colors_id_colors=col_bg.id_colors
INNER JOIN chart_borderColor AS cbdc on cbdc.id_chart_borderColor=se.chart_borderColor_id_chart_borderColor
INNER JOIN colors AS col_bd on cbdc.colors_id_colors=col_bd.id_colors
INNER JOIN chart_style AS cs on se.chart_style_id_chart_style = cs.id_chart_style
INNER JOIN chart_pointStyle AS cp on se.chart_pointStyle_id_chart_pointStyle = cp.id_chart_pointStyle
WHERE st.id_station = '.$station.' AND se.sensor_active = 1 AND st.station_active = 1 AND fi.field_active = 1
'.$clauseTime.'
'.$clauseTypes.'
ORDER BY c.ttn_m_time ASC, stype.sensor_type_order ASC, se.id_sensor ASC';
$sql_result = $connect->query($sql_select);
$data=array();
$dates=array(); // To collect the dates of the measures
$sensors_id = array();
if(is_object($sql_result))
{
if($sql_result->num_rows>0)
{
$j=0;
$afterLunchTime = false;
// Collect all times of measures and the id of the sensor
while($row = $sql_result->fetch_assoc())
{
/* Convert UTC to Local Time */
$dates[$j] = $row['ttn_m_time'];
// $dates[$j] = convertUTCtoZurich($row['ttn_m_time'], false);
// If the retrieved sensor ID is not in the array ...
// This has been done to make sure to initiate the data array with a default value
// !!!
if(!in_array($row['id_sensor'],$sensors_id))
{
// ... add the ID into the array as a key
array_push($sensors_id,$row['id_sensor']);
// ... give the default value
$data[$row['id_sensor']]['labels'][0] = 0;
$data[$row['id_sensor']]['datasets']['data'][0]['value'] = 0.1;
}
$j++;
}
$dates= array_unique($dates);
// ORDER
sort($dates);
$dates = array_reverse($dates);
// Add the date/time
foreach($data as $d => $dd){
foreach($dates as $ds => $dss){
//TODO
// Convert date from UTC to local time
$data[$d]['labels'][$ds]=$dss; // Add the date / time
if(count($type_ids_array)<=0){
$data[$d]['datasets']['data'][$ds]['value']=0.1; // and make sure the is corresponding row
$data[$d]['datasets']['data'][$ds]['date']=$dss;
}
$mmdd = date_create($dss)->format('M-d');
$hhii = date_create($dss)->format('h:i:s'); //$mmdd->format('h:i:s');
//$mmdd = $mmdd->format('m:d');
}
}
$sql_result->data_seek(0); // roll back the index to the begining
$j=0;
while($row = $sql_result->fetch_assoc())
{
$data[$row['id_sensor']]['unit'] = $row['measure_unit'];
$data[$row['id_sensor']]['chartContainer'] = $row['sensor_type_name'];
$data[$row['id_sensor']]['sensor_type'] = $row['sensor_type_name'];
$data[$row['id_sensor']]['sensor_type_awe'] = $row['sensor_type_awe'];
$data[$row['id_sensor']]['sensor_type_longname'] = $row['sensor_type_longname'];
$data[$row['id_sensor']]['datasets']['label'] = $row['sensor_longname']." [".$row['measure_unit']."]";
$data[$row['id_sensor']]['id_sensor_type'] = $row['id_sensor_type'];
$data[$row['id_sensor']]['id_sensor'] = $row['id_sensor'];
//$data[$row['id_sensor']]['station_archive'] = $row['station_archive'];
$data[$row['id_sensor']]['datasets']['yAxisID'] = 'y-axis-'.array_search($row['id_sensor_type'], $type_ids_array);
// Searches the retrived date into $date and return corresponding
$n = array_search($row['ttn_m_time'],$dates);
// And add now the measure into the $n index
if(count($type_ids_array)<=0){
$data[$row['id_sensor']]['datasets']['data'][$n]['value'] = $row['value'];
$data[$row['id_sensor']]['datasets']['data'][$n]['dataPointText'] = $row['value'];
$data[$row['id_sensor']]['datasets']['data'][$n]['label'] = date_create(convertUTCtoZurich($row['ttn_m_time'],false))->format('H:i');
}
else
{
// ICI ICI ICI ICI
$data[$row['id_sensor']]['datasets']['data'][$n] = $row['value'];
}
// Add other measures and parameters
$data[$row['id_sensor']]['datasets']['type'] = $row['chart_style_value'];
$data[$row['id_sensor']]['datasets']['borderWidth'] = $row['chart_borderWidth'];
$data[$row['id_sensor']]['datasets']['borderColor'] = 'rgba('.$row['bd_color_value'].', 1)';
$data[$row['id_sensor']]['datasets']['backgroundColor'] = 'rgba('.$row['bg_color_value'].', 1)';
if($row['chart_style_value'] != "bar")
{
$data[$row['id_sensor']]['datasets']['pointRadius'] = $row['chart_pointRadius'];
$data[$row['id_sensor']]['datasets']['pointHoverRadius'] = $row['chart_pointHoverRadius'];
$data[$row['id_sensor']]['datasets']['showLine'] = $row['chart_showLine'];
//$data[$row['id_sensor']]['datasets']['steppedLine'] = true;
}
$data[$row['id_sensor']]['datasets']['showLine'] = $row['chart_showLine'];
$data[$row['id_sensor']]['datasets']['pointStyle'] = $row['chart_pointStyle_value'];
if($row['chart_fill']==0){
$data[$row['id_sensor']]['datasets']['fill'] = false;
}
else
{
$data[$row['id_sensor']]['datasets']['fill'] = $row['chart_fill'];
}
} // END WHILE
$data = array_values($data);
return $data;
} // END if($sql_result->num_rows>0)
else
{
return $data; // No data
}
} // END if(is_object($sql_result))
else
{
return $data;
}
} |
Partager