diff --git a/drivers/leviton/acquisuite.cr b/drivers/leviton/acquisuite.cr index ea518915a82..4f34f357976 100644 --- a/drivers/leviton/acquisuite.cr +++ b/drivers/leviton/acquisuite.cr @@ -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 @@ -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 @@ -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 diff --git a/drivers/leviton/acquisuite_spec.cr b/drivers/leviton/acquisuite_spec.cr index aff4f90fd61..5405f10a4f2 100644 --- a/drivers/leviton/acquisuite_spec.cr +++ b/drivers/leviton/acquisuite_spec.cr @@ -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