void Dialog::onPreviewButtonClicked() { if (!m_ui->textEdit->toPlainText().isEmpty()) { m_ui->textEdit->clear(); } m_ui->textEdit->setText(prepareMessage()); }
bool SimpleMessageStyle::appendContent(QWidget *AWidget, const QString &AHtml, const IMessageStyleContentOptions &AOptions) { StyleViewer *view = qobject_cast<StyleViewer *>(AWidget); if (view) { WidgetStatus &wstatus = FWidgetStatus[AWidget]; bool scrollAtEnd = !AOptions.noScroll && view->verticalScrollBar()->sliderPosition()==view->verticalScrollBar()->maximum(); QTextCursor cursor(view->document()); int maxMessages = Options::node(OPV_MESSAGES_MAXMESSAGESINWINDOW).value().toInt(); if (maxMessages>0 && wstatus.content.count()>maxMessages+10) { int scrollMax = view->verticalScrollBar()->maximum(); int removeSize = 0; while(wstatus.content.count() > maxMessages) removeSize += wstatus.content.takeFirst().size; cursor.setPosition(wstatus.contentStartPosition); cursor.setPosition(wstatus.contentStartPosition+removeSize,QTextCursor::KeepAnchor); cursor.removeSelectedText(); if (!scrollAtEnd) { int newScrollPos = qMax(view->verticalScrollBar()->sliderPosition()-(scrollMax-view->verticalScrollBar()->maximum()),0); view->verticalScrollBar()->setSliderPosition(newScrollPos); } } cursor.movePosition(QTextCursor::End); bool sameSender = isSameSender(AWidget,AOptions); QString html = makeContentTemplate(AOptions,sameSender); fillContentKeywords(html,AOptions,sameSender); html.replace("%message%",prepareMessage(AHtml,AOptions)); ContentItem content; int startPos = cursor.position(); cursor.insertHtml(html); content.size = cursor.position()-startPos; if (scrollAtEnd) view->verticalScrollBar()->setSliderPosition(view->verticalScrollBar()->maximum()); wstatus.lastKind = AOptions.kind; wstatus.lastId = AOptions.senderId; wstatus.lastTime = AOptions.time; wstatus.content.append(content); emit contentAppended(AWidget,AHtml,AOptions); return true; } else { REPORT_ERROR("Failed to simple style append content: Invalid view"); } return false; }
void CMessageBrokerController::unsubscribeFrom(std::string property) { DBG_MSG(("CMessageBrokerController::unsubscribeFrom()\n")); Json::Value root; Json::Value params; prepareMessage(root); root["method"] = "MB.unsubscribeFrom"; params["propertyName"] = property; root["params"] = params; sendJsonMessage(root); }
void CMessageBrokerController::unregisterController() { DBG_MSG(("CMessageBrokerController::unregisterController()\n")); Json::Value root; Json::Value params; prepareMessage(root); root["method"] = "MB.unregisterComponent"; params["componentName"] = mControllersName; root["params"] = params; sendJsonMessage(root); }
// A message is to be sent void lmcMessaging::sendMessage(MessageType type, QString* lpszUserId, XmlMessage* pMessage) { QString data = QString::null; XmlMessage message; switch(type) { case MT_Group: data = pMessage->data(XN_GROUP); updateUser(type, *lpszUserId, data); break; case MT_Status: case MT_UserName: case MT_Note: case MT_PublicMessage: for(int index = 0; index < userList.count(); index++) prepareMessage(type, msgId, false, &userList[index].id, pMessage); msgId++; break; case MT_GroupMessage: if(lpszUserId) prepareMessage(type, msgId, false, lpszUserId, pMessage); else { for(int index = 0; index < userList.count(); index++) prepareMessage(type, msgId, false, &userList[index].id, pMessage); } msgId++; break; case MT_Avatar: // if user id is specified send to that user alone, else send to all if(lpszUserId) { if(pMessage->data(XN_FILEOP) == FileOpNames[FO_Request]) pMessage->addData(XN_FILEID, getUuid() ); prepareMessage(type, msgId, false, lpszUserId, pMessage); } else { for(int index = 0; index < userList.count(); index++) { message = pMessage->clone(); message.addData( XN_FILEID, getUuid() ); prepareMessage(type, msgId, false, &userList[index].id, &message); } } msgId++; break; case MT_Version: sendWebMessage(type, pMessage); break; default: prepareMessage(type, msgId, false, lpszUserId, pMessage); msgId++; break; } }
// A message is to be sent void lmcMessaging::sendMessage(MessageType type, QString* lpszUserId, XmlMessage* pMessage) { QString data = QString::null; XmlMessage message; switch(type) { case MT_Group: data = pMessage->data(XN_GROUP); updateUser(type, *lpszUserId, data); break; case MT_Status: case MT_UserName: case MT_Note: case MT_PublicMessage: for(int index = 0; index < userList.count(); index++) prepareMessage(type, msgId, false, &userList[index].id, pMessage); msgId++; break; case MT_GroupMessage: if(lpszUserId) prepareMessage(type, msgId, false, lpszUserId, pMessage); else { for(int index = 0; index < userList.count(); index++) prepareMessage(type, msgId, false, &userList[index].id, pMessage); } msgId++; break; case MT_Avatar: // if user id is specified send to that user alone, else send to all if(lpszUserId) { prepareMessage(type, msgId, false, lpszUserId, pMessage); } else { message = pMessage->clone(); emit messageReceived(MT_Avatar, &localUser->id, &message); for(int index = 0; index < userList.count(); index++) { message = pMessage->clone(); prepareMessage(type, msgId, false, &userList[index].id, &message); } } msgId++; break; case MT_Version: sendWebMessage(type, pMessage); break; default: prepareMessage(type, msgId, false, lpszUserId, pMessage); msgId++; break; } }
void lmcMessaging::resendMessage(MessageType type, qint64 msgId, QString* lpszUserId, XmlMessage* pMessage) { if(lpszUserId && !getUser(lpszUserId)) return; prepareMessage(type, msgId, true, lpszUserId, pMessage); }
void GreenHouseMiddleLayer::analyze() { float temperatureAverage = 0; float airHumidityAverage = 0; float lightAverage = 0; Message newMessage; DateTime dateTime; clock.createDateTime(dateTime); newMessage.dateTime = dateTime; //add time to message prepareMessage(newMessage, CommonValues::highLayerAddress); //we have some data to analyze if (isTemperatureReadyToAnalyze) { Serial.println(F("in isTemperatureReadyToAnalyze")); //calculate average temperatureAverage = doAverage(temperatureData); //check thresholds newMessage.action = handleThresholds(temperatureAverage, CommonValues::temperatureThresholdMin, CommonValues::temperatureThresholdMax, CommonValues::heatPin,CommonValues::fanPin); //todo assign action newMessage.data = temperatureAverage; newMessage.messageType = CommonValues::dataType; newMessage.sensorType = CommonValues::temperatureType; if (!(sendMessage(newMessage))) { //TODO handle if message fails } //clear the array after done isTemperatureReadyToAnalyze = false; temperatureData.clear(); } //we have some data to analyze if (isHumidityReadyToAnalyze) { Serial.println(F("in isHumidityReadyToAnalyze")); //calculate average airHumidityAverage = doAverage(humidityData); //check thresholds handleThresholds(airHumidityAverage, CommonValues::airHumidityThresholdMin, CommonValues::airHumidityThresholdMax, CommonValues::steamPin, CommonValues::ventPin); newMessage.action = STEAMER; newMessage.data = airHumidityAverage; newMessage.messageType = CommonValues::dataType; newMessage.sensorType = CommonValues::humidityType; if (!(sendMessage(newMessage))) { // Serial.println(F("message failed, adding to unsent")); // unsentImportantMessages.add(newMessage); } //clear the array after done isHumidityReadyToAnalyze = false; humidityData.clear(); Serial.print(F("after isHumidityReadyToAnalyze clear size is :")); Serial.println(humidityData.size()); } if (isLightReadyToAnalyze) { Serial.println(F("in isLightReadyToAnalyze")); //calculate average lightAverage = doAverage(lightData); //TODO decide what to do with light thresholds newMessage.action = handleThresholds(lightAverage, CommonValues::lightThresholdMin, CommonValues::lightThresholdMax, CommonValues::lampPin, CommonValues::lampPin); newMessage.data = lightAverage; newMessage.messageType = CommonValues::dataType; newMessage.sensorType = CommonValues::lightType; if (!(sendMessage(newMessage))) { //TODO } //clear the array after done isLightReadyToAnalyze = false; lightData.clear(); } //TODO reduce duplicate code all above //no need to do average on consumption data }
void GreenHouseMiddleLayer::decodeMessage(Message& msg) { Serial.print(msg.source); if (CommonValues::emptyMessage == msg.messageType) { //do nothing the message is empty return; } DateTime dateTime; clock.createDateTime(dateTime); msg.dateTime = dateTime; //add time to message if (msg.dest != CommonValues::middleLayerAddress) { //check if the message is for me sendMessage(msg); // if so, pass it on return; } //the message is from higer layer if (msg.source >= CommonValues::highLayerMinAddress && msg.source < CommonValues::highLayerMaxAddress) { switch (msg.messageType) { case CommonValues::policyChange: switch (msg.sensorType) { case CommonValues::soilHumidityType: //if it's soil Humidity policy changes, send it to the lower layers for (int i = 0; i<lowersIds.size() ; ++i) { if (lowersIds.get(i) != CommonValues::lowerLayerConsumptionAdress) { prepareMessage(msg, lowersIds.get(i)); if (!sendMessage(msg)) { //unsentImportantMessages.add(msg); } } } break; case CommonValues::temperatureType: CommonValues::temperatureThresholdMin = msg.data; CommonValues::temperatureThresholdMax = msg.additionalData; break; case CommonValues::humidityType: CommonValues::airHumidityThresholdMin = msg.data; CommonValues::airHumidityThresholdMax = msg.additionalData; break; case CommonValues::lightType: CommonValues::lightThresholdMin = msg.data; CommonValues::lightThresholdMax = msg.additionalData; break; } case CommonValues::loopTimeChange: break; case CommonValues::myAddressChange: break; case CommonValues::yourAddressChange: break; case CommonValues::ACTION_TYPE: switch (msg.action) { case PUMP1: prepareMessage(msg, lowersIds.get(0)); if (!sendMessage(msg)) { //unsentImportantMessages.add(msg); } break; case PUMP2: prepareMessage(msg, lowersIds.get(1)); if (!sendMessage(msg)) { //unsentImportantMessages.add(msg); } break; case FAN: msg.messageType = CommonValues::dataType; msg.action = actuate(CommonValues::fanPin,msg.data); prepareMessage(msg, CommonValues::highLayerAddress); if (!sendMessage(msg)) { //unsentImportantMessages.add(msg); } break; case LIGHT: msg.messageType = CommonValues::dataType; msg.action = actuate(CommonValues::lampPin, msg.data); prepareMessage(msg, CommonValues::highLayerAddress); if (!sendMessage(msg)) { //unsentImportantMessages.add(msg); } break; case HEATER: msg.messageType = CommonValues::dataType; msg.action = actuate(CommonValues::heatPin,msg.data); prepareMessage(msg, CommonValues::highLayerAddress); if (!sendMessage(msg)) { //unsentImportantMessages.add(msg); } break; case VENT: msg.messageType = CommonValues::dataType; msg.action = actuate(CommonValues::ventPin,msg.data); prepareMessage(msg, CommonValues::highLayerAddress); if (!sendMessage(msg)) { //unsentImportantMessages.add(msg); } break; case STEAMER: msg.messageType = CommonValues::dataType; msg.action = actuate(CommonValues::steamPin,msg.data); prepareMessage(msg, CommonValues::highLayerAddress); if (!sendMessage(msg)) { //unsentImportantMessages.add(msg); } break; case NONE: //TODO break; default: //TODO break; } break; case CommonValues::arduinoMalfunction: break; } } //if the meesgae came from bottom layer //the layer should act/send up the hirarchy if needed else if (msg.source >= CommonValues::lowerLayerMinAddress && msg.source < CommonValues::lowerLayerMaxAddress) { switch (msg.messageType) { //first check if it's an emergency message case CommonValues::emergencyType: actuate(CommonValues::fanPin, true); actuate(CommonValues::ventPin, true); prepareMessage(msg, CommonValues::highLayerAddress); // prepare to send to high if (!sendMessage(msg)) { //unsentImportantMessages.add(msg); } break; case CommonValues::dataType: switch (msg.sensorType) { case CommonValues::soilHumidityType: //if it's soil Humidity data, send it to the high layer msg.additionalData = getMyAddress(); // the higher needs to know which pot it is. msg.dest = CommonValues::highLayerAddress; //check if the message is important (action performed),and add to unsentImportantMessages if not sent if (NONE != msg.action) { if (!sendMessage(msg)) { // unsentImportantMessages.add(msg); } } else sendMessage(msg); break; case CommonValues::currentType: //if it's current consumption data, send it to the high layer prepareMessage(msg, CommonValues::highLayerAddress); // prepare to send to high sendMessage(msg); break; case CommonValues::waterType: //if it's water consumption data, send it to the high layer prepareMessage(msg, CommonValues::highLayerAddress); sendMessage(msg); break; case CommonValues::temperatureType: isTemperatureReadyToAnalyze = ((updateDataAndCheckIfFull(temperatureData,msg,plantsLowerLayers)) && isTimeConsistency(temperatureData, CommonValues::minutesInInterval)); break; case CommonValues::humidityType: isHumidityReadyToAnalyze = ((updateDataAndCheckIfFull(humidityData,msg, plantsLowerLayers)) && isTimeConsistency(humidityData, CommonValues::minutesInInterval)); break; case CommonValues::lightType: isLightReadyToAnalyze = ((updateDataAndCheckIfFull(lightData,msg, plantsLowerLayers)) && isTimeConsistency(lightData, CommonValues::minutesInInterval)); break; } break; //TODO think maybe handle with more messageTypes //TODO action type //TODO what happens if we are not erady to analyze?? }//end of switch (msg.messageType) analyze(); }//end of if (msg.source >= CommonValues::lowerLayerMinAddress && msg.source < CommonValues::lowerLayerMaxAddress) }
int main(int argc, char *argv[]) { int sockfd, portno, n, count = 0; struct sockaddr_in serv_addr; struct hostent *server; FILE *cfile; char config[50]; strcpy(config, argv[1]); char gipaddress[50], gport[5]; char *gtoken, *stoken; int starttime, endtime, value; char config_read[250], input_read[250]; char buffer[256]; char *reg, *currvalue; /* Reading configuration file*/ cfile = fopen (config,"r"); if (cfile == NULL) { perror("ERROR opening config file "); exit(1); } while ( fgets (config_read , 100 , cfile) != NULL ) { if(count == 0) { /* Tokening the stream on :*/ gtoken = strtok(config_read, ":"); strcpy(gipaddress, gtoken); gtoken = strtok(NULL, ":"); strcpy(gport, gtoken); gtoken = strtok(NULL, ":"); count++; } else if(count ==1) { stoken = strtok(config_read, ":"); strcpy(item_type, stoken); stoken = strtok(NULL, ":"); strcpy(sipaddress, stoken); stoken = strtok(NULL, ":"); strcpy(sport, stoken); stoken = strtok(NULL, ":"); strcpy(sarea, stoken); stoken = strtok(NULL, ":"); } } //printf("Gateway IP: %s\n", gipaddress); //printf("Gateway port %s\n", gport); //printf("Item type %s\n", item_type); //printf("Device ip %s\n", sipaddress); //printf("Device port %s\n", sport); //printf("Area %s\n", sarea); fclose(cfile); /* Prepare register request*/ reg = prepareMessage(0, NULL); //printf("Prepare message: %s\n",reg); portno = atoi(gport); /* Create a socket point */ sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("ERROR opening socket"); exit(1); } server = gethostbyname(gipaddress); if (server == NULL) { fprintf(stderr,"ERROR, no such host\n"); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); /* Now connect to the server */ if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { perror("ERROR connecting"); exit(1); } /* Now ask for a message from the user, this message * will be read by server */ bzero(buffer,256); //fgets(buffer,255,stdin); printf("Message: %s\n", reg); strcpy(buffer, reg); /* Send message to the server */ n = write(sockfd, buffer, strlen(buffer)); if (n < 0) { perror("ERROR writing to socket"); exit(1); } while(1) { /* Now read server response */ bzero(buffer,256); //printf("Buffer:\n%s", &buffer); n = read(sockfd, buffer, 255); //printf("N:%d\n",n); if (n < 0) { perror("ERROR reading from socket"); exit(1); } else if(n > 0) { // If gateway sends on if(strcmp(buffer, "Type:Switch;Action:on") == 0){ currState = 1; printf("Device switched ON\n"); } else if (strcmp(buffer, "Type:Switch;Action:off") == 0){ currState = 0; printf("Device switched OFF\n"); } printf("Got message: %s\n",buffer); } //sleep(1); } return 0; }
int main(int argc, char *argv[]) { int sockfd, portno, n, count = 0; struct sockaddr_in serv_addr; struct hostent *server; char config[50]; char gipaddress[50], gport[5], *gtoken; //char item_type[15], sipaddress[50], sport[5], sarea[5]; //char input[50] = "./SensorInputFile.txt"; char *itoken, *stoken; int starttime, endtime, value; char buffer[256]; char *reg, *currvalue; //char inputfilename[] = "./"; char input[50]; strcpy(config, argv[1]); strcpy(input, argv[2]); //strcpy(input, strcat(inputfilename, input)); /* Reading configuration file*/ cfile = fopen (config,"r"); if (cfile == NULL) { perror("ERROR opening config file "); exit(1); } while ( fgets (config_read , 100 , cfile) != NULL ) { //printf("Config file: %s\n", config_read); if(count == 0) { /* Tokening the stream on :*/ gtoken = strtok(config_read, ":"); //for(int i=0;i<2;i++){ //while (token != NULL){ //printf("Token1: %s\n", gtoken); strcpy(gipaddress, gtoken); gtoken = strtok(NULL, ":"); //printf("Token2: %s\n", gtoken); strcpy(gport, gtoken); gtoken = strtok(NULL, ":"); count++; } else if(count ==1) { stoken = strtok(config_read, ":"); //printf("Token3: %s\n", stoken); strcpy(item_type, stoken); stoken = strtok(NULL, ":"); //printf("Token4: %s\n", stoken); strcpy(sipaddress, stoken); stoken = strtok(NULL, ":"); //printf("Token5: %s\n", stoken); strcpy(sport, stoken); stoken = strtok(NULL, ":"); //printf("Token6: %s\n", stoken); strcpy(sarea, stoken); stoken = strtok(NULL, ":"); } } //printf("Gateway IP: %s\n", gipaddress); //printf("Gateway port %s\n", gport); //printf("Token3 %s\n", item_type); //printf("Token4 %s\n", sipaddress); //printf("Token5 %s\n", sport); //printf("Token6 %s\n", sarea); fclose(cfile); /* Reading configuration file*/ ifile = fopen (input,"r"); if (ifile == NULL) { perror("ERROR opening config file "); exit(1); } /*while ( fgets (input_read , 100 , ifile) != NULL ) { printf("Input file: %s", input_read); } if (argc <3) { fprintf(stderr,"usage %s hostname port\n", argv[0]); exit(0); } */ //fclose(ifile); /* Prepare register request*/ reg = prepareMessage(0, NULL); //printf("Prepare message: %s\n",reg); portno = atoi(gport); /* Create a socket point */ sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("ERROR opening socket"); exit(1); } server = gethostbyname(gipaddress); if (server == NULL) { fprintf(stderr,"ERROR, no such host\n"); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); /* Now connect to the server */ if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { perror("ERROR connecting"); exit(1); } /* Now ask for a message from the user, this message * will be read by server */ while(1) { //printf("Please enter the message: "); bzero(buffer,256); //fgets(buffer,255,stdin); //printf("Message: %s\n", reg); strcpy(buffer, reg); /* Send message to the server */ if (state == 1) { n = write(sockfd, buffer, strlen(buffer)); } if (n < 0) { perror("ERROR writing to socket"); exit(1); } /* Now read server response */ bzero(buffer,256); n = read(sockfd, buffer, 255); if (n < 0) { perror("ERROR reading from socket"); exit(1); } else { handleServerMessage(buffer); } //printf("printing buffer: %s\n",buffer); sleep(interval); // Prepare Message reg = NULL; char temp[10]; strcpy(temp,fetchValue()); //printf("Temp: %s\n", temp); reg = prepareMessage(2, temp); timeCounter = timeCounter + interval; printf("CurrValue Message: %s\n",reg); } bzero(buffer,256); return 0; }