Exemplo n.º 1
0
bool CRFLink::WriteToHardware(const char *pdata, const unsigned char length)
{
	if (!isOpen())
		return false;

	_tGeneralSwitch *pSwitch = (_tGeneralSwitch*)pdata;
	if (pSwitch->type != pTypeGeneralSwitch)
		return false; //only allowed to control switches

	std::string switchtype = GetGeneralRFLinkFromInt(rfswitches, pSwitch->subtype);
	if (switchtype.empty())
	{
		_log.Log(LOG_ERROR, "RFLink: trying to send unknown switch type: %d", pSwitch->subtype);
		return false;
	}
	std::string switchcmnd = GetGeneralRFLinkFromInt(rfswitchcommands, pSwitch->cmnd);
    
    // check setlevel command
    if (pSwitch->cmnd == gswitch_sSetLevel) {
        // Get device level to set
		float fvalue = (15.0f / 100.0f)*float(pSwitch->level);
		if (fvalue > 15.0f)
			fvalue = 15.0f; //99 is fully on
		int svalue = round(fvalue);        
		//_log.Log(LOG_ERROR, "RFLink: level: %d", svalue);
        char buffer[50] = {0};
		sprintf(buffer, "%d", svalue);
		switchcmnd = buffer;
    }    
    
	if (switchcmnd.empty())
	{
		_log.Log(LOG_ERROR, "RFLink: trying to send unknown switch command: %d", pSwitch->cmnd);
		return false;
	}

	//Build send string
	std::stringstream sstr;
	//10;NewKaku;00c142;1;ON;     => protocol;address;button number;action (ON/OFF/ALLON/ALLOFF/15 -11-15 for dim level)

	sstr << "10;" << switchtype << ";" << std::hex << std::nouppercase << std::setw(6) << std::setfill('0') << pSwitch->id << ";" << std::hex << std::nouppercase << pSwitch->unitcode << ";" << switchcmnd << ";\n";

//#ifdef _DEBUG
	_log.Log(LOG_STATUS, "RFLink Sending: %s", sstr.str().c_str());
//#endif
    m_bTXokay=false; // clear OK flag
	write(sstr.str());
	time_t atime = mytime(NULL);
	time_t btime = mytime(NULL);
    
	// Wait for an OK response from RFLink to make sure the command was executed
	while (m_bTXokay == false) {
        if (btime-atime > 4) {
			_log.Log(LOG_ERROR, "RFLink: TX time out...");
			return false;
        }
        btime = mytime(NULL);
	}
	return true;
}
Exemplo n.º 2
0
bool CRFLinkBase::WriteToHardware(const char *pdata, const unsigned char length)
{
	const _tGeneralSwitch *pSwitch = reinterpret_cast<const _tGeneralSwitch*>(pdata);

	if ((pSwitch->type != pTypeGeneralSwitch) && (pSwitch->type != pTypeLimitlessLights))
		return false; //only allowed to control regular switches and MiLight

	//_log.Log(LOG_ERROR, "RFLink: switch type: %d", pSwitch->subtype);
	std::string switchtype = GetGeneralRFLinkFromInt(rfswitches, pSwitch->subtype);
	if (switchtype.empty())
	{
		_log.Log(LOG_ERROR, "RFLink: trying to send unknown switch type: %d", pSwitch->subtype);
		return false;
	}
	//_log.Log(LOG_ERROR, "RFLink: subtype: %d", pSwitch->subtype);

	if (pSwitch->type == pTypeGeneralSwitch) {
		std::string switchcmnd = GetGeneralRFLinkFromInt(rfswitchcommands, pSwitch->cmnd);

        // check setlevel command
        if (pSwitch->cmnd == gswitch_sSetLevel) {
           // Get device level to set
			float fvalue = (15.0f / 100.0f)*float(pSwitch->level);
			if (fvalue > 15.0f)
				fvalue = 15.0f; //99 is fully on
			int svalue = round(fvalue);        
			//_log.Log(LOG_ERROR, "RFLink: level: %d", svalue);
	        char buffer[50] = {0};
			sprintf(buffer, "%d", svalue);
			switchcmnd = buffer;
	    }    
    
		if (switchcmnd.empty()) {
			_log.Log(LOG_ERROR, "RFLink: trying to send unknown switch command: %d", pSwitch->cmnd);
			return false;
		}

		//Build send string
		std::stringstream sstr;
		//10;NewKaku;00c142;1;ON;     => protocol;address;button number;action (ON/OFF/ALLON/ALLOFF/15 -11-15 for dim level)

		sstr << "10;" << switchtype << ";" << std::hex << std::nouppercase << std::setw(6) << std::setfill('0') << pSwitch->id << ";" << std::hex << std::nouppercase << pSwitch->unitcode << ";" << switchcmnd;
//#ifdef _DEBUG
		_log.Log(LOG_STATUS, "RFLink Sending: %s", sstr.str().c_str());
//#endif
		sstr << "\n";
		m_bTXokay = false; // clear OK flag
		WriteInt(sstr.str());
		time_t atime = mytime(NULL);
		time_t btime = mytime(NULL);
    
		// Wait for an OK response from RFLink to make sure the command was executed
		while (m_bTXokay == false) {
			if (btime-atime > 4) {
				_log.Log(LOG_ERROR, "RFLink: TX time out...");
				return false;
			}
			btime = mytime(NULL);
		}
		return true;
	}
	else {		// RFLink Milight extension
		_tLimitlessLights *pLed = (_tLimitlessLights*)pdata;

		//_log.Log(LOG_ERROR, "RFLink: ledtype: %d", pLed->type);			// type limitlessled
		//_log.Log(LOG_ERROR, "RFLink: subtype: %d", pLed->subtype);		// rgbw/rgb/white?
		//_log.Log(LOG_ERROR, "RFLink: id: %d", pLed->id);				// id
		//_log.Log(LOG_ERROR, "RFLink: unit: %d", pLed->dunit);			// unit 0=All, 1=Group1,2=Group2,3=Group3,4=Group4
		//_log.Log(LOG_ERROR, "RFLink: command: %d", pLed->command);		// command
		//_log.Log(LOG_ERROR, "RFLink: value: %d", pLed->value);			// brightness/color value
		bool bSendOn = false; 

		const int m_LEDType = pLed->type;
		std::string switchtype = GetGeneralRFLinkFromInt(rfswitches, 0x57);
		std::string switchcmnd = GetGeneralRFLinkFromInt(rfswitchcommands, pLed->command);
		unsigned int m_colorbright = 0;

		switch (pLed->command){
		case Limitless_LedOn:
			switchcmnd = "ON";
			break;
		case Limitless_LedOff:
			switchcmnd = "OFF";
			break;
		case Limitless_SetRGBColour:
			{
			m_colorbright = m_colorbright & 0xff;
			m_colorbright = (((unsigned char)pLed->value) << 8) + m_colorbright;
			switchcmnd = "COLOR";
			bSendOn = true;
		    }
			break;
		case Limitless_DiscoSpeedSlower:
			switchcmnd = "DISCO-";
			bSendOn = true;
			break;
		case Limitless_DiscoSpeedFaster:
			switchcmnd = "DISCO+";
			bSendOn = true;
			break;
		case Limitless_DiscoMode:
			switchcmnd = "MODE";
			break;
		case Limitless_SetColorToWhite:
			switchcmnd = "ALLON";
			bSendOn = true;
			break;
		case Limitless_SetBrightnessLevel:
			{
			//brightness (0-100) converted to 0x00-0xff
			int m_brightness = (unsigned char)pLed->value;
			m_brightness = (m_brightness * 255) / 100;
			m_brightness = m_brightness & 0xff;
			m_colorbright = m_colorbright & 0xff00;
			m_colorbright = m_colorbright + (unsigned char)m_brightness;
			switchcmnd = "BRIGHT";
			bSendOn = true;
		    }
			break;

		// need work:
		case Limitless_SetBrightUp:
			switchcmnd = "BRIGHT";
			bSendOn = true;
			break;
		case Limitless_SetBrightDown:
			switchcmnd = "BRIGHT";
			bSendOn = true;
			break;
		case Limitless_DiscoSpeedFasterLong:
			switchcmnd = "DISCO+";
			bSendOn = true;
			break;
		case Limitless_RGBDiscoNext:
			switchcmnd = "DISCO+";
			bSendOn = true;
			break;
		case Limitless_RGBDiscoPrevious:
			switchcmnd = "DISCO-";
			bSendOn = true;
			break;
		default: 
			_log.Log(LOG_ERROR, "RFLink: trying to send unknown led switch command: %d", pLed->command);
			return false;
		}

		// --- Sending first an "ON command" needed
		if (bSendOn == true) {
			std::string tswitchcmnd = "ON";
			//Build send string
			std::stringstream sstr;
			sstr << "10;" << switchtype << ";" << std::hex << std::nouppercase << std::setw(4) << std::setfill('0') << pLed->id << ";" << std::setw(2) << std::setfill('0') << int(pLed->dunit) << ";" << std::hex << std::nouppercase << std::setw(4) << m_colorbright << ";" << tswitchcmnd;
			_log.Log(LOG_STATUS, "RFLink Sending: %s", sstr.str().c_str());
			sstr << "\n";
			m_bTXokay = false; // clear OK flag
			WriteInt(sstr.str());
			time_t atime = mytime(NULL);
			time_t btime = mytime(NULL);

			// Wait for an OK response from RFLink to make sure the command was executed
			while (m_bTXokay == false) {
				if (btime - atime > 4) {
					_log.Log(LOG_ERROR, "RFLink: TX time out...");
					return false;
				}
				btime = mytime(NULL);
			}
		} 
		// ---

		//Build send string
		std::stringstream sstr;
		//10;MiLightv1;1234;01;5566;ON;     => protocol;address;unit number;color&brightness;action (ON/OFF/ALLON/ALLOFF etc)

		sstr << "10;" << switchtype << ";" << std::hex << std::nouppercase << std::setw(4) << std::setfill('0') << pLed->id << ";" << std::setw(2) << std::setfill('0') << int(pLed->dunit) << ";" << std::hex << std::nouppercase << std::setw(4) << m_colorbright << ";" << switchcmnd;
		//#ifdef _DEBUG
		_log.Log(LOG_STATUS, "RFLink Sending: %s", sstr.str().c_str());
		//#endif
		sstr << "\n";
		m_bTXokay = false; // clear OK flag
		WriteInt(sstr.str());
		time_t atime = mytime(NULL);
		time_t btime = mytime(NULL);

		// Wait for an OK response from RFLink to make sure the command was executed
		while (m_bTXokay == false) {
			if (btime - atime > 4) {
				_log.Log(LOG_ERROR, "RFLink: TX time out...");
				return false;
			}
			btime = mytime(NULL);
		}
		return true;
	}
}