cloud_connect-decoder 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ff9b56d2ec31387c0bc18b61e1e0ddb79806e1a5
4
+ data.tar.gz: 4b746d37e375598a1a0e2e971a2344b1db4c9b9a
5
+ SHA512:
6
+ metadata.gz: 273557f5ba00190ca8fb6f28b0d0d1327a30e338a42fddcc57deebd4c6c7b81b77196a4dca9de8f2ee47b62016a4ee06d49248dc2cabc56ca6619916d1f23ccd
7
+ data.tar.gz: ebf2e12c605b7e0477ed795c0450917f734591231be1bab41c4b39319bbd5df2d6771e3574604049da7839688c2628678c0d790f7038bba193a56a36c53e3cb2
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # CloudConnect::Decoder
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cloud_connect/decoder`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'cloud_connect-decoder'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cloud_connect-decoder
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cloud_connect-decoder.
36
+
@@ -0,0 +1,5 @@
1
+ module CloudConnect
2
+ module Decoder
3
+ VERSION = "0.2.0"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ require "cloud_connect/decoder/version"
2
+ require "cloud_connect/track"
3
+ require "cloud_connect/message"
4
+ require "cloud_connect/field"
5
+ require "json"
6
+ require "base64"
7
+ require "time"
8
+
9
+ module CloudConnect
10
+ module Decoder extend self
11
+ def decode(raw_notification_str)
12
+ data = JSON.parse raw_notification_str
13
+ tracks,messages = [[],[]]
14
+ data.each do |d|
15
+ case d['meta']['event']
16
+ when 'track'
17
+ tracks << CloudConnect::Track.new(d['payload'])
18
+ when 'message'
19
+ messages << CloudConnect::Message.new(d['payload'])
20
+ end
21
+ end
22
+ [tracks, messages]
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,27 @@
1
+ module CloudConnect
2
+ class Field
3
+ attr_accessor :name, :b64_value
4
+
5
+ def initialize(name, b64_value)
6
+ @name = name
7
+ @b64_value = b64_value
8
+ @decoded = Base64.decode64(@b64_value)
9
+ end
10
+
11
+ def to_i
12
+ @decoded.unpack('N').first
13
+ end
14
+
15
+ def to_s
16
+ @decoded
17
+ end
18
+
19
+ def to_f
20
+ @decoded.to_f
21
+ end
22
+
23
+ def to_b
24
+ @decoded.unpack('C').first == 1
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,45 @@
1
+ module CloudConnect
2
+ class Message
3
+ attr_accessor :asset,
4
+ :id,
5
+ :id_str,
6
+ :parent_id,
7
+ :parent_id_str,
8
+ :thread_id,
9
+ :thread_id_str,
10
+ :channel,
11
+ :type,
12
+ :sender,
13
+ :recipient,
14
+ :length,
15
+ :b64_payload,
16
+ :recorded_at,
17
+ :created_at,
18
+ :url
19
+
20
+ def initialize(payload)
21
+ @asset = payload['asset']
22
+ @id = payload['id']
23
+ @id_str = payload['id_str']
24
+ @parent_id = payload['parent_id']
25
+ @parent_id_str = payload['parent_id_str']
26
+ @thread_id = payload['thread_id']
27
+ @thread_id_str = payload['thread_id_str']
28
+ @channel = payload['channel']
29
+ @type = payload['type']
30
+ @sender = payload['sender']
31
+ @recipient = payload['recipient']
32
+ @length = payload['length']
33
+ @b64_payload = payload['b64_payload']
34
+ @recorded_at = Time.parse(payload['recorded_at']) if payload['recorded_at']
35
+ @received_at = Time.parse(payload['received_at']) if payload['received_at']
36
+ @created_at = Time.parse(payload['created_at']) if payload['created_at']
37
+ @url = payload['url']
38
+ end
39
+
40
+ def payload
41
+ @payload ||= Base64.decode(@b64_payload)
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,115 @@
1
+ module CloudConnect
2
+ class Track
3
+ attr_accessor :asset,
4
+ :id,
5
+ :id_str,
6
+ :recorded_at,
7
+ :recorded_at_ms,
8
+ :received_at,
9
+ :loc,
10
+ :connection_id,
11
+ :index,
12
+ :next_index,
13
+ :fields
14
+
15
+ def initialize(payload)
16
+ @asset = payload['asset']
17
+ @id = payload['id']
18
+ @id_str = payload['id_str']
19
+ @recorded_at = Time.parse(payload['recorded_at'])
20
+ @recorded_at_ms = Time.parse(payload['recorded_at_ms'])
21
+ @received_at = Time.parse(payload['received_at'])
22
+ @loc = payload['loc']
23
+ @connection_id = payload['connection_id']
24
+ @index = payload['index']
25
+ @next_index = (@index || 0) + payload['fields'].count
26
+ @fields = payload['fields'].collect { |k,v| [k, CloudConnect::Field.new(k, v['b64_value'])] }.to_h
27
+ end
28
+
29
+ def speed
30
+ @speed ||= ((@fields['GPS_SPEED'].try(:to_i) || 0) * 1.852 / 1000.0).round(2)
31
+ end
32
+
33
+ def total_kms
34
+ @total_kms ||= @fields['ODO_FULL'].try(:to_i) || 0
35
+ end
36
+
37
+ def driver_id
38
+ @driver_id ||= @fields['DRIVER_ID'].try(:to_s)
39
+ end
40
+
41
+ def ignition
42
+ @ignition ||= @fields['DIO_IGNITION'].try(:to_b) || false
43
+ end
44
+
45
+ def valid_fix
46
+ @valid_fix ||= @fields['GPRMC_VALID'].try(:to_s) == 'A'
47
+ end
48
+
49
+ def acceleration
50
+ @acceleration ||= \
51
+ case @fields['BEHAVE_ID'].try(:to_i)
52
+ when 11
53
+ :braking
54
+ when 12
55
+ :acceleration
56
+ when 13
57
+ :left_turn
58
+ when 14
59
+ :right_turn
60
+ end
61
+ end
62
+
63
+ def crash_level
64
+ if @fields.has_key? 'MDI_CRASH_DETECTED'
65
+ severity,status = @fields['MDI_CRASH_DETECTED'].to_s.split(":")
66
+ status == '2' ? severity.to_i : false
67
+ end
68
+ end
69
+
70
+ def battery_discharge
71
+ @battery_discharge ||= @fields['BATT'].try(:to_b)
72
+ end
73
+
74
+ def battery_voltage
75
+ @battery_voltage ||= @fields['BATT_VOLT'].try(:to_i)
76
+ end
77
+
78
+ def power_voltage
79
+ @power_voltage ||= @fields['MDI_EXT_BATT_VOLTAGE'].try(:to_f)
80
+ end
81
+
82
+ def direction
83
+ @direction ||= (@fields['GPS_DIR'].try(:to_i) || 0) / 100.0
84
+ end
85
+
86
+ def panic_button
87
+ @panic_button ||= @fields['MDI_PANIC_STATE'].try(:to_b)
88
+ end
89
+
90
+ def inputs
91
+ @inputs ||= @fields['DIO_IN_TOR'].try(:to_i)
92
+ end
93
+
94
+ def satellite_fixes
95
+ @satellite_fixes ||= @fields['GPS_FIXED_SAT_NUM'].try(:to_i)
96
+ end
97
+
98
+ def gprs_header
99
+ @gprs_header ||= @fields['GPRS_HEADER'].try(:to_s)
100
+ end
101
+
102
+ def rssi
103
+ @rssi ||= @fields['RSSI'].try(:to_i)
104
+ end
105
+
106
+ def movement
107
+ @movement ||= @fields['MVT_STATE'].try(:to_b)
108
+ end
109
+
110
+ def max_speed
111
+ @max_speed ||= @fields['MDI_MAX_SPEED_JOURNEY'].try(:to_i)
112
+ end
113
+
114
+ end
115
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloud_connect-decoder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Tyler Sampson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Utility to decode Cloud Connect notifications. Provides helper models
56
+ email:
57
+ - git@tylersampson.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README.md
63
+ - lib/cloud_connect/decoder.rb
64
+ - lib/cloud_connect/decoder/version.rb
65
+ - lib/cloud_connect/field.rb
66
+ - lib/cloud_connect/message.rb
67
+ - lib/cloud_connect/track.rb
68
+ homepage: https://github.com/tylersampson/cloud_connect-decoder
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.5.1
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Utility to decode Cloud Connect notifications
92
+ test_files: []