Class: OpenHAB::Core::Items::ImageItem

Inherits:
GenericItem show all
Defined in:
lib/openhab/core/items/image_item.rb

Overview

An ImageItem holds the binary image data as its state.

Examples:

Update from a base 64 encode image string

Image.update("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=")

Update from image bytes and mime type

Image.update_from_bytes(File.binread(File.join(Dir.tmpdir,'1x1.png')), mime_type: 'image/png')

Update from URL

Image.update_from_url('https://raw.githubusercontent.com/boc-tothefuture/openhab-jruby/main/features/assets/1x1.png')

Update from File

Image.update_from_file('/tmp/1x1.png')

Log image data

logger.info("Mime type: #{Image.state.mime_type}")
logger.info("Number of bytes: #{Image.state.bytes.length}")

Constant Summary

Constants included from Semantics

Semantics::Equipment, Semantics::Location, Semantics::Point, Semantics::Property, Semantics::Tag

Constants included from Persistence

Persistence::HistoricState

Instance Attribute Summary collapse

Attributes inherited from GenericItem

#category, #formatted_state, #label, #name, #raw_state, #tags

Attributes included from Semantics

#equipment, #equipment_type, #location, #location_type, #point_type, #property_type, #semantic_type

Attributes included from Item

#accepted_command_types, #accepted_data_types, #all_groups, #channel, #channel_uid, #channel_uids, #channels, #groups, #links, #metadata, #name, #provider, #thing, #things

Instance Method Summary collapse

Methods inherited from GenericItem

#command, #modify, #null?, #refresh, #state?, #time_series=, #undef?, #update

Methods included from Semantics

add, #equipment?, #location?, lookup, #point?, #points, remove, #semantic?, tags

Methods included from Item

#call_item?, #color_item?, #contact_item?, #date_time_item?, #dimmer_item?, #group_item?, #image_item?, #inspect, #link, #location_item?, #member_of?, #number_item?, #player_item?, #rollershutter_item?, #string_item?, #switch_item?, #tagged?, #to_s, #unlink

Methods included from DSL::Items::TimedCommand

#command

Methods included from Persistence

#all_states_between, #all_states_since, #all_states_until, #average_between, #average_since, #average_until, #changed_between?, #changed_since?, #changed_until?, #count_between, #count_since, #count_state_changes_between, #count_state_changes_since, #count_state_changes_until, #count_until, #delta_between, #delta_since, #delta_until, #deviation_between, #deviation_since, #deviation_until, #evolution_rate, #evolution_rate_between, #evolution_rate_since, #evolution_rate_until, #historic_state, #last_change, #last_update, #maximum_between, #maximum_since, #maximum_until, #median_between, #median_since, #median_until, #minimum_between, #minimum_since, #minimum_until, #next_change, #next_state, #next_update, #persist, #persisted_state, #previous_state, #remove_all_states_between, #remove_all_states_since, #remove_all_states_until, #sum_between, #sum_since, #sum_until, #updated_between?, #updated_since?, #updated_until?, #variance_between, #variance_since, #variance_until

Methods included from DSL::Items::Ensure::Ensurable

#ensure

Instance Attribute Details

#stateRawType? (readonly)

Returns:



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
# File 'lib/openhab/core/items/image_item.rb', line 37

class ImageItem < GenericItem
  #
  # Update image from file
  #
  # @param [String] file location
  # @param [String] mime_type of image
  #
  #
  def update_from_file(file, mime_type: nil)
    file_data = File.binread(file)
    mime_type ||= Marcel::MimeType.for(Pathname.new(file)) || Marcel::MimeType.for(file_data)
    update_from_bytes(file_data, mime_type: mime_type)
  end

  #
  # Update image from image at URL
  #
  # @param [String] uri location of image
  #
  #
  def update_from_url(uri)
    logger.trace { "Downloading image from #{uri}" }
    response = Net::HTTP.get_response(URI(uri))
    mime_type = response["content-type"]
    bytes = response.body
    mime_type ||= detect_mime_from_bytes(bytes: bytes)
    update_from_bytes(bytes, mime_type: mime_type)
  end

  #
  # Update image from image bytes
  #
  # @param [String] mime_type of image
  # @param [Object] bytes image data
  #
  #
  def update_from_bytes(bytes, mime_type: nil)
    mime_type ||= detect_mime_from_bytes(bytes: bytes)
    base_64_image = encode_image(mime_type: mime_type, bytes: bytes)
    update(base_64_image)
  end

  private

  #
  # Encode image information in the format required by openHAB
  #
  # @param [String] mime_type for image
  # @param [Object] bytes image data
  #
  # @return [String] openHAB image format with image data Base64 encoded
  #
  def encode_image(mime_type:, bytes:)
    "data:#{mime_type};base64,#{Base64.strict_encode64(bytes)}"
  end

  #
  # Detect the mime type based on bytes
  #
  # @param [Array] bytes representing image data
  #
  # @return [String] mime type if it can be detected, nil otherwise
  #
  def detect_mime_from_bytes(bytes:)
    logger.trace("Detecting mime type from file image contents")
    Marcel::MimeType.for(bytes)
  end
end

Instance Method Details

#update_from_bytes(bytes, mime_type: nil) ⇒ Object

Update image from image bytes

Parameters:

  • mime_type (String) (defaults to: nil)

    of image

  • bytes (Object)

    image data



73
74
75
76
77
# File 'lib/openhab/core/items/image_item.rb', line 73

def update_from_bytes(bytes, mime_type: nil)
  mime_type ||= detect_mime_from_bytes(bytes: bytes)
  base_64_image = encode_image(mime_type: mime_type, bytes: bytes)
  update(base_64_image)
end

#update_from_file(file, mime_type: nil) ⇒ Object

Update image from file

Parameters:

  • file (String)

    location

  • mime_type (String) (defaults to: nil)

    of image



45
46
47
48
49
# File 'lib/openhab/core/items/image_item.rb', line 45

def update_from_file(file, mime_type: nil)
  file_data = File.binread(file)
  mime_type ||= Marcel::MimeType.for(Pathname.new(file)) || Marcel::MimeType.for(file_data)
  update_from_bytes(file_data, mime_type: mime_type)
end

#update_from_url(uri) ⇒ Object

Update image from image at URL

Parameters:

  • uri (String)

    location of image



57
58
59
60
61
62
63
64
# File 'lib/openhab/core/items/image_item.rb', line 57

def update_from_url(uri)
  logger.trace { "Downloading image from #{uri}" }
  response = Net::HTTP.get_response(URI(uri))
  mime_type = response["content-type"]
  bytes = response.body
  mime_type ||= detect_mime_from_bytes(bytes: bytes)
  update_from_bytes(bytes, mime_type: mime_type)
end