more code
Started implementing the PLCBUS class
with definitions, getters and setters for the packet..
The structure is now starting to become apparent
Let me know what you think.. (I'm still learning Ruby, so if you can optimize it, let me know how!)
Regards,
Dan
class PLCBUS
attr_reader :HomeCodes, :UnitCodes, :CommandFunctions
STX=0x02.chr
ETX=0x03.chr
HomeCodes = {
'A'=>0b0000,
'B'=>0b0001,
'C'=>0b0010,
'D'=>0b0011,
'E'=>0b0100,
'F'=>0b0101,
'G'=>0b0110,
'H'=>0b0111,
'I'=>0b1000,
'J'=>0b1001,
'K'=>0b1010,
'L'=>0b1011,
'M'=>0b1100,
'N'=>0b1101,
'O'=>0b1110,
'P'=>0b1111}
UnitCodes = {
'1'=>0b0000,
'2'=>0b0001,
'3'=>0b0010,
'4'=>0b0011,
'5'=>0b0100,
'6'=>0b0101,
'7'=>0b0110,
'8'=>0b0111,
'9'=>0b1000,
'10'=>0b1001,
'11'=>0b1010,
'12'=>0b1011,
'13'=>0b1100,
'14'=>0b1101,
'15'=>0b1110,
'16'=>0b1111}
CommandFunctions = {
0x00=>'ALL UNIT OFF',
0x01=>'ALL LTS ON',
0x02=>'ON #',
0x03=>'OFF #',
0x04=>'DIM *#',
0x05=>'BRIGHT *#',
0x06=>'ALL LIGHT OFF',
0x07=>'ALL USER LTS ON',
0x08=>'ALL USER UNIT OFF',
0x09=>'ALL USER LIGHT OFF',
0x0A=>'BLINK *#',
0x0B=>'FADE STOP #',
0x0C=>'PRESETDIM *#',
0x0D=>'STATUS ON *',
0x0E=>'STATUS OFF',
0x0F=>'STATUS REQ',
0x10=>'(R)MASTER ADDRS SETUP *#',
0x11=>'(T)MASTER ADDRS SETUP *#',
0x12=>'SCENES ADDRS SETUP *',
0x13=>'SCENES ADDRS ERASE',
0x14=>'ALL SCENES ADDRS ERASE *#',
0x15=>'FOR FUTURE*',
0x16=>'FOR FUTURE*',
0x17=>'FOR FUTURE*',
0x18=>'GET SIGNAL STRENGTH #',
0x19=>'GET NOISE STRENGTH #',
0x1A=>'REPORT SIGNAL STRENGTH *',
0x1B=>'REPORT NOISE STRENGTH *',
0x1C=>'GET ALL ID PULSE',
0x1D=>'GET ONLYON ID PULSE',
0x1E=>'REPORT ALL ID PULSE',
0x1F=>'REPORT ONLY ON PULSE'}
def initialize()
@usercode = 0
@homeunit = 0
@command = 0
@data1 = 0
@data2 = 0
end
def command=(value)
@command = value
end
def command
return HomeCodes[@command]
end
def usercode=(value)
@usercode = value
end
def usercode
return @usercode
end
def homeunit=(value)
@homeunit = HomeCode[value[0].chr]
@homeunit += UnitCode[value[1].chr]
end
def homeunit
return @homeunit
end
def data1=(value)
@data1 = value
end
def data1
return @data1
end
def data2=(value)
@data2 = value
end
def data2
return @data2
end
def data
return (@usercode.chr @homeunit.chr + @command.chr + @data1.chr + @data2.chr)
end
def length
return data.length
end
def packet
return STX + length.chr + data + ETX
end
end
#Command Class
class RubyCommand
# Constants:
CommandType=['Command','Event']
Status=['Initializing', 'Sending Command', 'Waiting for Command ACK', 'Waiting for Response',
'Receiving Response', 'Processing Response', 'Sending Response', 'Complete', 'Error']
CommandDirection=['DCEtoGSD', 'GSDtoDCE'] #Direction of Command
DCEtoGSD = 0
GSDtoDCE = 1
def initialize(from, to, priority, cmdtype, command)
#initialize here
@from = from
@to = to
@priority = priority
@direction = CommandDirection[DCEtoGSD]
@command = command
@commandtype = CommandType[0]
@processing = false
@cmdsent = false
@cmdcomplete = false
@devdata = {}
@device = Device.new(to)
@status = Status[0]
end
def device=(deviceid)
@device = Devices(deviceid)
end
def cmdSent
return @cmdsent
end
def cmdComplete
#Boolean: Flag Set when Command has completed.
return @cmdcomplete
end
def dcecommand=(value)
@command = value
end
def dcecommand
return @command
end
def processing
return @processing
end
def gsdCommandOut
return @gsdcommand
end
def devdata(pkparam)
return @devdata[pkparam]
end
def setdevdata(pkparam,value)
@devdata[pkparam]= value
end
def commandtype
return @commandtype
end
def commandtype=(commandtype, default = 'Command')
if commandtype == 'Command'
@commandtype = CommandType[0]
end
if commandtype == 'Event'
@commandtype = CommandType[1]
end
end
end
class RubyCommands
def initialize
@commands = []
end
def add(command)
#@cmd = Command.new(command)
@commands << command
end
def remove
@commands.delete_at(0)
end
def execute
end
def nitems
return @commands.nitems
end
end
class Device
def initialize(devid)
@devdata = {}
@devid = devid
@template = 0
@parent = 0
end
def template
return @template
end
def template=(templateid)
@template = templateid.to_i
end
def parent
return @parentid
end
def parent=(parentid)
@parentid=parentid.to_i
end
def setdevdata(pkparam, value)
@devdata[pkparam] = value.to_s
end
def devdata(pkparam)
return @devdata[pkparam]
end
def state
return @state
end
def configuration
return @configuration
end
def configuration=(value)
@configuration = value
end
def devid
return @devid
end
end
class Devices
def initialize
@devices = {}
device_.childdevices_.each_key{|c|
dev = Device.new(c)
dev.devdata[12] =device_.childdevices_[c].devdata_[12].chomp.lstrip.rstrip
dev.template = device_.childdevices_[c].devtemplid_
dev.parent = device_.childdevices_[c].parent_
add(c,dev)
}
end
def add(device)
@devices[device.devid] = device
end
def remove(devid)
@devices.remove[devid]
end
end
#Test Code Below:
cmd = RubyCommand.new(-1000, 1, 1, 1, 184)
cmd2 = RubyCommand.new(-1000, 1, 1, 1, 192)
cmd.setdevdata(74, '100')
cmd2.setdevdata(74, '200')
cmd2.commandtype = 'Event'
puts cmd.devdata(74)
puts cmd.devdata(100)
cmd.setdevdata(50,'hello')
puts cmd.inspect
puts cmd.dcecommand
puts "Hello"
cmds = RubyCommands.new()
cmds.add(cmd)
puts cmds.inspect
cmds.add(cmd2)
puts cmds.inspect
#Show Queue Length
puts cmds.nitems
#Remove a command from the queue
cmds.remove
puts cmds.inspect
plc = PLCBUS.new
puts plc.HomeCodes.inspect