-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
214 lines (171 loc) · 3.9 KB
/
lib.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
$DB = false;
$REDIS = false;
$schema = [
'sessions' => [
'id' => 'INTEGER PRIMARY KEY',
'start' => 'TIMESTAMP',
'end' => 'TIMESTAMP',
'session'=> 'INTEGER',
'kwh' => 'FLOAT',
'cost' => 'FLOAT',
'evse' => 'INTEGER',
'user' => 'INTEGER',
'status' => 'TEXT',
'paid' => 'BOOLEAN default false'
]
];
function do_error($what) {
echo json_encode(['res' => false, 'data' => $what]);
exit(0);
}
function do_success($what) {
echo json_encode(['res' => true, 'data' => $what]);
exit(0);
}
function db_string($what) {
return "'$what'";
}
function db_date($what) {
$what = db_string(trim($what, '"\''));
return "datetime($what)";
}
function aget($source, $keyList, $default = null) {
if(!is_array($keyList)) {
$keyStr = $keyList;
$keyList = explode('.', $keyStr);
$orList = explode('|', $keyStr);
if(count($orList) > 1) {
$res = null;
foreach($orList as $key) {
// this resolves to the FIRST valid value
if($res === null) {
$res = aget($source, $key);
}
}
return ($res === null) ? $default : $res;
}
}
$key = array_shift($keyList);
if($source && isset($source[$key])) {
if(count($keyList) > 0) {
return aget($source[$key], $keyList);
}
return $source[$key];
}
return $default;
}
function get_column_list($table_name) {
$db = get_db();
$res = $db->query("pragma table_info( $table_name )");
return array_map(function($row) {
return $row['name'];
}, sql_all($res));
}
function get_redis() {
global $REDIS;
if(!$REDIS) {
$REDIS = new Redis();
$REDIS->connect('127.0.0.1');
}
return $REDIS;
}
function get_db() {
global $DB;
if(!$DB) {
$DB = new SQLite3(__DIR__ . "/../../ocpi.db");
}
return $DB;
}
function db_one($qstr) {
$db = get_db();
error_log($qstr);
return $db->querySingle($qstr, true);
}
function db_all($qstr) {
$res = [];
$db = get_db();
if($sql = $db->query($qstr)) {
while(($res[] = $sql->fetchArray(SQLITE3_ASSOC)) !== false);
}
array_pop($res);
return $res;
}
function db_get($key) {
if(empty($key)) {
return false;
}
$db = get_db();
$qstr = "select * from sessions where session=$key";
error_log($qstr);
return $db->querySingle($qstr, true);
}
function reservation($id, $user = false) {
if(!$id) {
return false;
}
$key = "evse:$id";
$redis = get_redis();
if($user === false) {
return $redis->get($key);
}
$row = db_one("select * from sessions where status='active' and evse=$id");
if($row) {
db_update('sessions', $row['id'], ['user' => $user]);
}
// we give it some time to start
$redis->set($key, $user, 240);
}
function db_update($table, $id, $kv) {
$fields = [];
$db = get_db();
foreach($kv as $k => $v) {
$fields[] = "$k=$v";
}
$fields = implode(',', $fields);
$qstr = "update $table set $fields where id=$id";
error_log($qstr);
if(!empty($id)) {
return $db->exec($qstr);
}
}
function db_insert($table, $kv) {
$fields = [];
$values = [];
$db = get_db();
foreach($kv as $k => $v) {
$fields[] = $k;
if($v === false) {
$values[] = 'false';
} else {
$values[] = $v;
}
}
$values = implode(',', $values);
$fields = implode(',', $fields);
$qstr = "insert into $table($fields) values($values)";
error_log($qstr);
if($db->exec($qstr)) {
return $db->lastInsertRowID();
}
return $qstr;
}
function sql_all($sql_res) {
$res = [];
while( ($res[] = $sql_res->fetchArray(SQLITE3_ASSOC)) );
array_pop($res);
return $res;
}
function sql_kv($hash, $operator = '=', $quotes = "'", $intList = []) {
$ret = [];
foreach($hash as $key => $value) {
if ( is_string($value) ) {
if(in_array($key, $intList)) {
$ret[] = "$key $operator $value";
} else {
$ret[] = "$key $operator $quotes$value$quotes";
}
}
}
return $ret;
}