public void messageReceived(ChannelHandlerContext ctx, User user, StringMessage message) {
    String[] split = message.body.split(BODY_SEPARATOR_STRING, 2);

    if (split.length < 2) {
      throw new IllegalCommandException("Wrong income message format.");
    }

    int dashId = ParseUtil.parseInt(split[0]);
    String widgetString = split[1];

    if (widgetString == null || widgetString.equals("")) {
      throw new IllegalCommandException("Income widget message is empty.");
    }

    if (widgetString.length() > MAX_WIDGET_SIZE) {
      throw new NotAllowedException("Widget is larger then limit.");
    }

    DashBoard dash = user.profile.getDashById(dashId);

    Widget newWidget = JsonParser.parseWidget(widgetString);

    log.debug("Updating widget {}.", widgetString);

    if (dash == null || newWidget == null) {
      log.error("Error updating widget {}. Project : {}", widgetString, dash);
      throw new IllegalCommandException("Empty widget or project.");
    }

    int existingWidgetIndex = dash.getWidgetIndex(newWidget.id);

    if (newWidget instanceof Tabs) {
      Tabs newTabs = (Tabs) newWidget;
      DeleteWidgetLogic.deleteTabs(user, dash, newTabs.tabs.length - 1);
    }

    // strange issue https://github.com/blynkkk/blynk-server/issues/227
    // just log error for now
    try {
      dash.widgets[existingWidgetIndex] = newWidget;
      dash.updatedAt = System.currentTimeMillis();
      user.lastModifiedTs = dash.updatedAt;
    } catch (ArrayIndexOutOfBoundsException e) {
      throw new BaseServerException(
          "Error updating widget. " + widgetString, Response.SERVER_EXCEPTION);
    }

    ctx.writeAndFlush(ok(message.id), ctx.voidPromise());
  }
Esempio n. 2
0
  @Test
  public void testTimerWidgetTriggered() throws Exception {
    Executors.newScheduledThreadPool(1)
        .scheduleAtFixedRate(
            new TimerWorker(holder.userDao, holder.sessionDao), 0, 1000, TimeUnit.MILLISECONDS);

    clientPair.appClient.send("deactivate 1");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(1, OK)));

    Timer timer = new Timer();
    timer.id = 1;
    timer.x = 1;
    timer.y = 1;
    timer.pinType = PinType.DIGITAL;
    timer.pin = 5;
    timer.startValue = "dw 5 1";
    timer.stopValue = "dw 5 0";
    LocalTime localDateTime = LocalTime.now(ZoneId.of("UTC"));
    long curTime =
        localDateTime.getSecond() + localDateTime.getMinute() * 60 + localDateTime.getHour() * 3600;
    timer.startTime = curTime + 1;
    timer.stopTime = curTime + 2;

    DashBoard dashBoard = new DashBoard();
    dashBoard.id = 1;
    dashBoard.name = "Test";
    dashBoard.widgets = new Widget[] {timer};

    clientPair.appClient.send("saveDash " + dashBoard.toString());
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(2, OK)));

    clientPair.appClient.send("activate 1");
    verify(clientPair.appClient.responseMock, timeout(500))
        .channelRead(any(), eq(new ResponseMessage(3, OK)));

    verify(clientPair.hardwareClient.responseMock, timeout(2000))
        .channelRead(any(), eq(produce(7777, HARDWARE, "dw 5 1")));
    clientPair.hardwareClient.reset();
    verify(clientPair.hardwareClient.responseMock, timeout(2000))
        .channelRead(any(), eq(produce(7777, HARDWARE, "dw 5 0")));
  }
  private static void completeLogin(
      Channel channel, Session session, User user, DashBoard dash, int msgId) {
    log.debug("completeLogin. {}", channel);

    if (dash.pinModeMessage == null) {
      dash.pinModeMessage = new HardwareMessage(1, dash.buildPMMessage());
    }

    session.hardwareChannels.add(channel);
    channel.write(new ResponseMessage(msgId, OK));

    if (dash.isActive && dash.pinModeMessage.length > 2) {
      channel.write(dash.pinModeMessage);
    }

    channel.flush();

    log.info("{} hardware joined.", user.name);
  }