示例#1
0
function vis_controller()
{
    global $mysqli, $redis, $session, $route, $user, $feed_settings;
    $result = false;
    require "Modules/feed/feed_model.php";
    $feed = new Feed($mysqli, $redis, $feed_settings);
    require "Modules/vis/multigraph_model.php";
    $multigraph = new Multigraph($mysqli);
    $visdir = "vis/visualisations/";
    require "Modules/vis/vis_object.php";
    $write_apikey = "";
    $read_apikey = "";
    if ($session['read']) {
        $read_apikey = $user->get_apikey_read($session['userid']);
    }
    if ($session['write']) {
        $write_apikey = $user->get_apikey_write($session['userid']);
    }
    if ($route->format == 'html') {
        if ($route->action == 'list' && $session['write']) {
            $multigraphs = $multigraph->getlist($session['userid']);
            $feedlist = $feed->get_user_feeds($session['userid']);
            $result = view("Modules/vis/Views/vis_main_view.php", array('user' => $user->get($session['userid']), 'feedlist' => $feedlist, 'apikey' => $read_apikey, 'visualisations' => $visualisations, 'multigraphs' => $multigraphs));
        } else {
            if ($route->action == "auto") {
                $feedid = intval(get('feedid'));
                $datatype = $feed->get_field($feedid, 'datatype');
                if ($datatype == 0) {
                    $result = "Feed type or authentication not valid";
                }
                if ($datatype == 1) {
                    $route->action = 'graph';
                }
                if ($datatype == 2) {
                    $route->action = 'bargraph';
                }
                if ($datatype == 3) {
                    $route->action = 'histgraph';
                }
            }
        }
        while ($vis = current($visualisations)) {
            $viskey = key($visualisations);
            // If the visualisation has a set property called action
            // then override the visualisation key and use the set action instead
            if (isset($vis['action'])) {
                $viskey = $vis['action'];
            }
            if ($route->action == $viskey) {
                $array = array();
                $array['valid'] = true;
                if (isset($vis['options'])) {
                    foreach ($vis['options'] as $option) {
                        $key = $option[0];
                        $type = $option[2];
                        if (isset($option[3])) {
                            $default = $option[3];
                        } else {
                            $default = "";
                        }
                        if ($type == 0 || $type == 1 || $type == 2 || $type == 3) {
                            $feedid = (int) get($key);
                            if ($feedid) {
                                $f = $feed->get($feedid);
                                $array[$key] = $feedid;
                                $array[$key . 'name'] = $f['name'];
                                if ($f['userid'] != $session['userid']) {
                                    $array['valid'] = false;
                                }
                                if ($f['public']) {
                                    $array['valid'] = true;
                                }
                            } else {
                                $array['valid'] = false;
                            }
                        } else {
                            if ($type == 4) {
                                // Boolean not used at the moment
                                if (get($key) == true || get($key) == false) {
                                    $array[$key] = get($key);
                                } else {
                                    $array[$key] = $default;
                                }
                            } else {
                                if ($type == 5) {
                                    $array[$key] = preg_replace('/[^\\p{L}_\\p{N}\\s£$€¥]/u', '', get($key)) ? get($key) : $default;
                                } else {
                                    if ($type == 6) {
                                        $array[$key] = str_replace(',', '.', floatval(get($key) ? get($key) : $default));
                                    } else {
                                        if ($type == 7) {
                                            $array[$key] = intval(get($key) ? get($key) : $default);
                                        } else {
                                            if ($type == 8) {
                                                $mid = (int) get($key);
                                                if ($mid) {
                                                    $f = $multigraph->get($mid, $session['userid']);
                                                    $array[$key] = intval($mid ? $mid : $default);
                                                    if (!isset($f['feedlist'])) {
                                                        $array['valid'] = false;
                                                    }
                                                } else {
                                                    $array['valid'] = false;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        # we need to either urlescape the colour, or just scrub out invalid chars. I'm doing the second, since
                        # we can be fairly confident that colours are eiter a hex or a simple word (e.g. "blue" or such)
                        if ($key == "colour") {
                            $array[$key] = preg_replace('/[^\\dA-Za-z]/', '', $array[$key]);
                        }
                    }
                }
                $array['apikey'] = $read_apikey;
                $array['write_apikey'] = $write_apikey;
                if ($array['valid'] == false) {
                    $result .= "<div style='position:absolute; top:0px; left:0px; width:100%; height:100%; display: table;'><div class='alert-error' style='text-align:center; display:table-cell; vertical-align:middle;'><h4>" . _('Not configured') . "<br>" . _('or') . "<br>" . _('Authentication not valid') . "</h4></div></div>";
                } else {
                    $result .= view("Modules/" . $visdir . $viskey . ".php", $array);
                }
            }
            next($visualisations);
        }
    } else {
        if ($route->format == 'json' && $route->action == 'multigraph') {
            if ($route->subaction == 'get') {
                $result = $multigraph->get(get('id'), $session['userid']);
            } else {
                if ($route->subaction == 'getlist') {
                    $result = $multigraph->getlist($session['userid']);
                } else {
                    if ($session['write']) {
                        if ($route->subaction == 'new') {
                            $result = $multigraph->create($session['userid']);
                        } else {
                            if ($route->subaction == 'delete') {
                                $result = $multigraph->delete(get('id'), $session['userid']);
                            } else {
                                if ($route->subaction == 'set') {
                                    $result = $multigraph->set(get('id'), $session['userid'], get('feedlist'), get('name'));
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return array('content' => $result);
}
示例#2
0
function feed_controller()
{
    global $mysqli, $redis, $session, $route, $feed_settings;
    $result = false;
    require_once "Modules/feed/feed_model.php";
    $feed = new Feed($mysqli, $redis, $feed_settings);
    if ($route->format == 'html') {
        if ($route->action == "list" && $session['write']) {
            $result = view("Modules/feed/Views/feedlist_view.php", array());
        } else {
            if ($route->action == "api" && $session['write']) {
                $result = view("Modules/feed/Views/feedapi_view.php", array());
            }
        }
    } else {
        if ($route->format == 'json') {
            // Public actions available on public feeds.
            if ($route->action == "list") {
                if ($session['read']) {
                    if (!isset($_GET['userid']) || isset($_GET['userid']) && $_GET['userid'] == $session['userid']) {
                        $result = $feed->get_user_feeds($session['userid']);
                    } else {
                        if (isset($_GET['userid']) && $_GET['userid'] != $session['userid']) {
                            $result = $feed->get_user_public_feeds(get('userid'));
                        }
                    }
                } else {
                    if (isset($_GET['userid'])) {
                        $result = $feed->get_user_public_feeds(get('userid'));
                    }
                }
            } elseif ($route->action == "create" && $session['write']) {
                $result = $feed->create($session['userid'], get('tag'), get('name'), get('datatype'), get('engine'), json_decode(get('options')));
            } elseif ($route->action == "updatesize" && $session['write']) {
                $result = $feed->update_user_feeds_size($session['userid']);
            } elseif ($route->action == "buffersize" && $session['write']) {
                $result = $feed->get_buffer_size();
                // To "fetch" multiple feed values in a single request
                // http://emoncms.org/feed/fetch.json?ids=123,567,890
            } elseif ($route->action == "fetch" && $session['read']) {
                $feedids = (array) explode(",", get('ids'));
                for ($i = 0; $i < count($feedids); $i++) {
                    $feedid = (int) $feedids[$i];
                    if ($feed->exist($feedid)) {
                        // if the feed exists
                        $result[$i] = $feed->get_value($feedid);
                        // null is a valid response
                    } else {
                        $result[$i] = false;
                    }
                    // false means feed not found
                }
            } else {
                $feedid = (int) get('id');
                // Actions that operate on a single existing feed that all use the feedid to select:
                // First we load the meta data for the feed that we want
                if ($feed->exist($feedid)) {
                    $f = $feed->get($feedid);
                    // if public or belongs to user
                    if ($f['public'] || $session['userid'] > 0 && $f['userid'] == $session['userid'] && $session['read']) {
                        if ($route->action == "timevalue") {
                            $result = $feed->get_timevalue($feedid);
                        } else {
                            if ($route->action == 'data') {
                                $skipmissing = 1;
                                $limitinterval = 1;
                                if (isset($_GET['skipmissing']) && $_GET['skipmissing'] == 0) {
                                    $skipmissing = 0;
                                }
                                if (isset($_GET['limitinterval']) && $_GET['limitinterval'] == 0) {
                                    $limitinterval = 0;
                                }
                                $result = $feed->get_data($feedid, get('start'), get('end'), get('interval'), $skipmissing, $limitinterval);
                            } else {
                                if ($route->action == "value") {
                                    $result = $feed->get_value($feedid);
                                } else {
                                    if ($route->action == "get") {
                                        $result = $feed->get_field($feedid, get('field'));
                                    } else {
                                        if ($route->action == "aget") {
                                            $result = $feed->get($feedid);
                                        } else {
                                            if ($route->action == 'histogram') {
                                                $result = $feed->histogram_get_power_vs_kwh($feedid, get('start'), get('end'));
                                            } else {
                                                if ($route->action == 'kwhatpower') {
                                                    $result = $feed->histogram_get_kwhd_atpower($feedid, get('min'), get('max'));
                                                } else {
                                                    if ($route->action == 'kwhatpowers') {
                                                        $result = $feed->histogram_get_kwhd_atpowers($feedid, get('points'));
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    // write session required
                    if (isset($session['write']) && $session['write'] && $session['userid'] > 0 && $f['userid'] == $session['userid']) {
                        // Storage engine agnostic
                        if ($route->action == 'set') {
                            $result = $feed->set_feed_fields($feedid, get('fields'));
                        } else {
                            if ($route->action == "insert") {
                                $result = $feed->insert_data($feedid, time(), get("time"), get("value"));
                            } else {
                                if ($route->action == "update") {
                                    $result = $feed->update_data($feedid, time(), get("time"), get('value'));
                                } else {
                                    if ($route->action == "delete") {
                                        $result = $feed->delete($feedid);
                                    } else {
                                        if ($route->action == "getmeta") {
                                            $result = $feed->get_meta($feedid);
                                        } else {
                                            if ($route->action == "csvexport") {
                                                $result = $feed->csv_export($feedid, get('start'), get('end'), get('interval'), get('timeformat'));
                                            } else {
                                                if ($route->action == "process") {
                                                    if ($f['engine'] != Engine::VIRTUALFEED) {
                                                        $result = array('success' => false, 'message' => 'Feed is not Virtual');
                                                    } else {
                                                        if ($route->subaction == "get") {
                                                            $result = $feed->get_processlist($feedid);
                                                        } else {
                                                            if ($route->subaction == "set") {
                                                                $result = $feed->set_processlist($feedid, post('processlist'));
                                                            } else {
                                                                if ($route->subaction == "reset") {
                                                                    $result = $feed->reset_processlist($feedid);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if ($f['engine'] == Engine::MYSQL || $f['engine'] == Engine::MYSQLMEMORY) {
                            if ($route->action == "export") {
                                $result = $feed->mysqltimeseries_export($feedid, get('start'));
                            } else {
                                if ($route->action == "deletedatapoint") {
                                    $result = $feed->mysqltimeseries_delete_data_point($feedid, get('feedtime'));
                                } else {
                                    if ($route->action == "deletedatarange") {
                                        $result = $feed->mysqltimeseries_delete_data_range($feedid, get('start'), get('end'));
                                    }
                                }
                            }
                        } elseif ($f['engine'] == Engine::PHPTIMESERIES) {
                            if ($route->action == "export") {
                                $result = $feed->phptimeseries_export($feedid, get('start'));
                            }
                        } elseif ($f['engine'] == Engine::PHPFIWA) {
                            if ($route->action == "export") {
                                $result = $feed->phpfiwa_export($feedid, get('start'), get('layer'));
                            }
                        } elseif ($f['engine'] == Engine::PHPFINA) {
                            if ($route->action == "export") {
                                $result = $feed->phpfina_export($feedid, get('start'));
                            }
                        }
                    }
                } else {
                    $result = array('success' => false, 'message' => 'Feed does not exist');
                }
            }
        }
    }
    return array('content' => $result);
}
示例#3
0
$usertimezone = $user->get_timezone($session['userid']);
$now = DateTime::createFromFormat("U", $time);
$now->setTimezone(new DateTimeZone($usertimezone));
$now->setTime(23, 59, 59);
// at 23:59:59
$now->modify("last day of previous month");
$end = $now->format("U");
$endText = $now->format("YmdHis");
// today
$now->setTime(0, 0);
// at 00:00
$now->modify("first day of this month");
$start = $now->format("U");
$startText = $now->format("YmdHis");
// Get user feeds
$userfeeds = $feed->get_user_feeds($session['userid']);
$groups = array();
foreach ($userfeeds as $f) {
    $groups[$f['tag']][] = $f['id'];
}
echo "Starting export job ({$startText}-{$endText}) interval {$outinterval} for user '{$userid}'.\n";
$log->info("Starting export job ({$startText}-{$endText}) interval {$outinterval} for user '{$userid}'.\n");
// Get feed ids grouped by tags
foreach ($groups as $tag => $ids) {
    echo "    Processing '{$tag}' tag with feeds: " . implode(",", $ids) . "\n";
    $log->info("    Processing '{$tag}' tag with feeds: " . implode(",", $ids) . "\n");
    // Write to output stream
    $filename = $exportpath . "/" . $startText . "_" . $endText . "_" . $tag . "_" . implode("_", $ids) . ".csv";
    $fh = @fopen($filename, 'w');
    if (!$fh) {
        echo "ERROR: Cant create file '{$filename}'.\n";
示例#4
0
function vis_controller()
{
    global $mysqli, $redis, $session, $route, $user, $feed_settings;
    $result = false;
    require "Modules/feed/feed_model.php";
    $feed = new Feed($mysqli, $redis, $feed_settings);
    require "Modules/vis/multigraph_model.php";
    $multigraph = new Multigraph($mysqli);
    $visdir = "vis/visualisations/";
    require "Modules/vis/vis_object.php";
    $write_apikey = "";
    $read_apikey = "";
    if ($session['read']) {
        $read_apikey = $user->get_apikey_read($session['userid']);
    }
    if ($session['write']) {
        $write_apikey = $user->get_apikey_write($session['userid']);
    }
    if ($route->format == 'html') {
        if ($route->action == 'list' && $session['write']) {
            $multigraphs = $multigraph->getlist($session['userid']);
            $feedlist = $feed->get_user_feeds($session['userid']);
            $result = view("Modules/vis/vis_main_view.php", array('user' => $user->get($session['userid']), 'feedlist' => $feedlist, 'apikey' => $read_apikey, 'visualisations' => $visualisations, 'multigraphs' => $multigraphs));
        } else {
            // and is used primarily for quick checking feeds from the feeds page.
            if ($route->action == "auto") {
                $feedid = (int) get('feedid');
                $route->action = 'graph';
            }
            while ($vis = current($visualisations)) {
                $viskey = key($visualisations);
                // If the visualisation has a set property called action
                // then override the visualisation key and use the set action instead
                if (isset($vis['action'])) {
                    $viskey = $vis['action'];
                }
                if ($route->action == $viskey) {
                    $array = array();
                    $array['valid'] = true;
                    if (isset($vis['options'])) {
                        foreach ($vis['options'] as $option) {
                            $key = $option[0];
                            $type = $option[1];
                            if (isset($option[2])) {
                                $default = $option[2];
                            } else {
                                $default = "";
                            }
                            if ($type == 0 || $type == 1 || $type == 2 || $type == 3) {
                                $feedid = (int) get($key);
                                if ($feedid) {
                                    $f = $feed->get($feedid);
                                    $array[$key] = $feedid;
                                    $array[$key . 'name'] = $f['name'];
                                    if ($f['userid'] != $session['userid']) {
                                        $array['valid'] = false;
                                        $array['error'] = "You dont have the required permission to view this feed";
                                    }
                                    if ($f['public']) {
                                        $array['valid'] = true;
                                    }
                                } else {
                                    $array['valid'] = false;
                                    $array['error'] = "Please select a feed";
                                }
                            }
                            // Boolean not used at the moment
                            if ($type == 4) {
                                if (get($key) == true || get($key) == false) {
                                    $array[$key] = get($key);
                                } else {
                                    $array[$key] = $default;
                                }
                            }
                            if ($type == 5) {
                                $array[$key] = preg_replace('/[^\\w\\s£$€¥]/', '', get($key)) ? get($key) : $default;
                            }
                            if ($type == 6) {
                                $array[$key] = str_replace(',', '.', floatval(get($key) ? get($key) : $default));
                            }
                            if ($type == 7) {
                                $array[$key] = intval(get($key) ? get($key) : $default);
                            }
                            # we need to either urlescape the colour, or just scrub out invalid chars. I'm doing the second, since
                            # we can be fairly confident that colours are eiter a hex or a simple word (e.g. "blue" or such)
                            if ($key == "colour") {
                                $array[$key] = preg_replace('/[^\\dA-Za-z]/', '', $array[$key]);
                            }
                        }
                    }
                    $array['apikey'] = $read_apikey;
                    $array['write_apikey'] = $write_apikey;
                    $result = view("Modules/" . $visdir . $viskey . ".php", $array);
                    if ($array['valid'] == false) {
                        $result .= "<div style='position:absolute; top:0px; left:0px; background-color:rgba(240,240,240,0.5); width:100%; height:100%; text-align:center; padding-top:100px;'><h3>" . $array['error'] . "</h3></div>";
                    }
                }
                next($visualisations);
            }
        }
    }
    /*
    MULTIGRAPH ACTIONS
    */
    if ($route->format == 'json' && $route->action == 'multigraph') {
        if ($route->subaction == 'new' && $session['write']) {
            $result = $multigraph->create($session['userid']);
        }
        if ($route->subaction == 'delete' && $session['write']) {
            $result = $multigraph->delete(get('id'), $session['userid']);
        }
        if ($route->subaction == 'set' && $session['write']) {
            $result = $multigraph->set(get('id'), $session['userid'], get('feedlist'), get('name'));
        }
        if ($route->subaction == 'get') {
            $result = $multigraph->get(get('id'), $session['userid']);
        }
        if ($route->subaction == 'getlist') {
            $result = $multigraph->getlist($session['userid']);
        }
        if ($route->subaction == 'getname') {
            $result = $multigraph->getname(get('id'), $session['userid']);
        }
    }
    return array('content' => $result);
}
示例#5
0
function feed_controller()
{
    global $mysqli, $redis, $session, $route, $feed_settings;
    $result = false;
    include "Modules/feed/feed_model.php";
    $feed = new Feed($mysqli, $redis, $feed_settings);
    if ($route->format == 'html') {
        if ($route->action == "list" && $session['write']) {
            $result = view("Modules/feed/Views/feedlist_view.php", array());
        }
        if ($route->action == "api" && $session['write']) {
            $result = view("Modules/feed/Views/feedapi_view.php", array());
        }
    }
    if ($route->format == 'json') {
        // Public actions available on public feeds.
        if ($route->action == "list") {
            if (!isset($_GET['userid']) && $session['read']) {
                $result = $feed->get_user_feeds($session['userid']);
            }
            if (isset($_GET['userid']) && $session['read'] && $_GET['userid'] == $session['userid']) {
                $result = $feed->get_user_feeds($session['userid']);
            }
            if (isset($_GET['userid']) && $session['read'] && $_GET['userid'] != $session['userid']) {
                $result = $feed->get_user_public_feeds(get('userid'));
            }
            if (isset($_GET['userid']) && !$session['read']) {
                $result = $feed->get_user_public_feeds(get('userid'));
            }
        } elseif ($route->action == "getid" && $session['read']) {
            $result = $feed->get_id($session['userid'], get('name'));
        } elseif ($route->action == "create" && $session['write']) {
            $result = $feed->create($session['userid'], get('name'), get('datatype'), get('engine'), json_decode(get('options')));
        } elseif ($route->action == "updatesize" && $session['write']) {
            $result = $feed->update_user_feeds_size($session['userid']);
            // To "fetch" multiple feed values in a single request
            // http://emoncms.org/feed/fetch.json?ids=123,567,890
        } elseif ($route->action == "fetch" && $session['read']) {
            $feedids = (array) explode(",", get('ids'));
            for ($i = 0; $i < count($feedids); $i++) {
                $feedid = (int) $feedids[$i];
                if ($feed->exist($feedid)) {
                    $result[$i] = (double) $feed->get_value($feedid);
                } else {
                    $result[$i] = "";
                }
            }
        } else {
            $feedid = (int) get('id');
            // Actions that operate on a single existing feed that all use the feedid to select:
            // First we load the meta data for the feed that we want
            if ($feed->exist($feedid)) {
                $f = $feed->get($feedid);
                // if public or belongs to user
                if ($f['public'] || $session['userid'] > 0 && $f['userid'] == $session['userid'] && $session['read']) {
                    if ($route->action == "value") {
                        $result = $feed->get_value($feedid);
                    }
                    if ($route->action == "timevalue") {
                        $result = $feed->get_timevalue_seconds($feedid);
                    }
                    if ($route->action == "get") {
                        $result = $feed->get_field($feedid, get('field'));
                    }
                    // '/[^\w\s-]/'
                    if ($route->action == "aget") {
                        $result = $feed->get($feedid);
                    }
                    if ($route->action == 'histogram') {
                        $result = $feed->histogram_get_power_vs_kwh($feedid, get('start'), get('end'));
                    }
                    if ($route->action == 'kwhatpower') {
                        $result = $feed->histogram_get_kwhd_atpower($feedid, get('min'), get('max'));
                    }
                    if ($route->action == 'kwhatpowers') {
                        $result = $feed->histogram_get_kwhd_atpowers($feedid, get('points'));
                    }
                    if ($route->action == 'data') {
                        $result = $feed->get_data($feedid, get('start'), get('end'), get('dp'));
                    }
                    if ($route->action == 'average') {
                        $result = $feed->get_average($feedid, get('start'), get('end'), get('interval'));
                    }
                    if ($route->action == 'history') {
                        $result = $feed->get_history($feedid, get('start'), get('end'), get('interval'));
                    }
                }
                // write session required
                if (isset($session['write']) && $session['write'] && $session['userid'] > 0 && $f['userid'] == $session['userid']) {
                    // Storage engine agnostic
                    if ($route->action == 'set') {
                        $result = $feed->set_feed_fields($feedid, get('fields'));
                    }
                    if ($route->action == "insert") {
                        $result = $feed->insert_data($feedid, time(), get("time"), get("value"));
                    }
                    if ($route->action == "update") {
                        $result = $feed->update_data($feedid, time(), get("time"), get('value'));
                    }
                    if ($route->action == "delete") {
                        $result = $feed->delete($feedid);
                    }
                    if ($route->action == "getmeta") {
                        $result = $feed->get_meta($feedid);
                    }
                    if ($route->action == "csvexport") {
                        $feed->csv_export($feedid, get('start'), get('end'), get('interval'));
                    }
                    if ($f['engine'] == Engine::TIMESTORE) {
                        if ($route->action == "export") {
                            $result = $feed->timestore_export($feedid, get('start'), get('layer'));
                        }
                        if ($route->action == "exportmeta") {
                            $result = $feed->timestore_export_meta($feedid);
                        }
                        if ($route->action == "scalerange") {
                            $result = $feed->timestore_scale_range($feedid, get('start'), get('end'), get('value'));
                        }
                    } elseif ($f['engine'] == Engine::MYSQL) {
                        if ($route->action == "export") {
                            $result = $feed->mysqltimeseries_export($feedid, get('start'));
                        }
                        if ($route->action == "deletedatapoint") {
                            $result = $feed->mysqltimeseries_delete_data_point($feedid, get('feedtime'));
                        }
                        if ($route->action == "deletedatarange") {
                            $result = $feed->mysqltimeseries_delete_data_range($feedid, get('start'), get('end'));
                        }
                    } elseif ($f['engine'] == Engine::PHPTIMESERIES) {
                        if ($route->action == "export") {
                            $result = $feed->phptimeseries_export($feedid, get('start'));
                        }
                    } elseif ($f['engine'] == Engine::PHPFIWA) {
                        if ($route->action == "export") {
                            $result = $feed->phpfiwa_export($feedid, get('start'), get('layer'));
                        }
                    } elseif ($f['engine'] == Engine::PHPFINA) {
                        if ($route->action == "export") {
                            $result = $feed->phpfina_export($feedid, get('start'));
                        }
                    }
                }
            } else {
                $result = array('success' => false, 'message' => 'Feed does not exist');
            }
        }
    }
    return array('content' => $result);
}
示例#6
0
function vis_controller()
{
    global $mysqli, $redis, $session, $route, $user, $settings;
    $result = false;
    require "Modules/feed/feed_model.php";
    $feed = new Feed($mysqli, $redis, $settings);
    require "Modules/vis/multigraph_model.php";
    $multigraph = new Multigraph($mysqli);
    $visdir = "vis/visualisations/";
    /*
      1 - realtime
      2 - daily
      3 - histogram
      4 - boolean (not used uncomment line 122)
      5 - text
      6 - float value
      7 - int value
    */
    $visualisations = array('realtime' => array('options' => array(array('feedid', 1))), 'rawdata' => array('options' => array(array('feedid', 1), array('fill', 7, 0), array('units', 5, 'W'), array('colour', 5, 'EDC240'))), 'bargraph' => array('options' => array(array('feedid', 2), array('colour', 5, 'EDC240'))), 'timestoredaily' => array('options' => array(array('feedid', 1), array('units', 5, 'kWh'))), 'smoothie' => array('options' => array(array('feedid', 1), array('ufac', 6))), 'histgraph' => array('options' => array(array('feedid', 3), array('barwidth', 7, 50), array('start', 7, 0), array('end', 7, 0))), 'zoom' => array('options' => array(array('power', 1), array('kwhd', 2), array('currency', 5, '&pound;'), array('currency_after_val', 7, 0), array('pricekwh', 6, 0.14))), 'stacked' => array('options' => array(array('bottom', 2), array('top', 2))), 'stackedsolar' => array('options' => array(array('solar', 2), array('consumption', 2))), 'threshold' => array('options' => array(array('feedid', 3), array('thresholdA', 6, 500), array('thresholdB', 6, 2500))), 'simplezoom' => array('options' => array(array('power', 1), array('kwhd', 2))), 'orderbars' => array('options' => array(array('feedid', 2))), 'orderthreshold' => array('options' => array(array('feedid', 3), array('power', 1), array('thresholdA', 6, 500), array('thresholdB', 6, 2500))), 'editrealtime' => array('options' => array(array('feedid', 1))), 'editdaily' => array('options' => array(array('feedid', 2))), 'multigraph' => array('action' => 'multigraph', 'options' => array(array('mid', 7))), 'compare' => array('action' => 'compare', 'options' => array(array('powerx', 1), array('powery', 1))));
    $write_apikey = "";
    $read_apikey = "";
    if ($session['read']) {
        $read_apikey = $user->get_apikey_read($session['userid']);
    }
    if ($session['write']) {
        $write_apikey = $user->get_apikey_write($session['userid']);
    }
    if ($route->format == 'html') {
        if ($route->action == 'list' && $session['write']) {
            $multigraphs = $multigraph->getlist($session['userid']);
            $feedlist = $feed->get_user_feeds($session['userid']);
            $result = view("Modules/vis/vis_main_view.php", array('user' => $user->get($session['userid']), 'feedlist' => $feedlist, 'apikey' => $read_apikey, 'visualisations' => $visualisations, 'multigraphs' => $multigraphs));
        }
        // Auto - automatically selects visualisation based on datatype
        // and is used primarily for quick checking feeds from the feeds page.
        if ($route->action == "auto") {
            $feedid = intval(get('feedid'));
            $datatype = $feed->get_field($feedid, 'datatype');
            if ($datatype == 0) {
                $result = "Feed type or authentication not valid";
            }
            if ($datatype == 1) {
                $route->action = 'rawdata';
            }
            if ($datatype == 2) {
                $route->action = 'bargraph';
            }
            if ($datatype == 3) {
                $route->action = 'histgraph';
            }
        }
        while ($vis = current($visualisations)) {
            $viskey = key($visualisations);
            // If the visualisation has a set property called action
            // then override the visualisation key and use the set action instead
            if (isset($vis['action'])) {
                $viskey = $vis['action'];
            }
            if ($route->action == $viskey) {
                $array = array();
                $array['valid'] = true;
                if (isset($vis['options'])) {
                    foreach ($vis['options'] as $option) {
                        $key = $option[0];
                        $type = $option[1];
                        if (isset($option[2])) {
                            $default = $option[2];
                        } else {
                            $default = "";
                        }
                        if ($type == 1 || $type == 2 || $type == 3) {
                            $feedid = (int) get($key);
                            if ($feedid) {
                                $f = $feed->get($feedid);
                                $array[$key] = $feedid;
                                $array[$key . 'name'] = $f['name'];
                                if ($f['userid'] != $session['userid'] || $f['datatype'] != $type) {
                                    $array['valid'] = false;
                                }
                                if ($f['public'] && $f['datatype'] == $type) {
                                    $array['valid'] = true;
                                }
                            } else {
                                $array['valid'] = false;
                            }
                        }
                        // Boolean not used at the moment
                        if ($type == 4) {
                            if (get($key) == true || get($key) == false) {
                                $array[$key] = get($key);
                            } else {
                                $array[$key] = $default;
                            }
                        }
                        if ($type == 5) {
                            $array[$key] = preg_replace('/[^\\w\\s£$€¥]/', '', get($key)) ? get($key) : $default;
                        }
                        if ($type == 6) {
                            $array[$key] = str_replace(',', '.', floatval(get($key) ? get($key) : $default));
                        }
                        if ($type == 7) {
                            $array[$key] = intval(get($key) ? get($key) : $default);
                        }
                        # we need to either urlescape the colour, or just scrub out invalid chars. I'm doing the second, since
                        # we can be fairly confident that colours are eiter a hex or a simple word (e.g. "blue" or such)
                        if ($key == "colour") {
                            $array[$key] = preg_replace('/[^\\dA-Za-z]/', '', $array[$key]);
                        }
                    }
                }
                $array['apikey'] = $read_apikey;
                $array['write_apikey'] = $write_apikey;
                $result = view("Modules/" . $visdir . $viskey . ".php", $array);
                if ($array['valid'] == false) {
                    $result .= "<div style='position:absolute; top:0px; left:0px; background-color:rgba(240,240,240,0.5); width:100%; height:100%; text-align:center; padding-top:100px;'><h3>Feed type or authentication not valid</h3></div>";
                }
            }
            next($visualisations);
        }
    }
    /*
    MULTIGRAPH ACTIONS
    */
    if ($route->format == 'json' && $route->action == 'multigraph') {
        if ($route->subaction == 'new' && $session['write']) {
            $result = $multigraph->create($session['userid']);
        }
        if ($route->subaction == 'delete' && $session['write']) {
            $result = $multigraph->delete(get('id'), $session['userid']);
        }
        if ($route->subaction == 'set' && $session['write']) {
            $result = $multigraph->set(get('id'), $session['userid'], get('feedlist'));
        }
        if ($route->subaction == 'get') {
            $result = $multigraph->get(get('id'), $session['userid']);
        }
        if ($route->subaction == 'getlist') {
            $result = $multigraph->getlist($session['userid']);
        }
    }
    return array('content' => $result);
}
示例#7
0
function event_controller()
{
    global $mysqli, $redis, $user, $session, $route, $timestore_adminkey;
    global $feed;
    include "Modules/feed/feed_model.php";
    $feed = new Feed($mysqli, $redis, $timestore_adminkey);
    require "Modules/event/event_model.php";
    $event = new Event($mysqli, $redis);
    $userid = $session['userid'];
    if ($route->action == 'add' && $session['write']) {
        $eventfeed = intval(get('eventfeed'));
        $eventtype = intval(get('eventtype'));
        $eventvalue = floatval(get('eventvalue'));
        $action = intval(get('action'));
        $setfeed = intval(get('setfeed'));
        $setemail = get('setemail');
        $setvalue = floatval(get('setvalue'));
        $callcurl = get('callcurl');
        $mutetime = get('mutetime');
        $priority = get('priority');
        $message = get('message');
        $mqtttopic = get('mqtttopic');
        $mqttqos = get('mqttqos');
        $event->add($userid, $eventfeed, $eventtype, $eventvalue, $action, $setfeed, $setemail, $setvalue, $callcurl, $message, $mutetime, $priority, $mqtttopic, $mqttqos);
        $result = "Event added";
    }
    if ($route->action == 'edit' && $session['write']) {
        $eventid = intval(get('eventid'));
        $eventfeed = intval(get('eventfeed'));
        $eventtype = intval(get('eventtype'));
        $eventvalue = floatval(get('eventvalue'));
        $action = intval(get('action'));
        $setfeed = intval(get('setfeed'));
        $setemail = get('setemail');
        $setvalue = floatval(get('setvalue'));
        $callcurl = get('callcurl');
        $mutetime = get('mutetime');
        $priority = get('priority');
        $message = get('message');
        $mqtttopic = get('mqtttopic');
        $mqttqos = get('mqttqos');
        $event->update($userid, $eventid, $eventfeed, $eventtype, $eventvalue, $action, $setfeed, $setemail, $setvalue, $callcurl, $message, $mutetime, $priority, $mqtttopic, $mqttqos);
        $result = "Event updated";
    } else {
        if ($route->action == 'delete' && $session['write']) {
            $id = intval(get('id'));
            $event->delete($userid, $id);
            $result = "Event deleted";
        } else {
            if ($route->action == 'status' && $session['write']) {
                $id = intval(get('id'));
                $status = intval(get('status'));
                $event->set_status($userid, $id, $status);
                $result = "Event deleted";
            } else {
                if ($route->action == 'test' && $session['write']) {
                    $id = intval(get('id'));
                    $feedid = intval(get('feedid'));
                    $event->test($userid, $id, $feedid);
                    $result = "Event Test Sent";
                } else {
                    if ($route->action == 'settings' && $session['write']) {
                        $settings = $event->get_settings($session['userid']);
                        $result = view("Modules/event/event_settings_view.php", array('settings' => $settings));
                    } else {
                        if ($route->action == 'savesettings' && $session['write']) {
                            // Store userlang in database
                            $prowlkey = post('prowlkey');
                            $nmakey = post('nmakey');
                            $smtpserver = post('smtpserver');
                            $smtpuser = post('smtpuser');
                            $salt = $user->get_salt($session['userid']);
                            $smtppassword = trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $salt, post('smtppassword'), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
                            $smtpport = preg_replace('/[^\\w\\s-]/', '', post('smtpport'));
                            $consumerkey = post('consumerkey');
                            $consumersecret = post('consumersecret');
                            $usertoken = post('usertoken');
                            $usersecret = post('usersecret');
                            $mqttbrokerip = post('mqttbrokerip');
                            $mqttbrokerport = post('mqttbrokerport');
                            $mqttusername = post('mqttusername');
                            $mqttpassword = trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $salt, post('mqttpassword'), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
                            $result = $event->set_settings($session['userid'], $prowlkey, $consumerkey, $consumersecret, $usertoken, $usersecret, $smtpserver, $smtpuser, $smtppassword, $smtpport, $nmakey, $mqttbrokerip, $mqttbrokerport, $mqttusername, $mqttpassword);
                        } else {
                            if ($session['write']) {
                                $list = $event->eventlist($userid);
                                $feeds = $feed->get_user_feeds($userid);
                                $result = view("Modules/event/event_list.php", array('event_list' => $list, 'feeds' => $feeds));
                            }
                        }
                    }
                }
            }
        }
    }
    return array('content' => $result);
}