Created
February 3, 2015 14:14
-
-
Save Envek/e4d25c8057693d0e5b70 to your computer and use it in GitHub Desktop.
Temporary script to get GLONASS/GPS data from Skuapso PSQL module: https://github.com/skuapso/psql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "https://rubygems.org" | |
gem "pg" | |
gem "activesupport" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
url: http://127.0.0.1:3000/tracker_points | |
db: | |
host: 192.168.1.1 # Your database hostname or IP there | |
user: skuapso | |
password: skuapso | |
dbname: skuapso |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'pg' | |
require 'yaml' | |
require 'json' | |
require 'uri' | |
require 'date' | |
require 'net/http' | |
require 'active_support/core_ext/hash' | |
default_settings = { | |
url: 'http://localhost:3000/tracker_points', | |
db: { | |
user: 'skuapso', | |
dbname: 'skuapso', | |
}, | |
} | |
settings = YAML.load_file(File.join(__dir__, 'settings.yml')) rescue default_settings | |
settings = settings.with_indifferent_access | |
uri = URI.parse(settings[:url]) | |
smp = Net::HTTP.new(uri.host, uri.port) | |
headers = { 'Content-Type' => 'application/json', 'Accept' => 'application/json' } | |
conn = PG.connect(settings[:db]) | |
begin | |
puts 'Start listening…' | |
conn.async_exec "LISTEN packets" | |
while true do | |
conn.wait_for_notify do |channel, pid, payload| | |
puts "#{channel}: #{payload}" | |
conn.exec_params( "SELECT * FROM data.packets WHERE id = $1::bigint", [payload]) do |result| | |
eventtime, data = result.first.values_at('eventtime', 'data') | |
body = JSON.parse(data).merge(eventtime: DateTime.parse(eventtime)).to_json | |
begin | |
smp.post(uri.path, body, headers) | |
rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT, Net::OpenTimeout, Net::ReadTimeout => error | |
STDERR.puts error | |
end | |
end | |
end | |
end | |
ensure | |
puts 'Stop listening…' | |
conn.async_exec "UNLISTEN *" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION data.notify_packet() RETURNS TRIGGER AS $$ | |
BEGIN | |
PERFORM triggers.notify('packets', NEW.id::varchar); | |
RETURN NEW; | |
END $$ LANGUAGE plpgsql; | |
CREATE TRIGGER "notify about packets" | |
AFTER INSERT | |
ON data.packets | |
FOR EACH ROW | |
EXECUTE PROCEDURE data.notify_packet(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment