Exemple #1
0
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
	//char cS[100];
	char * pBuffer = (char *)malloc(message->payloadlen + 1);
	char * pHash = (char *)malloc(20);
	char * pEnoch = (char *)malloc(20);
	
	//strncpy_s(cS, (char *)message->payload, min(message->payloadlen, sizeof(cS)));
	strncpy(pBuffer, (char *)message->payload, message->payloadlen);
	//cS[message->payloadlen] = 0x0;
	*(pBuffer+message->payloadlen) = 0x0;

	CWnd *tA = theApp.GetMainWnd();
	
	// Adjust counter
	CTT3Dlg *tA2 = (CTT3Dlg *)tA;
	//check if alert must play
	if (tA2->iAlert > 0) {
		// find substring match
		int i = 0;
		char * pAnswer;
		while (i < tA2->iAlert) {
			if (!strcmp(tA2->ALERTME[i][0], "1")) {
				if (strstr(pBuffer, tA2->ALERTME[i][1])) {
					Beep(atoi(tA2->ALERTME[i][2]), atoi(tA2->ALERTME[i][3]));
					// run exe
					char * cmdline = (char *)malloc(2000);
					PROCESS_INFORMATION processInformation;
					STARTUPINFO startupInfo;
					memset(&processInformation, 0, sizeof(processInformation));
					memset(&startupInfo, 0, sizeof(startupInfo));
					startupInfo.cb = sizeof(startupInfo);

					strcpy(cmdline, tA2->ALERTME[i][4]);
					if (strlen(cmdline) > 0) {
						strcat(cmdline, " ");
						strcat(cmdline, topicName);
						strcat(cmdline, ",");
						strcat(cmdline, pBuffer);
						if (!CreateProcess(NULL, cmdline, NULL, NULL, false, 0, NULL, NULL, &startupInfo, &processInformation)) {
							char * pErrMsg = (char *)malloc(500);
							strcpy(pErrMsg, "Unknown application ");
							strcat(pErrMsg, tA2->ALERTME[i][4]);
							MessageBox(NULL, pErrMsg, "Alert Error", MB_OK);
							free(pErrMsg);
						}
					}
				}
			}
			i++;
		}
	}
	CListCtrl *pListctrl = (CListCtrl *)tA->GetDlgItem(IDC_LIST3);
	CButton *pButton1 = (CButton*) tA->GetDlgItem(IDC_CHECK4);

	tA->SetDlgItemInt(IDC_EDIT14, ++tA2->iReceived,FALSE);
	tA->SetDlgItemInt(IDC_EDIT18, tA2->iReceived - tA2->iReceivedRemoved, FALSE);

	// Check maximum messages allowed in listbox
	
	if (pButton1->GetCheck() == BST_CHECKED) {
		int iMaxAllowed  = tA->GetDlgItemInt(IDC_EDIT13,NULL,FALSE);
		int iCurrentRows = pListctrl->GetItemCount();
		while (iCurrentRows >= iMaxAllowed) {
			// Delete oldest one
			pListctrl->DeleteItem(iCurrentRows - 1);
			tA2->iReceivedRemoved++;
			iCurrentRows--;
		}
		tA->SetDlgItemInt(IDC_EDIT15, tA2->iReceivedRemoved,FALSE);
		tA->SetDlgItemInt(IDC_EDIT18, tA2->iReceived - tA2->iReceivedRemoved,FALSE);
	}

	LVITEM lvi;
	
	lvi.mask = LVIF_TEXT;
	lvi.iItem = 0;
	
	lvi.iSubItem = 0;
	_itoa(iHash++, pHash, 10);
	lvi.pszText = pHash;
	pListctrl->InsertItem(&lvi);
	
	lvi.iSubItem = 1;
	time_t timer = time(NULL);
	lvi.pszText = ctime(&timer);
	pListctrl->SetItem(&lvi);

	lvi.iSubItem = 2;
	lvi.pszText = topicName;
	pListctrl->SetItem(&lvi);

	lvi.iSubItem = 3;
	lvi.pszText = pBuffer;
	pListctrl->SetItem(&lvi);

	lvi.iSubItem = 4;
	itoa(timer, pEnoch, 10);
	lvi.pszText = pEnoch;
	pListctrl->SetItem(&lvi);

	pListctrl->SetColumnWidth(0, LVSCW_AUTOSIZE);
	pListctrl->SetColumnWidth(1, LVSCW_AUTOSIZE);
	pListctrl->SetColumnWidth(3, LVSCW_AUTOSIZE);

	// Set Column width for topic
	int iTW = pListctrl->GetStringWidth(topicName);
	if (iTW > tA2->iTopicWidth && iTW < 400) {
		tA2->iTopicWidth = iTW;
		pListctrl->SetColumnWidth(2, iTW+15);
	}

	//Beep on receive if checkbox selected
	pButton1 = (CButton*) tA->GetDlgItem(IDC_CHECK2);
	if (pButton1->GetCheck() == BST_CHECKED) {
		Beep(300, 200);
	} 

	// See if topic is in subscriptions list
	CListBox *pListbox = (CListBox *)tA->GetDlgItem(IDC_LIST1);
	if (pListbox->FindStringExact(0, topicName) == LB_ERR) {
		pListbox->InsertString(0, topicName);
		tA->SetDlgItemText(IDC_EDIT11, "Consider saving setup");
	}
	
	free(pHash); free(pEnoch); free(pBuffer);
	MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
	
    return 1;
}