/
wattsup_controller.php
136 lines (114 loc) · 5.64 KB
/
wattsup_controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');
function wattsup_controller()
{
global $mysqli, $redis, $user, $session, $route, $feed_settings;
// First up, a little hack.
// We need to include an API key with our POST data from the Watts Up?,
// but the stupid thing limits how long our POST location string can be.
// SO, we put the API key in the user agent string, cause why not.
$apikey = $_SERVER["HTTP_USER_AGENT"];
$session = $user->apikey_session($apikey);
if (empty($session)) {
header($_SERVER["SERVER_PROTOCOL"]." 401 Unauthorized");
header('WWW-Authenticate: Bearer realm="API KEY", error="invalid_apikey", error_description="Invalid API key"');
print "Invalid API key";
$log = new EmonLogger(__FILE__);
$log->error("Invalid API key '" . $apikey. "'");
exit();
}
// There are no actions in the input module that can be performed with less than write privileges
if (!$session['write']) return array('content'=>false);
$result = false;
// Need to get correct files so that we can make inputs
require_once "Modules/feed/feed_model.php";
$feed = new Feed($mysqli, $redis, $feed_settings);
require_once "Modules/input/input_model.php";
$input = new Input($mysqli, $redis, $feed);
require_once "Modules/process/process_model.php";
$process = new Process($mysqli, $input, $feed, $user->get_timezone($session['userid']));
// Process /wattsup/post.text messages from Watts Up? .net
if ($route->action == 'post' && $route->format == 'text') {
// This looks like a correctly configured Watts Up? .net POST
$valid = true;
$error = '';
$userid = $session['userid'];
$dbinputs = $input->get_inputs($userid);
// id is set to the Watts Up? device ID
$nodeid = preg_replace('/[^\p{N}\p{L}_\s-.]/u', '', post('id'));
// Make sure we can do this. Copied from input_controller.php
$validate_access = $input->validate_access($dbinputs, $nodeid);
if (!$validate_access['success']) {
$valid = false;
$error = $validate_access['message'];
} else {
// Insert this record into the emoncms format
$time = time();
// Array to store the relevant fields in
$data = array();
$watts = post('w');
$volts = post('v');
$amps = post('a');
$watth = post('wh');
$maxwatts = post('wmx');
$maxvolts = post('vmx');
$maxamps = post('amx');
$minwatts = post('wmi');
$minvolts = post('vmi');
$minamps = post('ami');
$pf = post('pf');
$pcy = post('pcy');
$freq = post('frq');
$voltamps = post('va');
# Only include fields we actually got
if (is_numeric($watts)) $data['watts'] = $watts / 10;
if (is_numeric($volts)) $data['volts'] = $volts / 10;
if (is_numeric($amps)) $data['amps'] = $amps / 1000;
if (is_numeric($watth)) $data['watt_hours'] = $watth / 1000;
if (is_numeric($maxwatts)) $data['max_watts'] = $maxwatts / 10;
if (is_numeric($maxvolts)) $data['max_volts'] = $maxvolts / 10;
if (is_numeric($maxamps)) $data['max_amps'] = $maxamps / 1000;
if (is_numeric($minwatts)) $data['min_watts'] = $minwatts / 10;
if (is_numeric($minvolts)) $data['min_volts'] = $minvolts / 10;
if (is_numeric($minamps)) $data['min_amps'] = $minamps / 1000;
if (is_numeric($pf)) $data['power_factor'] = $pf;
if (is_numeric($pcy)) $data['power_cycle'] = $pcy;
if (is_numeric($freq)) $data['freq'] = $freq / 10;
if (is_numeric($voltamps)) $data['volt_amps'] = $voltamps / 10;
// Iterate all new data items to insert
$tmp = array();
foreach ($data as $name => $value) {
// Check if this is an existing field in this node or not
if (!isset($dbinputs[$nodeid][$name])) {
// New field.
$inputid = $input->create_input($userid, $nodeid, $name);
$dbinputs[$nodeid][$name] = true;
$dbinputs[$nodeid][$name] = array('id'=>$inputid, 'processList'=>'');
$input->set_timevalue($dbinputs[$nodeid][$name]['id'], $time, $value);
} else {
// Existing field, just insert
$input->set_timevalue($dbinputs[$nodeid][$name]['id'], $time, $value);
// If there are processes listening to this field, we need
// to pass the data to those as well
if ($dbinputs[$nodeid][$name]['processList']) {
$tmp[] = array('value'=>$value,
'processList'=>$dbinputs[$nodeid][$name]['processList'],
'opt'=>array('sourcetype'=>"WATTSUP",
'sourceid'=>$dbinputs[$nodeid][$name]['id']));
}
}
}
// Actually insert all of the data to the process
foreach ($tmp as $i) {
$process->input($time, $i['value'], $i['processList'], $i['opt']);
}
}
if ($valid) {
$result = 'ok';
} else {
$result = "Error: $error\n";
}
}
return array('content'=>$result);
}