-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSensorRecieveData.module
131 lines (100 loc) · 3.75 KB
/
SensorRecieveData.module
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
<?php namespace ProcessWire;
include "inc/dataObject.php";
/**
* Sensor data reciever module/page
*
* This module creates an URL hook to recieve data from different sensors.
* The Hook creates a page routing that pipes all incomming traffic to that URL to this module.
*/
class SensorRecieveData extends WireData implements Module {
/**
* getModuleInfo is a module required by all modules to tell ProcessWire about them
*
* @return array
*
*/
public static function getModuleInfo() {
return array(
'title' => 'Sensor data reciever module/page',
'version' => "0.1.2",
'summary' => 'URL Hook whith funktionality to recieve sensor data.',
'singular' => true,
'autoload' => true
);
}
/**
* This little hook does all the webpage routing to the recieve method.
*
* Init comes before ready so this comes before RedirOnMain
*/
public function Init() {
$this->pages->addHook('/reciever/', $this, 'recieveData');
}
/**
* Recieve method get called when Hook is triggered
*/
public function recieveData($event) {
// Fetch JSON Data
$jsonString = $this->input->post('jsonData');
// Nothing to process so end it here
if (empty($jsonString)) {
return "!! jsonData not found or empty !!";
}
$myData = dataObject::Deserialize($jsonString);
// Data was completely invalid return the error if debug is on
if (is_string($myData)) {
// only return information if debug is on
if ($this->config->debug){
return print_r($myData,true);
}
else {
return "!! Failed !!";
}
}
//return "\nSUCCESS\n\n". print_r($myData,true);
// From here we create and validate for the Data Page
//$myTemplate= $this->templates->get("Dataset");
// @todo I would prefer to add additional field based validation here
// So please keep this lines in.
/*$out ="Felder: ";
foreach ($myTemplate->fields as $f) {
$out.= $f->name . " ";
}
return $out;*/
//Try to find the matching sensor
$sensor=$this->pages->get("template=Sensor, naming={$myData->title}");
if(empty($sensor))
return "!! Sensor not found !!";
// fetch IP for comparison later
// Testsensors have invalid Ip
$Ip=$this->session->getIP();
// Check the hash
if ($sensor->password_hash != $myData->hash)
return "!! Sensor failed to authenticate Sensor: $sensor->password_hash Data: $myData->hash!!\n";
if (!$sensor->active)
return "!! Sensor Inactive !!";
// return "\nSUCCESS\n\n". print_r($myData,true);
//return "\nSUCCESS \n\n";
// Creaate a new page
$p = new Page();
$p->template = "Dataset";
$p->parent = 1106; //Datesets Parent
$p->title = "Dataset ". $sensor->naming." ". $sensor->sensor_room->title." ". date('Y-m-d');
$p->save();
// Save page data
$p->date_time=time();
$p->dataset_sensor=$sensor;
$p->dataset_room=$sensor->sensor_room;
$p->co2_ppm=$myData->co2Ppm;
$p->co2_value=$myData->co2Value;
$p->temperature=$myData->temperature;
$p->temp_value=$myData->tempValue;
$p->save();
//Save some values to sensor
//turn of output formating to save stuff directly $sensor->of(false);
$sensor->edit("co2_current_level", $myData->co2Ppm);
$sensor->edit("temp_current_level", $myData->temperature);
$sensor->save();
return "\nSUCCESS Saved\n\n";
}
}