/**
 * \brief Get a list of Starlight Express cameras.
 *
 * \return a vector of strings that uniquely descript devices
 */
std::vector<std::string>	SxCameraLocator::getDevicelist(DeviceName::device_type device) {
	std::vector<std::string>	names;

	// list all devices from the context
	std::vector<DevicePtr>	d = context.devices();
	std::vector<DevicePtr>::const_iterator	i;
	for (i = d.begin(); i != d.end(); i++) {
		// try to open the device. On Mac OS X, opening doesn't fail
		// ever, but on Linux, we may not have permission to open
		// all devices
		try {
			DevicePtr	devptr = *i;
			devptr->open();
			try {
				if (device == DeviceName::Camera) {
					names.push_back("camera:sx/" + sxname(devptr));
				}
				if (device == DeviceName::Guiderport) {
					names.push_back("guiderport:sx/" + sxname(devptr) + "/guiderport");
				}
			} catch (std::runtime_error& x) {
				debug(LOG_DEBUG, DEBUG_LOG, 0, "found a non"
					" SX device: %s", x.what());
			}
			devptr->close();
		} catch (std::exception& x) {
			// log the error, but don't do anything about it
			debug(LOG_ERR, DEBUG_LOG, 0, "cannot work with device");
		}
	}
	debug(LOG_DEBUG, DEBUG_LOG, 0, "found %d SX devices", names.size());
	return names;
}
/**
 * \brief Construct a camera from a camera description
 *
 * \param name		Name of the camera
 * \return Camera with that name
 */
CameraPtr	SxCameraLocator::getCamera0(const DeviceName& name) {
	std::string	sname = name.unitname();
	debug(LOG_DEBUG, DEBUG_LOG, 0, "analyzing '%s'", sname.c_str());
	// parse the name string
	int	busnumber = 0, deviceaddress = 0;
	sxparse(sname, busnumber, deviceaddress);
	debug(LOG_DEBUG, DEBUG_LOG, 0,
		"looking for busnumber=%d, deviceaddress=%d",
		busnumber, deviceaddress);

	// find the device with this bus number and address
	std::vector<DevicePtr>	d = context.devices();
	std::vector<DevicePtr>::const_iterator	i;
	for (i = d.begin(); i != d.end(); i++) {
		DevicePtr	dptr = (*i);
		if ((dptr->getBusNumber() == busnumber) &&
			(dptr->getDeviceAddress() == deviceaddress)) {
			debug(LOG_DEBUG, DEBUG_LOG, 0, "found matching device");
			dptr->open();
			return CameraPtr(new SxCamera(dptr));
		}
	}
	throw SxError("cannot create a camera from a name");
}