Skip to content

Commit

Permalink
Fix owntracks params
Browse files Browse the repository at this point in the history
  • Loading branch information
Freika committed Aug 15, 2024
1 parent 3b5a271 commit 5c16cc6
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 31 deletions.
16 changes: 6 additions & 10 deletions app/services/own_tracks/export_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ def call
user_id:
)

Point.create(
latitude: point_data[:latitude],
longitude: point_data[:longitude],
timestamp: point_data[:timestamp],
raw_data: point_data[:raw_data],
topic: point_data[:topic],
tracker_id: point_data[:tracker_id],
import_id: import.id,
user_id:
)
point = Point.new(point_data).tap do |p|
p.user_id = user_id
p.import_id = import.id
end

point.save

points += 1
end
Expand Down
12 changes: 6 additions & 6 deletions app/services/own_tracks/params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ def call
def battery_status
return 'unknown' if params[:bs].nil?

case params[:bs]
when 'u' then 'unplugged'
when 'c' then 'charging'
when 'f' then 'full'
case params[:bs].to_i
when 1 then 'unplugged'
when 2 then 'charging'
when 3 then 'full'
else 'unknown'
end
end

def trigger
return 'unknown' if params[:m].nil?
return 'unknown' if params[:t].nil?

case params[:m]
case params[:t]
when 'p' then 'background_event'
when 'c' then 'circular_region_event'
when 'b' then 'beacon_event'
Expand Down
2 changes: 1 addition & 1 deletion app/services/reverse_geocoding/fetch_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ def call
private

def reverse_geocoded?
point.city.present? && point.country.present? || point.geodata.present?
point.geodata.present?
end
end
53 changes: 53 additions & 0 deletions spec/services/own_tracks/export_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,59 @@
it 'creates points' do
expect { parser }.to change { Point.count }.by(9)
end

it 'correctly writes attributes' do
parser

expect(Point.first.attributes).to include(
'latitude' => 40.7128,
'longitude' => -74.006,
'battery_status' => 'charging',
'battery' => 85,
'ping' => nil,
'altitude' => 41,
'accuracy' => 8,
'vertical_accuracy' => 3,
'velocity' => nil,
'connection' => 'wifi',
'ssid' => 'Home Wifi',
'bssid' => 'b0:f2:8:45:94:33',
'trigger' => 'background_event',
'tracker_id' => 'RO',
'timestamp' => 1_706_965_203,
'inrids' => ['5f1d1b'],
'in_regions' => ['home'],
'topic' => 'owntracks/test/iPhone 12 Pro',
'visit_id' => nil,
'user_id' => user.id,
'country' => nil,
'raw_data' => {
'batt' => 85,
'lon' => -74.006,
'acc' => 8,
'bs' => 2,
'inrids' => ['5f1d1b'],
'BSSID' => 'b0:f2:8:45:94:33',
'SSID' => 'Home Wifi',
'vac' => 3,
'inregions' => ['home'],
'lat' => 40.7128,
'topic' => 'owntracks/test/iPhone 12 Pro',
't' => 'p',
'conn' => 'w',
'm' => 1,
'tst' => 1_706_965_203,
'alt' => 41,
'_type' => 'location',
'tid' => 'RO',
'_http' => true,
'ghash' => 'u33d773',
'isorcv' => '2024-02-03T13:00:03Z',
'isotst' => '2024-02-03T13:00:03Z',
'disptst' => '2024-02-03 13:00:03'
}
)
end
end
end
end
30 changes: 16 additions & 14 deletions spec/services/own_tracks/params_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe OwnTracks::Params do
Expand All @@ -15,7 +17,7 @@
{
latitude: 40.7128,
longitude: -74.006,
battery_status: 'unknown',
battery_status: 'charging',
battery: 85,
ping: nil,
altitude: 41,
Expand All @@ -25,9 +27,9 @@
connection: 'wifi',
ssid: 'Home Wifi',
bssid: 'b0:f2:8:45:94:33',
trigger: 'unknown',
trigger: 'background_event',
tracker_id: 'RO',
timestamp: 1706965203,
timestamp: 1_706_965_203,
inrids: ['5f1d1b'],
in_regions: ['home'],
topic: 'owntracks/test/iPhone 12 Pro',
Expand All @@ -46,7 +48,7 @@
't' => 'p',
'conn' => 'w',
'm' => 1,
'tst' => 1706965203,
'tst' => 1_706_965_203,
'alt' => 41,
'_type' => 'location',
'tid' => 'RO',
Expand All @@ -64,23 +66,23 @@
end

context 'when battery status is unplugged' do
let(:raw_point_params) { super().merge(bs: 'u') }
let(:raw_point_params) { super().merge(bs: 1) }

it 'returns parsed params' do
expect(params[:battery_status]).to eq('unplugged')
end
end

context 'when battery status is charging' do
let(:raw_point_params) { super().merge(bs: 'c') }
let(:raw_point_params) { super().merge(bs: 2) }

it 'returns parsed params' do
expect(params[:battery_status]).to eq('charging')
end
end

context 'when battery status is full' do
let(:raw_point_params) { super().merge(bs: 'f') }
let(:raw_point_params) { super().merge(bs: 3) }

it 'returns parsed params' do
expect(params[:battery_status]).to eq('full')
Expand All @@ -96,47 +98,47 @@
end

context 'when trigger is circular_region_event' do
let(:raw_point_params) { super().merge(m: 'c') }
let(:raw_point_params) { super().merge(t: 'c') }

it 'returns parsed params' do
expect(params[:trigger]).to eq('circular_region_event')
end
end

context 'when trigger is beacon_event' do
let(:raw_point_params) { super().merge(m: 'b') }
let(:raw_point_params) { super().merge(t: 'b') }

it 'returns parsed params' do
expect(params[:trigger]).to eq('beacon_event')
end
end

context 'when trigger is report_location_message_event' do
let(:raw_point_params) { super().merge(m: 'r') }
let(:raw_point_params) { super().merge(t: 'r') }

it 'returns parsed params' do
expect(params[:trigger]).to eq('report_location_message_event')
end
end

context 'when trigger is manual_event' do
let(:raw_point_params) { super().merge(m: 'u') }
let(:raw_point_params) { super().merge(t: 'u') }

it 'returns parsed params' do
expect(params[:trigger]).to eq('manual_event')
end
end

context 'when trigger is timer_based_event' do
let(:raw_point_params) { super().merge(m: 't') }
let(:raw_point_params) { super().merge(t: 't') }

it 'returns parsed params' do
expect(params[:trigger]).to eq('timer_based_event')
end
end

context 'when trigger is settings_monitoring_event' do
let(:raw_point_params) { super().merge(m: 'v') }
let(:raw_point_params) { super().merge(t: 'v') }

it 'returns parsed params' do
expect(params[:trigger]).to eq('settings_monitoring_event')
Expand Down Expand Up @@ -184,7 +186,7 @@
end

context 'when trigger is unknown' do
let(:raw_point_params) { super().merge(m: 'unknown') }
before { raw_point_params[:t] = 'unknown' }

it 'returns parsed params' do
expect(params[:trigger]).to eq('unknown')
Expand Down

0 comments on commit 5c16cc6

Please sign in to comment.