#### Written by Dan Damron
#### #355 Process Initialize ####
$red = 0x1b.chr + '[31m'
$green = 0x1b.chr + '[32m'
$yellow = 0x1b.chr + '[33m'
$blue = 0x1b.chr + '[34m'
$purple = 0x1b.chr + '[35m'
$aqua = 0x1b.chr + '[36m'
$grey = 0x1b.chr + '[37m'
$redBack = 0x1b.chr + '[41m'
$greenBack = 0x1b.chr + '[42m'
$yellowBack = 0x1b.chr + '[43m'
$blueBack = 0x1b.chr + '[44m'
$purpleBack = 0x1b.chr + '[45m'
$aquaBack = 0x1b.chr + '[46m'
$greyBack = 0x1b.chr + '[47m'
$blackBack = 0x1b.chr + '[48m'
$remoteLinkRecord = ''
#$remoteRecordReady = 0
$configstring = ''
$configparams = []
$childdatabases = {}
$plmdatabase = []
$plmlastrecord = true
$log = ''
$cmdTo = ''
$cmdFrom = ''
$cmdPriority = ''
$cmdType = ''
$cmdID = ''
$cmdParams = {}
$children = {}
$recvbuff = ''
$wAIT = false
$currentCmd = {}
$flagsBroadcastMessage = 128
$flagsDirectMessage = 0
$flagsAckDirectMessage = 32
$flagsNackDirectMessage = 160
$flagsGroupBroadcaseMessage = 192
$flagsGroupCleanupDirectMessage = 64
$flagsGroupAckDirectMessage =96
$flagsGroupNackDirectMessage = 224
$cmdqueue = []
$currentcmd = 0 # to save state of command when receiving more data.
$resetNext = 0 # used in Process IDLE to trap stalled responses.
$savedstate = nil
$dcequeue = []
$X10HouseCodes = {
'6', 'A',
'E', 'B',
'2', 'C',
'A', 'D',
'1', 'E',
'9', 'F',
'5', 'G',
'D', 'H',
'7', 'I',
'F', 'J',
'3', 'K',
'B', 'L',
'0', 'M',
'8', 'N',
'4', 'O',
'C', 'P'}
$X10UnitCodes = {'6', '1', 'E', '2', '2', '3', 'A', '4',
'1', '5', '9', '6', '5', '7', 'D', '8',
'7', '9', 'F', '10', '3', '11', 'B', '12',
'0', '13', '8', '14', '4', '15', 'C', '16'}
$X10CommandCodes = {'6', 'All Lights Off', 'E', 'Status = off',
'2', 'On', 'A', 'Pre-Set Dim',
'1', 'All Lights On', '9', 'Hail Ack',
'5', 'Bright', 'D', 'Status=on',
'7', 'Extended Code','F', 'Status Request',
'3', 'Off', 'B', 'Pre-set Dim',
'0', 'All Units Off', '8', 'Hail Request',
'4', 'Dim', 'C', 'Extended Data(analog)'}
$responseString = '<' + '/' + 'Response>'
############################## TEST Code below #################################
# testing Reading a devices Database
# Common Insteon Device Memory Locations:
# 0x0001 EESize - MSB of size of EEPROM chip in device
# 0x0002 EEVersion - Firmware Revision number
# 0x0020 EEOnLevel - Preset On-Level for lighting control
# 0x0021 EERampRate - Ramp Rate for Lighting Control
# 0x0030 EEX10BaseHouse - Base X10 House Code for device
# 0x0031 EEX10BaseUnit - Base X10 Unit Code for device
#
# ALDB/L
# Starts at the top of external serial eeprom and grows downward.
# most limited-memory INSTEON devices, top of memory is 0x0FFF
# Each Record is 8 bytes long, so the first record starts at
# 0x0FF8, the second record starts at 0x0FF0
#
# ALDB/L Record Format:
# Record Control: 1 byte
# Group: 1 Byte
# ID: 3 Bytes HB MB LB
# Data1: 1 Byte Link-specific data (eg. On-Level)
# Data2: 1 Byte Link specific data (eg. Ramp Rates)
# Data3: 1 Byte Link specific data (normally unused)
#
# Record Control Byte Bitmap:
# 7: 1=Record in use, 0=Record is available
# 6: 1=Controller of ID, 0=Responder of ID
# 5: Product Dependant
# 4: Product Dependant
# 3: Product Dependant
# 2: Product Dependant
# 1: 1=Record has been used before, 0=High water mark (last record)
# 0: Reserved
#PLM cat 03 subcat 05
# param = {'Command' => 'StLnk',
#'Parameter1' => '01', #Link code 0x01=controller
#'Parameter2' => '8F' #All Link Group
#}
#$cmdqueue << param
# param = {'Command' => 'CancelLnk'}
#$cmdqueue << param
#SndIns()
#
#Lets try the Assign to ALL-Link Group ### SUCCESS!!!
#param = {'Command' => 'SndIns',
#'Parameter1' => '0A', #HB
#'Parameter2' => '8F', #MB
#'Parameter3' => '5A', #LB
#'Parameter4' => '0F', #Flags
#'Parameter5' => '01', #Cmd1
#'Parameter6' => '8F' #Cmd2
#}
#$cmdqueue << param
#SndIns()
### Now to add record to the IM using 0x6F Manage All Link Record
#EZBridge command is MngLnk
#This gets NACKED probably because it already exists..
#but Group 8F should not exist...
#put IM in monitor mode..
param = {'Command' => 'SetCfg', 'Parameter1' => '40'}
$cmdqueue << param
param = {'Command' => 'MngLnk',
'Parameter1' => '00', # Control Code
'Parameter2' => 'C2', #All Link Record Flags AKA Record Control Byte
'Parameter3' => '01', #All Link Group
'Parameter4' => '0A', #HB
'Parameter5' => '97', #MB
'Parameter6' => '0B', #LB
'Parameter7' => '01', #Link Data 1 ON-Level
'Parameter8' => '01', #Link Data 2 Ramp Rate
'Parameter9' => '00'} #Link Data 3 not used
$cmdqueue << param
## Test Device: LampLinc ID:0A.97.0B
# Assign Test device to Group
param = {'Command' => 'SndIns',
'Parameter1' => '0A', #HB
'Parameter2' => '97', #MB
'Parameter3' => '0B', #LB
'Parameter4' => '0F', #Flags
'Parameter5' => '01', #Cmd1
'Parameter6' => '01' #Cmd2
}
$cmdqueue << param
############################## Real Code below #################################
#log('MY DEVICE:' + device_.devid_.to_s)
#################################################
#What I need to do and the order I need to do it.
#done read PLM database
#done get child devices
#done read child devices configuration
#done all child devices in the PLM database?
#done1 if not, add their ID to the PLM database (new group numbers?)
# get the status of each (child) device
# compare the Database delta with the value stored in the configuration
# Different?
# read the child database
# update configuration
# check devices links to see if any are new
# new link found, what to do?? send a message?
# possibly create new child device?
# Add new link to PLM
# PING the new device and back to 1
###
# easiest way?.. create hash for PLM records
# create hash for child devices is needed
# (not needed if DBDelta is same)
#get PLM database
getLnk
#get Child devices
log('Finding Children..')
device_.childdevices_.each{|c| $children[device_.childdevices_[c.to_s.to_i].devdata_[12].chomp.lstrip.rstrip] = c.to_s.to_i}
$children.keys.each{|c| log(c + ' = ' + $children[c].to_s)}
log('Getting Child configuration:')
$children.keys.each{|c|
#Get each Childs configuration
getdeviceconfig(c)
#the above getdeviceconfig packs $configparams array
log('Configuration for ' + c + ' = ' + $configparams.inspect.to_s)
# insteonID = c.chomp.split('.')
# param = {'Command' => 'MngLnk',
# 'Parameter1' => '00', # Control Code
# 'Parameter2' => 'C2', #All Link Record Flags AKA Record Control Byte
# 'Parameter3' => '01', #All Link Group
# 'Parameter4' => insteonID[0], #HB
# 'Parameter5' => insteonID[1], #MB
# 'Parameter6' => insteonID[2], #LB
# 'Parameter7' => 'FF', #Link Data 1 ON-Level
# 'Parameter8' => 'FF', #Link Data 2 Ramp Rate
# 'Parameter9' => '00'} #Link Data 3 not used
# $cmdqueue << param
}
## Test Device: LampLinc ID:0A.97.0B
## test remoteGetlnk
#remoteGetLnk('0A.97.0B')
## test remoteGetNext
#remoteGetNext('0A.97.0B')
##children found... get the status of each child...
#$children.keys.each{|c|
# if c.length > 2 #filter out X10 for now
# #pack insteon message here, and send it out!
# # receive message in parsemessage and relay commands back in it.
# insteonID = c.chomp.split('.')
# param = {'Command' => 'SndIns',
# 'Parameter1' => insteonID[0],
# 'Parameter2' => insteonID[1],
# 'Parameter3' => insteonID[2],
# 'Parameter4' => '0F', #flags
# 'Parameter5' => '19', #Status Request
# 'Parameter6' => '00'} #not used.
# $cmdqueue << param
## below gets the device Cat/SubCat/Firmware
# param = {'Command' => 'SndIns',
# 'Parameter1' => insteonID[0],
# 'Parameter2' => insteonID[1],
# 'Parameter3' => insteonID[2],
# 'Parameter4' => '0F', #flags
# 'Parameter5' => '10', #PING
# 'Parameter6' => '00'} #not used.
# $cmdqueue << param
#
# end
# }
SndIns() #Lastly, start executing commands in order.