|
1 <?php |
|
2 require_once('config.php'); |
|
3 |
|
4 $target_dir = "tmp/"; |
|
5 $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); |
|
6 $uploadOk = 1; |
|
7 $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); |
|
8 |
|
9 // Check if file already exists |
|
10 if (file_exists($target_file)) { |
|
11 echo "Fout 1: bestand bestaat al. "; |
|
12 $uploadOk = 0; |
|
13 } |
|
14 // Check file size |
|
15 if ($_FILES["fileToUpload"]["size"] > 500000 && $uploadOk) { |
|
16 echo "Fout 2: het bestand is te groot. "; |
|
17 $uploadOk = 0; |
|
18 } |
|
19 // Allow certain file formats |
|
20 if ($imageFileType != "json" && $uploadOk) { |
|
21 echo "Fout 3: alleen JSON bestanden toegestaan. "; |
|
22 $uploadOk = 0; |
|
23 } |
|
24 // Check if $uploadOk is set to 0 by an error |
|
25 if ($uploadOk == 0) { |
|
26 exit; |
|
27 } |
|
28 |
|
29 if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { |
|
30 echo "Verwerken bestand ". basename( $_FILES["fileToUpload"]["name"]). "<br />"; |
|
31 } else { |
|
32 echo "Fout 4: er gieng iets fout met de upload."; |
|
33 exit; |
|
34 } |
|
35 |
|
36 $json= file_get_contents($target_file); |
|
37 $brew = json_decode($json, true); |
|
38 $records = count($brew); |
|
39 if ($records != 1) { |
|
40 echo "Fout 5: dit is geen JSON brouw logfile"; |
|
41 exit; |
|
42 } |
|
43 |
|
44 $connect = mysqli_connect(DBASE_HOST, DBASE_USER, DBASE_PASS, DBASE_NAME); |
|
45 if (! $connect) { |
|
46 echo "Fout 6: ".mysqli_connect_errno()." ".mysqli_connect_error(); |
|
47 exit; |
|
48 } |
|
49 mysqli_set_charset($connect, "utf8" ); |
|
50 date_default_timezone_set('Europe/Amsterdam'); |
|
51 |
|
52 foreach($brew as $brewdata) { |
|
53 |
|
54 foreach($brewdata as $item) { |
|
55 $temps = $item['brewdata']; |
|
56 $events = $item['annotations']; |
|
57 $date = date("Y-m-d H:i:s" , strtotime($item['Date'])); |
|
58 $code = strtok($item['Recipe'], " "); |
|
59 $name = strtok('\0'); |
|
60 $insert = 0; |
|
61 $update = 0; |
|
62 $delete = 0; |
|
63 |
|
64 $sql = "SELECT uuid FROM products WHERE code='".$code."';"; |
|
65 $result = mysqli_query($connect, $sql) or die("SQL Error 1: " . mysqli_error($connect)); |
|
66 if ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { |
|
67 $uuid = $row['uuid']; |
|
68 } |
|
69 echo "Brouw: ".$code." ".$name."<br />"; |
|
70 |
|
71 $sql = "DELETE FROM log_brews WHERE code='".$code."';"; |
|
72 $result = mysqli_query($connect, $sql) or die("SQL Error 1: " . mysqli_error($connect)); |
|
73 $delete += mysqli_affected_rows($connect); |
|
74 |
|
75 foreach($temps as $temp) { |
|
76 // Create full datetime from the Label |
|
77 $t = floatval(substr($temp['Label'], 0, 2)) * 60 + floatval(substr($temp['Label'], 3, 2)); |
|
78 $time = new DateTime($date); |
|
79 $time->add(new DateInterval('PT' . $t . 'M')); |
|
80 $stamp = $time->format('Y-m-d H:i:s'); |
|
81 |
|
82 $sql = "INSERT INTO log_brews SET version='2"; |
|
83 $sql .= "', datetime='" . $stamp; |
|
84 $sql .= "', uuid='" . $uuid; |
|
85 $sql .= "', code='" . $code; |
|
86 $sql .= "', name='" . mysqli_real_escape_string($connect, $name); |
|
87 $sql .= "', pv_mlt='" . $temp['MLT_pv']; |
|
88 $sql .= "', sp_mlt='" . $temp['MLT_sp']; |
|
89 $sql .= "', pwm_mlt='" . $temp['MLT_pwm']; |
|
90 if (isset($temp['HLT_pv'])) |
|
91 $sql .= "', pv_hlt='" . $temp['HLT_pv']; |
|
92 if (isset($temp['HLT_sp'])) |
|
93 $sql .= "', sp_hlt='" . $temp['HLT_sp']; |
|
94 if (isset($temp['HLT_pwm'])) |
|
95 $sql .= "', pwm_hlt='" . $temp['HLT_pwm']; |
|
96 $sql .= "';"; |
|
97 $result = mysqli_query($connect, $sql) or die("SQL Error 1: " . mysqli_error($connect)); |
|
98 $insert += mysqli_affected_rows($connect); |
|
99 } |
|
100 |
|
101 // The events are annotations in the JSON file. |
|
102 foreach($events as $event) { |
|
103 $t = floatval(substr($event['value'], 0, 2)) * 60 + floatval(substr($event['value'], 3, 2)); |
|
104 $time = new DateTime($date); |
|
105 $time->add(new DateInterval('PT' . $t . 'M')); |
|
106 $stamp = $time->format('Y-m-d H:i:s'); |
|
107 |
|
108 $sql = "UPDATE log_brews SET event='".mysqli_real_escape_string($connect, $event['label']['content'])."' WHERE "; |
|
109 $sql .= "datetime='".$stamp."' AND uuid='".$uuid."';"; |
|
110 $result = mysqli_query($connect, $sql) or die("SQL Error 1: " . mysqli_error($connect)); |
|
111 $update += mysqli_affected_rows($connect); |
|
112 } |
|
113 |
|
114 $sql = "UPDATE products SET log_brew='1' WHERE code='".$code."';"; |
|
115 $result = mysqli_query($connect, $sql) or die("SQL Error 1: " . mysqli_error($connect)); |
|
116 $update += mysqli_affected_rows($connect); |
|
117 echo "Database delete: ".$delete.", insert: ".$insert.", update: ".$update." records." . PHP_EOL; |
|
118 } |
|
119 } |
|
120 // Don't clutter the upload directory. |
|
121 unlink($target_file); |
|
122 |
|
123 ?> |