]> git.openstreetmap.org Git - rails.git/blobdiff - lib/daemons/gpx_import.rb
gpx insert now works
[rails.git] / lib / daemons / gpx_import.rb
index c3cd9b06b414b56998a3ff9b96df418e7095b93e..1fc60858a3cebb423fa0aca1f2dc5ebd7e7576f4 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/env ruby
 
 #You might want to change this
-ENV["RAILS_ENV"] ||= "production"
+ENV["RAILS_ENV"] ||= "development"
 
 require File.dirname(__FILE__) + "/../../config/environment"
 
@@ -10,10 +10,47 @@ Signal.trap("TERM") do
   $running = false
 end
 
+logger = ActiveRecord::Base.logger
+
 while($running) do
   
-  # Replace this with your code
-  ActiveRecord::Base.logger << "This daemon is still running at #{Time.now}.\n"
-  
-  sleep 10
-end
\ No newline at end of file
+  ActiveRecord::Base.logger.info("GPX Import daemon wake @ #{Time.now}.")
+
+  traces = Trace.find(:all, :conditions => ['inserted = ?', false])
+
+  if traces and traces.length > 0
+    traces.each do |trace|
+      begin
+
+        logger.info("GPX Import importing #{trace.name} from #{trace.user.email}")
+
+        gzipped = `file -b /tmp/#{trace.id}.gpx`.chomp =~/^gzip/
+
+        if gzipped
+          logger.info("gzipped")
+        else
+          logger.info("not gzipped")
+        end
+        gpx = OSM::GPXImporter.new("/tmp/#{trace.id}.gpx")
+
+        gpx.points do |point|
+          tp = Tracepoint.new
+          tp.latitude = point['latitude']
+          tp.latitude = point['longitude']
+          tp.altitude = point['altitude']
+          tp.user_id = trace.user.id
+          tp.gpx_id = trace.id
+          tp.trackid = point['segment']
+        end 
+        trace.size = gpx.actual_points
+        trace.inserted = true
+        trace.save
+        Notifier::deliver_gpx_success(trace, gpx.possible_points)
+      rescue Exception => ex
+        trace.destroy
+        Notifier::deliver_gpx_failure(trace, ex.to_s)
+      end
+    end
+  end
+  sleep 15.minutes
+end