forked from croustibat/OVHPaaSTimeseries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ovhiot.php
84 lines (70 loc) · 2.14 KB
/
ovhiot.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
<?php
/**
* send/read data from OVH Paas IOT plateform
*
*/
class OvhIOT {
// singleton instance
private static $_instance = false;
// store cURL resource
private static $_cURL;
// OVH url where to put data
const OVHIOT_URL_PUT = 'https://opentsdb.iot.runabove.io/api/put';
// OVH url where to read data
const OVHIOT_URL_QUERY = 'https://opentsdb.iot.runabove.io/api/query';
private function __construct()
{
self::$_cURL = curl_init();
}
/**
* instance
*/
public static function getInstance()
{
if (self::$_instance === false)
self::$_instance = new OvhIOT();
return self::$_instance;
}
/**
* read datas
* @param $data array array of data to send
* @param $read_token_id token id given by OVH plateform
* @param $read_token_key token key given by OVH plateform
* @return curl result
*/
public function read(array $data, $read_token_id, $read_token_key)
{
curl_setopt(self::$_cURL, CURLOPT_URL, self::OVHIOT_URL_QUERY);
curl_setopt(self::$_cURL, CURLOPT_USERPWD, $read_token_id.":".$read_token_key);
return self::doCall(json_encode($data));
}
/**
* write datas
* @param $data array array of data to send
* @param $write_token_id token id given by OVH plateform
* @param $write_token_key token key given by OVH plateform
* @return curl result
*/
public function write(array $data, $write_token_id, $write_token_key)
{
curl_setopt(self::$_cURL, CURLOPT_URL, self::OVHIOT_URL_PUT);
curl_setopt(self::$_cURL, CURLOPT_USERPWD, $write_token_id.":".$write_token_key);
return self::doCall(json_encode($data));
}
/**
* exec curl call
* @param $data string json string
* @return curl result
*/
private static function doCall($data_string)
{
curl_setopt(self::$_cURL, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt(self::$_cURL, CURLOPT_POSTFIELDS, $data_string);
curl_setopt(self::$_cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt(self::$_cURL, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
return curl_exec(self::$_cURL);
}
}