Skip to content

Commit

Permalink
Fix all Timezone issues. Plus merging with latest and minor bug fixing.
Browse files Browse the repository at this point in the history
User/timezone.json has changed from offset in hours to seconds to give
support to 15min offset country zones. This may break old external
modules not converted yet.
All is tested and working as of this commit!
  • Loading branch information
chaveiro committed Mar 21, 2015
1 parent c65f8d0 commit ee2bf29
Show file tree
Hide file tree
Showing 23 changed files with 775 additions and 265 deletions.
44 changes: 29 additions & 15 deletions Lib/listjs/list.js
Expand Up @@ -13,6 +13,7 @@ var list = {
'data':{},
'fields':{},
'element':"#table",
'timezones':{},

'init':function()
{
Expand All @@ -21,7 +22,7 @@ var list = {
for (field in list.fields) {
tr = $("<tr />").attr("field", field);
tr.append(' <td type="name" class="muted" style="width:150px;">'+list.fields[field].title+'</td>');
tr.append(' <td type="value">'+(list.fieldtypes[list.fields[field].type].draw(list.data[field],field)||'N/A')+'</td>');
tr.append(' <td type="value">'+(list.fieldtypes[list.fields[field].type].draw(list.data[field])||'N/A')+'</td>');
tr.append(' <td type="edit" action="edit"><i class="icon-pencil" style="display:none"></i></td>');
table.append(tr);
}
Expand All @@ -40,7 +41,7 @@ var list = {
if (action=='save')
{
list.data[field] = list.fieldtypes[list.fields[field].type].save(field);
$(list.element+" tr[field="+field+"] td[type=value]").html(list.fieldtypes[list.fields[field].type].draw(list.data[field], field));
$(list.element+" tr[field="+field+"] td[type=value]").html(list.fieldtypes[list.fields[field].type].draw(list.data[field]));
$(this).html("<i class='icon-pencil' style='display:none'></i>").attr('action','edit');
$(list.element).trigger("onSave",[]);
}
Expand Down Expand Up @@ -68,11 +69,26 @@ var list = {

'select':
{
'draw':function(value,field)
'draw':function(value) { return value },
'edit':function(field,value)
{
var options = '';
for (i in list.fields[field].options)
{
if (list.fields[field].options[i] == value) return list.fields[field].label[i];
var selected = ""; if (list.fields[field].options[i] == value) selected = 'selected';
options += "<option value="+list.fields[field].options[i]+" "+selected+">"+list.fields[field].options[i]+"</option>";
}
return "<select>"+options+"</select>";
},
'save':function(field) { return $(list.element+' tr[field='+field+'] td[type=value] select').val();}
},

'language':
{
'draw':function(value) {
for (i in list.fields['language'].options)
{
if (list.fields['language'].options[i] == value) return list.fields['language'].label[i];
}
},
'edit':function(field,value)
Expand All @@ -92,27 +108,25 @@ var list = {
{
'draw':function(value)
{
var sign = value >= 0 ? '+' : '';
return "UTC "+sign+(value||0)+":00";
return value;
},
'edit':function(field,value)
{
var select = $('<select />'),
selectedIndex = null,
sign;
selectedIndex = null;

for (var i=-12; i<=14; i++) {
for (i in list.timezones) {
var tz = list.timezones[i];
var selected = "";
if (value == i) {
if (value == tz.id) {
selected = 'selected';
selectedIndex = i;
selectedIndex = tz.id;
}
sign = i >= 0 ? '+' : '';
select.append("<option value="+i+" "+selected+">UTC "+sign+i+":00</option>");
select.append("<option value="+tz.id+" "+selected+">"+tz.id+" ("+tz.gmt_offset_text+")</option>");
}
//If no selected index were set, then default to 0
//If no selected index were set, then default to UTC
if ( selectedIndex === null ) {
select.find("option[value='0']").attr('selected', 'selected');
select.find("option[value='UTC']").attr('selected', 'selected');
}
return select.wrap('<p>').parent().html(); //return HTML-string
},
Expand Down
11 changes: 0 additions & 11 deletions Lib/listjs/list.min.js

This file was deleted.

2 changes: 1 addition & 1 deletion Modules/dashboard/Views/js/widgets/vis/vis_render.js
Expand Up @@ -44,7 +44,7 @@ function vis_widgetlist()
"options":["feedid","colour","interval","units","dp","scale","delta"],
"optionstype":["feedid","colour_picker","value","value","value","value","value"],
"optionsname":[_Tr("Feed"),_Tr("Colour"),_Tr("interval"),_Tr("units"),_Tr("dp"),_Tr("scale"),_Tr("delta")],
"optionshint":[_Tr("Feed source"),_Tr("Line colour in hex. Blank is use default."),_Tr("Interval (seconds)-you can set \"d\" for day, \"m\" for month, or \"y\" for year"),_Tr("Units"),_Tr("Decimal points"),_Tr("Scale by"),_Tr("St to \"1\" to show diff between each bar. It displays an ever-increasing Wh feed as a daily\/montly\/yeayly kWh feed (set interval to \"d\", or \"m\", or \"y\" )")],
"optionshint":[_Tr("Feed source"),_Tr("Line colour in hex. Blank is use default."),_Tr("Interval (seconds)-you can set \"d\" for day, \"m\" for month, or \"y\" for year"),_Tr("Units"),_Tr("Decimal points"),_Tr("Scale by"),_Tr("St to \"1\" to show diff between each bar. It displays an ever-increasing Wh feed as a daily\/montly\/yeayly kWh feed (set interval to \"d\", or \"m\", or \"y\")")],
"html":""
},

Expand Down
16 changes: 8 additions & 8 deletions Modules/feed/Views/feedlist_view.php
Expand Up @@ -97,8 +97,8 @@
</select>
</td>
<td>
<p><b>Timezone (for day export):</b></p>
<input id="export-timezone" type="text" />
<p><b>Timezone offset (for day export):</b></p>
<input id="export-timezone-offset" type="text" />
</td>
</tr>
<tr>
Expand Down Expand Up @@ -232,10 +232,10 @@ function updaterStart(func, interval)
$("#SelectedExportFeed").html(table.data[row].tag+": "+table.data[row].name);
$("#export").attr('feedid',table.data[row].id);

if ($("#export-timezone").val()=="") {
var u = user.get();
if (u.timezone==null) u.timezone = 0;
$("#export-timezone").val(parseInt(u.timezone));
if ($("#export-timezone-offset").val()=="") {
var timezoneoffset = user.timezoneoffset();
if (timezoneoffset==null) timezoneoffset = 0;
$("#export-timezone-offset").val(parseInt(timezoneoffset));
}

$('#ExportModal').modal('show');
Expand Down Expand Up @@ -274,7 +274,7 @@ function updaterStart(func, interval)
var export_start = parse_timepicker_time($("#export-start").val());
var export_end = parse_timepicker_time($("#export-end").val());
var export_interval = $("#export-interval").val();
var export_timezone = parseInt($("#export-timezone").val());
var export_timezone_offset = parseInt($("#export-timezone-offset").val());

if (!export_start) {alert("Please enter a valid start date"); return false; }
if (!export_end) {alert("Please enter a valid end date"); return false; }
Expand All @@ -284,7 +284,7 @@ function updaterStart(func, interval)

if (downloadsize>(10*1048576)) {alert("Download file size to large (download limit: 10Mb)"); return false; }

window.open(path+"feed/csvexport.json?id="+feedid+"&start="+(export_start+(export_timezone*3600))+"&end="+(export_end+(export_timezone*3600))+"&interval="+export_interval);
window.open(path+"feed/csvexport.json?id="+feedid+"&start="+(export_start+(export_timezone_offset))+"&end="+(export_end+(export_timezone_offset))+"&interval="+export_interval);
});

function parse_timepicker_time(timestr)
Expand Down
4 changes: 1 addition & 3 deletions Modules/feed/feed_model.php
Expand Up @@ -621,9 +621,7 @@ public function set_timevalue($feedid, $value, $time)
if ($this->redis) {
$this->redis->hMset("feed:lastvalue:$feedid", array('value' => $value, 'time' => $updatetime));
} else {
// Explicitly set decimal separator, in case current locale doesn't use a dot (MySQL expects always a dot, regardless of locale)
$value2=number_format($value,10,'.','');
$this->mysqli->query("UPDATE feeds SET `time` = '$updatetime', `value` = '$value2' WHERE `id`= '$feedid'");
$this->mysqli->query("UPDATE feeds SET `time` = '$updatetime', `value` = '$value' WHERE `id`= '$feedid'");
}
}

Expand Down
6 changes: 2 additions & 4 deletions Modules/input/input_controller.php
Expand Up @@ -31,10 +31,8 @@ function input_controller()
$input = new Input($mysqli,$redis, $feed);

require "Modules/input/process_model.php"; // 886
$process = new Process($mysqli,$input,$feed);
$process = new Process($mysqli,$input,$feed,$user->get_timezone($session['userid']));

$process->set_timezone_offset($user->get_timezone($session['userid']));

if ($route->format == 'html')
{
if ($route->action == 'api') $result = view("Modules/input/Views/input_api.php", array());
Expand Down Expand Up @@ -136,7 +134,7 @@ function input_controller()
for ($i=2; $i<count($item); $i++)
{
if (strlen($item[$i]))
{
{
$value = (float) $item[$i];
$inputs[$name] = $value;
}
Expand Down
31 changes: 12 additions & 19 deletions Modules/input/process_model.php
Expand Up @@ -25,17 +25,18 @@ class Process
private $proc_skip_next; // skip execution of next process in process list
private $proc_goto; // goto step in process list

private $timezoneoffset = 0;
private $timezone = 'UTC';

public function __construct($mysqli,$input,$feed)
public function __construct($mysqli,$input,$feed,$timezone)
{
$this->mysqli = $mysqli;
$this->input = $input;
$this->feed = $feed;
$this->log = new EmonLogger(__FILE__);
if (!($timezone === NULL)) $this->timezone = $timezone;

include "Modules/schedule/schedule_model.php";
$this->schedule = new Schedule($mysqli);
$this->schedule = new Schedule($mysqli, $this->timezone);

// Load MQTT if enabled
// Publish value to MQTT topic, see: http://openenergymonitor.org/emon/node/5943
Expand All @@ -47,11 +48,6 @@ public function __construct($mysqli,$input,$feed)
$this->mqtt = $mqtt;
}
}

public function set_timezone_offset($timezoneoffset)
{
$this->timezoneoffset = $timezoneoffset;
}

public function get_process_list()
{
Expand Down Expand Up @@ -703,23 +699,19 @@ public function publish_to_mqtt($topic, $time, $value)

// Schedule
public function if_not_schedule_zero($scheduleid, $time, $value) {
$expression = $this->schedule->get_expression($scheduleid)["expression"];
$result = $this->schedule->match($expression, $time);
$result = $this->schedule->match($scheduleid, $time);
return ($result ? $value : 0);
}
public function if_not_schedule_null($scheduleid, $time, $value) {
$expression = $this->schedule->get_expression($scheduleid)["expression"];
$result = $this->schedule->match($expression, $time);
$result = $this->schedule->match($scheduleid, $time);
return ($result ? $value : null);
}
public function if_schedule_zero($scheduleid, $time, $value) {
$expression = $this->schedule->get_expression($scheduleid)["expression"];
$result = $this->schedule->match($expression, $time);
$result = $this->schedule->match($scheduleid, $time);
return ($result ? 0 : $value);
}
public function if_schedule_null($scheduleid, $time, $value) {
$expression = $this->schedule->get_expression($scheduleid)["expression"];
$result = $this->schedule->match($expression, $time);
$result = $this->schedule->match($scheduleid, $time);
return ($result ? null : $value);
}

Expand Down Expand Up @@ -798,9 +790,10 @@ public function heat_flux($feedid,$time_now,$value) { return $value; } // Remove
// Get the start of the day
private function getstartday($time_now)
{
// $midnight = mktime(0, 0, 0, date("m",$time_now), date("d",$time_now), date("Y",$time_now)) - ($this->timezoneoffset * 3600);
// $this->log->warn($midnight." ".date("Y-n-j H:i:s",$midnight)." [".$this->timezoneoffset."]");
return mktime(0, 0, 0, date("m",$time_now), date("d",$time_now), date("Y",$time_now)) - ($this->timezoneoffset * 3600);
$now = DateTime::createFromFormat("U", $time_now);
$now->setTimezone(new DateTimeZone($this->timezone));
$now->setTime(0,0); // Today at 00:00
return $now->format("U");
}

}
10 changes: 5 additions & 5 deletions Modules/schedule/Views/schedule.js
Expand Up @@ -25,11 +25,11 @@ var schedule = {
{
$.ajax({ url: path+"schedule/delete.json", data: "id="+id, async: false, success: function(data){} });
},
'test':function(expression)
'test':function(id)
{
var result = {};
$.ajax({ url: path+"schedule/test.json", data: "expression="+expression, async: false, success: function(data){result = data;} });
return result;
$.ajax({ url: path+"schedule/test.json", data: "id="+id, async: false, success: function(data){result = data;} });
return result;
}
}
}

0 comments on commit ee2bf29

Please sign in to comment.