-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathukhasnet_ruby_curse.rb
159 lines (137 loc) · 4.68 KB
/
ukhasnet_ruby_curse.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Ruby based parser for uploading to UKHASnet
# Reads either a serial port
#
# Need to install the serialport ruby gem (you might need sudo):
# > gem install serialport
#
# Also need to change the serial port (port_str) to your device
# as well as the gateway node ID
#
# Uses inbuilt curses lib and wrapper for GUI
require 'serialport'
require "uri"
require "net/http"
require "curses"
include Curses
#SETTINGS THAT NEED TO BE CHANGED
gateway_ID = 'AB'
port_str="/dev/tty.SLAB_USBtoUART"
class String
def numeric?
return true if self =~ /^\d+$/
true if Float(self) rescue false
end
end
number = 0
init_screen
table_pos = 10
setpos(0,0)
addstr("UKHASnet Ruby Gateway")
setpos(7,0)
addstr("Boot Time: #{Time.now}")
setpos(1,0)
addstr("***********************************************")
setpos(9,0)
addstr("Data from packets received by gateway")
setpos(table_pos,0)
addstr("Nodes |")
setpos(table_pos,10)
addstr("RSSI |")
setpos(table_pos,20)
addstr("Seconds|")
setpos(table_pos,30)
addstr("Packets|")
setpos(table_pos,40)
addstr("% Rx |")
refresh
uri = URI.parse('http://ukhas.net/api/upload')
req = Net::HTTP::Post.new('http://ukhas.net/api/upload')
complete_id = [gateway_ID]
rssi_array = [0]
time_array = [0]
packet_array = [0]
baud_rate=115200
data_bits=8
stop_bits=1
parity=SerialPort::NONE
sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
#sp.read_timeout = 10000
#just read forever
while true do
setpos(1,0)
addstr("***#{Time.now}")
sp_char = sp.readline
if sp_char
# puts sp_char
if sp_char[0..1] == 'rx'
packet = sp_char[4..-1] #remove the first 4 characters 'r' 'x' ':' ' '
#sp.flush_input
#check packet is real (ensure first position of packet is a number
if packet[0].numeric?
sections = packet.chomp.split('|')
rx_rssi = sections.last
packet_data = sections.first
#Extract IDs
parts = packet_data.chop.split('[')
path = parts.last
id = path.split(',')
#Curses section
setpos(1,0)
addstr("***#{Time.now}")
number = number + 1
setpos(2,0)
clrtoeol
addstr("Total Rx: #{number}")
setpos(3,0)
clrtoeol
addstr("Packet: #{packet_data}")
setpos(4,0)
clrtoeol
addstr("RSSI: #{rx_rssi}")
setpos(5,0)
clrtoeol
addstr("Path: #{id}")
#Compare arrays and remove duplicates
complete_id = complete_id | id
result = complete_id.index(id.last)
rssi_array[result] = rx_rssi.to_i
time_array[result] = Time.now
if packet_array[result].inspect.to_i > 0
packet_array[result] +=1
else
packet_array[result] = 1
end
complete_id.each_index do |index|
setpos(index+table_pos + 1,0)
clrtoeol
addstr("#{complete_id[index].inspect}")
setpos(index+table_pos + 1,10)
addstr("#{rssi_array[index].inspect}")
setpos(index+table_pos + 1,20)
if index == 0
addstr("-")
else
addstr("#{Time.now.to_i - time_array[index].to_i}")
end
setpos(index+table_pos + 1,30)
addstr("#{packet_array[index]}")
end
refresh
#Send to UKHASnet
req = Net::HTTP::Post.new(uri)
req.set_form_data({
'origin' => gateway_ID,
'data' => packet_data,
'rssi' => rx_rssi
})
#res = Net::HTTP.new(uri.host, uri.port).start { |http| http.request(req) }
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
# puts res #this is the debug from uploading
sleep 1
end
end
end
end