Skip to content

Commit

Permalink
Implement command notificator
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Feb 20, 2023
1 parent 1439422 commit cd1d808
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 7 deletions.
25 changes: 25 additions & 0 deletions schema/changelog-5.7.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd"
logicalFilePath="changelog-5.7">

<changeSet author="author" id="changelog-5.7">

<addColumn tableName="tc_notifications">
<column name="commandid" type="INT" />
</addColumn>

<addForeignKeyConstraint
baseTableName="tc_notifications"
baseColumnNames="commandid"
constraintName="fk_notifications_commandid"
referencedTableName="tc_commands"
referencedColumnNames="id"
onDelete="CASCADE" />

</changeSet>

</databaseChangeLog>
1 change: 1 addition & 0 deletions schema/changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
<include file="changelog-5.4.xml" relativeToChangelogFile="true" />
<include file="changelog-5.5.xml" relativeToChangelogFile="true" />
<include file="changelog-5.6.xml" relativeToChangelogFile="true" />
<include file="changelog-5.7.xml" relativeToChangelogFile="true" />

</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 - 2022 Anton Tananaev (anton@traccar.org)
* Copyright 2016 - 2023 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -80,7 +80,7 @@ public Collection<Typed> getNotificators() {

@POST
@Path("test")
public Response testMessage() throws MessageException, InterruptedException, StorageException {
public Response testMessage() throws MessageException, StorageException {
User user = permissionsService.getUser(getUserId());
for (Typed method : notificatorManager.getAllNotificatorTypes()) {
notificatorManager.getNotificator(method.getType()).send(null, user, new Event("test", 0), null);
Expand All @@ -91,7 +91,7 @@ public Response testMessage() throws MessageException, InterruptedException, Sto
@POST
@Path("test/{notificator}")
public Response testMessage(@PathParam("notificator") String notificator)
throws MessageException, InterruptedException, StorageException {
throws MessageException, StorageException {
User user = permissionsService.getUser(getUserId());
notificatorManager.getNotificator(notificator).send(null, user, new Event("test", 0), null);
return Response.noContent().build();
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/org/traccar/api/security/PermissionsService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Anton Tananaev (anton@traccar.org)
* Copyright 2022 - 2023 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@
import org.traccar.model.Group;
import org.traccar.model.GroupedModel;
import org.traccar.model.ManagedUser;
import org.traccar.model.Notification;
import org.traccar.model.ScheduledModel;
import org.traccar.model.Server;
import org.traccar.model.User;
Expand Down Expand Up @@ -129,7 +130,7 @@ public void checkEdit(long userId, BaseModel object, boolean addition) throws St
GroupedModel before = null;
if (!addition) {
before = storage.getObject(after.getClass(), new Request(
new Columns.Include("groupId"), new Condition.Equals("id", object.getId())));
new Columns.Include("groupId"), new Condition.Equals("id", after.getId())));
}
if (before == null || before.getGroupId() != after.getGroupId()) {
checkPermission(Group.class, userId, after.getGroupId());
Expand All @@ -142,13 +143,26 @@ public void checkEdit(long userId, BaseModel object, boolean addition) throws St
ScheduledModel before = null;
if (!addition) {
before = storage.getObject(after.getClass(), new Request(
new Columns.Include("calendarId"), new Condition.Equals("id", object.getId())));
new Columns.Include("calendarId"), new Condition.Equals("id", after.getId())));
}
if (before == null || before.getCalendarId() != after.getCalendarId()) {
checkPermission(Calendar.class, userId, after.getCalendarId());
}
}
}
if (object instanceof Notification) {
Notification after = ((Notification) object);
if (after.getCommandId() > 0) {
Notification before = null;
if (!addition) {
before = storage.getObject(after.getClass(), new Request(
new Columns.Include("commandId"), new Condition.Equals("id", after.getId())));
}
if (before == null || before.getCommandId() != after.getCommandId()) {
checkPermission(Command.class, userId, after.getCommandId());
}
}
}
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/main/java/org/traccar/model/Notification.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org)
* Copyright 2016 - 2023 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -46,6 +46,16 @@ public void setType(String type) {
this.type = type;
}

private long commandId;

public long getCommandId() {
return commandId;
}

public void setCommandId(long commandId) {
this.commandId = commandId;
}

private String notificators;

public String getNotificators() {
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/org/traccar/notificators/NotificatorCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2023 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.notificators;

import org.traccar.database.CommandsManager;
import org.traccar.model.Command;
import org.traccar.model.Event;
import org.traccar.model.Notification;
import org.traccar.model.Position;
import org.traccar.model.User;
import org.traccar.notification.MessageException;
import org.traccar.storage.Storage;
import org.traccar.storage.query.Columns;
import org.traccar.storage.query.Condition;
import org.traccar.storage.query.Request;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class NotificatorCommand implements Notificator {

private final Storage storage;
private final CommandsManager commandsManager;

@Inject
public NotificatorCommand(Storage storage, CommandsManager commandsManager) {
this.storage = storage;
this.commandsManager = commandsManager;
}

@Override
public void send(Notification notification, User user, Event event, Position position) throws MessageException {

if (notification == null || notification.getCommandId() <= 0) {
throw new MessageException("Saved command not provided");
}

try {
Command command = storage.getObject(Command.class, new Request(
new Columns.All(), new Condition.Equals("id", notification.getCommandId())));
command.setDeviceId(event.getDeviceId());
commandsManager.sendCommand(command);
} catch (Exception e) {
throw new MessageException(e);
}
}

}

0 comments on commit cd1d808

Please sign in to comment.