Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions drivers/leviton/acquisuite.cr
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,26 @@ class Leviton::Acquisuite < PlaceOS::Driver
define_setting(:device_list, @device_list)
return {HTTP::Status::NOT_ACCEPTABLE.to_i, {} of String => String, ""}
end
csv = CSV.new(log_file, headers: true)
return if log_contents.nil?
csv = CSV.new(log_contents, headers: true)
# NOTE: This csv.next structure assumes that there will be a header row we don't need
# if this is not the case we should add logic to check for a header
while csv.next
reading = {
time: Time.parse(csv[0].gsub("'", "").strip, "%Y-%m-%d %H:%M:%S", Time::Location::UTC).to_unix,
data: [] of NamedTuple(reading: (String | Float64), name: String, units: String),
}.as(NamedTuple(time: Int64, data: Array(NamedTuple(reading: (String | Float64), name: String, units: String))))
data = [] of NamedTuple(time: Int64, reading: String, name: String, units: String)
@config_list[form_data["MODBUSDEVICE"]].each_with_index do |conf, i|
next if @config_list[form_data["MODBUSDEVICE"]][i]["NAME"] == "-\r"
# Disregard the first 4 columns of the csv
csv_index = i + 4
reading[:data].push({
reading: csv[csv_index],
name: @config_list[form_data["MODBUSDEVICE"]][i]["NAME"].as(String),
units: @config_list[form_data["MODBUSDEVICE"]][i]["UNITS"].as(String),
})
time = Time.parse(csv[0].gsub("'", "").strip, "%Y-%m-%d %H:%M:%S", Time::Location::UTC).to_unix
reading = {
"time": time,
"reading": csv[csv_index],
"name": @config_list[form_data["MODBUSDEVICE"]][i]["NAME"].as(String),
"units": @config_list[form_data["MODBUSDEVICE"]][i]["UNITS"].as(String),
}
data << reading
end
self["mb-%03d" % modbus_index] = reading.dup
self["mb-%03d" % modbus_index] = { value: data }
end
{HTTP::Status::OK.to_i, {} of String => String, ""}
end
Expand All @@ -115,7 +116,7 @@ class Leviton::Acquisuite < PlaceOS::Driver
define_setting(:device_list, @device_list)

# Now update our config list with the new config
store_config(form_data["MODBUSDEVICE"], config_file)
store_config(form_data["MODBUSDEVICE"], config_contents) unless config_contents.nil?
{HTTP::Status::OK.to_i, {} of String => String, ""}
end

Expand All @@ -126,12 +127,13 @@ class Leviton::Acquisuite < PlaceOS::Driver
# If the file is gzipped then unzip it
file_name = file_object.filename
if file_name && file_name[-3..-1] == ".gz"
Compress::Gzip::Reader.open(IO::Memory.new(file_contents)) do |gzip|
file_unzipped = Compress::Gzip::Reader.open(IO::Memory.new(file_contents)) do |gzip|
gzip.gets_to_end
end
else
file_unzipped = file_contents
end

{file_contents, file_contents}
{file_object, file_unzipped}
end

def device_list
Expand Down
3 changes: 3 additions & 0 deletions drivers/leviton/acquisuite_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ DriverSpecs.mock_driver "Leviton::Acquisuite" do
body = body.gsub("\n", "\r\n")
body = body.gsub("fileplaceholder", dev_log)
resp = exec(:receive_webhook, "POST", headers, Base64.encode(body)).get
# TODO:: Should really parse the JSON and make sure it is the below
# status[test_devices[0]].should be_a(Hash(String, Array(Hash(String, String)) | Int32))
status[test_devices[0]].should be_a(JSON::Any)
end

# Some of these fields may not be present in every request but
Expand Down