public Tag copy() { return new Tag(id, name, deviceIds); }
public void deleteDeviceFromTags(DashBoard dash, int deviceId) { for (Tag tag : dash.tags) { tag.deleteDevice(deviceId); } }
public HardwareInfo(String[] info) { HardwareInfoPrivate hardwareInfoPrivate = new HardwareInfoPrivate(info); this.version = hardwareInfoPrivate.version; this.blynkVersion = hardwareInfoPrivate.blynkVersion; this.boardType = hardwareInfoPrivate.boardType; this.cpuType = hardwareInfoPrivate.cpuType; this.connectionType = hardwareInfoPrivate.connectionType; this.build = hardwareInfoPrivate.build; this.templateId = hardwareInfoPrivate.templateId; this.heartbeatInterval = hardwareInfoPrivate.heartbeatInterval; this.buffIn = hardwareInfoPrivate.buffIn <= 0 ? DEFAULT_HARDWARE_BUFFER_SIZE : hardwareInfoPrivate.buffIn; }
private Set<Channel> filter(int bodySize, int activeDashId, int[] deviceIds) { Set<Channel> targetChannels = new HashSet<>(); for (Channel channel : hardwareChannels) { HardwareStateHolder hardwareState = getHardState(channel); if (hardwareState != null && hardwareState.dash.id == activeDashId && (deviceIds.length == 0 || ArrayUtil.contains(deviceIds, hardwareState.device.id))) { if (hardwareState.device.fitsBufferSize(bodySize)) { targetChannels.add(channel); } else { log.trace("Message is to large. Size {}.", bodySize); } } } return targetChannels; }
public static void messageReceived(ChannelHandlerContext ctx, User user, StringMessage message) { String[] split = split2(message.body); if (split.length < 2) { throw new IllegalCommandException("Wrong income message format."); } int dashId = Integer.parseInt(split[0]); String tagString = split[1]; if (tagString == null || tagString.isEmpty()) { throw new IllegalCommandException("Income tag message is empty."); } DashBoard dash = user.profile.getDashByIdOrThrow(dashId); Tag newTag = JsonParser.parseTag(tagString, message.id); log.debug("Updating new tag {}.", tagString); if (newTag.isNotValid()) { throw new IllegalCommandException("Income tag name is not valid."); } Tag existingTag = user.profile.getTagById(dash, newTag.id); if (existingTag == null) { throw new IllegalCommandException("Attempt to update tag with non existing id."); } existingTag.update(newTag); user.lastModifiedTs = System.currentTimeMillis(); ctx.writeAndFlush(ok(message.id), ctx.voidPromise()); }
public static void messageReceived(ChannelHandlerContext ctx, User user, StringMessage message) { String[] split = split2(message.body); if (split.length < 2) { throw new IllegalCommandException("Wrong income message format."); } int dashId = Integer.parseInt(split[0]); String deviceString = split[1]; if (deviceString == null || deviceString.isEmpty()) { throw new IllegalCommandException("Income device message is empty."); } DashBoard dash = user.profile.getDashByIdOrThrow(dashId); Device newDevice = JsonParser.parseDevice(deviceString, message.id); log.debug("Updating new device {}.", deviceString); if (newDevice.isNotValid()) { throw new IllegalCommandException("Income device message is not valid."); } Device existingDevice = user.profile.getDeviceById(dash, newDevice.id); if (existingDevice == null) { log.debug("Attempt to update device with non existing id."); ctx.writeAndFlush(illegalCommandBody(message.id), ctx.voidPromise()); return; } existingDevice.update(newDevice); dash.updatedAt = System.currentTimeMillis(); user.lastModifiedTs = dash.updatedAt; ctx.writeAndFlush(ok(message.id), ctx.voidPromise()); }
private Response singleDeviceOTA(ChannelHandlerContext ctx, String token, String pathToFirmware) { TokenValue tokenValue = tokenManager.getTokenValueByToken(token); if (tokenValue == null) { log.debug("Requested token {} not found.", token); return badRequest("Invalid token."); } User user = tokenValue.user; int dashId = tokenValue.dash.id; int deviceId = tokenValue.device.id; Session session = sessionDao.get(new UserKey(user)); if (session == null) { log.debug("No session for user {}.", user.email); return badRequest("Device wasn't connected yet."); } String body = OTAInfo.makeHardwareBody(otaManager.serverHostUrl, pathToFirmware); if (session.sendMessageToHardware(dashId, BLYNK_INTERNAL, 7777, body, deviceId)) { log.debug("No device in session."); return badRequest("No device in session."); } User initiator = ctx.channel().attr(AuthHeadersBaseHttpHandler.USER).get(); if (initiator != null) { tokenValue.device.updateOTAInfo(initiator.email); } return ok(pathToFirmware); }
public void updateOTAInfo(String initiatedBy) { long now = System.currentTimeMillis(); this.deviceOtaInfo = new DeviceOtaInfo(initiatedBy, now, now); }
private void makeSingleTokenEmail(ChannelHandlerContext ctx, DashBoard dash, Device device, String to, int msgId) { String dashName = dash.getNameOrDefault(); String deviceName = device.getNameOrDefault(); String subj = "Auth Token for " + dashName + " project and device " + deviceName; String body = "Auth Token : " + device.token + "\n"; log.trace("Sending single token mail for user {}, with token : '{}'.", to, device.token); mail(ctx.channel(), to, subj, body + tokenMailBody, msgId, false); }
public static DeviceStatusDTO[] transform(Device[] devices) { DeviceStatusDTO[] deviceStatusDTO = new DeviceStatusDTO[devices.length]; for (int i = 0; i < devices.length; i++) { deviceStatusDTO[i] = new DeviceStatusDTO(devices[i]); } return deviceStatusDTO; } }
public static Tag[] copyTags(Tag[] tagsToCopy) { if (tagsToCopy.length == 0) { return tagsToCopy; } Tag[] copy = new Tag[tagsToCopy.length]; for (int i = 0; i < copy.length; i++) { copy[i] = tagsToCopy[i].copy(); } return copy; }
public HardwareInfoPrivate(String[] info) { for (int i = 0; i < info.length; i++) { if (i < info.length - 1) { intiField(info[i], info[++i]); } } }
public static void messageReceived(ChannelHandlerContext ctx, User user, StringMessage message) { int dashId = Integer.parseInt(message.body); DashBoard dash = user.profile.getDashByIdOrThrow(dashId); String devicesJson; if (dash.devices == null || dash.devices.length == 0) { devicesJson = "[]"; } else { DeviceStatusDTO[] deviceStatusDTOS = DeviceStatusDTO.transform(dash.devices); devicesJson = JsonParser.toJson(deviceStatusDTOS); } if (ctx.channel().isWritable()) { ctx.writeAndFlush(makeUTF8StringMessage(GET_DEVICES, message.id, devicesJson), ctx.voidPromise()); } }
private void sentOfflineMessage(ChannelHandlerContext ctx, Session session, DashBoard dashBoard, Device device) { //this is special case. //in case hardware quickly reconnects we do not mark it as disconnected //as it is already online after quick disconnect. //https://github.com/blynkkk/blynk-server/issues/403 boolean isHardwareConnected = session.isHardwareConnected(dashBoard.id, device.id); if (!isHardwareConnected) { log.trace("Changing device status. DeviceId {}, dashId {}", device.id, dashBoard.id); device.disconnected(); } if (!dashBoard.isActive) { return; } Notification notification = dashBoard.getNotificationWidget(); if (notification != null && notification.notifyWhenOffline) { sendPushNotification(ctx, notification, dashBoard.id, device); } else if (!dashBoard.isNotificationsOff) { session.sendOfflineMessageToApps(dashBoard.id, device.id); } }
private static void parseHardwareInfo(Holder holder, ChannelHandlerContext ctx, String[] messageParts, HardwareStateHolder state, int msgId) { HardwareInfo hardwareInfo = new HardwareInfo(messageParts); int newHardwareInterval = hardwareInfo.heartbeatInterval; log.trace("Info command. heartbeat interval {}", newHardwareInterval); OTAManager otaManager = holder.otaManager; int hardwareIdleTimeout = holder.limits.hardwareIdleTimeout; //no need to change IdleStateHandler if heartbeat interval wasn't changed or wasn't provided if (hardwareIdleTimeout != 0 && newHardwareInterval > 0 && newHardwareInterval != hardwareIdleTimeout) { int newReadTimeout = NumberUtil.calcHeartbeatTimeout(newHardwareInterval); log.debug("Changing read timeout interval to {}", newReadTimeout); ctx.pipeline().replace(IdleStateHandler.class, "H_IdleStateHandler_Replaced", new IdleStateHandler(newReadTimeout, 0, 0)); } DashBoard dashBoard = state.dash; Device device = state.device; if (device != null) { otaManager.initiateHardwareUpdate(ctx, state.userKey, hardwareInfo, dashBoard, device); device.hardwareInfo = hardwareInfo; dashBoard.updatedAt = System.currentTimeMillis(); } ctx.writeAndFlush(ok(msgId), ctx.voidPromise()); }
public static void messageReceived(ChannelHandlerContext ctx, User user, StringMessage message) { String[] split = StringUtils.split2(message.body); int dashId = Integer.parseInt(split[0]); int deviceId = Integer.parseInt(split[1]); DashBoard dash = user.profile.getDashByIdOrThrow(dashId); Device device = user.profile.getDeviceById(dash, deviceId); if (device == null) { ctx.writeAndFlush(illegalCommandBody(message.id), ctx.voidPromise()); } else { if (ctx.channel().isWritable()) { ctx.writeAndFlush(makeUTF8StringMessage(MOBILE_GET_DEVICE, message.id, device.toString()), ctx.voidPromise()); } } }
private void completeLogin(Channel channel, Session session, User user, DashBoard dash, Device device, int msgId) { log.debug("completeLogin. {}", channel); session.addHardChannel(channel); channel.write(ok(msgId)); String body = dash.buildPMMessage(device.id); if (dash.isActive && body != null) { channel.write(makeASCIIStringMessage(HARDWARE, HARDWARE_PIN_MODE_MSG_ID, body)); } channel.flush(); String responseBody = String.valueOf(dash.id) + DEVICE_SEPARATOR + device.id; session.sendToApps(HARDWARE_CONNECTED, msgId, dash.id, responseBody); log.trace("Connected device id {}, dash id {}", device.id, dash.id); device.connected(); if (device.firstConnectTime == 0) { device.firstConnectTime = device.connectTime; } if (allowStoreIp) { device.lastLoggedIP = IPUtils.getIp(channel.remoteAddress()); } log.info("{} hardware joined.", user.email); }
private Set<Channel> filter(int bodySize, int activeDashId, int deviceId) { Set<Channel> targetChannels = new HashSet<>(); for (Channel channel : hardwareChannels) { HardwareStateHolder hardwareState = getHardState(channel); if (hardwareState != null && hardwareState.isSameDashAndDeviceId(activeDashId, deviceId)) { if (hardwareState.device.fitsBufferSize(bodySize)) { targetChannels.add(channel); } else { log.trace("Message is to large. Size {}.", bodySize); } } } return targetChannels; }
private void sendOtaCommand(ChannelHandlerContext ctx, Device device, OTAInfo otaInfo) { StringMessage msg = makeASCIIStringMessage(BLYNK_INTERNAL, 7777, otaInfo.makeHardwareBody(serverHostUrl)); if (ctx.channel().isWritable()) { device.deviceOtaInfo = new DeviceOtaInfo(otaInfo.initiatedBy, otaInfo.initiatedAt, System.currentTimeMillis()); ctx.write(msg, ctx.voidPromise()); } }
private void sendMultiTokenEmail(ChannelHandlerContext ctx, User user, DashBoard dash, int msgId) { String dashName = dash.getNameOrDefault(); String subj = "Auth Tokens for " + dashName + " project and " + dash.devices.length + " devices"; StringBuilder body = new StringBuilder(); for (Device device : dash.devices) { String deviceName = device.getNameOrDefault(); body.append("Auth Token for device '") .append(deviceName) .append("' : ") .append(device.token) .append("\n"); } body.append(tokenMailBody); String to = user.email; log.trace("Sending multi tokens mail for user {}, with {} tokens.", to, dash.devices.length); mail(ctx.channel(), to, subj, body.toString(), msgId, false); }