Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@flsafe
Created January 3, 2012 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flsafe/1553417 to your computer and use it in GitHub Desktop.
Save flsafe/1553417 to your computer and use it in GitHub Desktop.
<SentryReport>
<DeviceID>80c96863-1e37-4</DeviceID>
<MessageDateTime>01/07/2012 19:59:20</MessageDateTime>
<ReportInterval>900</ReportInterval>
<Latitude>31.75559907548127</Latitude>
<Longitude>-106.4933067789597</Longitude>
<LocationFixType>GPS</LocationFixType>
<Temperature>95</Temperature>
<TemperatureProbe>99999.0</TemperatureProbe>
<Pressure>0.0</Pressure>
<Humidity>0.0</Humidity>
<Light>0</Light>
<AccelerationX>1</AccelerationX>
<AccelerationY>2</AccelerationY>
<AccelerationZ>99</AccelerationZ>
<BatteryLevel>100</BatteryLevel>
<TetherStatus>1</TetherStatus>
<TetherConnectionStatus>1</TetherConnectionStatus>
<IsICUDevice>0</IsICUDevice>
<PowerSourceConnected>1</PowerSourceConnected>
<CameraStatus>1</CameraStatus>
<LowBatteryIndicator>0</LowBatteryIndicator>
<DoorTamper>0</DoorTamper>
<TransTimestamp>01/07/2012 19:59:20</TransTimestamp>
</SentryReport>
-106.4930386454095,31.75567039469036,0
-106.4933067789597,31.75559907548127,0
-106.4935185215678,31.7555546939303,0
-106.4937444379161,31.75548291296306,0
-106.4936977010694,31.75529720909734,0
-106.4935596464942,31.75511143605309,0
-106.4935513241208,31.75509688691311,0
-106.4933316389852,31.75497188353929,0
-106.4930651746005,31.75477986665801,0
-106.492657129873,31.75461401608551,0
-106.4924839626367,31.75471098513395,0
-106.4925202214965,31.75486701331301,0
-106.4925624323233,31.755022317244,0
-106.4926363254896,31.75522560148913,0
-106.4927326030047,31.75553458510192,0
-106.4927719266734,31.75571061212048,0
-106.492444478373,31.75578855734877,0
-106.4921469409357,31.75586313865317,0
-106.4918507093696,31.75590665802947,0
-106.4917885977581,31.75605269524832,0
-106.4918661454714,31.75623966851196,0
-106.4919166080628,31.7564861773001,0
-106.4919960181155,31.75672409163107,0
-106.4922971265822,31.75667490001769,0
-106.4926360059716,31.75659578843968,0
-106.4929769868461,31.7564907117153,0
-106.4929497853528,31.75631380397714,0
-106.4928325097639,31.7559460019187,0
-106.4928461058279,31.75575899198357,0
require 'builder'
require 'net/http'
require 'optparse'
# Send Data Every 60 Seconds
SEND_RATE = 60
# Set This To The GPS ID. Here I've Used The ID For Device 2.
GPS_DEVICE_ID = '80c96863-1e37-4'
# Specify The Path File To Load Here. I've Specified The File 'trailer.txt'
PATH_FILE = File.join(File.dirname(__FILE__), 'SampleRoute.txt')
class Simulator
@run_sim = true
class << self
attr_accessor :run_sim
end
def initialize(path_files, output)
@path_files = path_files
@output = output
@trailers = []
create_trailers
end
def start
report = SecureOriginsOnAssetReport.new
while Simulator.run_sim
t = next_trailer
t.next_point
@output.put(report.gen(t))
sleep SEND_RATE
end
end
def next_trailer
@trailers.push(@trailers.shift).first
end
private
def create_trailers
@trailers = @path_files.each.map{|f| Trailer.new(f)}
end
end
class Trailer
@counter = 0
class << self
attr_accessor :counter
end
attr_accessor :id
def initialize(path_file)
@id = GPS_DEVICE_ID
@path_file = path_file
load_path
end
def next_point
@points.push(@points.shift).first
end
def lat
@points.first.split(',')[1]
end
def lon
@points.first.split(',')[0]
end
private
def load_path
@points = []
File.open(@path_file) do |f|
@points = f.readlines.map{|l|l.chomp}
end
end
end
class SecureOriginsOnAssetReport
def gen(trailer)
x = Builder::XmlMarkup.new(indent: 2)
x.instruct!
x.SentryReport{
x.DeviceID trailer.id
x.MessageDateTime Time.now.to_s
x.ReportInterval 900
x.Latitude trailer.lat
x.Longitude trailer.lon
x.LocationFixType 'GPS'
x.Temperature 95
x.TemperatureProbe 99999.0
x.Pressure 0.0
x.Humidity 0.0
x.Light 0
x.AccelerationX 1
x.AccelerationY 2
x.AccelerationZ 99
x.BatteryLevel 100
x.TetherStatus 1
x.TetherConnectionStatus 1
x.IsICUDevice 0
x.PowerSourceConnected 1
x.CameraStatus 1
x.LowBatteryIndicator 0
x.DoorTamper 0
x.TransTimestamp Time.now.to_s
}
end
end
class Stdout
def put(str)
puts str
end
end
class HttpOut
def initialize(uri)
@uri = URI(uri)
end
def put(str)
req = Net::HTTP::Post.new(@uri.path)
req.body = str
Net::HTTP.start(@uri.host, @uri.port) do |http|
http.request(req)
end
end
end
class CmdLineApp
def start
parse_opts
select_out
sim = Simulator.new [PATH_FILE], @output
begin
puts "--> Simulator Started"
sim.start
rescue SystemExit, Interrupt
Simulator.run_sim = false
puts "--> Simulator Stopped"
end
end
private
def select_out
if @options[:post]
@output = HttpOut.new(@options[:post])
else
@output = Stdout.new
end
end
def parse_opts
@options = {}
OptionParser.new do |opts|
opts.banner = "Usage: ruby script/simulator.rb [options]"
opts.on("-p", "--post [URI]", "Post to url") do |uri|
@options[:post] = uri
end
end.parse!
end
end
app = CmdLineApp.new
app.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment