Advertisement
boelle11

Untitled

Nov 30th, 2021
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.48 KB | None | 0 0
  1. <?php
  2. //error_reporting(E_ALL);
  3. //ini_set('display_errors', 1);
  4.  
  5. // ADJUST THESE PARAMETERS
  6. $vacuum_log = 'http://192.168.0.9:3001/vacuum2.log'; # Could also be HTTPS
  7. $file_append = 'Robert'; # Allows differentiation of files for different floors or robots
  8. $robot_type = 'roomba'; # Select between roomba and braava for different icons
  9. $set_first_coordinate = 0; # Ability to skip initial coordinate(s) if incorrect data logged
  10. $overlay_image = 'floor.png'; # Background Layer
  11. $overlay_walls = false; # Allows overlaying of walls, used in fill mode to cover 'spray'
  12. $walls_image = 'walls.png'; # Walls Image must contain transparent floor
  13. $show_stuck_positions = true;
  14. $line_thickness = 30; # Default 2, Set to ~60 for Fill Mode
  15. $map_width = 3118; # Ensure overlay and wall images match this size
  16. $map_height = 4413; # Ensure overlay and wall images match this size
  17. $x_offset = 570; #op ned -Ned
  18. $y_offset = -505; #Højre venstre -Venstre
  19. $flip_vertical = true;
  20. $flip_horizontal = true;
  21. $render_status_text = true;
  22. $rotate_angle = -88; # Allows rotating of the roomba lines
  23. $x_scale=3.62; # Allows scaling of roomba x lines
  24. $y_scale=3.62; # Allows scaling of roomba y lines
  25. $ha_rest980 = 'http://192.168.0.9:8123/api/states/sensor.rest980_2';
  26. $ha_token = 'FORMYEYESONLY';
  27. $ha_timezone = 'Europe/Copenhagen'; # Supported Timezones https://www.php.net/manual/en/timezones.php
  28. $ha_text_delimiter = " \n"; # How text is displayed on the map top " \n" --> New Line ## " |" --> Show on one line
  29. //
  30. // Line Color - RGB
  31. // -1 represents gradual increase from 0 to 255 based on number of logged locations
  32. //
  33. $color_red = -1;
  34. $color_green = 255;
  35. $color_blue = -1;
  36. //
  37. // Examples
  38. // red = -1 , green = 255 , blue = -1 ---> Green to White Fade
  39. // red = 0 , green = -1 , blue = 255 ---> Blue to Aqua Fade
  40. // red = 0 , green = 0 , blue = 255 ---> Solid Blue
  41. //
  42. $path_opacity = 0.5; # Opacity of Roomba path --> 0.0 = completely transparent, 1.0 = completely opaque
  43. //
  44. ///////////////////////////////////////////////////////////////////
  45.  
  46. if(isset($_GET['clear'])) {
  47. @unlink("latest".$file_append.".png");
  48. die();
  49. }
  50. if(is_file("latest".$file_append.".png")&&!isset($_GET['last'])) {
  51. header("Content-Type: image/png");
  52. echo file_get_contents("latest".$file_append.".png");
  53. die();
  54. }
  55.  
  56. $coords = file_get_contents($robot_log."?v=".time());
  57. $coords = str_replace("(", "", $coords);
  58. $coords = str_replace(")", "", $coords);
  59. $coords = explode("\n", $coords);
  60.  
  61. if (count($coords) < 2) {
  62. echo "No Coordinates found in file, is it reachable and populated? Log file - $robot_log?";
  63. die();
  64. }
  65.  
  66. $date = strtotime(substr($coords[0], 42));
  67.  
  68. $lastline = $coords[sizeof($coords)-2];
  69. $end = ["Stuck", "Finished"]; // PAUSE also available
  70.  
  71. array_shift($coords);
  72. array_shift($coords);
  73. array_pop($coords);
  74.  
  75. function imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1)
  76. {
  77. if ($thick == 1) {
  78. return imageline($image, $x1, $y1, $x2, $y2, $color);
  79. }
  80. $t = $thick / 2 - 0.5;
  81. if ($x1 == $x2 || $y1 == $y2) {
  82. return imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
  83. }
  84. $k = ($y2 - $y1) / ($x2 - $x1);
  85. $a = $t / sqrt(1 + pow($k, 2));
  86. $points = array(
  87. round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a),
  88. round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a),
  89. round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
  90. round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
  91. );
  92. imagefilledpolygon($image, $points, 4, $color);
  93. return imagepolygon($image, $points, 4, $color);
  94. }
  95.  
  96. $image = imagecreatetruecolor($map_width,$map_height);
  97. imagesavealpha($image, true);
  98. $black = imagecolorallocatealpha($image,0,0,0, 127);
  99. imagefill($image,0,0,$black);
  100.  
  101. $robot = imagecreatefrompng($robot_type.'.png');
  102. imagealphablending($robot, false);
  103. imagesavealpha($robot, true);
  104.  
  105. foreach($coords as $i => $coord) {
  106. # Skip initial coordinates if needed
  107. if ($i < $set_first_coordinate) {
  108. continue;
  109. }
  110. $split = explode(",", $coord);
  111. if(sizeof($split)<2) {
  112. if(($coord == "Stuck") & ($show_stuck_positions)) {
  113. $robot_stuck = imagecreatefrompng($robot_type.'_stuck.png');
  114. imagealphablending($robot_stuck, false);
  115. imagesavealpha($robot_stuck, true);
  116. $robot_stuck = imagerotate($robot_stuck, $oldtheta*-1, imageColorAllocateAlpha($robot_stuck, 0, 0, 0, 127));
  117. imagealphablending($robot_stuck, false);
  118. imagesavealpha($robot_stuck, true);
  119. imagecopy($image, $robot_stuck, $oldx-10, $oldy-5, 0, 0, imagesx($robot_stuck), imagesy($robot_stuck));
  120. imagedestroy($robot_stuck);
  121. }
  122. continue;
  123. }
  124.  
  125. $part= hexdec("ff");
  126. $part = round($part * $i/sizeof($coords));
  127.  
  128. // Calculate Line Color
  129. $red = ($color_red === -1 ? $part : $color_red);
  130. $green = ($color_green === -1 ? $part : $color_green);
  131. $blue = ($color_blue === -1 ? $part : $color_blue);
  132.  
  133. $alpha = (1.0 - $path_opacity) * 127;
  134. $color = imagecolorallocatealpha($image, $red, $green, $blue, $alpha);
  135. $tmpx = $split[1]+$x_offset;
  136. $tmpy = $split[0]+$y_offset;
  137. $theta = $split[2];
  138.  
  139. // Rotate Calculations
  140. $x=($tmpx*cos(deg2rad($rotate_angle))+$tmpy*sin(deg2rad($rotate_angle)))*$x_scale;
  141. $y=(-1*$tmpx*sin(deg2rad($rotate_angle))+$tmpy*cos(deg2rad($rotate_angle)))*$y_scale;
  142.  
  143. $boxsize=4;
  144. $shift_y = 2;
  145. $shift_x = -2;
  146.  
  147. imagerectangle($image, $x+$shift_x, $y+$shift_y, $x+$boxsize+$shift_x, $y+$boxsize+$shift_y, $color);
  148. if(isset($oldx) && isset($oldy)) {
  149. imagelinethick($image, $oldx+($boxsize/2)+$shift_x, $oldy+($boxsize/2)+$shift_y, $x+($boxsize/2)+$shift_x, $y+($boxsize/2)+$shift_y, $color, $line_thickness);
  150. }
  151.  
  152. if($i+1==sizeof($coords)) {
  153. if (sizeof($split)>2) {
  154. $robot = imagerotate($robot, $theta*-1, imageColorAllocateAlpha($robot, 0, 0, 0, 127));
  155. imagealphablending($robot, false);
  156. imagesavealpha($robot, true);
  157. imagecopy($image, $robot, $x-10, $y-5, 0, 0, imagesx($robot), imagesy($robot));
  158. }
  159. }
  160.  
  161. $oldx = $x;
  162. $oldy = $y;
  163. $oldtheta = $theta;
  164. }
  165.  
  166. if(in_array($lastline, $end)) {
  167. imagedestroy($robot);
  168.  
  169. if($lastline == "Stuck") {
  170. $overlayImage = imagecreatefrompng($robot_type.'_stuck.png');
  171. imagealphablending($overlayImage, false);
  172. imagesavealpha($overlayImage, true);
  173. $color = imagecolorallocate($image, 0, 149, 223);
  174. $finishedRoomba = imagerotate($overlayImage, $theta*-1, imageColorAllocateAlpha($overlayImage, 0, 0, 0, 127));
  175. imagelinethick($image, $oldx+($boxsize/2), $oldy+($boxsize/2), $x+($boxsize/2)+3, $y+($boxsize/2)+10, $color, 2);
  176. imagecopy($image, $finishedRoomba, $oldx-10, $oldy-5, 0, 0, imagesx($finishedRoomba), imagesy($finishedRoomba));
  177. }
  178. else if($lastline == "Finished") {
  179. $overlayImage = imagecreatefrompng($robot_type.'_charging.png');
  180. imagealphablending($overlayImage, false);
  181. imagesavealpha($overlayImage, true);
  182. $color = imagecolorallocate($image, 0, 149, 223);
  183. $finishedRoomba = imagerotate($overlayImage, $theta*-1, imageColorAllocateAlpha($overlayImage, 0, 0, 0, 127));
  184. imagelinethick($image, $oldx+($boxsize/2), $oldy+($boxsize/2), $x+($boxsize/2)+3, $y+($boxsize/2)+10, $color, 2);
  185. imagecopy($image, $finishedRoomba, $oldx-10, $oldy-5, 0, 0, imagesx($finishedRoomba), imagesy($finishedRoomba));
  186. }
  187.  
  188. }
  189. if($flip_vertical) {
  190. imageflip( $image, IMG_FLIP_VERTICAL );
  191. }
  192.  
  193. if($flip_horizontal) {
  194. imageflip( $image, IMG_FLIP_HORIZONTAL );
  195. }
  196.  
  197. // Create Final Image
  198. $dest = imagecreatetruecolor($map_width,$map_height);
  199. imagesavealpha($dest, true);
  200. // Create Background Image
  201. $overlayImage = imagecreatefrompng($overlay_image);
  202. // Merge Background Image
  203. imagecopy($dest, $overlayImage, 0, 0, 0, 0, imagesx($overlayImage), imagesy($overlayImage));
  204. imagedestroy($overlayImage);
  205. // Merge Roomba Lines
  206. imagecopy($dest, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
  207.  
  208. if ($overlay_walls) {
  209. // Create Walls Image
  210. $overlayWalls = imagecreatefrompng($walls_image);
  211. // Merge Walls Image
  212. imagecopy($dest, $overlayWalls, 0, 0, 0, 0, imagesx($overlayWalls), imagesy($overlayWalls));
  213. imagedestroy($overlayWalls);
  214. }
  215.  
  216. $string = "";
  217.  
  218. if($lastline == "Finished") {
  219. $finished=true;
  220. $status="Finished";
  221.  
  222. $ch = curl_init();
  223. curl_setopt($ch, CURLOPT_URL, $ha_rest980);
  224. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  225. $headers = [
  226. 'Authorization: Bearer '.$ha_token,
  227. 'Content-Type: application/json'
  228. ];
  229. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  230. $server_output = curl_exec ($ch);
  231. curl_close ($ch);
  232. $data = json_decode($server_output);
  233. $battery_level = $data->attributes->batPct;
  234. $string.=$ha_text_delimiter." Battery: ".$battery_level."%";
  235.  
  236. }
  237. else if($lastline == "Stuck"){
  238. $finished=false;
  239. $status="Stuck";
  240. }
  241. else {
  242. $finished=false;
  243. $status="Running";
  244. }
  245.  
  246. if ($render_status_text) {
  247. date_default_timezone_set($ha_timezone);
  248. $dt = date('H:i:s Y-m-d', $date);
  249. $txt = " Started: ".$dt.$ha_text_delimiter." Status: ".$status.$string;
  250. $white = imagecolorallocate($dest, 255, 255, 255);
  251. $font = "./monaco.ttf";
  252. imagettftext($dest, 10, 0, 5, 15, $white, $font, $txt);
  253. }
  254.  
  255. header("Content-Type: image/png");
  256. imagepng($dest);
  257. if(isset($_GET['last'])) {
  258. imagepng($dest, "latest".$file_append.".png");
  259. imagepng($dest, $date.$file_append.".png");
  260. }
  261. imagedestroy($dest);
  262. imagedestroy($robot);
  263. imagedestroy($robot_stuck);
  264. imagedestroy($overlayImage);
  265. exit;
  266.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement