public void deleteTimers(UserKey userKey, DashBoard dash) { for (Widget widget : dash.widgets) { if (widget instanceof DeviceTiles) { DeviceTiles deviceTiles = (DeviceTiles) widget; deleteTimers(userKey, dash.id, deviceTiles); } else if (widget instanceof Timer) { delete(userKey, (Timer) widget, dash.id, -1L, -1L); } else if (widget instanceof Eventor) { delete(userKey, (Eventor) widget, dash.id); } } }
private void deleteTimers(UserKey userKey, int dashId, DeviceTiles deviceTiles) { for (TileTemplate template : deviceTiles.templates) { for (Widget widgetInTemplate : template.widgets) { if (widgetInTemplate instanceof Timer) { delete(userKey, (Timer) widgetInTemplate, dashId, deviceTiles.id, template.id); } } } } }
public void addTimers(TimerWorker timerWorker, UserKey userKey) { for (Widget widget : widgets) { if (widget instanceof DeviceTiles) { timerWorker.add(userKey, (DeviceTiles) widget, id); } else if (widget instanceof Timer) { timerWorker.add(userKey, (Timer) widget, id, -1L, -1L); } else if (widget instanceof Eventor) { timerWorker.add(userKey, (Eventor) widget, id); } } }
public void messageReceived(ChannelHandlerContext ctx, MobileStateHolder state, StringMessage message) { var id = message.body; var user = state.user; var existingAppIndex = user.profile.getAppIndexById(id); if (existingAppIndex == -1) { throw new NotAllowedException("App with passed is not exists.", message.id); } var projectIds = user.profile.apps[existingAppIndex].projectIds; var result = new ArrayList<DashBoard>(); for (DashBoard dash : user.profile.dashBoards) { if (ArrayUtil.contains(projectIds, dash.id)) { timerWorker.deleteTimers(state.userKey, dash); tokenManager.deleteDash(dash); sessionDao.closeHardwareChannelByDashId(state.userKey, dash.id); } else { result.add(dash); } } user.profile.dashBoards = result.toArray(new DashBoard[0]); user.profile.apps = ArrayUtil.remove(user.profile.apps, existingAppIndex, App.class); user.lastModifiedTs = System.currentTimeMillis(); ctx.writeAndFlush(ok(message.id), ctx.voidPromise()); }
private void add(UserKey userKey, int dashId, int deviceId, long widgetId, int additionalId, TimerTime time, BaseAction[] actions) { ArrayList<BaseAction> validActions = new ArrayList<>(actions.length); for (BaseAction action : actions) { if (action.isValid()) { validActions.add(action); } } if (!validActions.isEmpty()) { getExecutorOrCreate(time.time).put( new TimerKey(userKey, dashId, deviceId, widgetId, additionalId, -1L, -1L, time), validActions.toArray(new BaseAction[0])); } }
if (key.isTilesTimer()) { Widget widget = dash.getWidgetById(key.deviceTilesId); if (widget instanceof DeviceTiles) { triggerTimer(sessionDao, key.userKey, setPinAction.makeHardwareBody(), key.dashId, deviceIds); } else if (action instanceof NotifyAction) { NotifyAction notifyAction = (NotifyAction) action;
@SuppressWarnings("unchecked") public TimerWorker(UserDao userDao, SessionDao sessionDao, GCMWrapper gcmWrapper) { this.userDao = userDao; this.sessionDao = sessionDao; this.gcmWrapper = gcmWrapper; //array cell for every second in a day, //yes, it costs a bit of memory, but still cheap :) this.timerExecutors = new AtomicReferenceArray<>(size); init(userDao.users); }
private void delete(UserKey userKey, int dashId, int deviceId, long widgetId, int additionalId, long deviceTilesId, long templateId, TimerTime time) { ConcurrentHashMap<TimerKey, BaseAction[]> secondExecutor = timerExecutors.get(time.time); if (secondExecutor != null) { secondExecutor.remove(new TimerKey(userKey, dashId, deviceId, widgetId, additionalId, deviceTilesId, templateId, time)); } }
@Override public void run() { log.trace("Starting timer..."); long now = System.currentTimeMillis(); ConcurrentMap<TimerKey, BaseAction[]> tickedExecutors = timerExecutors.get((int) ((now / 1000) % 86400)); if (tickedExecutors == null) { return; } try { this.activeTimers = 0; this.actuallySendTimers = 0; send(tickedExecutors, now); } catch (Exception e) { log.error("Error running timers. ", e); } if (activeTimers > 0) { log.info("Timer finished. Ready {}, Active {}, Actual {}. Processing time : {} ms", tickedExecutors.size(), activeTimers, actuallySendTimers, System.currentTimeMillis() - now); } }
private void send(ConcurrentMap<TimerKey, BaseAction[]> tickedExecutors, long now) { ZonedDateTime currentDateTime = ZonedDateTime.now(DateTimeUtils.UTC); for (Map.Entry<TimerKey, BaseAction[]> entry : tickedExecutors.entrySet()) { TimerKey key = entry.getKey(); BaseAction[] actions = entry.getValue(); if (key.time.isTickTime(currentDateTime)) { User user = userDao.users.get(key.userKey); if (user != null) { DashBoard dash = user.profile.getDashById(key.dashId); if (dash != null && dash.isActive) { activeTimers++; process(user.profile, dash, key, actions, now); } } } } }
public void delete(UserKey userKey, Eventor eventor, int dashId) { if (eventor.rules != null) { for (Rule rule : eventor.rules) { if (rule.isValidTimerRule()) { delete(userKey, dashId, eventor.deviceId, eventor.id, rule.triggerTime.id, -1L, -1L, rule.triggerTime); } } } }
public int add(UserKey userKey, DeviceTiles deviceTiles, int dashId) { int counter = 0; for (TileTemplate template : deviceTiles.templates) { for (Widget widgetInTemplate : template.widgets) { if (widgetInTemplate instanceof Timer) { add(userKey, (Timer) widgetInTemplate, dashId, deviceTiles.id, template.id); counter++; } } } return counter; }
public static void messageReceived(Holder holder, ChannelHandlerContext ctx, MobileStateHolder state, StringMessage message) { String dashString = message.body; if (dashString == null || dashString.isEmpty()) { throw new IllegalCommandException("Income create dash message is empty."); } if (dashString.length() > holder.limits.profileSizeLimitBytes) { throw new NotAllowedException("User dashboard is larger then limit.", message.id); } log.debug("Trying to parse user dash : {}", dashString); DashBoard updatedDash = JsonParser.parseDashboard(dashString, message.id); if (updatedDash == null) { throw new IllegalCommandException("Project parsing error."); } log.debug("Saving dashboard."); User user = state.user; DashBoard existingDash = user.profile.getDashByIdOrThrow(updatedDash.id); TimerWorker timerWorker = holder.timerWorker; timerWorker.deleteTimers(state.userKey, existingDash); updatedDash.addTimers(timerWorker, state.userKey); existingDash.updateFields(updatedDash); user.profile.cleanPinStorage(existingDash, false, true); user.lastModifiedTs = existingDash.updatedAt; ctx.writeAndFlush(ok(message.id), ctx.voidPromise()); }
public void add(UserKey userKey, Timer timer, int dashId, long deviceTilesId, long templateId) { if (timer.isValid()) { if (timer.isValidStart()) { TimerTime timerTime = new TimerTime(timer.startTime); SetPinAction action = new SetPinAction(timer.pin, timer.pinType, timer.startValue); TimerKey timerKey = new TimerKey(userKey, dashId, timer.deviceId, timer.id, 0, deviceTilesId, templateId, timerTime); getExecutorOrCreate(timerTime.time).put(timerKey, new BaseAction[]{action}); } if (timer.isValidStop()) { TimerTime timerTime = new TimerTime(timer.stopTime); SetPinAction action = new SetPinAction(timer.pin, timer.pinType, timer.stopValue); TimerKey timerKey = new TimerKey(userKey, dashId, timer.deviceId, timer.id, 1, deviceTilesId, templateId, timerTime); getExecutorOrCreate(timerTime.time).put(timerKey, new BaseAction[]{action}); } } }
/** * Removes all widgets with tabId greater than lastTabIndex */ static Widget[] deleteTabs(TimerWorker timerWorker, User user, UserKey userKey, int dashId, long deviceTilesId, long templateId, Widget[] widgets, int lastTabIndex) { ArrayList<Widget> zeroTabWidgets = new ArrayList<>(); int removedWidgetPrice = 0; for (Widget widgetToDelete : widgets) { if (widgetToDelete.tabId > lastTabIndex) { removedWidgetPrice += widgetToDelete.getPrice(); if (widgetToDelete instanceof Timer) { timerWorker.delete(userKey, (Timer) widgetToDelete, dashId, deviceTilesId, templateId); } else if (widgetToDelete instanceof Eventor) { timerWorker.delete(userKey, (Eventor) widgetToDelete, dashId); } } else { zeroTabWidgets.add(widgetToDelete); } } user.addEnergy(removedWidgetPrice); return zeroTabWidgets.toArray(new Widget[0]); }
public void add(UserKey userKey, Eventor eventor, int dashId) { if (eventor.rules != null) { for (Rule rule : eventor.rules) { if (rule.isValidTimerRule()) { add(userKey, dashId, eventor.deviceId, eventor.id, rule.triggerTime.id, rule.triggerTime, rule.actions); } } } }
private static void deleteDash(Holder holder, MobileStateHolder state, int dashId) { User user = state.user; int index = user.profile.getDashIndexOrThrow(dashId); log.debug("Deleting dashboard {}.", dashId); DashBoard dash = user.profile.dashBoards[index]; user.addEnergy(dash.energySum()); holder.timerWorker.deleteTimers(state.userKey, dash); holder.tokenManager.deleteDash(dash); holder.sessionDao.closeHardwareChannelByDashId(state.userKey, dashId); holder.reportScheduler.cancelStoredFuture(user, dashId); holder.blockingIOProcessor.executeHistory(() -> { for (Device device : dash.devices) { try { holder.reportingDiskDao.delete(state.user, dashId, device.id); } catch (Exception e) { log.warn("Error removing device data. Reason : {}.", e.getMessage()); } } }); user.profile.dashBoards = ArrayUtil.remove(user.profile.dashBoards, index, DashBoard.class); }
public void delete(UserKey userKey, Timer timer, int dashId, long deviceTilesId, long templateId) { if (timer.isValidStart()) { delete(userKey, dashId, timer.deviceId, timer.id, 0, deviceTilesId, templateId, new TimerTime(timer.startTime)); } if (timer.isValidStop()) { delete(userKey, dashId, timer.deviceId, timer.id, 1, deviceTilesId, templateId, new TimerTime(timer.stopTime)); } }
private void init(ConcurrentMap<UserKey, User> users) { int counter = 0; for (Map.Entry<UserKey, User> entry : users.entrySet()) { for (DashBoard dash : entry.getValue().profile.dashBoards) { int dashId = dash.id; for (Widget widget : dash.widgets) { if (widget instanceof DeviceTiles) { DeviceTiles deviceTiles = (DeviceTiles) widget; counter += add(entry.getKey(), deviceTiles, dashId); } else if (widget instanceof Timer) { Timer timer = (Timer) widget; add(entry.getKey(), timer, dashId, -1, -1); counter++; } else if (widget instanceof Eventor) { Eventor eventor = (Eventor) widget; add(entry.getKey(), eventor, dashId); counter++; } } } } log.info("Timers : {}", counter); }