How to add new table to traccar backend source code?

faris73 years ago

Hi I am new to traccar and kind of ineterested in extending traccar application to other application. I have managed to run the traccar server source code on IntelliJ IDE.

Right now I am trying to add a table called test and call the table's data through GET http request from browser. Following are my steps in achieving the purpose:

  1. Creating api resource file called TestResource in src > main > java >org.traccar > api > resource folder
@Path("test")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class TestResource extends ExtendedObjectResource<Test> {

    public TestResource() {
        super(Test.class);
    }
}
  1. Create Test model in src > main > java >org.traccar > api > model folder
public class Test extends ExtendedModel {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String uniqueId;

    public String getUniqueId() {
        return uniqueId;
    }

    public void setUniqueId(String uniqueId) {
        this.uniqueId = uniqueId;
    }
}
  1. Create TestManager in src > main > java >org.traccar > api > database folder
public class TestManager extends ExtendedObjectManager<Test> {

    private Map<String, Test> testsByUniqueId;

    public TestManager(DataManager dataManager) {
        super(dataManager, Test.class);
        try {
            writeLock();
            if (testsByUniqueId == null) {
                testsByUniqueId = new ConcurrentHashMap<>();
            }
        } finally {
            writeUnlock();
        }
    }

    private void addByUniqueId(Test test) {
        try {
            writeLock();
            if (testsByUniqueId == null) {
                testsByUniqueId = new ConcurrentHashMap<>();
            }
            testsByUniqueId.put(test.getUniqueId(), test);
        } finally {
            writeUnlock();
        }
    }

    private void removeByUniqueId(String driverUniqueId) {
        try {
            writeLock();
            if (testsByUniqueId == null) {
                testsByUniqueId = new ConcurrentHashMap<>();
            }
            testsByUniqueId.remove(driverUniqueId);
        } finally {
            writeUnlock();
        }
    }

    @Override
    protected void addNewItem(Test test) {
        super.addNewItem(test);
        addByUniqueId(test);
    }

    @Override
    protected void updateCachedItem(Test test) {
        Test cachedTest = getById(test.getId());
        cachedTest.setName(test.getName());
        if (!test.getUniqueId().equals(cachedTest.getUniqueId())) {
            removeByUniqueId(cachedTest.getUniqueId());
            cachedTest.setUniqueId(test.getUniqueId());
            addByUniqueId(cachedTest);
        }
        cachedTest.setAttributes(test.getAttributes());
    }

    @Override
    protected void removeCachedItem(long testId) {
        Test cachedTest = getById(testId);
        if (cachedTest != null) {
            String driverUniqueId = cachedTest.getUniqueId();
            super.removeCachedItem(testId);
            removeByUniqueId(driverUniqueId);
        }
    }

    public Test getTestByUniqueId(String uniqueId) {
        try {
            readLock();
            return testsByUniqueId.get(uniqueId);
        } finally {
            readUnlock();
        }
    }

}
  1. Declare and initialize TestManager object in Context file
    private static TestManager testManager;

    public static TestManager getTestManager() {
        return testManager;
    }

testManager = new TestManager(dataManager);

When I run the code I got following error. What do I do wrong. I seems could not figure out what is the missing piece in my code. Could anybody help me?

Exception in thread "main" java.lang.RuntimeException: java.lang.IllegalMonitorStateException
    at org.traccar.Main.run(Main.java:165)
    at org.traccar.Main.main(Main.java:110)
Caused by: java.lang.IllegalMonitorStateException
    at java.util.concurrent.locks.ReentrantReadWriteLock$Sync.tryRelease(ReentrantReadWriteLock.java:371)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1261)
    at java.util.concurrent.locks.ReentrantReadWriteLock$WriteLock.unlock(ReentrantReadWriteLock.java:1131)
    at org.traccar.database.BaseObjectManager.writeUnlock(BaseObjectManager.java:63)
    at org.traccar.database.ExtendedObjectManager.refreshExtendedPermissions(ExtendedObjectManager.java:139)
    at org.traccar.database.ExtendedObjectManager.<init>(ExtendedObjectManager.java:44)
    at org.traccar.database.TestManager.<init>(TestManager.java:28)
Caused by: java.lang.IllegalMonitorStateException

    at org.traccar.Context.init(Context.java:328)
    at org.traccar.Main.run(Main.java:137)
    ... 1 more

> Task :Main.main() FAILED

Execution failed for task ':Main.main()'.
> Process 'command 'C:/Program Files/Java/jdk1.8.0_241/bin/java.exe'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Bogdan Matei2 years ago

I am also interested to add new table in mysql db and than use the data inside traccar custom protocol.
Please advice.