update sensor page
diff --git a/boost-dbus/CMakeLists.txt b/boost-dbus/CMakeLists.txt
index 736d030..57bd8bd 100644
--- a/boost-dbus/CMakeLists.txt
+++ b/boost-dbus/CMakeLists.txt
@@ -49,6 +49,6 @@
target_link_libraries(dbustests ${Boost_LIBRARIES})
target_link_libraries(dbustests ${DBus_LIBRARIES})
target_link_libraries(dbustests ${GTEST_BOTH_LIBRARIES} gmock)
-target_link_libraries(dbustests -pthread)
+target_link_libraries(dbustests pthread)
add_test(dbustests dbustests "--gtest_output=xml:${test_name}.xml")
diff --git a/boost-dbus/include/dbus/connection.hpp b/boost-dbus/include/dbus/connection.hpp
index a7185a5..50e0207 100644
--- a/boost-dbus/include/dbus/connection.hpp
+++ b/boost-dbus/include/dbus/connection.hpp
@@ -53,6 +53,33 @@
this->get_service().open(this->get_implementation(), bus);
}
+
+ /// Request a name on the bus.
+ /**
+ * @param name The name requested on the bus
+ *
+ * @return
+ *
+ * @throws boost::system::system_error When the response timed out or
+ * there was some other error.
+ */
+ void request_name(const string& name) {
+ this->get_implementation().request_name(name);
+ }
+
+ /// Reply to a message.
+ /**
+ * @param m The message from which to create the reply
+ *
+ * @return The new reply message
+ *
+ * @throws boost::system::system_error When the response timed out or
+ * there was some other error.
+ */
+ message reply(message& m) {
+ return this->get_implementation().new_method_return(m);
+ }
+
/// Send a message.
/**
* @param m The message to send.
@@ -125,6 +152,8 @@
friend class filter;
};
+typedef std::shared_ptr<connection> connection_ptr;
+
} // namespace dbus
#endif // DBUS_CONNECTION_HPP
diff --git a/boost-dbus/include/dbus/endpoint.hpp b/boost-dbus/include/dbus/endpoint.hpp
index a574e8f..2955a34 100644
--- a/boost-dbus/include/dbus/endpoint.hpp
+++ b/boost-dbus/include/dbus/endpoint.hpp
@@ -16,17 +16,32 @@
string process_name_;
string path_;
string interface_;
+ string member_;
public:
endpoint(const string& process_name, const string& path,
const string& interface)
: process_name_(process_name), path_(path), interface_(interface) {}
+ endpoint(const string& process_name, const string& path,
+ const string& interface, const string& member)
+ : process_name_(process_name), path_(path),
+ interface_(interface), member_(member) {}
+
const string& get_path() const { return path_; }
const string& get_interface() const { return interface_; }
const string& get_process_name() const { return process_name_; }
+
+ const string& get_member() const { return member_; }
+
+ const bool operator == (const endpoint &other) const {
+ return (process_name_ == other.process_name_ &&
+ path_ == other.path_ &&
+ interface_ == other.interface_ &&
+ member_ == other.member_);
+ }
};
} // namespace dbus
diff --git a/boost-dbus/include/dbus/filter.hpp b/boost-dbus/include/dbus/filter.hpp
index 5d60d33..b0f0db4 100644
--- a/boost-dbus/include/dbus/filter.hpp
+++ b/boost-dbus/include/dbus/filter.hpp
@@ -19,7 +19,7 @@
* Filters examine incoming messages, demuxing them to multiple queues.
*/
class filter {
- connection& connection_;
+ connection_ptr connection_;
std::function<bool(message&)> predicate_;
detail::queue<message> queue_;
@@ -31,24 +31,25 @@
}
template <typename MessagePredicate>
- filter(connection& c, BOOST_ASIO_MOVE_ARG(MessagePredicate) p)
+ filter(connection_ptr c, BOOST_ASIO_MOVE_ARG(MessagePredicate) p)
: connection_(c),
predicate_(BOOST_ASIO_MOVE_CAST(MessagePredicate)(p)),
- queue_(connection_.get_io_service()) {
- connection_.new_filter(*this);
+ queue_(connection_->get_io_service()) {
+ connection_->new_filter(*this);
}
- ~filter() { connection_.delete_filter(*this); }
+ ~filter() { connection_->delete_filter(*this); }
template <typename MessageHandler>
inline BOOST_ASIO_INITFN_RESULT_TYPE(MessageHandler,
void(boost::system::error_code, message))
async_dispatch(BOOST_ASIO_MOVE_ARG(MessageHandler) handler) {
// begin asynchronous operation
- connection_.get_implementation().start(connection_.get_io_service());
+ connection_->get_implementation().start(connection_->get_io_service());
return queue_.async_pop(BOOST_ASIO_MOVE_CAST(MessageHandler)(handler));
}
+
};
} // namespace dbus
diff --git a/boost-dbus/include/dbus/impl/connection.ipp b/boost-dbus/include/dbus/impl/connection.ipp
index 24a257d..c4c0605 100644
--- a/boost-dbus/include/dbus/impl/connection.ipp
+++ b/boost-dbus/include/dbus/impl/connection.ipp
@@ -44,6 +44,13 @@
detail::set_watch_timeout_dispatch_functions(conn, io);
}
+ void request_name(const string& name) {
+ error e;
+ dbus_bus_request_name(conn, name.c_str(),
+ DBUS_NAME_FLAG_DO_NOT_QUEUE | DBUS_NAME_FLAG_REPLACE_EXISTING, e);
+ e.throw_if_set();
+ }
+
~connection() {
if (conn != NULL) {
dbus_connection_close(conn);
@@ -51,6 +58,10 @@
}
}
+ message new_method_return(message &m) {
+ return dbus_message_new_method_return(m);
+ }
+
operator DBusConnection*() { return conn; }
operator const DBusConnection*() const { return conn; }
diff --git a/boost-dbus/include/dbus/match.hpp b/boost-dbus/include/dbus/match.hpp
index 0488aa0..cdcd169 100644
--- a/boost-dbus/include/dbus/match.hpp
+++ b/boost-dbus/include/dbus/match.hpp
@@ -22,16 +22,16 @@
* dispose of the object.
*/
class match {
- connection& connection_;
+ connection_ptr connection_;
std::string expression_;
public:
- match(connection& c, BOOST_ASIO_MOVE_ARG(std::string) e)
+ match(connection_ptr c, BOOST_ASIO_MOVE_ARG(std::string) e)
: connection_(c), expression_(BOOST_ASIO_MOVE_CAST(std::string)(e)) {
- connection_.new_match(*this);
+ connection_->new_match(*this);
}
- ~match() { connection_.delete_match(*this); }
+ ~match() { connection_->delete_match(*this); }
const std::string& get_expression() const { return expression_; }
diff --git a/boost-dbus/include/dbus/message.hpp b/boost-dbus/include/dbus/message.hpp
index 695e1fc..e4b0255 100644
--- a/boost-dbus/include/dbus/message.hpp
+++ b/boost-dbus/include/dbus/message.hpp
@@ -77,6 +77,10 @@
dbus_message_type_to_string(dbus_message_get_type(message_.get())));
}
+ string get_signature() const {
+ return sanitize(dbus_message_get_signature(message_.get()));
+ }
+
string get_sender() const {
return sanitize(dbus_message_get_sender(message_.get()));
}
diff --git a/boost-dbus/test/avahi.cpp b/boost-dbus/test/avahi.cpp
index 8cbb82f..5d2deb8 100644
--- a/boost-dbus/test/avahi.cpp
+++ b/boost-dbus/test/avahi.cpp
@@ -19,11 +19,11 @@
dbus::endpoint test_daemon("org.freedesktop.Avahi", "/",
"org.freedesktop.Avahi.Server");
boost::asio::io_service io;
- dbus::connection system_bus(io, dbus::bus::system);
+ auto system_bus = std::make_shared<dbus::connection>(io, dbus::bus::system);
dbus::message m = dbus::message::new_call(test_daemon, "GetHostName");
- system_bus.async_send(
+ system_bus->async_send(
m, [&](const boost::system::error_code ec, dbus::message r) {
std::string avahi_hostname;
@@ -52,7 +52,7 @@
TEST(AvahiTest, ServiceBrowser) {
boost::asio::io_service io;
- dbus::connection system_bus(io, dbus::bus::system);
+ auto system_bus = std::make_shared<dbus::connection>(io, dbus::bus::system);
dbus::endpoint test_daemon("org.freedesktop.Avahi", "/",
"org.freedesktop.Avahi.Server");
@@ -64,7 +64,7 @@
.pack<std::string>("local")
.pack<uint32_t>(0);
- dbus::message r = system_bus.send(m1);
+ dbus::message r = system_bus->send(m1);
std::string browser_path;
r.unpack(browser_path);
testing::Test::RecordProperty("browserPath", browser_path);
@@ -102,13 +102,13 @@
FAIL() << "Callback was never called\n";
});
- dbus::connection system_bus(io, dbus::bus::system);
+ auto system_bus = std::make_shared<dbus::connection>(io, dbus::bus::system);
dbus::endpoint test_daemon("org.freedesktop.DBus", "/",
"org.freedesktop.DBus");
// create new service browser
dbus::message m = dbus::message::new_call(test_daemon, "ListNames");
- system_bus.async_send(
+ system_bus->async_send(
m, [&](const boost::system::error_code ec, dbus::message r) {
io.stop();
std::vector<std::string> services;
@@ -123,13 +123,13 @@
io.run();
}
-void query_interfaces(dbus::connection& system_bus, std::string& service_name,
- std::string& object_name) {
+void query_interfaces(dbus::connection_ptr system_bus,
+ std::string& service_name, std::string& object_name) {
dbus::endpoint service_daemon(service_name, object_name,
"org.freedestop.DBus.Introspectable");
dbus::message m = dbus::message::new_call(service_daemon, "Introspect");
try {
- auto r = system_bus.send(m);
+ auto r = system_bus->send(m);
std::vector<std::string> names;
// Todo(ed) figure out why we're occassionally getting access
// denied errors
@@ -152,10 +152,12 @@
TEST(BOOST_DBUS, SingleSensorChanged) {
boost::asio::io_service io;
- dbus::connection system_bus(io, dbus::bus::system);
+
+ auto system_bus = std::make_shared<dbus::connection>(io, dbus::bus::system);
dbus::match ma(system_bus,
"type='signal',path_namespace='/xyz/openbmc_project/sensors'");
+
dbus::filter f(system_bus, [](dbus::message& m) {
auto member = m.get_member();
return member == "PropertiesChanged";
@@ -199,15 +201,15 @@
auto removed = std::vector<uint32_t>();
m.pack(removed);
- system_bus.async_send(m,
- [&](boost::system::error_code ec, dbus::message s) {});
+ system_bus->async_send(m,
+ [&](boost::system::error_code ec, dbus::message s) {});
io.run();
}
TEST(BOOST_DBUS, MultipleSensorChanged) {
boost::asio::io_service io;
- dbus::connection system_bus(io, dbus::bus::system);
+ auto system_bus = std::make_shared<dbus::connection>(io, dbus::bus::system);
dbus::match ma(system_bus,
"type='signal',path_namespace='/xyz/openbmc_project/sensors'");
@@ -217,7 +219,8 @@
});
int count = 0;
- f.async_dispatch([&](boost::system::error_code ec, dbus::message s) {
+ std::function<void(boost::system::error_code, dbus::message)> callback = [&](
+ boost::system::error_code ec, dbus::message s) {
std::string object_name;
EXPECT_EQ(s.get_path(),
"/xyz/openbmc_project/sensors/temperature/LR_Brd_Temp");
@@ -233,9 +236,12 @@
count++;
if (count == 2) {
io.stop();
+ } else {
+ f.async_dispatch(callback);
}
- });
+ };
+ f.async_dispatch(callback);
dbus::endpoint test_endpoint(
"org.freedesktop.Avahi",
@@ -255,9 +261,70 @@
auto removed = std::vector<uint32_t>();
m.pack(removed);
- system_bus.async_send(m,
- [&](boost::system::error_code ec, dbus::message s) {});
- system_bus.async_send(m,
- [&](boost::system::error_code ec, dbus::message s) {});
+ system_bus->async_send(m,
+ [&](boost::system::error_code ec, dbus::message s) {});
+ system_bus->async_send(m,
+ [&](boost::system::error_code ec, dbus::message s) {});
io.run();
-}
\ No newline at end of file
+}
+
+TEST(BOOST_DBUS, MethodCall) {
+ boost::asio::io_service io;
+ boost::asio::deadline_timer t(io, boost::posix_time::seconds(30));
+ t.async_wait([&](const boost::system::error_code& /*e*/) {
+ io.stop();
+ FAIL() << "Callback was never called\n";
+ });
+ std::string requested_name = "xyz.openbmc_project.fwupdate1.server";
+ auto system_bus = std::make_shared<dbus::connection>(io, dbus::bus::system);
+ //system_bus->request_name(requested_name);
+
+ /* not sure we even need to add a match for method calls,
+ * but this is how you might do it .... */
+ dbus::match ma(
+ system_bus,
+ "type='method_call',path_namespace='/xyz/openbmc_project/fwupdate1'");
+
+ dbus::filter f(system_bus, [](dbus::message& m) {
+ // std::cerr << "filter called: " << m << std::endl;
+ return (m.get_member() == "Get" &&
+ m.get_interface() == "org.freedesktop.DBus.Properties" &&
+ m.get_signature() == "ss");
+ });
+
+ std::function<void(boost::system::error_code, dbus::message)> method_handler =
+ [&](boost::system::error_code ec, dbus::message s) {
+ if (ec) {
+ FAIL() << ec;
+ } else {
+ std::string intf_name, prop_name;
+ s.unpack(intf_name).unpack(prop_name);
+
+ EXPECT_EQ(intf_name, "xyz.openbmc_project.fwupdate1");
+ EXPECT_EQ(prop_name, "State");
+
+ // send a reply so dbus doesn't get angry?
+ auto r = system_bus->reply(s);
+ r.pack("IDLE");
+ system_bus->async_send(
+ r, [&](boost::system::error_code ec, dbus::message s) {});
+ io.stop();
+ }
+ };
+ f.async_dispatch(method_handler);
+
+ dbus::endpoint test_endpoint(requested_name, "/xyz/openbmc_project/fwupdate1",
+ "org.freedesktop.DBus.Properties");
+
+ auto method_name = std::string("Get");
+ auto m = dbus::message::new_call(test_endpoint, method_name);
+
+ m.pack("xyz.openbmc_project.fwupdate1");
+ m.pack("State");
+
+ system_bus->async_send(m, [&](boost::system::error_code ec, dbus::message s) {
+ std::cerr << "received s: " << s << std::endl;
+ });
+
+ io.run();
+}
diff --git a/src/webserver_main.cpp b/src/webserver_main.cpp
index ec21f6d..0da73f4 100644
--- a/src/webserver_main.cpp
+++ b/src/webserver_main.cpp
@@ -44,7 +44,6 @@
static std::shared_ptr<dbus::connection> system_bus;
static std::shared_ptr<dbus::match> sensor_match;
static std::shared_ptr<dbus::filter> sensor_filter;
-static std::shared_ptr<dbus::filter> sensor_callback;
std::unordered_set<crow::websocket::connection*> users;
@@ -58,8 +57,9 @@
boost::apply_visitor([&](auto val) { j[s.get_path()] = val; },
value.second);
}
+ auto data_to_send = crow::json::dump(j);
for (auto conn : users) {
- conn->send_text(crow::json::dump(j));
+ conn->send_text(data_to_send);
}
sensor_filter->async_dispatch(on_sensor_update);
};
@@ -113,12 +113,12 @@
}
if (!sensor_match) {
sensor_match = std::make_shared<dbus::match>(
- *system_bus,
+ system_bus,
"type='signal',path_namespace='/xyz/openbmc_project/sensors'");
}
if (!sensor_filter) {
sensor_filter =
- std::make_shared<dbus::filter>(*system_bus, [](dbus::message& m) {
+ std::make_shared<dbus::filter>(system_bus, [](dbus::message& m) {
auto member = m.get_member();
return member == "PropertiesChanged";
});
@@ -149,26 +149,30 @@
system_bus.async_send(
m,
[&j, &system_bus](const boost::system::error_code ec, dbus::message r) {
- std::string xml;
- r.unpack(xml);
- std::vector<std::string> dbus_objects;
- dbus::read_dbus_xml_names(xml, dbus_objects);
+ if (ec) {
+
+ } else {
+ std::string xml;
+ r.unpack(xml);
+ std::vector<std::string> dbus_objects;
+ dbus::read_dbus_xml_names(xml, dbus_objects);
- for (auto& object : dbus_objects) {
- dbus::endpoint test_daemon("org.openbmc.Sensors",
- "/org/openbmc/sensors/tach/" + object,
- "org.openbmc.SensorValue");
- dbus::message m2 = dbus::message::new_call(test_daemon, "getValue");
+ for (auto& object : dbus_objects) {
+ dbus::endpoint test_daemon("org.openbmc.Sensors",
+ "/org/openbmc/sensors/tach/" + object,
+ "org.openbmc.SensorValue");
+ dbus::message m2 =
+ dbus::message::new_call(test_daemon, "getValue");
- system_bus.async_send(
- m2, [&](const boost::system::error_code ec, dbus::message r) {
- int32_t value;
- r.unpack(value);
- // TODO(ed) if we ever go multithread, j needs a lock
- j[object] = value;
- });
+ system_bus.async_send(
+ m2, [&](const boost::system::error_code ec, dbus::message r) {
+ int32_t value;
+ r.unpack(value);
+ // TODO(ed) if we ever go multithread, j needs a lock
+ j[object] = value;
+ });
+ }
}
-
});
});
diff --git a/static/CMakeLists.txt b/static/CMakeLists.txt
index 4e6831c..6e5bc2b 100644
--- a/static/CMakeLists.txt
+++ b/static/CMakeLists.txt
@@ -19,6 +19,7 @@
js/run_prettify.js
js/angular.js
js/angular-ui-router-uib-modal.js
+ js/smart-table.js
noVNC/core/inflator.js
noVNC/core/input/xtscancodes.js
@@ -72,6 +73,8 @@
find_program(UGLIFY_MINIFIER uglifyjs)
if(NOT UGLIFY_MINIFIER)
message("uglifyjs not found")
+else()
+ message("Found ${UGLIFY_MINIFIER}")
endif()
foreach(JAVASCRIPT_ASSET ${JAVASCRIPT_ASSETS})
@@ -81,7 +84,7 @@
file(MAKE_DIRECTORY "${FOLDERNAME}")
if(UGLIFY_MINIFIER)
add_custom_command(OUTPUT ${MINIFIED_FILENAME}
- COMMAND uglifyjs --compress --mangle
+ COMMAND ${UGLIFY_MINIFIER} --compress --mangle
--output "${MINIFIED_FILENAME}"
"${CMAKE_CURRENT_SOURCE_DIR}/${JAVASCRIPT_ASSET}"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${JAVASCRIPT_ASSET}"
@@ -102,6 +105,8 @@
find_program(CSS_MINIFIER cssnano)
if(NOT CSS_MINIFIER)
message("cssnano not found")
+else()
+ message("Found ${CSS_MINIFIER}")
endif()
# for now CSS is included as is
foreach(CSS_ASSET ${CSS_ASSETS})
@@ -110,7 +115,7 @@
file(MAKE_DIRECTORY "${FOLDERNAME}")
if(CSS_MINIFIER)
add_custom_command(OUTPUT ${MINIFIED_FILENAME}
- COMMAND cssnano
+ COMMAND ${CSS_MINIFIER}
"${CMAKE_CURRENT_SOURCE_DIR}/${CSS_ASSET}"
"${CMAKE_CURRENT_BINARY_DIR}/${CSS_ASSET}"
diff --git a/static/index.html b/static/index.html
index 6e5f9bd..61e779b 100644
--- a/static/index.html
+++ b/static/index.html
@@ -22,6 +22,8 @@
<script type="text/javascript" src="static/js/angular-ui-router-uib-modal.js" defer></script>
<script type="text/javascript" src="static/js/lodash.core.js" defer></script>
+ <script type="text/javascript" src="static/js/smart-table.js" defer></script>
+
<script type="text/javascript" src="static/js/ui-bootstrap-tpls-2.5.0.js" defer></script>
<script type="text/javascript" src="static/js/bmcApp.js" defer></script>
@@ -54,6 +56,38 @@
</head>
<body ng-controller="MainCtrl" ng-class="(is_logged_in()) ? '' : 'auth-main'">
+ <main ng-if="$pageFinishedLoading" ng-class="{ 'menu-collapsed': $baSidebarService.isMenuCollapsed() }">
+
+ <ba-sidebar></ba-sidebar>
+ <page-top></page-top>
+
+ <div class="al-main">
+ <div class="al-content">
+ <content-top></content-top>
+ <div ui-view autoscroll="true" autoscroll-body-top></div>
+ </div>
+ </div>
+
+ <footer class="al-footer clearfix">
+ <div class="al-footer-right">Created with <i class="ion-heart"></i></div>
+ <div class="al-footer-main clearfix">
+ <div class="al-copy">Blur Admin 2016</div>
+ <ul class="al-share clearfix">
+ <li><i class="socicon socicon-facebook"></i></li>
+ <li><i class="socicon socicon-twitter"></i></li>
+ <li><i class="socicon socicon-google"></i></li>
+ <li><i class="socicon socicon-github"></i></li>
+ </ul>
+ </div>
+ </footer>
+
+ <back-top></back-top>
+ </main>
+
+ <div id="preloader" ng-show="!$pageFinishedLoading">
+ <div></div>
+ </div>
+
<div>
<nav class="navbar navbar-inverse" ng-if='is_logged_in()'>
<div class="container-fluid">
diff --git a/static/js/bmcApp.js b/static/js/bmcApp.js
index 55e4a42..1812261 100644
--- a/static/js/bmcApp.js
+++ b/static/js/bmcApp.js
@@ -1,8 +1,15 @@
'use strict';
angular.module('Authentication', []);
var app = angular.module('bmcApp', [
- 'ngCookies', 'ngAnimate', 'ngSanitize', 'ui.bootstrap',
- 'ui.router', 'ngWebSocket', 'Authentication', 'ui.router.modal',
+ 'ngCookies',
+ 'ngAnimate',
+ 'ngSanitize',
+ 'ui.bootstrap',
+ 'ui.router',
+ 'ngWebSocket',
+ 'Authentication',
+ 'ui.router.modal',
+ 'smart-table',
]);
app.service('loginInterceptor', [
@@ -128,7 +135,7 @@
.state('fwupdate.confirm', {
url : '/confirm',
templateUrl : 'static/partial-fwupdateconfirm.html',
- modal: true
+ modal : true
})
// ABOUT PAGE AND MULTIPLE NAMED VIEWS =================================
.state('about',
@@ -216,7 +223,7 @@
};
service.IsLoggedIn = function() {
- if ($rootScope.globals['currentUser']){
+ if ($rootScope.globals['currentUser']) {
return true;
} else {
return false;
diff --git a/static/js/sensorController.js b/static/js/sensorController.js
index 67c9d82..0fa9045 100644
--- a/static/js/sensorController.js
+++ b/static/js/sensorController.js
@@ -1,41 +1,54 @@
angular.module('bmcApp').controller('sensorController', [
'$scope', '$http', '$location', 'websocketService',
function($scope, $http, $location, websocketService) {
- $scope.sensor_values = {};
+ $scope.smartTablePageSize = 10;
+ $scope.next_id = 0;
+ websocketService.start('/sensorws', function(evt) {
+ var obj = JSON.parse(evt.data);
- var host = $location.host();
- var port = $location.port();
- var protocol = "ws://";
- if ($location.protocol() === 'https') {
- protocol = 'wss://';
- }
- websocketService.start(protocol + host + ":" + port + "/sensorws", function (evt) {
- var obj = JSON.parse(evt.data);
- $scope.$apply(function () {
- for (var key in obj) {
- if (obj.hasOwnProperty(key)) {
- console.log(key + " -> " + obj[key]);
- $scope.sensor_values[key] = obj[key];
- }
+ $scope.$apply(function() {
+ for (var sensor_name in obj) {
+ var found = false;
+ for (var sensor_index in $scope.rowCollection) {
+ var sensor_object = $scope.rowCollection[sensor_index];
+ if (sensor_object.name === sensor_name) {
+ sensor_object.value = obj[sensor_name];
+ found = true;
+ break;
}
- });
+ }
+ if (!found) {
+ console.log(sensor_name + ' -> ' + obj[sensor_name]);
+ $scope.next_id = $scope.next_id + 1;
+
+ $scope.rowCollection.push({
+ id : $scope.next_id,
+ name : sensor_name,
+ value : obj[sensor_name],
+ });
+ }
+ };
+ });
});
+ $scope.rowCollection = [];
+
}
]);
-app.factory('websocketService', function () {
- return {
- start: function (url, callback) {
- var websocket = new WebSocket(url);
- websocket.onopen = function () {
- };
- websocket.onclose = function () {
- };
- websocket.onmessage = function (evt) {
- callback(evt);
- };
- }
+app.factory('websocketService', function($location) {
+ return {
+ start: function(url, callback) {
+ var host = $location.host();
+ var port = 18080;
+ var protocol = 'wss://';
+ if ($location.protocol() === 'http') {
+ protocol = 'ws://';
}
+ var websocket = new WebSocket(protocol + host + ':' + port + url);
+ websocket.onopen = function() {};
+ websocket.onclose = function() {};
+ websocket.onmessage = function(evt) { callback(evt); };
}
-);
\ No newline at end of file
+ }
+});
\ No newline at end of file
diff --git a/static/js/smart-table.js b/static/js/smart-table.js
new file mode 100644
index 0000000..e0246d4
--- /dev/null
+++ b/static/js/smart-table.js
@@ -0,0 +1,534 @@
+/**
+* @version 2.1.8
+* @license MIT
+*/
+(function (ng, undefined){
+ 'use strict';
+
+ng.module('smart-table', []).run(['$templateCache', function ($templateCache) {
+ $templateCache.put('template/smart-table/pagination.html',
+ '<nav ng-if="numPages && pages.length >= 2"><ul class="pagination">' +
+ '<li ng-repeat="page in pages" ng-class="{active: page==currentPage}"><a href="javascript: void(0);" ng-click="selectPage(page)">{{page}}</a></li>' +
+ '</ul></nav>');
+}]);
+
+
+ng.module('smart-table')
+ .constant('stConfig', {
+ pagination: {
+ template: 'template/smart-table/pagination.html',
+ itemsByPage: 10,
+ displayedPages: 5
+ },
+ search: {
+ delay: 400, // ms
+ inputEvent: 'input'
+ },
+ select: {
+ mode: 'single',
+ selectedClass: 'st-selected'
+ },
+ sort: {
+ ascentClass: 'st-sort-ascent',
+ descentClass: 'st-sort-descent',
+ descendingFirst: false,
+ skipNatural: false,
+ delay:300
+ },
+ pipe: {
+ delay: 100 //ms
+ }
+ });
+ng.module('smart-table')
+ .controller('stTableController', ['$scope', '$parse', '$filter', '$attrs', function StTableController ($scope, $parse, $filter, $attrs) {
+ var propertyName = $attrs.stTable;
+ var displayGetter = $parse(propertyName);
+ var displaySetter = displayGetter.assign;
+ var safeGetter;
+ var orderBy = $filter('orderBy');
+ var filter = $filter('filter');
+ var safeCopy = copyRefs(displayGetter($scope));
+ var tableState = {
+ sort: {},
+ search: {},
+ pagination: {
+ start: 0,
+ totalItemCount: 0
+ }
+ };
+ var filtered;
+ var pipeAfterSafeCopy = true;
+ var ctrl = this;
+ var lastSelected;
+
+ function copyRefs (src) {
+ return src ? [].concat(src) : [];
+ }
+
+ function updateSafeCopy () {
+ safeCopy = copyRefs(safeGetter($scope));
+ if (pipeAfterSafeCopy === true) {
+ ctrl.pipe();
+ }
+ }
+
+ function deepDelete (object, path) {
+ if (path.indexOf('.') != -1) {
+ var partials = path.split('.');
+ var key = partials.pop();
+ var parentPath = partials.join('.');
+ var parentObject = $parse(parentPath)(object)
+ delete parentObject[key];
+ if (Object.keys(parentObject).length == 0) {
+ deepDelete(object, parentPath);
+ }
+ } else {
+ delete object[path];
+ }
+ }
+
+ if ($attrs.stSafeSrc) {
+ safeGetter = $parse($attrs.stSafeSrc);
+ $scope.$watch(function () {
+ var safeSrc = safeGetter($scope);
+ return safeSrc && safeSrc.length ? safeSrc[0] : undefined;
+ }, function (newValue, oldValue) {
+ if (newValue !== oldValue) {
+ updateSafeCopy();
+ }
+ });
+ $scope.$watch(function () {
+ var safeSrc = safeGetter($scope);
+ return safeSrc ? safeSrc.length : 0;
+ }, function (newValue, oldValue) {
+ if (newValue !== safeCopy.length) {
+ updateSafeCopy();
+ }
+ });
+ $scope.$watch(function () {
+ return safeGetter($scope);
+ }, function (newValue, oldValue) {
+ if (newValue !== oldValue) {
+ tableState.pagination.start = 0;
+ updateSafeCopy();
+ }
+ });
+ }
+
+ /**
+ * sort the rows
+ * @param {Function | String} predicate - function or string which will be used as predicate for the sorting
+ * @param [reverse] - if you want to reverse the order
+ */
+ this.sortBy = function sortBy (predicate, reverse) {
+ tableState.sort.predicate = predicate;
+ tableState.sort.reverse = reverse === true;
+
+ if (ng.isFunction(predicate)) {
+ tableState.sort.functionName = predicate.name;
+ } else {
+ delete tableState.sort.functionName;
+ }
+
+ tableState.pagination.start = 0;
+ return this.pipe();
+ };
+
+ /**
+ * search matching rows
+ * @param {String} input - the input string
+ * @param {String} [predicate] - the property name against you want to check the match, otherwise it will search on all properties
+ */
+ this.search = function search (input, predicate) {
+ var predicateObject = tableState.search.predicateObject || {};
+ var prop = predicate ? predicate : '$';
+
+ input = ng.isString(input) ? input.trim() : input;
+ $parse(prop).assign(predicateObject, input);
+ // to avoid to filter out null value
+ if (!input) {
+ deepDelete(predicateObject, prop);
+ }
+ tableState.search.predicateObject = predicateObject;
+ tableState.pagination.start = 0;
+ return this.pipe();
+ };
+
+ /**
+ * this will chain the operations of sorting and filtering based on the current table state (sort options, filtering, ect)
+ */
+ this.pipe = function pipe () {
+ var pagination = tableState.pagination;
+ var output;
+ filtered = tableState.search.predicateObject ? filter(safeCopy, tableState.search.predicateObject) : safeCopy;
+ if (tableState.sort.predicate) {
+ filtered = orderBy(filtered, tableState.sort.predicate, tableState.sort.reverse);
+ }
+ pagination.totalItemCount = filtered.length;
+ if (pagination.number !== undefined) {
+ pagination.numberOfPages = filtered.length > 0 ? Math.ceil(filtered.length / pagination.number) : 1;
+ pagination.start = pagination.start >= filtered.length ? (pagination.numberOfPages - 1) * pagination.number : pagination.start;
+ output = filtered.slice(pagination.start, pagination.start + parseInt(pagination.number));
+ }
+ displaySetter($scope, output || filtered);
+ };
+
+ /**
+ * select a dataRow (it will add the attribute isSelected to the row object)
+ * @param {Object} row - the row to select
+ * @param {String} [mode] - "single" or "multiple" (multiple by default)
+ */
+ this.select = function select (row, mode) {
+ var rows = copyRefs(displayGetter($scope));
+ var index = rows.indexOf(row);
+ if (index !== -1) {
+ if (mode === 'single') {
+ row.isSelected = row.isSelected !== true;
+ if (lastSelected) {
+ lastSelected.isSelected = false;
+ }
+ lastSelected = row.isSelected === true ? row : undefined;
+ } else {
+ rows[index].isSelected = !rows[index].isSelected;
+ }
+ }
+ };
+
+ /**
+ * take a slice of the current sorted/filtered collection (pagination)
+ *
+ * @param {Number} start - start index of the slice
+ * @param {Number} number - the number of item in the slice
+ */
+ this.slice = function splice (start, number) {
+ tableState.pagination.start = start;
+ tableState.pagination.number = number;
+ return this.pipe();
+ };
+
+ /**
+ * return the current state of the table
+ * @returns {{sort: {}, search: {}, pagination: {start: number}}}
+ */
+ this.tableState = function getTableState () {
+ return tableState;
+ };
+
+ this.getFilteredCollection = function getFilteredCollection () {
+ return filtered || safeCopy;
+ };
+
+ /**
+ * Use a different filter function than the angular FilterFilter
+ * @param filterName the name under which the custom filter is registered
+ */
+ this.setFilterFunction = function setFilterFunction (filterName) {
+ filter = $filter(filterName);
+ };
+
+ /**
+ * Use a different function than the angular orderBy
+ * @param sortFunctionName the name under which the custom order function is registered
+ */
+ this.setSortFunction = function setSortFunction (sortFunctionName) {
+ orderBy = $filter(sortFunctionName);
+ };
+
+ /**
+ * Usually when the safe copy is updated the pipe function is called.
+ * Calling this method will prevent it, which is something required when using a custom pipe function
+ */
+ this.preventPipeOnWatch = function preventPipe () {
+ pipeAfterSafeCopy = false;
+ };
+ }])
+ .directive('stTable', function () {
+ return {
+ restrict: 'A',
+ controller: 'stTableController',
+ link: function (scope, element, attr, ctrl) {
+
+ if (attr.stSetFilter) {
+ ctrl.setFilterFunction(attr.stSetFilter);
+ }
+
+ if (attr.stSetSort) {
+ ctrl.setSortFunction(attr.stSetSort);
+ }
+ }
+ };
+ });
+
+ng.module('smart-table')
+ .directive('stSearch', ['stConfig', '$timeout','$parse', function (stConfig, $timeout, $parse) {
+ return {
+ require: '^stTable',
+ link: function (scope, element, attr, ctrl) {
+ var tableCtrl = ctrl;
+ var promise = null;
+ var throttle = attr.stDelay || stConfig.search.delay;
+ var event = attr.stInputEvent || stConfig.search.inputEvent;
+
+ attr.$observe('stSearch', function (newValue, oldValue) {
+ var input = element[0].value;
+ if (newValue !== oldValue && input) {
+ ctrl.tableState().search = {};
+ tableCtrl.search(input, newValue);
+ }
+ });
+
+ //table state -> view
+ scope.$watch(function () {
+ return ctrl.tableState().search;
+ }, function (newValue, oldValue) {
+ var predicateExpression = attr.stSearch || '$';
+ if (newValue.predicateObject && $parse(predicateExpression)(newValue.predicateObject) !== element[0].value) {
+ element[0].value = $parse(predicateExpression)(newValue.predicateObject) || '';
+ }
+ }, true);
+
+ // view -> table state
+ element.bind(event, function (evt) {
+ evt = evt.originalEvent || evt;
+ if (promise !== null) {
+ $timeout.cancel(promise);
+ }
+
+ promise = $timeout(function () {
+ tableCtrl.search(evt.target.value, attr.stSearch || '');
+ promise = null;
+ }, throttle);
+ });
+ }
+ };
+ }]);
+
+ng.module('smart-table')
+ .directive('stSelectRow', ['stConfig', function (stConfig) {
+ return {
+ restrict: 'A',
+ require: '^stTable',
+ scope: {
+ row: '=stSelectRow'
+ },
+ link: function (scope, element, attr, ctrl) {
+ var mode = attr.stSelectMode || stConfig.select.mode;
+ element.bind('click', function () {
+ scope.$apply(function () {
+ ctrl.select(scope.row, mode);
+ });
+ });
+
+ scope.$watch('row.isSelected', function (newValue) {
+ if (newValue === true) {
+ element.addClass(stConfig.select.selectedClass);
+ } else {
+ element.removeClass(stConfig.select.selectedClass);
+ }
+ });
+ }
+ };
+ }]);
+
+ng.module('smart-table')
+ .directive('stSort', ['stConfig', '$parse', '$timeout', function (stConfig, $parse, $timeout) {
+ return {
+ restrict: 'A',
+ require: '^stTable',
+ link: function (scope, element, attr, ctrl) {
+
+ var predicate = attr.stSort;
+ var getter = $parse(predicate);
+ var index = 0;
+ var classAscent = attr.stClassAscent || stConfig.sort.ascentClass;
+ var classDescent = attr.stClassDescent || stConfig.sort.descentClass;
+ var stateClasses = [classAscent, classDescent];
+ var sortDefault;
+ var skipNatural = attr.stSkipNatural !== undefined ? attr.stSkipNatural : stConfig.sort.skipNatural;
+ var descendingFirst = attr.stDescendingFirst !== undefined ? attr.stDescendingFirst : stConfig.sort.descendingFirst;
+ var promise = null;
+ var throttle = attr.stDelay || stConfig.sort.delay;
+
+ if (attr.stSortDefault) {
+ sortDefault = scope.$eval(attr.stSortDefault) !== undefined ? scope.$eval(attr.stSortDefault) : attr.stSortDefault;
+ }
+
+ //view --> table state
+ function sort () {
+ if (descendingFirst) {
+ index = index === 0 ? 2 : index - 1;
+ } else {
+ index++;
+ }
+
+ var func;
+ predicate = ng.isFunction(getter(scope)) || ng.isArray(getter(scope)) ? getter(scope) : attr.stSort;
+ if (index % 3 === 0 && !!skipNatural !== true) {
+ //manual reset
+ index = 0;
+ ctrl.tableState().sort = {};
+ ctrl.tableState().pagination.start = 0;
+ func = ctrl.pipe.bind(ctrl);
+ } else {
+ func = ctrl.sortBy.bind(ctrl, predicate, index % 2 === 0);
+ }
+ if (promise !== null) {
+ $timeout.cancel(promise);
+ }
+ if (throttle < 0) {
+ func();
+ } else {
+ promise = $timeout(func, throttle);
+ }
+ }
+
+ element.bind('click', function sortClick () {
+ if (predicate) {
+ scope.$apply(sort);
+ }
+ });
+
+ if (sortDefault) {
+ index = sortDefault === 'reverse' ? 1 : 0;
+ sort();
+ }
+
+ //table state --> view
+ scope.$watch(function () {
+ return ctrl.tableState().sort;
+ }, function (newValue) {
+ if (newValue.predicate !== predicate) {
+ index = 0;
+ element
+ .removeClass(classAscent)
+ .removeClass(classDescent);
+ } else {
+ index = newValue.reverse === true ? 2 : 1;
+ element
+ .removeClass(stateClasses[index % 2])
+ .addClass(stateClasses[index - 1]);
+ }
+ }, true);
+ }
+ };
+ }]);
+
+ng.module('smart-table')
+ .directive('stPagination', ['stConfig', function (stConfig) {
+ return {
+ restrict: 'EA',
+ require: '^stTable',
+ scope: {
+ stItemsByPage: '=?',
+ stDisplayedPages: '=?',
+ stPageChange: '&'
+ },
+ templateUrl: function (element, attrs) {
+ if (attrs.stTemplate) {
+ return attrs.stTemplate;
+ }
+ return stConfig.pagination.template;
+ },
+ link: function (scope, element, attrs, ctrl) {
+
+ scope.stItemsByPage = scope.stItemsByPage ? +(scope.stItemsByPage) : stConfig.pagination.itemsByPage;
+ scope.stDisplayedPages = scope.stDisplayedPages ? +(scope.stDisplayedPages) : stConfig.pagination.displayedPages;
+
+ scope.currentPage = 1;
+ scope.pages = [];
+
+ function redraw () {
+ var paginationState = ctrl.tableState().pagination;
+ var start = 1;
+ var end;
+ var i;
+ var prevPage = scope.currentPage;
+ scope.totalItemCount = paginationState.totalItemCount;
+ scope.currentPage = Math.floor(paginationState.start / paginationState.number) + 1;
+
+ start = Math.max(start, scope.currentPage - Math.abs(Math.floor(scope.stDisplayedPages / 2)));
+ end = start + scope.stDisplayedPages;
+
+ if (end > paginationState.numberOfPages) {
+ end = paginationState.numberOfPages + 1;
+ start = Math.max(1, end - scope.stDisplayedPages);
+ }
+
+ scope.pages = [];
+ scope.numPages = paginationState.numberOfPages;
+
+ for (i = start; i < end; i++) {
+ scope.pages.push(i);
+ }
+
+ if (prevPage !== scope.currentPage) {
+ scope.stPageChange({newPage: scope.currentPage});
+ }
+ }
+
+ //table state --> view
+ scope.$watch(function () {
+ return ctrl.tableState().pagination;
+ }, redraw, true);
+
+ //scope --> table state (--> view)
+ scope.$watch('stItemsByPage', function (newValue, oldValue) {
+ if (newValue !== oldValue) {
+ scope.selectPage(1);
+ }
+ });
+
+ scope.$watch('stDisplayedPages', redraw);
+
+ //view -> table state
+ scope.selectPage = function (page) {
+ if (page > 0 && page <= scope.numPages) {
+ ctrl.slice((page - 1) * scope.stItemsByPage, scope.stItemsByPage);
+ }
+ };
+
+ if (!ctrl.tableState().pagination.number) {
+ ctrl.slice(0, scope.stItemsByPage);
+ }
+ }
+ };
+ }]);
+
+ng.module('smart-table')
+ .directive('stPipe', ['stConfig', '$timeout', function (config, $timeout) {
+ return {
+ require: 'stTable',
+ scope: {
+ stPipe: '='
+ },
+ link: {
+
+ pre: function (scope, element, attrs, ctrl) {
+
+ var pipePromise = null;
+
+ if (ng.isFunction(scope.stPipe)) {
+ ctrl.preventPipeOnWatch();
+ ctrl.pipe = function () {
+
+ if (pipePromise !== null) {
+ $timeout.cancel(pipePromise)
+ }
+
+ pipePromise = $timeout(function () {
+ scope.stPipe(ctrl.tableState(), ctrl);
+ }, config.pipe.delay);
+
+ return pipePromise;
+ }
+ }
+ },
+
+ post: function (scope, element, attrs, ctrl) {
+ ctrl.pipe();
+ }
+ }
+ };
+ }]);
+
+})(angular);
\ No newline at end of file
diff --git a/static/partial-sensor.html b/static/partial-sensor.html
index 3486a63..4503312 100644
--- a/static/partial-sensor.html
+++ b/static/partial-sensor.html
@@ -1,25 +1,43 @@
<div class="container" ng-controller="sensorController">
<div class="row">
- <div class="col-lg-8">
+ <div class="col-md-12">
<div class="box box-primary">
<div class="box-header with-border">
<h4>Summary
- <h4>
- </div>
- <div class="box-body">
- <table class="table table-striped system-status-table">
+ </h4>
+ <div class="pull-right">
+ <select class="form-control show-tick" title="Rows on page" selectpicker ng-model="smartTablePageSize" ng-options="i for i in [5,10,25,50,100,500]">
+ </select>
+ </div>
+ <div class="horizontal-scroll box-body"> </div>
+
+ <table class="table table-striped system-status-table" st-table="smartTableData" st-safe-src="rowCollection">
<thead>
+ <tr class="sortable ">
+ <th class="table-id" st-sort="id" st-sort-default="true">#</th>
+ <th st-sort="name">Name</th>
+ <th st-sort="value">Value</th>
+ </tr>
<tr>
- <th>Sensor</th>
- <th>Value</th>
+ <th></th>
+ <th><input st-search="name" placeholder="Search Name" class="input-sm form-control search-input" type="search" /></th>
+ <th><input st-search="value" placeholder="Search Value" class="input-sm form-control search-input" type="search" /></th>
</tr>
</thead>
<tbody>
- <tr ng-animate="'animate'" ng-repeat="(sensor_name, value) in sensor_values">
- <td class="fit">{{ sensor_name }}</td>
- <td class="fit">{{ value }}</td>
+ <tr ng-repeat="item in smartTableData">
+ <td class="table-id">{{item.id}}</td>
+ <td>{{item.name}}</td>
+ <td>{{item.value}}</td>
</tr>
</tbody>
+ <tfoot>
+ <tr>
+ <td colspan="6" class="text-center">
+ <div st-pagination="" st-items-by-page="smartTablePageSize" st-displayed-pages="7"></div>
+ </td>
+ </tr>
+ </tfoot>
</table>
</div>
</div>