Skip to content

Commit de10dc5

Browse files
authored
Merge pull request #177 from emoncms/master
stable merge V2.6.6
2 parents 944ce52 + e0cd158 commit de10dc5

File tree

24 files changed

+1500
-111
lines changed

24 files changed

+1500
-111
lines changed

Lib/appconf.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ var config = {
1717

1818
// Check that the config is complete first otherwise show config interface
1919
if (!config.check()) {
20-
config.showConfig(); // Show setup block
21-
config.UI(); // Populate setup UI options
20+
if (!public_userid) {
21+
config.showConfig(); // Show setup block
22+
config.UI(); // Populate setup UI options
23+
} else {
24+
$("#app-block").show(); // Show app block
25+
}
2226
$(".ajax-loader").hide(); // Hide AJAX loader
2327
} else {
2428
$("#app-block").show(); // Show app block

app_controller.php

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
function app_controller()
1616
{
17-
global $mysqli,$path,$session,$route,$user,$settings,$v;
17+
global $mysqli,$redis,$path,$session,$route,$user,$settings,$v;
1818
// Force cache reload of css and javascript
19-
$v = 17;
19+
$v = 25;
2020

2121
$result = false;
2222

@@ -41,15 +41,19 @@ function app_controller()
4141
if ($route->action == "view") {
4242
// enable apikey read access
4343
$userid = false;
44-
if (isset($session['write']) && $session['write']) {
44+
$apikey = "";
45+
46+
if (isset($session['read']) && $session['read']) {
4547
$userid = $session['userid'];
46-
$apikey = $user->get_apikey_write($session['userid']);
48+
if (isset($_GET['apikey'])) {
49+
$apikey = $user->get_apikey_read($session['userid']);
50+
}
4751
} else if (isset($_GET['readkey'])) {
48-
$apikey = $_GET['readkey'];
49-
$userid = $user->get_id_from_apikey($apikey);
50-
} else if (isset($_GET['apikey'])) {
51-
$apikey = $_GET['apikey'];
52-
$userid = $user->get_id_from_apikey($apikey);
52+
if ($userid = $user->get_id_from_apikey($_GET['readkey'])) {
53+
$apikey = $user->get_apikey_read($userid);
54+
}
55+
} else if ($session['public_userid']) {
56+
$userid = (int) $session['public_userid'];
5357
}
5458

5559
if ($userid)
@@ -61,8 +65,24 @@ function app_controller()
6165
} else {
6266
$app = urldecode(get("name"));
6367
}
68+
69+
// If no app specified fine one to load
6470
if (!isset($applist->$app)) {
65-
foreach (array_keys((array) $applist) as $key) { $app = $key; break; }
71+
foreach (array_keys((array) $applist) as $key) {
72+
if ($session['public_userid']) {
73+
if (isset($applist->$key->config->public) && $applist->$key->config->public) {
74+
$app = $key; break;
75+
}
76+
} else {
77+
$app = $key; break;
78+
}
79+
}
80+
}
81+
82+
if ($session['public_userid']) {
83+
if (!isset($applist->$app->config->public) || !$applist->$app->config->public) {
84+
return array('content'=>false);
85+
}
6686
}
6787

6888
$route->format = "html";
@@ -89,63 +109,85 @@ function app_controller()
89109
} else {
90110
$result .= view("Modules/app/Views/app_view.php",array("apps"=>$appavail));
91111
}
112+
return $result;
113+
} else {
114+
return "";
92115
}
93116
}
94117
else if ($route->action == "list" && $session['read']) {
95118
$route->format = "json";
96-
$result = $appconfig->get_list($session['userid']);
119+
return $appconfig->get_list($session['userid']);
97120
}
98121
else if ($route->action == "available" && $session['read']) {
99122
$route->format = "json";
100-
$result = $appavail;
123+
return $appavail;
101124
}
102125
else if ($route->action == "add" && $session['write']) {
103126
$route->format = "json";
104127
$appname = get("app");
105128
if (isset($appavail[$appname])) {
106-
$result = $appconfig->add($session['userid'],$appname,get("name"));
129+
return $appconfig->add($session['userid'],$appname,get("name"));
107130
} else {
108-
$result = "Invalid app";
131+
return "Invalid app";
109132
}
110133
}
111134
else if ($route->action == "new" && $session['write']) {
112135
$applist = $appconfig->get_list($session['userid']);
113136
$route->format = "html";
114137
$result .= "<link href='".$path."Modules/app/Views/css/app.css?v=".$v."' rel='stylesheet'>";
115138
$result .= view("Modules/app/Views/app_view.php", array("apps"=>$appavail));
139+
return $result;
116140
}
117141
else if ($route->action == "remove" && $session['write']) {
118142
$route->format = "json";
119-
$result = $appconfig->remove($session['userid'],get("name"));
143+
return $appconfig->remove($session['userid'],get("name"));
120144
}
121145
else if ($route->action == "setconfig" && $session['write']) {
122146
$route->format = "json";
123-
$result = $appconfig->set_config($session['userid'],get('name'),get('config'));
147+
return $appconfig->set_config($session['userid'],get('name'),get('config'));
124148
}
125149
else if ($route->action == "getconfig" && $session['read']) {
126150
$route->format = "json";
127-
$result = $appconfig->get_config($session['userid'],get('name'));
151+
return $appconfig->get_config($session['userid'],get('name'));
128152
}
129153
else if ($route->action == "dataremote") {
130154
$route->format = "json";
131155
$id = (int) get("id");
132156
$start = (float) get("start");
133157
$end = (float) get("end");
134158
$interval = (int) get("interval");
135-
$result = json_decode(file_get_contents("https://emoncms.org/feed/data.json?id=$id&start=$start&end=$end&interval=$interval&skipmissing=0&limitinterval=0"));
159+
$average = (int) get("average",false,0);
160+
$delta = (int) get("delta",false,0);
161+
$skipmissing = (int) get("skipmissing",false,0);
162+
$limitinterval = (int) get("limitinterval",false,0);
163+
$timeformat = get('timeformat',false,'unixms');
164+
$dp = (int) get('dp',false,-1);
165+
166+
if (!in_array($timeformat,array("unix","unixms","excel","iso8601","notime"))) {
167+
return array('success'=>false, 'message'=>'Invalid time format');
168+
}
169+
170+
//if ($result = $redis->get("app:cache:$id-$start-$end-$interval-$average-$delta")) {
171+
// return json_decode($result);
172+
//} else {
173+
$result = file_get_contents("http://emoncms.org/feed/data.json?id=$id&start=$start&end=$end&interval=$interval&average=$average&delta=$delta&skipmissing=$skipmissing&limitinterval=$limitinterval&timeformat=$timeformat&dp=$dp");
174+
//$redis->set("app:cache:$id-$start-$end-$interval-$average-$delta",$result);
175+
return json_decode($result);
176+
//}
177+
136178
}
137179
else if ($route->action == "valueremote") {
138180
$route->format = "json";
139181
$id = (int) get("id");
140-
$result = (float) json_decode(file_get_contents("https://emoncms.org/feed/value.json?id=$id"));
182+
return (float) json_decode(file_get_contents("https://emoncms.org/feed/value.json?id=$id"));
141183
}
142184
else if ($route->action == "ukgridremote") {
143185
$route->format = "json";
144186
$start = (float) get("start");
145187
$end = (float) get("end");
146188
$interval = (int) get("interval");
147-
$result = json_decode(file_get_contents("https://openenergymonitor.org/ukgrid/api.php?q=data&id=1&start=$start&end=$end&interval=$interval"));
189+
return json_decode(file_get_contents("https://openenergymonitor.org/ukgrid/api.php?q=data&id=1&start=$start&end=$end&interval=$interval"));
148190
}
149191

150-
return array('content'=>$result);
192+
return array('content'=>EMPTY_ROUTE);
151193
}

app_menu.php

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,67 @@
22

33
global $mysqli,$session,$user,$app_settings;
44

5-
if ($session["write"]) {
6-
7-
// Level 1 top bar
8-
$menu["app"] = array("name"=>"Apps", "order"=>2, "icon"=>"apps", "default"=>"app/view", "l2"=>array());
5+
if ($session["read"] || $session["public_userid"]) {
96

107
require_once "Modules/app/app_model.php";
118
$appconfig = new AppConfig($mysqli, $app_settings);
129

1310
// enable apikey read access
1411
$userid = false;
15-
if (isset($session['write']) && $session['write']) {
12+
if ($session['read']) {
1613
$userid = $session['userid'];
17-
$apikey = $user->get_apikey_write($session['userid']);
18-
} else if (isset($_GET['readkey'])) {
19-
$apikey = $_GET['readkey'];
20-
$userid = $user->get_id_from_apikey($apikey);
21-
} else if (isset($_GET['apikey'])) {
22-
$apikey = $_GET['apikey'];
23-
$userid = $user->get_id_from_apikey($apikey);
14+
$apikey = $user->get_apikey_read($session['userid']);
15+
} else if ($session["public_userid"]) {
16+
$userid = $session["public_userid"];
17+
$apikey = "";
2418
}
2519

20+
//} else if (isset($_GET['readkey'])) {
21+
// $apikey = $_GET['readkey'];
22+
// $userid = $user->get_id_from_apikey($apikey);
23+
24+
$l2 = array();
25+
2626
if ($userid)
2727
{
28+
$apikey_str = "";
29+
if ($apikey) $apikey_str = '&readkey='.$apikey;
30+
2831
$applist = $appconfig->get_list($userid);
32+
2933
$_i = 0;
3034
foreach ($applist as $name=>$appitem) {
31-
$menu["app"]['l2']["$_i"] = array(
35+
$item = array(
3236
"name"=>$name,
33-
"href"=>"app/view?name=".urlencode($name).'&apikey='.$apikey,
37+
"href"=>"app/view?name=".urlencode($name).$apikey_str,
3438
"icon"=>"star_border",
3539
"order"=>$_i
3640
);
37-
$_i++;
41+
if ($session['public_userid']) {
42+
$item['href'] = $session['public_username']."/".$item["href"];
43+
// Only show app if public
44+
if (isset($appitem->config->public) && $appitem->config->public) {
45+
$l2["$_i"] = $item;
46+
$_i++;
47+
}
48+
} else {
49+
$l2["$_i"] = $item;
50+
$_i++;
51+
}
3852
}
3953
}
4054

41-
$menu["app"]['l2']['new'] = array(
42-
"name"=>_('New'),
43-
"href"=>"app/new",
44-
"icon"=>"plus",
45-
"order"=>$_i
46-
);
55+
if ($session["write"]) {
56+
$l2['new'] = array(
57+
"name"=>_('New'),
58+
"href"=>"app/new",
59+
"icon"=>"plus",
60+
"order"=>$_i
61+
);
62+
}
4763

64+
// Level 1 top bar
65+
if (count($l2)) {
66+
$menu["app"] = array("name"=>"Apps", "order"=>2, "icon"=>"apps", "default"=>"app/view", "l2"=>$l2);
67+
}
4868
}

apps/OpenEnergyMonitor/costcomparison/costcomparison.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@
127127
var sessionwrite = <?php echo $session['write']; ?>;
128128

129129
feed.apikey = apikey;
130+
feed.public_userid = public_userid;
131+
feed.public_username = public_username;
130132

131133
if (!sessionwrite) $(".config-open").hide();
132134

@@ -151,7 +153,8 @@
151153
"default": "10.00",
152154
"name": "Max currency value",
153155
"description": "Maximum daily amount to show on currency axis (default of 10.00)"
154-
}
156+
},
157+
"public":{"type":"checkbox", "name": "Public", "default": 0, "optional":true, "description":"Make app public"}
155158
};
156159
config.name = "<?php echo $name; ?>";
157160
config.db = <?php echo json_encode($config); ?>;

apps/OpenEnergyMonitor/myelectric/myelectric.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ function getTranslations(){
152152
var sessionwrite = <?php echo $session['write']; ?>;
153153

154154
feed.apikey = apikey;
155+
feed.public_userid = public_userid;
156+
feed.public_username = public_username;
155157
// ----------------------------------------------------------------------
156158
// Display
157159
// ----------------------------------------------------------------------
@@ -170,7 +172,8 @@ function getTranslations(){
170172
"use_kwh":{"type":"feed", "autoname":"use_kwh", "engine":5, "description":"Cumulative use in kWh"},
171173
"unitcost":{"type":"value", "default":0.1508, "name": "Unit cost", "description":"Unit cost of electricity e.g £/kWh"},
172174
"currency":{"type":"value", "default":"£", "name": "Currency", "description":"Currency symbol (£,$,€...)"},
173-
"kw":{"type":"checkbox", "default":0, "name": "Show kW", "description":_("Display power as kW")}
175+
"kw":{"type":"checkbox", "default":0, "name": "Show kW", "description":_("Display power as kW")},
176+
"public":{"type":"checkbox", "name": "Public", "default": 0, "optional":true, "description":"Make app public"}
174177
};
175178

176179
config.name = "<?php echo $name; ?>";
@@ -522,9 +525,19 @@ function slowupdate()
522525

523526
if (usetoday_kwh!==null) {
524527
if (usetoday_kwh<100) {
525-
$("#usetoday").html((usetoday_kwh).toFixed(1));
528+
if (viewmode=="energy") {
529+
$("#usetoday").html((usetoday_kwh).toFixed(1));
530+
} else {
531+
scale = config.app.unitcost.value;
532+
$("#usetoday").html((usetoday_kwh*scale).toFixed(1));
533+
}
526534
} else {
527-
$("#usetoday").html((usetoday_kwh).toFixed(0));
535+
if (viewmode=="energy") {
536+
$("#usetoday").html((usetoday_kwh).toFixed(0));
537+
} else {
538+
scale = config.app.unitcost.value;
539+
$("#usetoday").html((usetoday_kwh*scale).toFixed(0));
540+
}
528541
}
529542
} else {
530543
$("#usetoday").html("---");

apps/OpenEnergyMonitor/myelectric2/myelectric2.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@
186186
var apikey = "<?php print $apikey; ?>";
187187
var sessionwrite = <?php echo $session['write']; ?>;
188188
feed.apikey = apikey;
189-
189+
feed.public_userid = public_userid;
190+
feed.public_username = public_username;
190191
// ----------------------------------------------------------------------
191192
// Display
192193
// ----------------------------------------------------------------------
@@ -207,7 +208,8 @@
207208
"use_kwh":{"type":"feed", "autoname":"use_kwh", "engine":5},
208209
"unitcost":{"type":"value", "default":0.1508, "name": "Unit cost", "description":"Unit cost of electricity £/kWh"},
209210
"currency":{"type":"value", "default":"£", "name": "Currency", "description":"Currency symbol (£,$..)"},
210-
"showcomparison":{"type":"checkbox", "default":true, "name": "Show comparison", "description":"Energy stack comparison"}
211+
"showcomparison":{"type":"checkbox", "default":true, "name": "Show comparison", "description":"Energy stack comparison"},
212+
"public":{"type":"checkbox", "name": "Public", "default": 0, "optional":true, "description":"Make app public"}
211213
};
212214
config.name = "<?php echo $name; ?>";
213215
config.db = <?php echo json_encode($config); ?>;

apps/OpenEnergyMonitor/myenergy/myenergy.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ function getTranslations(){
131131
var apikey = "<?php echo $apikey; ?>";
132132
var sessionwrite = <?php echo $session['write']; ?>;
133133
feed.apikey = apikey;
134-
134+
feed.public_userid = public_userid;
135+
feed.public_username = public_username;
135136
// ----------------------------------------------------------------------
136137
// Display
137138
// ----------------------------------------------------------------------
@@ -149,7 +150,8 @@ function getTranslations(){
149150
"use":{"type":"feed", "autoname":"use", "engine":"5", "description":_("House or building use in watts")},
150151
"solar":{"optional":true, "type":"feed", "autoname":"solar", "engine":"5", "description":_("Solar pv generation in watts")},
151152
"windkwh":{"type":"value", "default":2000, "name": "kWh Wind", "description":_("kWh of wind energy bought annually")},
152-
"kw":{"type":"checkbox", "default":0, "name": "Show kW", "description":_("Display power as kW")}
153+
"kw":{"type":"checkbox", "default":0, "name": "Show kW", "description":_("Display power as kW")},
154+
"public":{"type":"checkbox", "name": "Public", "default": 0, "optional":true, "description":"Make app public"}
153155
};
154156
config.name = "<?php echo $name; ?>";
155157
config.db = <?php echo json_encode($config); ?>;

0 commit comments

Comments
 (0)