Skip to content
Snippets Groups Projects
Backend.php 101 KiB
Newer Older
root's avatar
root committed
                                case Unit::I64:
                                    $value = Unpack::uInt64($value_blob);
                                    break;
                                case Unit::F32:
                                    $value = Unpack::f32($value_blob);
                                    break;
                                case Unit::D64:
                                    $value = Unpack::d64($value_blob);
                                    break;
                                default:
                                    if(strlen($value_blob) == 1)
                                        $value = Unpack::uInt8($value_blob);
                                    ////Logger::debug("size: ".strlen($value_blob), true);
                                    //$value = Unpack::uInt8($value_blob)  ??
                                    //         Unpack::uInt32($value_blob) ??
                                    //         Unpack::uInt64($value_blob) ??
                                    //         Unpack::f32($value_blob)    ??
                                    //         Unpack::d64($value_blob);
                                    break;
                            }

                            if($value === null){
                                if($value_blob != null)
                                    $value = $value_blob; // TODO: remove this in the new API
                                else
                                    $value = -400; // TODO: remover isso quando valores inválidos não puderem mais ser inseridos
                            }
                        } else {
                            $value = (double)$entry->get('value'); // double
                        }

                        $x = $selected_rows[$row_id]->x;
                        $y = $selected_rows[$row_id]->y;
                        $z = $selected_rows[$row_id]->z;

                        // TODO: what about digital entries?
                        $error = (int)$entry->get('error');             //tinyint
                        $conf  = (int)$entry->get('confidence');        //tinyint
                        //$x     = (int)$entry->get('x');                 //int    // ONLY MOBILE
                        //$y     = (int)$entry->get('y');                 //int    // ONLY MOBILE
                        //$z     = (int)$entry->get('z');                 //int    // ONLY MOBILE
                        $d     = 0; // TODO: what about the dev origin?
                        $gw    = (int)$entry->get('gw');                //int (TO DO GET GATEWAY)

                        // TODO: check this filter here
                        if ( $filter_sphere && !$series->contains_point($x, $y, $z) )
                            continue;

                        $smartdata = null;

                        switch ($series->version) {
                            case SmartData::STATIC_VERSION:
                                $smartdata = new StaticSmartData($series->unit,$value,$error,$conf,$x,$y,$z,$time,$d,$gw);
                                break;
                            case SmartData::MOBILE_VERSION:
                                $smartdata = new MobileSmartData($series->unit,$value,$error,$conf,$x,$y,$z,$time,$d,$gw, null);
                                break;
                            default:
                                continue 2;
                                break;
                        }

                        if($aggregator != null) {
                            $smartdata = $aggregator->aggregate($smartdata);
                        }

                        if($smartdata != null) {
                            if(is_array($smartdata)){
                                foreach ($smartdata as $key => $data) {
                                    $response_json['series'][$index++] = $data->toArray();
                                    if ($index >= Config::config()::POINTS_LIMIT) break;
                                }
                            } else {
                                $response_json['series'][$index++] = $smartdata->toArray();
                                if($this->internal()){
                                    $response_json['series'][$index-1]['slice'] = $row_id;
                                    $response_json['series'][$index-1]['gw'] = $gw;
                                }
                            }
                        } else {
                            continue;
                        }

                        if ($index >= Config::config()::POINTS_LIMIT) break 2;
                    }
                    $result_page = $result_page->nextPage();

                    if(!$result_page && isset($aggregator) && $index < Config::config()::POINTS_LIMIT){
                        $smartdata = $aggregator->finish();
                        if($smartdata != null) {
                            $response_json['series'][$index++] = $smartdata->toArray();
                        }
                    }

                }

                $session->close();

                usort($response_json['series'], function ($a, $b) {
                    $a_timestamp  = gmp_init($a['timestamp']);
                    $b_timestamp  = gmp_init($b['timestamp']);
                    return gmp_cmp($a_timestamp, $b_timestamp);
                });
                //self::debug_X('Returning the query result');
            }
        } catch (\Exception $e) {
            Logger::exception($e);
            $response_json = NULL;
        } finally {
            if (isset($session))
                $session->close();
            $conn = null;
        }

        return $response_json;
    }

    public function track(Series $series, $aggr=null) {
        return array('series' => array());
    }
}

switch (Config_Common::api_version()) {
    case Config_Common::VERSION_1_0: class Backend extends Backend_V1_0 {} break;
    case Config_Common::VERSION_1_1: class Backend extends Backend_V1_1 {} break;
    default:
        die ("Bad API version!\n");
}