/** * Adds a sensor to the appropriate protocol. Checks if this sensor is supported by the protocol * * @return the protocol to which the sensor has been added * @throws NotFoundException if the protocol specified in the sensor config does not exist * @throws ConfigurationException if the sensor config is invalid (no protocol name, or specified * protocol not found) */ AbstractProtocol associateSensor(Sensor sensor) throws ConfigurationException, NotFoundException { AbstractProtocol p = null; String pname = null, ptype = null; try { pname = sensor.getParameter(SMLConstants.PROTOCOL_NAME_ATTRIBUTE_NODE); ptype = sensor.getParameter(SMLConstants.PROTOCOL_TYPE_ATTRIBUTE_NODE); } catch (NotFoundException e) { logger.error("Can not find the protocol name / type to associate the sensor with"); logger.error( "Cant associate sensor " + sensor.getID().toString() + "(Cant find protocol " + pname + ")"); throw new ConfigurationException( "cant find the name of protocol to associate with from sensor config", e); } if ((p = getComponent(new ProtocolID(pname))) != null) { if (ptype.equals(p.getType())) p.associateSensor(sensor); else { logger.error( "Specified protocol type " + ptype + " doesnt match existing protocol name " + pname + " 's type (" + p.getType() + ")"); throw new ConfigurationException( "Specified protocol type " + ptype + " doesnt match existing protocol name " + pname + " 's type (" + p.getType() + ")"); } } else { logger.error("Cant find protocol " + pname + " to associate the sensor with"); throw new NotFoundException("Cant find protocol '" + pname + "'"); } return p; }
/** * Returns the protocol associated with a SensorID (assuming the sensor owner is already * associated with a protocol) * * @throws NotFoundException if the sensor can not be found * @throws SALRunTimeException if the sensoor isnt associated with any protocol, or the protocol * object cant be found (shouldnt happen, but...) */ AbstractProtocol getProtocol(SensorID sid) throws NotFoundException { AbstractProtocol p = null; String pName = null; Sensor s; if ((s = SensorManager.getSensorManager().getComponent(sid)) == null) { // logger.error("Cannot find the any sensor with this sensorID: " + sid.toString()); throw new NotFoundException("No sensor with this sensorID"); } if ((pName = s.getID().getPIDName()) == null) { logger.error("Cannot find the protocolID associated with this sensorID: " + sid.toString()); throw new SALRunTimeException( "we shouldnt be here - sensor not associated with any protocol ??"); } if ((p = getComponent(new ProtocolID(pName))) == null) { logger.error("Cannot find the protocol associated with this sensorID: " + sid.toString()); throw new SALRunTimeException( "We shouldnt be here - cant find the protocol the sensor is associated with"); } return p; }
/** * Unassociate a sensor from the protocol. This method returns only when the sensor is * unassociated, ie if a command is being run while this method is called, it will block until the * command finishes, then unassociated the sensor, thereby preventing it from being used again. * * @throws ConfigurationException if the protocol name cant be found in the sensor config, if the * protocol name doesnt exist or if the sensor isnt associated with the protocol anymore */ void unassociateSensor(Sensor s) throws ConfigurationException { AbstractProtocol p = null; String pname = null; try { pname = s.getParameter(SMLConstants.PROTOCOL_NAME_ATTRIBUTE_NODE); } catch (NotFoundException e) { logger.error("Can not find the protocol name to unassociate the sensor from"); logger.error( "Cant unassociate sensor " + s.getID().toString() + "(Cant find protocol " + pname + ")"); throw new ConfigurationException("cant find protocol name in sensor config", e); } if ((p = getComponent(new ProtocolID(pname))) != null) try { p.unassociateSensor(s.getID()); } catch (NotFoundException e) { throw new ConfigurationException("Sensor not associated with protocol '" + pname + "'", e); } else { logger.error( "Cant unassociate sensor " + s.getID().toString() + "(Cant find protocol " + pname + ")"); throw new ConfigurationException("cant find protocol name '" + pname + "'"); } }