Advertisement
dag_karlsson

Untitled

Apr 25th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.77 KB | None | 0 0
  1. ob_start(); // Allows the session header to be cached
  2.  
  3. $GLOBALS['server'] = "http://localhost/tests/Tuto_OCR/SailingRobotsWebsite/";
  4. //$GLOBALS['server'] = "http://www.sailingrobots.com/testdata/";
  5. $GLOBALS['username'] = 'root';
  6. //$GLOBALS['username'] = 'ithaax_testdata';
  7. $GLOBALS['password'] = '';
  8. //$GLOBALS['password'] = 'test123data';
  9. $GLOBALS['hostname'] = '127.0.0.1';
  10.  
  11. $GLOBALS['database_name_testdata'] = 'ithaax_testdata';
  12. $GLOBALS['database_name'] = 'ithaax_website_config';
  13. $GLOBALS['database_ASPire'] = 'ithaax_ASPire_config';
  14. $GLOBALS['database_mission'] = 'ithaax_mission';
  15.  
  16.  
  17.  
  18. $hostname = $GLOBALS['hostname'];
  19. $username = $GLOBALS['username'];
  20. $password = $GLOBALS['password'];
  21. $dbname = $GLOBALS['database_ASPire'];
  22. try
  23. {
  24. $db = new PDO("mysql:host=$hostname;dbname=$dbname;charset=utf8;port=3306",
  25. $username,
  26. $password,
  27. array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  28. );
  29. }
  30. catch(Exception $e)
  31. {
  32. die('Error : '.$e->getMessage());
  33. }
  34. $GLOBALS['db_connection'] = $db;
  35.  
  36.  
  37. $db = $GLOBALS['db_connection'];
  38. $data='{"dataLogs_actuator_feedback":[{"id":"155","rc_on":"0","rudder_position":"-2000.0","t_timestamp":"2018-04-25 05:51:03.861","wind_vane_angle":"-2000.0","wingsail_position":"-2000.0"}],"dataLogs_compass":[{"heading":"123.0","id":"155","pitch":"0.0","roll":"0.0","t_timestamp":"2018-04-25 05:51:03.861"}],"dataLogs_course_calculation":[{"bearing_to_waypoint":"98.0","course_to_steer":"142.1232758","distance_to_waypoint":"1784.544189","going_starboard":"1","id":"155","t_timestamp":"2018-04-25 05:51:03.861","tack":"0"}],"dataLogs_current_sensors":[{"actuator_unit":"-2000.0","id":"155","navigation_unit":"-2000.0","sailboat_drive":"-2000.0","t_timestamp":"2018-04-25 05:51:03.861","wind_vane_angle":"-2000.0","wind_vane_clutch":"-2000.0"}],"dataLogs_gps":[{"course":"123.0","has_fix":"1","id":"155","latitude":"60.10724258","longitude":"19.92242813","online":"1","route_started":"0","satellites_used":"0","speed":"0.06338219345","t_timestamp":"2018-04-25 05:51:03.861","time":"2018-04-25 05:51:03.861"}],"dataLogs_marine_sensors":[{"conductivity":"1.171875","id":"155","ph":"7.171875","salinity":"0.01003836561","t_timestamp":"2018-04-25 05:51:03.861","temperature":"20.171875"}],"dataLogs_system":[{"actuator_feedback_id":"155","compass_id":"155","course_calculation_id":"155","current_mission_id":"1501671669","current_sensors_id":"155","gps_id":"155","id":"155","marine_sensors_id":"155","vessel_state_id":"155","wind_state_id":"155","windsensor_id":"155"}],"dataLogs_vessel_state":[{"course":"123.0","heading":"123.0","id":"155","latitude":"60.10724258","longitude":"19.92242813","speed":"0.06338219345","t_timestamp":"2018-04-25 05:51:03.861"}],"dataLogs_wind_state":[{"apparent_wind_direction":"147.0","apparent_wind_speed":"2.947221518","id":"155","t_timestamp":"2018-04-25 05:51:03.861","true_wind_direction":"270.6591797","true_wind_speed":"3.000576973"}],"dataLogs_windsensor":[{"direction":"147.0","id":"155","speed":"2.947221518","t_timestamp":"2018-04-25 05:51:03.861","temperature":"21.0"}]}';
  39.  
  40. $data = json_decode($data,true);
  41.  
  42. if (!empty($data))
  43. {
  44. error_log("GOT HERE");
  45.  
  46. foreach ($data as $table_name => $table)
  47. {
  48. // Generate the array to be bind with the prepared SQL query
  49. foreach ($table as $id_log => $log)
  50. {
  51. $param_stmt = "(";
  52. $param_to_fill = "(";
  53. $param_array = array();
  54. foreach ($log as $column_name => $value)
  55. {
  56. $param_array[$column_name] = $value;
  57. $param_stmt = $param_stmt . ''.$column_name .',' ;
  58. $param_to_fill = $param_to_fill . ':'.$column_name.',';
  59. }
  60.  
  61. // Remove the extra comma
  62. $param_stmt = substr($param_stmt, 0, -1).')';
  63. $param_to_fill = substr($param_to_fill, 0, -1).')';// Now whe hace something like (?, ?, ?, ?)
  64.  
  65. $param_array['id'] = NULL;
  66. // Prepare the SQL Query
  67. $query = $db->prepare("INSERT INTO $table_name $param_stmt VALUES $param_to_fill ;");
  68. $query->execute($param_array);
  69.  
  70.  
  71.  
  72. }
  73.  
  74. }
  75. //$result = array('result' => 1);
  76. //return json_encode($result);
  77. }
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. $data='{"dataLogs_actuator_feedback":[{"id":"155","rc_on":"0","rudder_position":"-2000.0","t_timestamp":"2018-04-25 05:51:03.861","wind_vane_angle":"-2000.0","wingsail_position":"-2000.0"}],"dataLogs_compass":[{"heading":"123.0","id":"155","pitch":"0.0","roll":"0.0","t_timestamp":"2018-04-25 05:51:03.861"}],"dataLogs_course_calculation":[{"bearing_to_waypoint":"98.0","course_to_steer":"142.1232758","distance_to_waypoint":"1784.544189","going_starboard":"1","id":"155","t_timestamp":"2018-04-25 05:51:03.861","tack":"0"}],"dataLogs_current_sensors":[{"actuator_unit":"-2000.0","id":"155","navigation_unit":"-2000.0","sailboat_drive":"-2000.0","t_timestamp":"2018-04-25 05:51:03.861","wind_vane_angle":"-2000.0","wind_vane_clutch":"-2000.0"}],"dataLogs_gps":[{"course":"123.0","has_fix":"1","id":"155","latitude":"60.10724258","longitude":"19.92242813","online":"1","route_started":"0","satellites_used":"0","speed":"0.06338219345","t_timestamp":"2018-04-25 05:51:03.861","time":"2018-04-25 05:51:03.861"}],"dataLogs_marine_sensors":[{"conductivity":"1.171875","id":"155","ph":"7.171875","salinity":"0.01003836561","t_timestamp":"2018-04-25 05:51:03.861","temperature":"20.171875"}],"dataLogs_system":[{"actuator_feedback_id":"155","compass_id":"155","course_calculation_id":"155","current_mission_id":"1501671669","current_sensors_id":"155","gps_id":"155","id":"155","marine_sensors_id":"155","vessel_state_id":"155","wind_state_id":"155","windsensor_id":"155"}],"dataLogs_vessel_state":[{"course":"123.0","heading":"123.0","id":"155","latitude":"60.10724258","longitude":"19.92242813","speed":"0.06338219345","t_timestamp":"2018-04-25 05:51:03.861"}],"dataLogs_wind_state":[{"apparent_wind_direction":"147.0","apparent_wind_speed":"2.947221518","id":"155","t_timestamp":"2018-04-25 05:51:03.861","true_wind_direction":"270.6591797","true_wind_speed":"3.000576973"}],"dataLogs_windsensor":[{"direction":"147.0","id":"155","speed":"2.947221518","t_timestamp":"2018-04-25 05:51:03.861","temperature":"21.0"}]}';
  86.  
  87. $data = json_decode($data,true);
  88.  
  89. if (!empty($data))
  90. {
  91. $idMap = array();
  92.  
  93. $dataLogs_system = $data['dataLogs_system'];
  94. unset($data['dataLogs_system']);
  95. //print_r($data);
  96.  
  97. foreach ($data as $table_name => $table)
  98. {
  99. // Generate the array to be bind with the prepared SQL query
  100. foreach ($table as $id_log => $log)
  101. {
  102.  
  103. $param_stmt = "(";
  104. $param_to_fill = "(";
  105. $param_array = array();
  106. foreach ($log as $column_name => $value)
  107. {
  108. $param_array[$column_name] = $value;
  109. $param_stmt = $param_stmt . ''.$column_name .',' ;
  110. $param_to_fill = $param_to_fill . ':'.$column_name.',';
  111. }
  112. // Remove the extra comma
  113. $param_stmt = substr($param_stmt, 0, -1).')';
  114. $param_to_fill = substr($param_to_fill, 0, -1).')';// Now whe hace something like (?, ?, ?, ?)
  115. $param_array['id'] = NULL;
  116. // Prepare the SQL Query
  117.  
  118.  
  119. $query = $db->prepare("INSERT INTO $table_name $param_stmt VALUES $param_to_fill ;");
  120. $query->execute($param_array);
  121.  
  122.  
  123. $trueTableNameStartPos = strpos($table_name, 'dataLogs_');
  124.  
  125. if ($trueTableNameStartPos !== false) {
  126. $sql= "SELECT MAX(id) FROM $table_name";
  127. $result = $db->query($sql);
  128.  
  129.  
  130. $trueTableNameStart = substr($table_name, $trueTableNameStartPos);
  131. $idMap[$trueTableNameStart] = $result->fetch()['MAX(id)'];
  132. }
  133.  
  134.  
  135. }
  136.  
  137. }
  138. //print_r($idMap);
  139.  
  140.  
  141.  
  142. foreach ($idMap as $column_name => $value)
  143. {
  144. $dataLogs_system[$column_name."_id"] = $value;
  145. }
  146.  
  147.  
  148. $param_stmt = "(";
  149. $param_to_fill = "(";
  150. $param_array = array();
  151.  
  152. print_r($idMap);
  153. echo "<br><br><br>";
  154.  
  155.  
  156. print_r($dataLogs_system[0]);
  157. echo "<br><br><br>";
  158. foreach ($dataLogs_system[0] as $column_name => $value)
  159. {
  160. $param_array[$column_name] = $value;
  161. $param_stmt = $param_stmt . ''.$column_name .',' ;
  162. $param_to_fill = $param_to_fill . ':'.$column_name.',';
  163. }
  164. // Remove the extra comma
  165. $param_stmt = substr($param_stmt, 0, -1).')';
  166. $param_to_fill = substr($param_to_fill, 0, -1).')';// Now whe hace something like (?, ?, ?, ?)
  167.  
  168. $param_array['id'] = NULL;
  169. // Prepare the SQL Query
  170.  
  171. echo "INSERT INTO dataLogs_system $param_stmt VALUES $param_to_fill ;";
  172. echo "<br><br><br>";
  173. print_r($param_array);
  174.  
  175. $query = $db->prepare("INSERT INTO dataLogs_system $param_stmt VALUES $param_to_fill ;");
  176.  
  177.  
  178. $query->execute($param_array);
  179.  
  180.  
  181.  
  182. //$result = array('result' => 1);
  183. //return json_encode($result);
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement