def addXbeeDevice(_xbm,_dserial): default_key='012345' # 0. Retrieve network key network_key='0ABCDE' # Try receiving up to 5 frames for ok response print "Entering addXbeeDevice" # 0. First try if Coor can already talk to remote device on Network KY (existing device) #if not checkCommandResponse(_xbm,'dapply',_dserial,'SL',''): # Cant talk to device the way it is, need to start normal setup # 1. Change Coordinator to default KY if not checkCommandResponse(_xbm,'coor','','KY',default_key): _message = "Failed to change coordinator to default KY" print _message return _message print "Done Coordinator KY default" # 1b. Confirm if Coordinator can talk to EndDevice now if not checkCommandResponse(_xbm,'dapply',_dserial,'SL',''): _message = "Failed to talk to end device with default KY" print _message # Revert Coordinator back to Network key if not checkCommandResponse(_xbm,'coor','','KY',network_key): _message = "Failed to revert coordinator to network KY. Please restart Pi!" print _message return _message return _message print "Done checking talking in default KY" # 2. Change EndDevice to network KY if not checkCommandResponse(_xbm,'dapply',_dserial,'KY',network_key): _message = "Failed to change end device to network KY" print _message # Revert Coordinator back to Network key if not checkCommandResponse(_xbm,'coor','','KY',network_key): _message = "Failed to revert coordinator to network KY. Please restart Pi!" print _message return _message return _message print "Done changing Device KY network" # 3. Change Coordinator to network KY if not checkCommandResponse(_xbm,'coor','','KY',network_key): _message = "Failed to change coordinator to network KY. Please restart Pi!" print _message return _message print "Done changing Coordinator KY network" # 3.b Confirm if coordinator can talk to end device using network KY if not checkCommandResponse(_xbm,'dapply',_dserial,'SL',''): _message = "Failed to talk to end device with network KY" print _message return _message print "Done talking to device in network KY" #else: # Device is already on Network KY! Proceed with step 4 # print "Device is already on network KY" # 4. Lock EndDevice to Coordinator if not checkCommandResponse(_xbm,'dapply',_dserial,'A1','04'): _message = "Failed to lock end device with coordinator" print _message return _message print "Done locking device to network KY" # 5. Write changes to EndDevice if not checkCommandResponse(_xbm,'dapply',_dserial,'WR',''): _message = "Failed to write to end device" print _message return _message print "Done writing lock" # Retrieving device type based on NI field OK = False for i in range(1,10): _xbm.sendRemoteHexApply(_dserial,'NI') dtype = _xbm.waitReadFrame('NI') if dtype: OK = True break # Wait for 2 sec before retry time.sleep(2) # If still havent got ok resposne, abort! if not OK: _message = "Failed to write to end device" print _message return _message if dtype == 'DS': dtype = 1 elif dtype == 'PS': dtype = 0 else: _message = "Unknown device type %s" % dtype print _message return _message # Store device type to database ('0013A20040A57AE9') -> 0x0013A20040A57AE9 db.changeDeviceType(int(_dserial,16),dtype) print "Done checking NI %s and storing device type to database" % dtype # Set sleep mode if it's a DoorSensor if dtype == 1: # 6. Set EndDevice SLEEP mode to 1 WITHOUT APPLYING CHANGE if not checkCommandResponse(_xbm,'dnotapply',_dserial,'SM','01'): _message = "Failed to configure sleep mode for end device" print _message return _message print "Done sleep mode" # 7. Write changes to EndDevice if not checkCommandResponse(_xbm,'dnotapply',_dserial,'WR',''): _message = "Failed to write to end device" print _message return _message print "Done sleep write" # 8. Apply changes to enable SLEEP MODE if not checkCommandResponse(_xbm,'dapply',_dserial,'AC',''): _message = "Failed to apply changes to end device" print _message return _message print "Done apply changes" # Everything went through! Return okay _message = "New device is added successfully" return _message