void startCh2() { // setup channel 2
  time_t epoch = now();
  if (ch2fnc == 0) {
    ch2en = 1;
    digitalWrite(sw2, _ON); // nothing fancy for manual mode, just turn switch on
  } else if (ch2fnc == 1) { // duration mode
    ch2en = 1;
    ch2start = epoch; // current time in minutes
    ch2end = ch2start + (ch2off * 60); // off time is ch1off minutes from now
    ch2rest = 0; // not used for duration mode
    digitalWrite(sw2, _ON);
    wsSend("CH2 Duration Start");
    wsSendTime("start %d", ch2start);
    wsSendTime("end %d", ch2end);
  } else if (ch2fnc == 2) { // interval mode
    ch2en = 1;
    ch2start = epoch; // current time in minutes
    ch2end = ch2start + (ch2on * 60); // off time is ch1on minutes from now
    ch2rest = ch2end + (ch2off * 60); // rest time is ch1off minutes after the end
    digitalWrite(sw2, _ON);
    wsSend("CH2 Interval Start");
    wsSendTime("start %d", ch2start);
    wsSendTime("end %d", ch2end);
  }
}
void doSpeedout() {
    sprintf(str,"fanspd=%u", fanSpeed);
    if (useMQTT) mqtt.publish(mqttpub, str);
    wsSend(str);
    sprintf(str,"fandir=%u", fanDirection);
    if (useMQTT) mqtt.publish(mqttpub, str);
    wsSend(str);
}
time_t getNtptime() {
  time_t epoch = 0;

  //get a random server from the pool
  WiFi.hostByName(ntpServerName, timeServerIP);

  sendNTPpacket(timeServerIP); // send an NTP packet to a time server
  // wait to see if a reply is available
  delay(500);

  int cb = udp.parsePacket();
  if (!cb) {
    //Serial.println("no packet!?");
    wsSend("NTP Error");
  }
  else {
    // Serial.print("packet received, length=");
    // Serial.println(cb);
    // We've received a packet, read the data from it
    udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer

    //the timestamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. First, esxtract the two words:

    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
    // combine the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900):
    unsigned long secsSince1900 = highWord << 16 | lowWord;
    // Serial.print("Seconds since Jan 1 1900 = " );
    // Serial.println(secsSince1900);

    // now convert NTP time into everyday time:
    // Serial.print("Unix time = ");
    // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
    const unsigned long seventyYears = 2208988800UL;
    // subtract seventy years:
    epoch = secsSince1900 - seventyYears;

    epoch = epoch - (60 * 60 * ntpOffset); // take off 4 hrs for EDT offset
    sprintf(str, "NTP epoch=%d", epoch);
    wsSend(str);



  }
  return epoch;
}
void i2c_scan() {
  scanI2C = false;
  byte error, address;
  int nDevices;

  if (useMQTT) mqtt.publish(mqttpub, "Scanning I2C Bus...");

  nDevices = 0;
  for(address = 1; address < 127; address++ ) {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0) {
      sprintf(str,"i2c dev %d: %x", nDevices, address);
      wsSend(str);
      if (useMQTT) {
        mqtt.publish(mqttpub, str);
        mqtt.loop();
      }
      delay(10);
      nDevices++;
    }
  }
  if (useMQTT) mqtt.publish(mqttpub, "I2C scan complete.");
}
void stopCh2() { // setup channel 2
  time_t epoch = now();
  if (ch2fnc == 0) digitalWrite(sw2, _OFF); // turn off switch, manual mode
  else if (ch2fnc == 1) { // duration mode
    ch2en = 0; // stop polling
    digitalWrite(sw2, _OFF); // turn off switch
    wsSend("CH2 Duration Complete");
  } else if (ch2fnc == 2) { // interval mode
    if (epoch >= ch2rest) {
      wsSend("CH2 Interval Looping");
      startCh2(); // rest time over, restart interval
      return;
    } // otherwise, rest time...
    wsSend("CH2 Interval Rest");
    digitalWrite(sw2, _OFF); // turn off switch, but keep polling
    ch2end = ch2rest; // we just stopped, now wait for rest period.
    wsSendTime("end %d", ch1end);
  }
}
void doRGBout() {
  if(hasRGB) {
  	memset(str,0,sizeof(str));
  	sprintf(str, "red=%u,green=%u,blue=%u,white=%u", red, green, blue, white);
  	mqtt.publish(mqttpub, str);
  	wsSend(str);
  } else {
	mqtt.publish("RGB support not enabled.", str);
  }
  getRGB=false;
}
void wsSendlabels(uint8_t _num) { // send switch labels only to newly connected websocket client
  // uint8_t _num = _x - 1; // client number is one less
  // if (useMQTT) mqtt.publish(mqttpub, "wsSendlabels");
  // if (_num == 0) return;
  memset(str,0,sizeof(str));
  sprintf(str,"sending labels: sw1=%d %d sw2=%d %d sw3=%d %d sw4=%d %d",sw1,sw1type,sw2,sw2type,sw3,sw3type,sw4,sw4type);
  if (useMQTT) mqtt.publish(mqttpub, str);
  wsSend(str);
  char labelStr[8];
  if (sw1>=0) {
    if (sw1type==0) strcpy(labelStr,"switch\0");
    else if (sw1type==1) strcpy(labelStr,"label\0");
    else if (sw1type==2) strcpy(labelStr,"rgb\0");
    else if (sw1type>=8) strcpy(labelStr,"fan\0");
    sprintf(str,"%s=%s",labelStr, sw1label);
    wsSend(str);
  }
  if (sw2>=0) {
    if (sw2type==0) strcpy(labelStr,"switch\0");
    else if (sw2type==1) strcpy(labelStr,"label\0");
    else if (sw2type==2) strcpy(labelStr,"rgb\0");
    sprintf(str,"%s=%s",labelStr, sw2label);
    wsSend(str);
  }
  if (sw3>=0) {
    if (sw3type==0) strcpy(labelStr,"switch\0");
    else if (sw3type==1) strcpy(labelStr,"label\0");
    else if (sw3type==2) strcpy(labelStr,"rgb\0");
    sprintf(str,"%s=%s",labelStr, sw3label);
    wsSend(str);
  }
  if (sw4>=0) {
    if (sw4type==0) strcpy(labelStr,"switch\0");
    else if (sw4type==1) strcpy(labelStr,"label\0");
    else if (sw4type==2) strcpy(labelStr,"rgb\0");
    sprintf(str,"%s=%s",labelStr, sw4label);
    wsSend(str);
  }
  // i2c_scan();
  newWScon = 0;
}
void wsSwitchstatus() {
  char swChr[7];
  if (newWScon>0) wsSendlabels(newWScon);
  memset(swChr,0,sizeof(swChr));
  if (ch1en>=0) {
    sprintf(swChr,"sw1=%u",ch1en);
    wsSend(swChr);
  }
  if (ch2en>=0) {
    sprintf(swChr,"sw2=%u",ch2en);
    wsSend(swChr);
  }
  wsSend(swChr);
  if (ch3en>=0) {
    sprintf(swChr,"sw3=%u",ch3en);
    wsSend(swChr);
  }
  if (ch4en>=0) {
    sprintf(swChr,"sw4=%u",ch4en);
    wsSend(swChr);
  }

}
void wsData() { // send some websockets data if client is connected
  if (wsConcount<=0) return;

  // if (newWScon>0 && hasRGB) wsSwitchstatus(); // update switch status once for rgb controllers
  //else if (!hasRGB) wsSwitchstatus(); // regular updates for other node types
  wsSwitchstatus();

  if (timeStatus() == timeSet) wsSendTime("time=%d",now()); // send time to ws client

  if (hasRGB) return; // stop here if we're an rgb controller

  if (hasVout) { // send bat/vcc string
    wsSend(voltsChr);
    if (rawadc) wsSend(adcChr);
  }

  if (hasRSSI) wsSend(rssiChr); // send rssi info
  if (hasSpeed) doSpeedout();

  if (hasTout) wsSend(tmpChr); // send temperature

  if (hasIout) { // send readings from ADC
    sprintf(str,"raw0=%d", raw0);
    wsSend(amps0Chr);
    if (rawadc) wsSend(str);
    memset(str,0,sizeof(str));
    sprintf(str,"raw1=%d", raw1);
    wsSend(amps1Chr);
    if (rawadc) wsSend(str);
    memset(str,0,sizeof(str));
    sprintf(str,"raw2=%d", raw2);
    wsSend(voltsChr);
    if (rawadc) wsSend(str);
    memset(str,0,sizeof(str));
  }
}
Beispiel #10
0
int main(int argc, char** argv) {
	if(argc < 2) {
		puts("Usage: parrot <port>");
		return 1;
	}

	int port = atoi(argv[1]);
	if(port <= 0) {
		puts("Invalid port number");

		return 1;
	}

	int sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sockfd < 0) {
		perror("socket");
		return 1;
	}

	int val = 1;
	if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
		perror("setsockopt");

		close(sockfd);
		return 1;
	}

	struct sockaddr_in addr = {0};
	addr.sin_addr.s_addr = INADDR_ANY;
	addr.sin_port = htons(port);

	if(bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
		perror("bind");

		close(sockfd);
		return 1;
	}

	if(listen(sockfd, 10) < 0) {
		perror("listen");

		close(sockfd);
		return 1;
	}

	while(1) {
		struct sockaddr_in addr = {0};
		socklen_t len = sizeof(addr);

		int clientfd = accept(sockfd, (struct sockaddr*)&addr, &len);
		if(clientfd < 0) {
			perror("accept");
			continue;
		}

		struct wsStatus status = {0};
		if(!wsHandshake(clientfd, &status)) {
			fputs("Failed to process websocket handshake", stderr);
			continue;
		}

		printf("WebSocket connection on %s using protocol version %d\n", status.url, status.version);

		struct wsMessage msg = {0};
		while(wsRecv(clientfd, &msg)) {
			if(!wsSend(clientfd, WS_OPCODE_FRAME_TEXT, msg.payload, msg.len)) {
				fputs("Failed to send message", stderr);
			}

			wsMessageFree(&msg);
		}
	}

	close(sockfd);
	return 0;
}
Beispiel #11
0
void wsSendTime(const char* msg, time_t mytime) {
  memset(str,0,sizeof(str));
  sprintf(str, msg, mytime);
  wsSend(str);
}
Beispiel #12
0
ret_ CXMLLoaderActions::LoadProcessBlock(CProgram &Program,
										 const DOMElement *pElement,
										 const CPDUInfo *pPDU)
{
#ifdef _DEBUG_
	if (!pElement)
		return (PARAMETER_NULL | PARAMETER_2);
#endif

	DOMElement *pChild = (DOMElement *)pElement->getFirstChild();

	if (!pChild)
		return XML_LOADER_ERROR;

	auto_xerces_str wsEmpty				("empty");
	auto_xerces_str	wsUnitaryCalculate	("unitary_calculate");
	auto_xerces_str	wsDualityCalculate	("duality_calculate");
	auto_xerces_str wsConfigGroup		("config_group");
	auto_xerces_str	wsIf				("if");
	auto_xerces_str	wsWhile				("while");
	auto_xerces_str wsContinue			("continue");
	auto_xerces_str wsBlock				("block");
	auto_xerces_str wsBreak				("break");
	auto_xerces_str	wsSend				("send");
	auto_xerces_str wsReadFile			("read_file");
	auto_xerces_str wsSaveFile			("save_file");
	auto_xerces_str wsDeleteFile		("delete_file");
	auto_xerces_str wsSplit				("split");
	auto_xerces_str	wsDie				("die");
	auto_xerces_str wsShowWindow		("show_window");
	auto_xerces_str wsWaitMessage		("wait_message");
	auto_xerces_str wsAlert				("alert");
	auto_xerces_str wsEnable			("enable");
	auto_xerces_str wsAddItem			("add_item");
	auto_xerces_str wsProgram			("program");

	while (pChild)
	{
		if (0 == XMLString::compareString(pChild->getNodeName(),
										  wsEmpty))
		{
			if (SUCCESS != LoadEmpty(Program, pChild, pPDU))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsUnitaryCalculate))
		{
			if (SUCCESS != LoadUnitaryCalculate(Program, pChild, pPDU))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsDualityCalculate))
		{
			if (SUCCESS != LoadDualityCalculate(Program, pChild, pPDU))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsConfigGroup))
		{
			if (SUCCESS != LoadConfigGroup(Program, pChild, pPDU))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsIf))
		{
			if (SUCCESS != LoadIf(Program, pChild, pPDU))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsWhile))
		{
			if (SUCCESS != LoadWhile(Program, pChild, pPDU))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsContinue))
		{
			if (SUCCESS != LoadContinue(Program))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsBlock))
		{
			if (SUCCESS != LoadBlock(Program))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsBreak))
		{
			if (SUCCESS != LoadBreak(Program))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsSend))
		{
			if (SUCCESS != LoadSend(Program, pChild, pPDU))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsReadFile))
		{
			if (SUCCESS != LoadReadFile(Program, pChild, pPDU))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsSaveFile))
		{
			if (SUCCESS != LoadSaveFile(Program, pChild, pPDU))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsDeleteFile))
		{
			if (SUCCESS != LoadDeleteFile(Program, pChild, pPDU))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsSplit))
		{
			if (SUCCESS != LoadSplit(Program, pChild, pPDU))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsDie))
		{
			if (SUCCESS != LoadDie(Program))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsShowWindow))
		{
			if (SUCCESS != LoadShowWindow(Program, pChild))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsWaitMessage))
		{
			if (SUCCESS != LoadWaitMessage(Program, pChild))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsAlert))
		{
			if (SUCCESS != LoadAlert(Program, pChild))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsEnable))
		{
			if (SUCCESS != LoadEnable(Program, pChild))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsAddItem))
		{
			if (SUCCESS != LoadAddItem(Program, pChild, pPDU))
				return XML_LOADER_ERROR;
		}
		else if (0 == XMLString::compareString(pChild->getNodeName(),
											   wsProgram))
		{
			CProgram *pSubProgram = new CProgram();

			if (SUCCESS != LoadProgram(&Program.Data(),
										*pSubProgram,
										pChild,
										pPDU))
            {
				return XML_LOADER_ERROR;
            }

			if (false_v == Program.AddOperator(pSubProgram))
				return XML_LOADER_ERROR;
		}

		pChild = (DOMElement *)pChild->getNextSibling();
	}

	return SUCCESS;
}