Class: Escalator::PlcDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/escalator/plc_device.rb

Constant Summary collapse

NUMBER_TYPE_DEC =
0
NUMBER_TYPE_DEC_HEX =
1
NUMBER_TYPE_HEX =
2
ESC_SUFFIXES =
%w(X Y M - C T L SC CC TC D - CS TS H SD)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a, b = nil) ⇒ PlcDevice

Returns a new instance of PlcDevice.



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
# File 'lib/escalator/plc_device.rb', line 53

def initialize a, b = nil
  @suffix = nil
  @value = 0
  case a
  when Fixnum
    @suffix = ESC_SUFFIXES[a]
    @number = b
  when String
    if b
      @suffix = a.upcase
      @number = b
    else
      /([A-Z]+)?(\d+)/i =~ a
      @suffix = ($1 || "").upcase
      case number_type
      when NUMBER_TYPE_DEC_HEX
        n = $2.to_i
        @number = (n / 100) * 16 + (n % 100)
      when NUMBER_TYPE_HEX
        @number = $2.to_i(16)
      else
        @number = $2.to_i
      end
    end
  end
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



28
29
30
# File 'lib/escalator/plc_device.rb', line 28

def number
  @number
end

#suffixObject (readonly)

Returns the value of attribute suffix.



28
29
30
# File 'lib/escalator/plc_device.rb', line 28

def suffix
  @suffix
end

#valueObject Also known as: word

Returns the value of attribute value.



29
30
31
# File 'lib/escalator/plc_device.rb', line 29

def value
  @value
end

Class Method Details

.program_area_deviceObject



47
48
49
# File 'lib/escalator/plc_device.rb', line 47

def program_area_device
  @program_area_device ||= new "PRG0"
end

.status_from_plc_deviceObject



43
44
45
# File 'lib/escalator/plc_device.rb', line 43

def status_from_plc_device
  @status_from_plc_device ||= new "SD1"
end

.status_to_plc_deviceObject



39
40
41
# File 'lib/escalator/plc_device.rb', line 39

def status_to_plc_device
  @status_to_plc_device ||= new "SD0"
end

Instance Method Details

#+(value) ⇒ Object



110
111
112
# File 'lib/escalator/plc_device.rb', line 110

def + value
  self.class.new self.suffix, self.number + value
end

#-(value) ⇒ Object



114
115
116
# File 'lib/escalator/plc_device.rb', line 114

def - value
  self.class.new self.suffix, [self.number - value, 0].max
end

#bit_device?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/escalator/plc_device.rb', line 106

def bit_device?
  SUFFIXES_FOR_BIT.include? @suffix
end

#boolObject



122
123
124
125
126
127
128
129
# File 'lib/escalator/plc_device.rb', line 122

def bool
  case @value
  when Fixnum
    @value != 0
  else
    !!@value
  end
end

#bool=(v) ⇒ Object



130
# File 'lib/escalator/plc_device.rb', line 130

def bool= v; @value = v; end

#device_codeObject



134
135
136
# File 'lib/escalator/plc_device.rb', line 134

def device_code
  ESC_SUFFIXES.index @suffix
end

#input?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/escalator/plc_device.rb', line 118

def input?
  suffixes_for_input.include? @suffix
end

#nameObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/escalator/plc_device.rb', line 80

def name
  case number_type
  when NUMBER_TYPE_DEC
    "#{@suffix}#{@number}"
  when NUMBER_TYPE_DEC_HEX
    a = [@number / 16, @number % 16]
    ns = begin
      s = a.last.to_s.rjust(2, "0")
      s = a.first.to_s + s unless a.first == 0
      s
    end
    "#{@suffix}#{ns}"
  when NUMBER_TYPE_HEX
    ns = @number.to_s(16)
    ns = "0" + ns unless /^[0-9]/ =~ ns
    "#{@suffix}#{ns}"
  else
    nil
  end
end

#next_deviceObject



101
102
103
104
# File 'lib/escalator/plc_device.rb', line 101

def next_device
  d = self.class.new @suffix, @number + 1
  d
end

#resetObject



138
139
140
# File 'lib/escalator/plc_device.rb', line 138

def reset
  @value = 0
end