From 2aaa4a5e00e801b9b310e42f538113dd83e60188 Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Wed, 19 Jul 2017 09:38:51 +0200 Subject: [NEW] add GET with list of uniqueId --- swagger.json | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'swagger.json') diff --git a/swagger.json b/swagger.json index 734e42eab..a82232f02 100644 --- a/swagger.json +++ b/swagger.json @@ -65,6 +65,14 @@ "required" : false, "type" : "integer", "collectionFormat" : "multi" + }, + { + "name" : "uniqueId", + "in" : "query", + "description" : "To fetch one or more devices. Multiple params can be passed like `uniqueId=333331&uniqieId=44442`", + "required" : false, + "type" : "string", + "collectionFormat" : "multi" } ], "responses": { -- cgit v1.2.3 From f4058d92243ecd7c5e6b4d1669f38a81041eaa40 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 24 Jul 2017 10:57:12 +0500 Subject: Describe Permissions API in swagger --- swagger.json | 355 ++++++++--------------------------------------------------- 1 file changed, 44 insertions(+), 311 deletions(-) (limited to 'swagger.json') diff --git a/swagger.json b/swagger.json index a82232f02..a8d4f5641 100644 --- a/swagger.json +++ b/swagger.json @@ -164,37 +164,6 @@ } } }, - "/devices/geofences": { - "post": { - "summary": "Link a Geofence to a Device", - "parameters": [ - { - "$ref": "#/parameters/DeviceGeofence" - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/DeviceGeofence" - } - } - } - }, - "delete": { - "summary": "Remove a Geofence from a Device", - "parameters": [ - { - "$ref": "#/parameters/DeviceGeofence" - } - ], - "responses": { - "204": { - "description": "No Content" - } - } - } - }, "/groups": { "get": { "summary": "Fetch a list of Groups", @@ -273,50 +242,19 @@ } } }, - "/groups/geofences": { - "post": { - "summary": "Link a Geofence to a Group", - "parameters": [ - { - "$ref": "#/parameters/GroupGeofence" - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/GroupGeofence" - } - } - } - }, - "delete": { - "summary": "Remove a Geofence from a Group", - "parameters": [ - { - "$ref": "#/parameters/GroupGeofence" - } - ], - "responses": { - "204": { - "description": "No Content" - } - } - } - }, - "/permissions/devices": { + "/permissions": { "post": { - "summary": "Link a Device to a User", + "summary": "Link an Object to another Object", "parameters": [ { - "$ref": "#/parameters/DevicePermission" + "$ref": "#/parameters/Permission" } ], "responses": { "200": { "description": "OK", "schema": { - "$ref": "#/definitions/DevicePermission" + "$ref": "#/definitions/Permission" } }, "400": { @@ -325,134 +263,10 @@ } }, "delete": { - "summary": "Remove a Device from a User", - "parameters": [ - { - "$ref": "#/parameters/DevicePermission" - } - ], - "responses": { - "204": { - "description": "No Content" - } - } - } - }, - "/permissions/groups": { - "post": { - "summary": "Link a Group to a User", - "parameters": [ - { - "$ref": "#/parameters/GroupPermission" - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/GroupPermission" - } - } - } - }, - "delete": { - "summary": "Remove a Group from a User", - "parameters": [ - { - "$ref": "#/parameters/GroupPermission" - } - ], - "responses": { - "204": { - "description": "No Content" - } - } - } - }, - "/permissions/geofences": { - "post": { - "summary": "Link a Geofence to a User", - "parameters": [ - { - "$ref": "#/parameters/GeofencePermission" - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/GeofencePermission" - } - } - } - }, - "delete": { - "summary": "Remove a Geofence from a User", + "summary": "Unlink an Object from another Object", "parameters": [ { - "$ref": "#/parameters/GeofencePermission" - } - ], - "responses": { - "204": { - "description": "No Content" - } - } - } - }, - "/permissions/calendars": { - "post": { - "summary": "Link a Calendar to a User", - "parameters": [ - { - "$ref": "#/parameters/CalendarPermission" - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/CalendarPermission" - } - } - } - }, - "delete": { - "summary": "Remove a Calendar from a User", - "parameters": [ - { - "$ref": "#/parameters/CalendarPermission" - } - ], - "responses": { - "204": { - "description": "No Content" - } - } - } - }, - "/permissions/users": { - "post": { - "summary": "Link a User to a manager User", - "parameters": [ - { - "$ref": "#/parameters/UserPermission" - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/UserPermission" - } - } - } - }, - "delete": { - "summary": "Remove a User from a manager User", - "parameters": [ - { - "$ref": "#/parameters/UserPermission" + "$ref": "#/parameters/Permission" } ], "responses": { @@ -1473,75 +1287,42 @@ "attributes": {} } }, - "DevicePermission": { - "properties": { - "userId": { - "type": "integer" - }, - "deviceId": { - "type": "integer" - } - } - }, - "GroupPermission": { - "properties": { - "userId": { - "type": "integer" - }, - "groupId": { - "type": "integer" - } - } - }, - "GeofencePermission": { - "properties": { - "userId": { - "type": "integer" - }, - "geofenceId": { - "type": "integer" - } - } - }, - "CalendarPermission": { - "properties": { - "userId": { - "type": "integer" - }, - "calendarId": { - "type": "integer" - } - } - }, - "UserPermission": { - "properties": { - "userId": { - "type": "integer" - }, - "managedUserId": { - "type": "integer" - } - } - }, - "GroupGeofence": { - "properties": { - "groupId": { - "type": "integer" - }, - "geofenceId": { - "type": "integer" - } - } - }, - "DeviceGeofence": { - "properties": { - "deviceId": { - "type": "integer" - }, - "geofenceId": { - "type": "integer" - } - } + "Permission": { + "description": "This is a permission map that contain two object indexes. It is used to link/unlink objects. Order is important. Example: { deviceId:8, geofenceId: 16 }", + "properties": { + "userId": { + "description": "User Id, can be only first parameter", + "type": "integer" + }, + "deviceId": { + "description": "Device Id, can be first parameter or second only in combination with userId", + "type": "integer" + }, + "groupId": { + "description": "Group Id, can be first parameter or second only in combination with userId", + "type": "integer" + }, + "geofenceId": { + "description": "Geofence Id, can be second parameter only", + "type": "integer" + }, + "calendarId": { + "description": "Geofence Id, can be second parameter only and only in combination with userId", + "type": "integer" + }, + "attributeId": { + "description": "Computed Attribute Id, can be second parameter only", + "type": "integer" + }, + "driverId": { + "description": "Driver Id, can be second parameter only", + "type": "integer" + }, + "managedUserId": { + "description": "User Id, can be second parameter only and only in combination with userId", + "type": "integer" + } + } }, "CommandType": { "properties": { @@ -1793,12 +1574,12 @@ "$ref": "#/definitions/Device" } }, - "DeviceGeofence": { + "Permission": { "name": "body", "in": "body", "required": true, "schema": { - "$ref": "#/definitions/DeviceGeofence" + "$ref": "#/definitions/Permission" } }, "Group": { @@ -1809,54 +1590,6 @@ "$ref": "#/definitions/Group" } }, - "GroupGeofence": { - "name": "body", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/GroupGeofence" - } - }, - "DevicePermission": { - "name": "body", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/DevicePermission" - } - }, - "GroupPermission": { - "name": "body", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/GroupPermission" - } - }, - "GeofencePermission": { - "name": "body", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/GeofencePermission" - } - }, - "CalendarPermission": { - "name": "body", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/CalendarPermission" - } - }, - "UserPermission": { - "name": "body", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/UserPermission" - } - }, "User": { "name": "body", "in": "body", -- cgit v1.2.3 From f57b2094210de3de6f7333fd5169b8218478f8f9 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 24 Jul 2017 11:17:18 +0500 Subject: Add Computed Attributes to swagger --- swagger.json | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 117 insertions(+), 4 deletions(-) (limited to 'swagger.json') diff --git a/swagger.json b/swagger.json index a8d4f5641..970052e79 100644 --- a/swagger.json +++ b/swagger.json @@ -607,12 +607,10 @@ "$ref": "#/parameters/userId" }, { - "name": "groupId", - "in": "query", - "type": "integer" + "$ref": "#/parameters/deviceId" }, { - "$ref": "#/parameters/deviceId" + "$ref": "#/parameters/groupId" }, { "name": "refresh", @@ -1036,6 +1034,87 @@ } } } + }, + "/attributes/computed": { + "get": { + "summary": "Fetch a list of Attributes", + "description": "Without params, it returns a list of Attributes the user has access to", + "parameters": [ + { + "$ref": "#/parameters/all" + }, + { + "$ref": "#/parameters/userId" + }, + { + "$ref": "#/parameters/deviceId" + }, + { + "$ref": "#/parameters/groupId" + }, + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/Attribute" + } + } + } + } + }, + "post": { + "summary": "Create an Attribute", + "parameters": [ + { + "$ref": "#/parameters/Attribute" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/Attribute" + } + } + } + } + }, + "/attributes/computed/{id}": { + "put": { + "summary": "Update an Attribute", + "parameters": [ + { + "$ref": "#/parameters/entityId" + }, + { + "$ref": "#/parameters/Attribute" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/Attribute" + } + } + } + }, + "delete": { + "summary": "Delete an Attribute", + "parameters": [ + { + "$ref": "#/parameters/entityId" + } + ], + "responses": { + "204": { + "description": "No Content" + } + } + } } }, "definitions": { @@ -1539,6 +1618,26 @@ }, "atributes": {} } + }, + "Attribute": { + "properties": { + "id": { + "type": "integer" + }, + "description": { + "type": "string" + }, + "attribute": { + "type": "string" + }, + "expression": { + "type": "string" + }, + "type": { + "type": "string", + "description": "String|Number|Boolean" + } + } } }, "parameters": { @@ -1566,6 +1665,12 @@ "description": "Standard users can use this only with _deviceId_s, they have access to", "type": "integer" }, + "groupId": { + "name": "groupId", + "in": "query", + "description": "Standard users can use this only with _groupId_s, they have access to", + "type": "integer" + }, "Device": { "name": "body", "in": "body", @@ -1622,6 +1727,14 @@ "$ref": "#/definitions/Calendar" } }, + "Attribute": { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/Attribute" + } + }, "deviceIdArray": { "name": "deviceId", "in": "query", -- cgit v1.2.3 From d54ea579899e7d03a3065fbc3e119ca4e676696a Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 24 Jul 2017 11:28:14 +0500 Subject: Add Drivers to swagger --- swagger.json | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 117 insertions(+), 4 deletions(-) (limited to 'swagger.json') diff --git a/swagger.json b/swagger.json index 970052e79..fcc9f5153 100644 --- a/swagger.json +++ b/swagger.json @@ -613,10 +613,7 @@ "$ref": "#/parameters/groupId" }, { - "name": "refresh", - "in": "query", - "required": false, - "type": "boolean" + "$ref": "#/parameters/refresh" } ], "responses": { @@ -1052,6 +1049,9 @@ { "$ref": "#/parameters/groupId" }, + { + "$ref": "#/parameters/refresh" + } ], "responses": { "200": { @@ -1115,6 +1115,90 @@ } } } + }, + "/drivers": { + "get": { + "summary": "Fetch a list of Drivers", + "description": "Without params, it returns a list of Drivers the user has access to", + "parameters": [ + { + "$ref": "#/parameters/all" + }, + { + "$ref": "#/parameters/userId" + }, + { + "$ref": "#/parameters/deviceId" + }, + { + "$ref": "#/parameters/groupId" + }, + { + "$ref": "#/parameters/refresh" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/Driver" + } + } + } + } + }, + "post": { + "summary": "Create a Driver", + "parameters": [ + { + "$ref": "#/parameters/Driver" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/Driver" + } + } + } + } + }, + "/drivers/{id}": { + "put": { + "summary": "Update a Driver", + "parameters": [ + { + "$ref": "#/parameters/entityId" + }, + { + "$ref": "#/parameters/Driver" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/Driver" + } + } + } + }, + "delete": { + "summary": "Delete a Driver", + "parameters": [ + { + "$ref": "#/parameters/entityId" + } + ], + "responses": { + "204": { + "description": "No Content" + } + } + } } }, "definitions": { @@ -1638,6 +1722,20 @@ "description": "String|Number|Boolean" } } + }, + "Driver": { + "properties": { + "id": { + "type": "integer" + }, + "name": { + "type": "string" + }, + "uniqueId": { + "type": "string" + }, + "atributes": {} + } } }, "parameters": { @@ -1653,6 +1751,13 @@ "description": "Can only be used by admins or managers to fetch all entities", "type": "boolean" }, + { + "refresh": { + "name": "refresh", + "in": "query", + "required": false, + "type": "boolean" + }, "userId": { "name": "userId", "in": "query", @@ -1735,6 +1840,14 @@ "$ref": "#/definitions/Attribute" } }, + "Driver": { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/Driver" + } + }, "deviceIdArray": { "name": "deviceId", "in": "query", -- cgit v1.2.3 From a8c885e4e3d893181bed355f55d8d79b6722441b Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 24 Jul 2017 11:40:10 +0500 Subject: Add Stops report --- swagger.json | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'swagger.json') diff --git a/swagger.json b/swagger.json index fcc9f5153..d6850fca5 100644 --- a/swagger.json +++ b/swagger.json @@ -1579,6 +1579,10 @@ "type": "number", "description": "in meters" }, + "spentFuel": { + "type": "number", + "description": "in liters" + }, "engineHours": { "type": "integer" } @@ -1604,6 +1608,10 @@ "type": "number", "description": "in meters" }, + "spentFuel": { + "type": "number", + "description": "in liters" + }, "duration": { "type": "integer" }, @@ -1634,7 +1642,52 @@ }, "endLon": { "type": "number" + }, + "driverUniqueId": { + "type": "integer" + }, + "driverName": { + "type": "string" + }, + }, + "ReportStops": { + "properties": { + "deviceId": { + "type": "integer" + }, + "deviceName": { + "type": "string" + }, + "duration": { + "type": "integer" + }, + "startTime": { + "type": "string", + "format": "date-time", + "description": "in IS0 8601 format. eg. `1963-11-22T18:30:00Z`" + }, + "address": { + "type": "string" + }, + "lat": { + "type": "number" + }, + "lon": { + "type": "number" + }, + "endTime": { + "type": "string", + "format": "date-time", + "description": "in IS0 8601 format. eg. `1963-11-22T18:30:00Z`" + }, + "spentFuel": { + "type": "number", + "description": "in liters" + }, + "engineHours": { + "type": "integer" } + } }, "Statistics": { -- cgit v1.2.3 From 4292ab3b4bb638895a654e407cc548fd12ff49a8 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 24 Jul 2017 12:12:15 +0500 Subject: Fix JSON --- swagger.json | 79 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 39 insertions(+), 40 deletions(-) (limited to 'swagger.json') diff --git a/swagger.json b/swagger.json index d6850fca5..f4e816a5a 100644 --- a/swagger.json +++ b/swagger.json @@ -1451,41 +1451,41 @@ } }, "Permission": { - "description": "This is a permission map that contain two object indexes. It is used to link/unlink objects. Order is important. Example: { deviceId:8, geofenceId: 16 }", - "properties": { - "userId": { - "description": "User Id, can be only first parameter", - "type": "integer" - }, - "deviceId": { - "description": "Device Id, can be first parameter or second only in combination with userId", - "type": "integer" - }, - "groupId": { - "description": "Group Id, can be first parameter or second only in combination with userId", - "type": "integer" - }, - "geofenceId": { - "description": "Geofence Id, can be second parameter only", - "type": "integer" - }, - "calendarId": { - "description": "Geofence Id, can be second parameter only and only in combination with userId", - "type": "integer" - }, - "attributeId": { - "description": "Computed Attribute Id, can be second parameter only", - "type": "integer" - }, - "driverId": { - "description": "Driver Id, can be second parameter only", - "type": "integer" - }, - "managedUserId": { - "description": "User Id, can be second parameter only and only in combination with userId", - "type": "integer" - } - } + "description": "This is a permission map that contain two object indexes. It is used to link/unlink objects. Order is important. Example: { deviceId:8, geofenceId: 16 }", + "properties": { + "userId": { + "description": "User Id, can be only first parameter", + "type": "integer" + }, + "deviceId": { + "description": "Device Id, can be first parameter or second only in combination with userId", + "type": "integer" + }, + "groupId": { + "description": "Group Id, can be first parameter or second only in combination with userId", + "type": "integer" + }, + "geofenceId": { + "description": "Geofence Id, can be second parameter only", + "type": "integer" + }, + "calendarId": { + "description": "Geofence Id, can be second parameter only and only in combination with userId", + "type": "integer" + }, + "attributeId": { + "description": "Computed Attribute Id, can be second parameter only", + "type": "integer" + }, + "driverId": { + "description": "Driver Id, can be second parameter only", + "type": "integer" + }, + "managedUserId": { + "description": "User Id, can be second parameter only and only in combination with userId", + "type": "integer" + } + } }, "CommandType": { "properties": { @@ -1648,9 +1648,10 @@ }, "driverName": { "type": "string" - }, - }, - "ReportStops": { + } + } + }, + "ReportStops": { "properties": { "deviceId": { "type": "integer" @@ -1687,7 +1688,6 @@ "engineHours": { "type": "integer" } - } }, "Statistics": { @@ -1804,7 +1804,6 @@ "description": "Can only be used by admins or managers to fetch all entities", "type": "boolean" }, - { "refresh": { "name": "refresh", "in": "query", -- cgit v1.2.3 From 8c85694fcbdeb709e7088edb833a1e1a412206f2 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 19 Aug 2017 11:02:08 +1200 Subject: Update swagger version --- swagger.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'swagger.json') diff --git a/swagger.json b/swagger.json index f4e816a5a..ffe56c9da 100644 --- a/swagger.json +++ b/swagger.json @@ -1,7 +1,7 @@ { "swagger": "2.0", "info": { - "version": "3.10", + "version": "3.13", "title": "traccar" }, "host": "demo.traccar.org", -- cgit v1.2.3 From ba892a34c4a78ddc4ec7b24cc72b62278f608322 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 5 Sep 2017 18:14:36 +0500 Subject: Remove Attribute Aliases support --- schema/changelog-3.15.xml | 17 ++++ schema/changelog-master.xml | 1 + setup/default.xml | 21 ---- src/org/traccar/Context.java | 9 -- src/org/traccar/api/BaseObjectResource.java | 4 - .../api/resource/AttributeAliasResource.java | 93 ----------------- src/org/traccar/database/AliasesManager.java | 113 --------------------- src/org/traccar/database/DataManager.java | 24 ----- src/org/traccar/model/AttributeAlias.java | 51 ---------- swagger.json | 109 +++++--------------- 10 files changed, 44 insertions(+), 398 deletions(-) create mode 100644 schema/changelog-3.15.xml delete mode 100644 src/org/traccar/api/resource/AttributeAliasResource.java delete mode 100644 src/org/traccar/database/AliasesManager.java delete mode 100644 src/org/traccar/model/AttributeAlias.java (limited to 'swagger.json') diff --git a/schema/changelog-3.15.xml b/schema/changelog-3.15.xml new file mode 100644 index 000000000..a2d944abb --- /dev/null +++ b/schema/changelog-3.15.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + diff --git a/schema/changelog-master.xml b/schema/changelog-master.xml index d1b51b40e..58b2a8307 100644 --- a/schema/changelog-master.xml +++ b/schema/changelog-master.xml @@ -15,4 +15,5 @@ + diff --git a/setup/default.xml b/setup/default.xml index d96ceeb6d..95b69e216 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -62,27 +62,6 @@ DELETE FROM events WHERE serverTime < :serverTime - - SELECT * FROM attribute_aliases - - - - INSERT INTO attribute_aliases (deviceId, attribute, alias) - VALUES (:deviceId, :attribute, :alias) - - - - UPDATE attribute_aliases SET - deviceId = :deviceId, - attribute = :attribute, - alias = :alias - WHERE id = :id - - - - DELETE FROM attribute_aliases WHERE id = :id - - SELECT * FROM statistics WHERE captureTime BETWEEN :from AND :to ORDER BY captureTime diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 10fdd9f29..87d8257ee 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -25,7 +25,6 @@ import java.util.Properties; import org.apache.velocity.app.VelocityEngine; import org.eclipse.jetty.util.URIUtil; -import org.traccar.database.AliasesManager; import org.traccar.database.CalendarManager; import org.traccar.database.AttributesManager; import org.traccar.database.BaseObjectManager; @@ -202,12 +201,6 @@ public final class Context { return eventForwarder; } - private static AliasesManager aliasesManager; - - public static AliasesManager getAliasesManager() { - return aliasesManager; - } - private static AttributesManager attributesManager; public static AttributesManager getAttributesManager() { @@ -395,8 +388,6 @@ public final class Context { eventForwarder = new EventForwarder(); } - aliasesManager = new AliasesManager(dataManager); - attributesManager = new AttributesManager(dataManager); driversManager = new DriversManager(dataManager); diff --git a/src/org/traccar/api/BaseObjectResource.java b/src/org/traccar/api/BaseObjectResource.java index a1b90bc28..b13dc2e71 100644 --- a/src/org/traccar/api/BaseObjectResource.java +++ b/src/org/traccar/api/BaseObjectResource.java @@ -138,10 +138,6 @@ public abstract class BaseObjectResource extends BaseResour Context.getPermissionsManager().refreshAllExtendedPermissions(); } } - // Next should be removed with Attribute Aliases - if (baseClass.equals(Device.class)) { - Context.getAliasesManager().removeDevice(id); - } return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/AttributeAliasResource.java b/src/org/traccar/api/resource/AttributeAliasResource.java deleted file mode 100644 index b2636acf1..000000000 --- a/src/org/traccar/api/resource/AttributeAliasResource.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@traccar.org) - * Copyright 2016 Andrey Kunitsyn (andrey@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.api.resource; - -import java.sql.SQLException; -import java.util.Collection; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.AttributeAlias; - -@Path("attributes/aliases") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class AttributeAliasResource extends BaseResource { - - @GET - public Collection get(@QueryParam("deviceId") long deviceId) throws SQLException { - if (deviceId != 0) { - if (!Context.getPermissionsManager().isAdmin(getUserId())) { - Context.getPermissionsManager().checkDevice(getUserId(), deviceId); - } - return Context.getAliasesManager().getAttributeAliases(deviceId); - } else { - return Context.getAliasesManager().getAllAttributeAliases(getUserId()); - } - } - - @POST - public Response add(AttributeAlias entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDeviceReadonly(getUserId()); - if (!Context.getPermissionsManager().isAdmin(getUserId())) { - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - } - Context.getAliasesManager().addAttributeAlias(entity); - return Response.ok(entity).build(); - } - - @Path("{id}") - @PUT - public Response update(AttributeAlias entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDeviceReadonly(getUserId()); - if (!Context.getPermissionsManager().isAdmin(getUserId())) { - AttributeAlias oldEntity = Context.getAliasesManager().getAttributeAlias(entity.getId()); - Context.getPermissionsManager().checkDevice(getUserId(), oldEntity.getDeviceId()); - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - } - Context.getAliasesManager().updateAttributeAlias(entity); - return Response.ok(entity).build(); - } - - @Path("{id}") - @DELETE - public Response remove(@PathParam("id") long id) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDeviceReadonly(getUserId()); - if (!Context.getPermissionsManager().isAdmin(getUserId())) { - AttributeAlias entity = Context.getAliasesManager().getAttributeAlias(id); - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - } - Context.getAliasesManager().removeArrtibuteAlias(id); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/database/AliasesManager.java b/src/org/traccar/database/AliasesManager.java deleted file mode 100644 index 4f4f09731..000000000 --- a/src/org/traccar/database/AliasesManager.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@traccar.org) - * Copyright 2016 Andrey Kunitsyn (andrey@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.database; - -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - -import org.traccar.Context; -import org.traccar.helper.Log; -import org.traccar.model.AttributeAlias; - -public class AliasesManager { - - private final DataManager dataManager; - - private final Map> deviceAliases = new ConcurrentHashMap<>(); - private final Map aliasesById = new ConcurrentHashMap<>(); - - public AliasesManager(DataManager dataManager) { - this.dataManager = dataManager; - if (dataManager != null) { - try { - for (AttributeAlias attributeAlias : dataManager.getAttributeAliases()) { - getAttributeAliases(attributeAlias.getDeviceId()) - .add(attributeAlias); - aliasesById.put(attributeAlias.getId(), attributeAlias); - } - } catch (SQLException error) { - Log.warning(error); - } - } - } - - public Set getAttributeAliases(long deviceId) { - if (!deviceAliases.containsKey(deviceId)) { - deviceAliases.put(deviceId, new HashSet()); - } - return deviceAliases.get(deviceId); - } - - public void removeDevice(long deviceId) { - for (AttributeAlias attributeAlias : getAttributeAliases(deviceId)) { - aliasesById.remove(attributeAlias.getId()); - } - deviceAliases.remove(deviceId); - } - - public void addAttributeAlias(AttributeAlias attributeAlias) throws SQLException { - dataManager.addAttributeAlias(attributeAlias); - aliasesById.put(attributeAlias.getId(), attributeAlias); - getAttributeAliases(attributeAlias.getDeviceId()).add(attributeAlias); - } - - public void updateAttributeAlias(AttributeAlias attributeAlias) throws SQLException { - dataManager.updateAttributeAlias(attributeAlias); - AttributeAlias cachedAlias = aliasesById.get(attributeAlias.getId()); - if (cachedAlias.getDeviceId() != attributeAlias.getDeviceId()) { - getAttributeAliases(cachedAlias.getDeviceId()).remove(cachedAlias); - cachedAlias.setDeviceId(attributeAlias.getDeviceId()); - getAttributeAliases(cachedAlias.getDeviceId()).add(cachedAlias); - } - cachedAlias.setAttribute(attributeAlias.getAttribute()); - cachedAlias.setAlias(attributeAlias.getAlias()); - } - - public void removeArrtibuteAlias(long attributeAliasId) throws SQLException { - dataManager.removeAttributeAlias(attributeAliasId); - AttributeAlias cachedAlias = aliasesById.get(attributeAliasId); - getAttributeAliases(cachedAlias.getDeviceId()).remove(cachedAlias); - aliasesById.remove(attributeAliasId); - } - - public AttributeAlias getAttributeAlias(long deviceId, String attribute) { - for (AttributeAlias alias : getAttributeAliases(deviceId)) { - if (alias.getAttribute().equals(attribute)) { - return alias; - } - } - return null; - } - - public Collection getAllAttributeAliases(long userId) { - Collection userDevicesAliases = new ArrayList<>(); - for (long deviceId : Context.getPermissionsManager().getDevicePermissions(userId)) { - userDevicesAliases.addAll(getAttributeAliases(deviceId)); - } - return userDevicesAliases; - } - - public AttributeAlias getAttributeAlias(long id) { - return aliasesById.get(id); - } - -} diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 2e3e68bc9..4535a9c38 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -42,7 +42,6 @@ import liquibase.resource.ResourceAccessor; import org.traccar.Config; import org.traccar.helper.Log; import org.traccar.model.Attribute; -import org.traccar.model.AttributeAlias; import org.traccar.model.Device; import org.traccar.model.Driver; import org.traccar.model.Event; @@ -366,29 +365,6 @@ public class DataManager { .executeQuery(Event.class); } - public Collection getAttributeAliases() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectAttributeAliases")) - .executeQuery(AttributeAlias.class); - } - - public void addAttributeAlias(AttributeAlias attributeAlias) throws SQLException { - attributeAlias.setId(QueryBuilder.create(dataSource, getQuery("database.insertAttributeAlias"), true) - .setObject(attributeAlias) - .executeUpdate()); - } - - public void updateAttributeAlias(AttributeAlias attributeAlias) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.updateAttributeAlias")) - .setObject(attributeAlias) - .executeUpdate(); - } - - public void removeAttributeAlias(long attributeAliasId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.deleteAttributeAlias")) - .setLong("id", attributeAliasId) - .executeUpdate(); - } - public Collection getStatistics(Date from, Date to) throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectStatistics")) .setDate("from", from) diff --git a/src/org/traccar/model/AttributeAlias.java b/src/org/traccar/model/AttributeAlias.java deleted file mode 100644 index 1599ab939..000000000 --- a/src/org/traccar/model/AttributeAlias.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@traccar.org) - * Copyright 2016 Andrey Kunitsyn (andrey@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.model; - -public class AttributeAlias extends BaseModel { - - private long deviceId; - - public long getDeviceId() { - return deviceId; - } - - public void setDeviceId(long deviceId) { - this.deviceId = deviceId; - } - - private String attribute; - - public String getAttribute() { - return attribute; - } - - public void setAttribute(String attribute) { - this.attribute = attribute; - } - - private String alias; - - public String getAlias() { - return alias; - } - - public void setAlias(String alias) { - this.alias = alias; - } - -} diff --git a/swagger.json b/swagger.json index ffe56c9da..eba9e0910 100644 --- a/swagger.json +++ b/swagger.json @@ -1,7 +1,7 @@ { "swagger": "2.0", "info": { - "version": "3.13", + "version": "3.14", "title": "traccar" }, "host": "demo.traccar.org", @@ -861,10 +861,25 @@ } } }, - "/statistics": { + "/reports/stops": { "get": { - "summary": "Fetch server Statistics", + "summary": "Fetch a list of ReportStops within the time period for the Devices or Groups", + "description": "At least one _deviceId_ or one _groupId_ must be passed", + "consumes": [ + "application/json", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + ], + "produces": [ + "application/json", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + ], "parameters": [ + { + "$ref": "#/parameters/deviceIdArray" + }, + { + "$ref": "#/parameters/groupIdArray" + }, { "$ref": "#/parameters/fromTime" }, @@ -878,20 +893,22 @@ "schema": { "type": "array", "items": { - "$ref": "#/definitions/Statistics" + "$ref": "#/definitions/ReportStops" } } } } } }, - "/attributes/aliases": { + "/statistics": { "get": { - "summary": "Fetch a list of AttributeAlias", - "description": "Without params, it returns a list of AttributeAlias from all the user's Devices", + "summary": "Fetch server Statistics", "parameters": [ { - "$ref": "#/parameters/deviceId" + "$ref": "#/parameters/fromTime" + }, + { + "$ref": "#/parameters/toTime" } ], "responses": { @@ -900,61 +917,11 @@ "schema": { "type": "array", "items": { - "$ref": "#/definitions/AttributeAlias" + "$ref": "#/definitions/Statistics" } } } } - }, - "post": { - "summary": "Set an AttributeAlias", - "parameters": [ - { - "$ref": "#/parameters/AttributeAlias" - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/AttributeAlias" - } - } - } - } - }, - "/attributes/aliases/{id}": { - "put": { - "summary": "Update an AttributeAlias", - "parameters": [ - { - "$ref": "#/parameters/entityId" - }, - { - "$ref": "#/parameters/AttributeAlias" - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/AttributeAlias" - } - } - } - }, - "delete": { - "summary": "Delete an AttributeAlias", - "parameters": [ - { - "$ref": "#/parameters/entityId" - } - ], - "responses": { - "204": { - "description": "No Content" - } - } } }, "/calendars": { @@ -1714,22 +1681,6 @@ } } }, - "AttributeAlias": { - "properties": { - "id": { - "type": "integer" - }, - "deviceId": { - "type": "integer" - }, - "attribute": { - "type": "string" - }, - "alias": { - "type": "string" - } - } - }, "DeviceTotalDistance": { "properties": { "deviceId": { @@ -1868,14 +1819,6 @@ "$ref": "#/definitions/Geofence" } }, - "AttributeAlias": { - "name": "body", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/AttributeAlias" - } - }, "Calendar": { "name": "body", "in": "body", -- cgit v1.2.3 From c721d6ccdd37bd6833887c40a8d9aa857b0fc6b6 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 13 Sep 2017 16:17:56 +0500 Subject: Update swagger documentation to recent changes --- swagger.json | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 138 insertions(+), 15 deletions(-) (limited to 'swagger.json') diff --git a/swagger.json b/swagger.json index eba9e0910..762ef3525 100644 --- a/swagger.json +++ b/swagger.json @@ -22,8 +22,116 @@ ], "paths": { "/commands": { + "get": { + "summary": "Fetch a list of Saved Commands", + "description": "Without params, it returns a list of Drivers the user has access to", + "parameters": [ + { + "$ref": "#/parameters/all" + }, + { + "$ref": "#/parameters/userId" + }, + { + "$ref": "#/parameters/deviceId" + }, + { + "$ref": "#/parameters/groupId" + }, + { + "$ref": "#/parameters/refresh" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/Command" + } + } + } + } + }, + "post": { + "summary": "Create a Saved Command", + "parameters": [ + { + "$ref": "#/parameters/Command" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/Command" + } + } + } + } + }, + "/commands/{id}": { + "put": { + "summary": "Update a Saved Command", + "parameters": [ + { + "$ref": "#/parameters/entityId" + }, + { + "$ref": "#/parameters/Command" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/Command" + } + } + } + }, + "delete": { + "summary": "Delete a Saved Command", + "parameters": [ + { + "$ref": "#/parameters/entityId" + } + ], + "responses": { + "204": { + "description": "No Content" + } + } + } + }, + "/commands/send": { + "get": { + "summary": "Fetch a list of Saved Commands supported by Device at the moment", + "description": "Return a list of saved commands linked to Device and its groups, filtered by current Device state and protocol support", + "parameters": [ + { + "$ref": "#/parameters/deviceId" + }, + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/Command" + } + } + }, + "400": { + "description": "Could happen when the user doesn't have permission for the device" + } + } + }, "post": { "summary": "Dispatch commands to device", + "description": "Dispatch a new command or Saved Command if _body.id_ set", "parameters": [ { "name": "body", @@ -570,13 +678,17 @@ }, "/commandtypes": { "get": { - "summary": "Fetch a list of available Commands for the Device", + "summary": "Fetch a list of available Commands for the Device or all possible Commands if Device ommited", "parameters": [ { "name": "deviceId", "in": "query", - "required": true, "type": "integer" + }, + { + "name": "textChannel", + "in": "query", + "type": "boolean" } ], "responses": { @@ -590,7 +702,7 @@ } }, "400": { - "description": "Could happen when trying to fetch from an offline device or the user does not have permission" + "description": "Could happen when trying to fetch from a device the user does not have permission" } } } @@ -1249,12 +1361,6 @@ "map": { "type": "string" }, - "distanceUnit": { - "type": "string" - }, - "speedUnit": { - "type": "string" - }, "latitude": { "type": "number" }, @@ -1290,6 +1396,9 @@ "deviceReadonly": { "type": "boolean" }, + "limitCommands": { + "type": "boolean" + }, "token": { "type": "string" }, @@ -1307,6 +1416,12 @@ "readonly": { "type": "boolean" }, + "deviceReadonly": { + "type": "boolean" + }, + "limitCommands": { + "type": "boolean" + }, "map": { "type": "string" }, @@ -1316,12 +1431,6 @@ "mapUrl": { "type": "string" }, - "distanceUnit": { - "type": "string" - }, - "speedUnit": { - "type": "string" - }, "latitude": { "type": "number" }, @@ -1348,9 +1457,15 @@ }, "Command": { "properties": { + "id": { + "type": "integer" + }, "deviceId": { "type": "integer" }, + "description": { + "type": "string" + }, "type": { "type": "string" }, @@ -1843,6 +1958,14 @@ "$ref": "#/definitions/Driver" } }, + "Command": { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/Command" + } + }, "deviceIdArray": { "name": "deviceId", "in": "query", -- cgit v1.2.3 From 9519b653a8cef055366b0903fdd371c8f86d1206 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 19 Sep 2017 10:42:07 +0500 Subject: Implement per device Notifications --- schema/changelog-3.15.xml | 50 ++++++ src/org/traccar/Context.java | 3 + src/org/traccar/api/BaseObjectResource.java | 2 - .../traccar/api/resource/CommandTypeResource.java | 4 +- .../traccar/api/resource/NotificationResource.java | 34 ++-- src/org/traccar/api/resource/UserResource.java | 3 - src/org/traccar/database/CommandsManager.java | 16 +- src/org/traccar/database/DataManager.java | 3 + src/org/traccar/database/NotificationManager.java | 187 +++++---------------- src/org/traccar/database/PermissionsManager.java | 11 +- src/org/traccar/model/CommandType.java | 33 ---- src/org/traccar/model/Notification.java | 10 +- src/org/traccar/model/Typed.java | 33 ++++ swagger.json | 110 ++++++++++-- 14 files changed, 264 insertions(+), 235 deletions(-) delete mode 100644 src/org/traccar/model/CommandType.java create mode 100644 src/org/traccar/model/Typed.java (limited to 'swagger.json') diff --git a/schema/changelog-3.15.xml b/schema/changelog-3.15.xml index f6ed306dc..9756fe696 100644 --- a/schema/changelog-3.15.xml +++ b/schema/changelog-3.15.xml @@ -83,5 +83,55 @@ + + + + + + + + + + + + + + + + + + + INSERT INTO user_notification (notificationid, userid) SELECT id AS notificationid, userid FROM notifications; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 210d52429..340eb742c 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -62,6 +62,7 @@ import org.traccar.model.Device; import org.traccar.model.Driver; import org.traccar.model.Geofence; import org.traccar.model.Group; +import org.traccar.model.Notification; import org.traccar.model.User; import org.traccar.geolocation.GoogleGeolocationProvider; import org.traccar.geolocation.GeolocationProvider; @@ -433,6 +434,8 @@ public final class Context { return (BaseObjectManager) driversManager; } else if (clazz.equals(Command.class)) { return (BaseObjectManager) commandsManager; + } else if (clazz.equals(Notification.class)) { + return (BaseObjectManager) notificationManager; } return null; } diff --git a/src/org/traccar/api/BaseObjectResource.java b/src/org/traccar/api/BaseObjectResource.java index f0f31a154..634957a49 100644 --- a/src/org/traccar/api/BaseObjectResource.java +++ b/src/org/traccar/api/BaseObjectResource.java @@ -111,8 +111,6 @@ public abstract class BaseObjectResource extends BaseResour if (baseClass.equals(Group.class) || baseClass.equals(Device.class)) { Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); Context.getPermissionsManager().refreshAllExtendedPermissions(); - } else if (baseClass.equals(User.class) && Context.getNotificationManager() != null) { - Context.getNotificationManager().refresh(); } return Response.ok(entity).build(); } diff --git a/src/org/traccar/api/resource/CommandTypeResource.java b/src/org/traccar/api/resource/CommandTypeResource.java index 30f9300cb..0a904bd8a 100644 --- a/src/org/traccar/api/resource/CommandTypeResource.java +++ b/src/org/traccar/api/resource/CommandTypeResource.java @@ -18,7 +18,7 @@ package org.traccar.api.resource; import org.traccar.Context; import org.traccar.api.BaseResource; -import org.traccar.model.CommandType; +import org.traccar.model.Typed; import javax.ws.rs.Consumes; import javax.ws.rs.GET; @@ -34,7 +34,7 @@ import java.util.Collection; public class CommandTypeResource extends BaseResource { @GET - public Collection get(@QueryParam("deviceId") long deviceId, + public Collection get(@QueryParam("deviceId") long deviceId, @QueryParam("textChannel") boolean textChannel) { if (deviceId != 0) { Context.getPermissionsManager().checkDevice(getUserId(), deviceId); diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java index dee972607..540f02926 100644 --- a/src/org/traccar/api/resource/NotificationResource.java +++ b/src/org/traccar/api/resource/NotificationResource.java @@ -15,7 +15,6 @@ */ package org.traccar.api.resource; -import java.sql.SQLException; import java.util.Collection; import javax.mail.MessagingException; @@ -24,14 +23,14 @@ import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.traccar.Context; -import org.traccar.api.BaseResource; +import org.traccar.api.ExtendedObjectResource; import org.traccar.model.Event; import org.traccar.model.Notification; +import org.traccar.model.Typed; import org.traccar.notification.NotificationMail; import org.traccar.notification.NotificationSms; @@ -40,34 +39,23 @@ import com.cloudhopper.smpp.type.SmppChannelException; import com.cloudhopper.smpp.type.SmppTimeoutException; import com.cloudhopper.smpp.type.UnrecoverablePduException; -@Path("users/notifications") +@Path("notifications") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class NotificationResource extends BaseResource { +public class NotificationResource extends ExtendedObjectResource { - @GET - public Collection get(@QueryParam("all") boolean all, - @QueryParam("userId") long userId) throws SQLException { - if (all) { - return Context.getNotificationManager().getAllNotifications(); - } - if (userId == 0) { - userId = getUserId(); - } - Context.getPermissionsManager().checkUser(getUserId(), userId); - return Context.getNotificationManager().getAllUserNotifications(userId); + public NotificationResource() { + super(Notification.class); } - @POST - public Response update(Notification entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getNotificationManager().updateNotification(entity); - return Response.ok(entity).build(); + @GET + @Path("types") + public Collection get() { + return Context.getNotificationManager().getAllNotificationTypes(); } - @Path("test") @POST + @Path("test") public Response testMessage() throws MessagingException, RecoverablePduException, UnrecoverablePduException, SmppTimeoutException, SmppChannelException, InterruptedException { NotificationMail.sendMailSync(getUserId(), new Event("test", 0), null); diff --git a/src/org/traccar/api/resource/UserResource.java b/src/org/traccar/api/resource/UserResource.java index 0eb328ab5..0f6f6edba 100644 --- a/src/org/traccar/api/resource/UserResource.java +++ b/src/org/traccar/api/resource/UserResource.java @@ -85,9 +85,6 @@ public class UserResource extends BaseObjectResource { Context.getDataManager().linkObject(User.class, getUserId(), ManagedUser.class, entity.getId(), true); } Context.getUsersManager().refreshUserItems(); - if (Context.getNotificationManager() != null) { - Context.getNotificationManager().refresh(); - } return Response.ok(entity).build(); } diff --git a/src/org/traccar/database/CommandsManager.java b/src/org/traccar/database/CommandsManager.java index deb802b29..521a2e1d1 100644 --- a/src/org/traccar/database/CommandsManager.java +++ b/src/org/traccar/database/CommandsManager.java @@ -26,7 +26,7 @@ import org.traccar.BaseProtocol; import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Command; -import org.traccar.model.CommandType; +import org.traccar.model.Typed; import org.traccar.model.Position; public class CommandsManager extends ExtendedObjectManager { @@ -102,29 +102,29 @@ public class CommandsManager extends ExtendedObjectManager { return result; } - public Collection getCommandTypes(long deviceId, boolean textChannel) { - List result = new ArrayList<>(); + public Collection getCommandTypes(long deviceId, boolean textChannel) { + List result = new ArrayList<>(); Position lastPosition = Context.getIdentityManager().getLastPosition(deviceId); if (lastPosition != null) { BaseProtocol protocol = Context.getServerManager().getProtocol(lastPosition.getProtocol()); Collection commands; commands = textChannel ? protocol.getSupportedTextCommands() : protocol.getSupportedDataCommands(); for (String commandKey : commands) { - result.add(new CommandType(commandKey)); + result.add(new Typed(commandKey)); } } else { - result.add(new CommandType(Command.TYPE_CUSTOM)); + result.add(new Typed(Command.TYPE_CUSTOM)); } return result; } - public Collection getAllCommandTypes() { - List result = new ArrayList<>(); + public Collection getAllCommandTypes() { + List result = new ArrayList<>(); Field[] fields = Command.class.getDeclaredFields(); for (Field field : fields) { if (Modifier.isStatic(field.getModifiers()) && field.getName().startsWith("TYPE_")) { try { - result.add(new CommandType(field.get(null).toString())); + result.add(new Typed(field.get(null).toString())); } catch (IllegalArgumentException | IllegalAccessException error) { Log.warning(error); } diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 261541b4d..e88ff7f0d 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -48,6 +48,7 @@ import org.traccar.model.Event; import org.traccar.model.Geofence; import org.traccar.model.Group; import org.traccar.model.ManagedUser; +import org.traccar.model.Notification; import org.traccar.model.Permission; import org.traccar.model.BaseModel; import org.traccar.model.Calendar; @@ -393,6 +394,8 @@ public class DataManager { return Calendar.class; case "command": return Command.class; + case "notification": + return Notification.class; default: throw new ClassNotFoundException(); } diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index 98cae3499..a6ca0ea66 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -1,5 +1,6 @@ /* * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2017 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,57 +19,70 @@ package org.traccar.database; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.sql.SQLException; -import java.util.Collection; -import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import java.util.concurrent.locks.ReadWriteLock; -import java.util.concurrent.locks.ReentrantReadWriteLock; import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Event; import org.traccar.model.Notification; import org.traccar.model.Position; +import org.traccar.model.Typed; import org.traccar.notification.NotificationMail; import org.traccar.notification.NotificationSms; -public class NotificationManager { - - private final DataManager dataManager; - - private final Map> userNotifications = new HashMap<>(); - - private final ReadWriteLock notificationsLock = new ReentrantReadWriteLock(); +public class NotificationManager extends ExtendedObjectManager { public NotificationManager(DataManager dataManager) { - this.dataManager = dataManager; - refresh(); + super(dataManager, Notification.class); + } + + private Set getEffectiveNotifications(long userId, long deviceId) { + Set result = new HashSet<>(); + Set deviceNotifications = getAllDeviceItems(deviceId); + for (long itemId : getUserItems(userId)) { + if (getById(itemId).getAlways() || deviceNotifications.contains(itemId)) { + result.add(itemId); + } + } + return result; } public void updateEvent(Event event, Position position) { try { - dataManager.addObject(event); + getDataManager().addObject(event); } catch (SQLException error) { Log.warning(error); } - Set users = Context.getPermissionsManager().getDeviceUsers(event.getDeviceId()); + long deviceId = event.getDeviceId(); + Set users = Context.getPermissionsManager().getDeviceUsers(deviceId); for (long userId : users) { if (event.getGeofenceId() == 0 || Context.getGeofenceManager() != null && Context.getGeofenceManager().checkItemPermission(userId, event.getGeofenceId())) { - Notification notification = getUserNotificationByType(userId, event.getType()); - if (notification != null) { - if (notification.getWeb()) { - Context.getConnectionManager().updateEvent(userId, event); - } - if (notification.getMail()) { - NotificationMail.sendMailAsync(userId, event, position); + boolean webSent = false; + boolean mailSent = false; + boolean smsSent = Context.getSmppManager() == null; + for (long notificationId : getEffectiveNotifications(userId, deviceId)) { + Notification notification = getById(notificationId); + if (getById(notificationId).getType().equals(event.getType())) { + if (!webSent && notification.getWeb()) { + Context.getConnectionManager().updateEvent(userId, event); + webSent = true; + } + if (!mailSent && notification.getMail()) { + NotificationMail.sendMailAsync(userId, event, position); + mailSent = true; + } + if (!smsSent && notification.getSms()) { + NotificationSms.sendSmsAsync(userId, event, position); + smsSent = true; + } } - if (notification.getSms()) { - NotificationSms.sendSmsAsync(userId, event, position); + if (webSent && mailSent && smsSent) { + break; } } } @@ -84,135 +98,18 @@ public class NotificationManager { } } - private Set getUserNotificationsUnsafe(long userId) { - if (!userNotifications.containsKey(userId)) { - userNotifications.put(userId, new HashSet()); - } - return userNotifications.get(userId); - } - - public Set getUserNotifications(long userId) { - notificationsLock.readLock().lock(); - try { - return getUserNotificationsUnsafe(userId); - } finally { - notificationsLock.readLock().unlock(); - } - } - - public final void refresh() { - if (dataManager != null) { - try { - notificationsLock.writeLock().lock(); - try { - userNotifications.clear(); - for (Notification notification : dataManager.getObjects(Notification.class)) { - getUserNotificationsUnsafe(notification.getUserId()).add(notification); - } - } finally { - notificationsLock.writeLock().unlock(); - } - } catch (SQLException error) { - Log.warning(error); - } - } - } - - public Notification getUserNotificationByType(long userId, String type) { - notificationsLock.readLock().lock(); - try { - for (Notification notification : getUserNotificationsUnsafe(userId)) { - if (notification.getType().equals(type)) { - return notification; - } - } - } finally { - notificationsLock.readLock().unlock(); - } - return null; - } - - public void updateNotification(Notification notification) { - Notification cachedNotification = getUserNotificationByType(notification.getUserId(), notification.getType()); - if (cachedNotification != null) { - if (cachedNotification.getWeb() != notification.getWeb() - || cachedNotification.getMail() != notification.getMail() - || cachedNotification.getSms() != notification.getSms()) { - if (!notification.getWeb() && !notification.getMail() && !notification.getSms()) { - try { - dataManager.removeObject(Notification.class, cachedNotification.getId()); - } catch (SQLException error) { - Log.warning(error); - } - notificationsLock.writeLock().lock(); - try { - getUserNotificationsUnsafe(notification.getUserId()).remove(cachedNotification); - } finally { - notificationsLock.writeLock().unlock(); - } - } else { - notificationsLock.writeLock().lock(); - try { - cachedNotification.setWeb(notification.getWeb()); - cachedNotification.setMail(notification.getMail()); - cachedNotification.setSms(notification.getSms()); - cachedNotification.setAttributes(notification.getAttributes()); - } finally { - notificationsLock.writeLock().unlock(); - } - try { - dataManager.updateObject(cachedNotification); - } catch (SQLException error) { - Log.warning(error); - } - } - } else { - notification.setId(cachedNotification.getId()); - } - } else if (notification.getWeb() || notification.getMail() || notification.getSms()) { - try { - dataManager.addObject(notification); - } catch (SQLException error) { - Log.warning(error); - } - notificationsLock.writeLock().lock(); - try { - getUserNotificationsUnsafe(notification.getUserId()).add(notification); - } finally { - notificationsLock.writeLock().unlock(); - } - } - } - - public Set getAllNotifications() { - Set notifications = new HashSet<>(); - long id = 1; + public Set getAllNotificationTypes() { + Set types = new HashSet<>(); Field[] fields = Event.class.getDeclaredFields(); for (Field field : fields) { if (Modifier.isStatic(field.getModifiers()) && field.getName().startsWith("TYPE_")) { try { - Notification notification = new Notification(); - notification.setType(field.get(null).toString()); - notification.setId(id++); - notifications.add(notification); + types.add(new Typed(field.get(null).toString())); } catch (IllegalArgumentException | IllegalAccessException error) { Log.warning(error); } } } - return notifications; + return types; } - - public Collection getAllUserNotifications(long userId) { - Map notifications = new HashMap<>(); - for (Notification notification : getAllNotifications()) { - notification.setUserId(userId); - notifications.put(notification.getType(), notification); - } - for (Notification notification : getUserNotifications(userId)) { - notifications.put(notification.getType(), notification); - } - return notifications.values(); - } - } diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 0d9c780a6..07b60ba58 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -26,6 +26,7 @@ import org.traccar.model.Driver; import org.traccar.model.Geofence; import org.traccar.model.Group; import org.traccar.model.ManagedUser; +import org.traccar.model.Notification; import org.traccar.model.Permission; import org.traccar.model.Server; import org.traccar.model.User; @@ -320,6 +321,8 @@ public class PermissionsManager { manager = Context.getCalendarManager(); } else if (object.equals(Command.class)) { manager = Context.getCommandsManager(); + } else if (object.equals(Notification.class)) { + manager = Context.getNotificationManager(); } else { throw new IllegalArgumentException("Unknown object type"); } @@ -344,7 +347,7 @@ public class PermissionsManager { Context.getAttributesManager().refreshUserItems(); Context.getCommandsManager().refreshUserItems(); if (Context.getNotificationManager() != null) { - Context.getNotificationManager().refresh(); + Context.getNotificationManager().refreshUserItems(); } } @@ -375,6 +378,9 @@ public class PermissionsManager { Context.getCalendarManager().refreshUserItems(); } else if (permission.getPropertyClass().equals(Command.class)) { Context.getCommandsManager().refreshUserItems(); + } else if (permission.getPropertyClass().equals(Notification.class) + && Context.getNotificationManager() != null) { + Context.getNotificationManager().refreshUserItems(); } } else if (permission.getOwnerClass().equals(Device.class) || permission.getOwnerClass().equals(Group.class)) { if (permission.getPropertyClass().equals(Geofence.class) && Context.getGeofenceManager() != null) { @@ -385,6 +391,9 @@ public class PermissionsManager { Context.getAttributesManager().refreshExtendedPermissions(); } else if (permission.getPropertyClass().equals(Command.class)) { Context.getCommandsManager().refreshExtendedPermissions(); + } else if (permission.getPropertyClass().equals(Notification.class) + && Context.getNotificationManager() != null) { + Context.getNotificationManager().refreshExtendedPermissions(); } } } diff --git a/src/org/traccar/model/CommandType.java b/src/org/traccar/model/CommandType.java deleted file mode 100644 index 210316f71..000000000 --- a/src/org/traccar/model/CommandType.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2016 Gabor Somogyi (gabor.g.somogyi@gmail.com) - * - * 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.model; - -public class CommandType { - - private String type; - - public CommandType(String type) { - this.type = type; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } -} diff --git a/src/org/traccar/model/Notification.java b/src/org/traccar/model/Notification.java index e7bb69903..9d6034fff 100644 --- a/src/org/traccar/model/Notification.java +++ b/src/org/traccar/model/Notification.java @@ -17,14 +17,14 @@ package org.traccar.model; public class Notification extends ExtendedModel { - private long userId; + private boolean always; - public long getUserId() { - return userId; + public boolean getAlways() { + return always; } - public void setUserId(long userId) { - this.userId = userId; + public void setAlways(boolean always) { + this.always = always; } private String type; diff --git a/src/org/traccar/model/Typed.java b/src/org/traccar/model/Typed.java new file mode 100644 index 000000000..313ec7bcd --- /dev/null +++ b/src/org/traccar/model/Typed.java @@ -0,0 +1,33 @@ +/* + * Copyright 2016 Gabor Somogyi (gabor.g.somogyi@gmail.com) + * + * 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.model; + +public class Typed { + + private String type; + + public Typed(String type) { + this.type = type; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } +} diff --git a/swagger.json b/swagger.json index 762ef3525..5f9abb9b8 100644 --- a/swagger.json +++ b/swagger.json @@ -627,19 +627,25 @@ } } }, - "/users/notifications": { + "/notifications": { "get": { - "summary": "Fetch a list of Notification types", - "description": "Without params, it returns a list of the user's enabled Notifications", + "summary": "Fetch a list of Notifications", + "description": "Without params, it returns a list of Notifications the user has access to", "parameters": [ { - "name": "all", - "in": "query", - "description": "To fetch a list of all available Notifications", - "type": "boolean" + "$ref": "#/parameters/all" }, { "$ref": "#/parameters/userId" + }, + { + "$ref": "#/parameters/deviceId" + }, + { + "$ref": "#/parameters/groupId" + }, + { + "$ref": "#/parameters/refresh" } ], "responses": { @@ -655,16 +661,32 @@ } }, "post": { - "summary": "Set or unset a Notification", + "summary": "Create a Notification", "parameters": [ { - "name": "body", - "in": "body", - "required": true, + "$ref": "#/parameters/Notification" + } + ], + "responses": { + "200": { + "description": "OK", "schema": { "$ref": "#/definitions/Notification" } } + } + } + }, + "/notifications/{id}": { + "put": { + "summary": "Update a Notification", + "parameters": [ + { + "$ref": "#/parameters/entityId" + }, + { + "$ref": "#/parameters/Notification" + } ], "responses": { "200": { @@ -674,6 +696,50 @@ } } } + }, + "delete": { + "summary": "Delete a Notification", + "parameters": [ + { + "$ref": "#/parameters/entityId" + } + ], + "responses": { + "204": { + "description": "No Content" + } + } + } + }, + "/notifications/types": { + "get": { + "summary": "Fetch a list of available Notification types", + "parameters": [], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/NotificationType" + } + } + } + } + } + }, + "/notifications/test": { + "post": { + "summary": "Send test notification to current user via Email and SMS", + "parameters": [], + "responses": { + "204": { + "description": "Successful sending" + }, + "400": { + "description": "Could happen if sending has failed" + } + } } }, "/commandtypes": { @@ -1604,8 +1670,8 @@ "type": { "type": "string" }, - "userId": { - "type": "integer" + "always": { + "type": "boolean" }, "web": { "type": "boolean" @@ -1613,9 +1679,19 @@ "mail": { "type": "boolean" }, + "sms": { + "type": "boolean" + }, "attributes": {} } }, + "NotificationType": { + "properties": { + "type": { + "type": "string" + } + } + }, "Event": { "properties": { "id": { @@ -1966,6 +2042,14 @@ "$ref": "#/definitions/Command" } }, + "Notification": { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/Notification" + } + }, "deviceIdArray": { "name": "deviceId", "in": "query", -- cgit v1.2.3 From 9daa51652f28066534d42046961990e15b490562 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 20 Sep 2017 13:26:21 +0500 Subject: Move Commandtypes API to Commands --- src/org/traccar/api/resource/CommandResource.java | 12 +++++ .../traccar/api/resource/CommandTypeResource.java | 47 ---------------- swagger.json | 62 +++++++++++----------- 3 files changed, 43 insertions(+), 78 deletions(-) delete mode 100644 src/org/traccar/api/resource/CommandTypeResource.java (limited to 'swagger.json') diff --git a/src/org/traccar/api/resource/CommandResource.java b/src/org/traccar/api/resource/CommandResource.java index 6a258497f..7300840f0 100644 --- a/src/org/traccar/api/resource/CommandResource.java +++ b/src/org/traccar/api/resource/CommandResource.java @@ -19,6 +19,7 @@ import org.traccar.Context; import org.traccar.api.ExtendedObjectResource; import org.traccar.database.CommandsManager; import org.traccar.model.Command; +import org.traccar.model.Typed; import java.sql.SQLException; import java.util.Collection; @@ -72,4 +73,15 @@ public class CommandResource extends ExtendedObjectResource { return Response.ok(entity).build(); } + @GET + @Path("types") + public Collection get(@QueryParam("deviceId") long deviceId, + @QueryParam("textChannel") boolean textChannel) { + if (deviceId != 0) { + Context.getPermissionsManager().checkDevice(getUserId(), deviceId); + return Context.getCommandsManager().getCommandTypes(deviceId, textChannel); + } else { + return Context.getCommandsManager().getAllCommandTypes(); + } + } } diff --git a/src/org/traccar/api/resource/CommandTypeResource.java b/src/org/traccar/api/resource/CommandTypeResource.java deleted file mode 100644 index 0a904bd8a..000000000 --- a/src/org/traccar/api/resource/CommandTypeResource.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2016 Gabor Somogyi (gabor.g.somogyi@gmail.com) - * Copyright 2017 Andrey Kunitsyn (andrey@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.api.resource; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.Typed; - -import javax.ws.rs.Consumes; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.MediaType; -import java.util.Collection; - -@Path("commandtypes") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class CommandTypeResource extends BaseResource { - - @GET - public Collection get(@QueryParam("deviceId") long deviceId, - @QueryParam("textChannel") boolean textChannel) { - if (deviceId != 0) { - Context.getPermissionsManager().checkDevice(getUserId(), deviceId); - return Context.getCommandsManager().getCommandTypes(deviceId, textChannel); - } else { - return Context.getCommandsManager().getAllCommandTypes(); - } - } - -} diff --git a/swagger.json b/swagger.json index 5f9abb9b8..120544798 100644 --- a/swagger.json +++ b/swagger.json @@ -155,6 +155,37 @@ } } }, + "/commands/types": { + "get": { + "summary": "Fetch a list of available Commands for the Device or all possible Commands if Device ommited", + "parameters": [ + { + "name": "deviceId", + "in": "query", + "type": "integer" + }, + { + "name": "textChannel", + "in": "query", + "type": "boolean" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/CommandType" + } + } + }, + "400": { + "description": "Could happen when trying to fetch from a device the user does not have permission" + } + } + } + }, "/devices": { "get": { "summary": "Fetch a list of Devices", @@ -742,37 +773,6 @@ } } }, - "/commandtypes": { - "get": { - "summary": "Fetch a list of available Commands for the Device or all possible Commands if Device ommited", - "parameters": [ - { - "name": "deviceId", - "in": "query", - "type": "integer" - }, - { - "name": "textChannel", - "in": "query", - "type": "boolean" - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/CommandType" - } - } - }, - "400": { - "description": "Could happen when trying to fetch from a device the user does not have permission" - } - } - } - }, "/geofences": { "get": { "summary": "Fetch a list of Geofences", -- cgit v1.2.3 From a2f215fe63bd313bfac2a894c170673ccbc252c4 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 19 Oct 2017 10:03:02 +0500 Subject: Implement buffered commands --- src/org/traccar/api/resource/CommandResource.java | 8 ++- src/org/traccar/database/CommandsManager.java | 60 ++++++++++++++--------- src/org/traccar/database/ConnectionManager.java | 5 +- swagger.json | 14 ++++-- 4 files changed, 56 insertions(+), 31 deletions(-) (limited to 'swagger.json') diff --git a/src/org/traccar/api/resource/CommandResource.java b/src/org/traccar/api/resource/CommandResource.java index 8da9f8447..996c15daf 100644 --- a/src/org/traccar/api/resource/CommandResource.java +++ b/src/org/traccar/api/resource/CommandResource.java @@ -62,15 +62,19 @@ public class CommandResource extends ExtendedObjectResource { Context.getPermissionsManager().checkReadonly(getUserId()); long deviceId = entity.getDeviceId(); long id = entity.getId(); + boolean sent; if (deviceId != 0 && id != 0) { Context.getPermissionsManager().checkPermission(Command.class, getUserId(), id); Context.getPermissionsManager().checkDevice(getUserId(), deviceId); Context.getPermissionsManager().checkUserDeviceCommand(getUserId(), deviceId, id); - Context.getCommandsManager().sendCommand(id, deviceId); + sent = Context.getCommandsManager().sendCommand(id, deviceId); } else { Context.getPermissionsManager().checkLimitCommands(getUserId()); Context.getPermissionsManager().checkDevice(getUserId(), deviceId); - Context.getCommandsManager().sendCommand(entity); + sent = Context.getCommandsManager().sendCommand(entity, deviceId); + } + if (!sent) { + return Response.accepted(entity).build(); } return Response.ok(entity).build(); } diff --git a/src/org/traccar/database/CommandsManager.java b/src/org/traccar/database/CommandsManager.java index 521a2e1d1..624fe56cd 100644 --- a/src/org/traccar/database/CommandsManager.java +++ b/src/org/traccar/database/CommandsManager.java @@ -21,6 +21,10 @@ import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; import org.traccar.BaseProtocol; import org.traccar.Context; @@ -31,26 +35,22 @@ import org.traccar.model.Position; public class CommandsManager extends ExtendedObjectManager { - private boolean fallbackToText; + private final Map> deviceQueues = new ConcurrentHashMap<>(); public CommandsManager(DataManager dataManager) { super(dataManager, Command.class); - fallbackToText = Context.getConfig().getBoolean("command.fallbackToSms"); } public boolean checkDeviceCommand(long deviceId, long commandId) { return !getAllDeviceItems(deviceId).contains(commandId); } - public void sendCommand(Command command) throws Exception { - sendCommand(command, command.getDeviceId(), fallbackToText); + public boolean sendCommand(long commandId, long deviceId) throws Exception { + return sendCommand(getById(commandId), deviceId); } - public void sendCommand(long commandId, long deviceId) throws Exception { - sendCommand(getById(commandId), deviceId, false); - } - - public void sendCommand(Command command, long deviceId, boolean fallbackToText) throws Exception { + public boolean sendCommand(Command command, long deviceId) throws Exception { + boolean sent = true; if (command.getTextChannel()) { Position lastPosition = Context.getIdentityManager().getLastPosition(deviceId); String phone = Context.getIdentityManager().getById(deviceId).getPhone(); @@ -71,32 +71,26 @@ public class CommandsManager extends ExtendedObjectManager { if (activeDevice != null) { activeDevice.sendCommand(command); } else { - if (fallbackToText) { - command.setTextChannel(true); - sendCommand(command, deviceId, false); - } else { - throw new RuntimeException("Device is not online"); - } + sent = !getDeviceQueue(deviceId).add(command); } } + return sent; } public Collection getSupportedCommands(long deviceId) { List result = new ArrayList<>(); Position lastPosition = Context.getIdentityManager().getLastPosition(deviceId); - boolean online = Context.getConnectionManager().getActiveDevice(deviceId) != null; for (long commandId : getAllDeviceItems(deviceId)) { Command command = getById(commandId); - if (command.getTextChannel() || online) { - if (lastPosition != null) { - BaseProtocol protocol = Context.getServerManager().getProtocol(lastPosition.getProtocol()); - if (protocol.getSupportedTextCommands().contains(command.getType()) - || online && protocol.getSupportedDataCommands().contains(command.getType())) { - result.add(commandId); - } - } else if (command.getType().equals(Command.TYPE_CUSTOM)) { + if (lastPosition != null) { + BaseProtocol protocol = Context.getServerManager().getProtocol(lastPosition.getProtocol()); + if ((command.getTextChannel() + ? protocol.getSupportedTextCommands() : protocol.getSupportedDataCommands()) + .contains(command.getType())) { result.add(commandId); } + } else if (command.getType().equals(Command.TYPE_CUSTOM)) { + result.add(commandId); } } return result; @@ -133,4 +127,22 @@ public class CommandsManager extends ExtendedObjectManager { return result; } + private Queue getDeviceQueue(long deviceId) { + if (!deviceQueues.containsKey(deviceId)) { + deviceQueues.put(deviceId, new ConcurrentLinkedQueue()); + } + return deviceQueues.get(deviceId); + } + + public void sendQueuedCommands(ActiveDevice activeDevice) { + Queue deviceQueue = deviceQueues.get(activeDevice.getDeviceId()); + if (deviceQueue != null) { + Command command = deviceQueue.poll(); + while (command != null) { + activeDevice.sendCommand(command); + command = deviceQueue.poll(); + } + } + } + } diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java index de11db21b..e5a7a272f 100644 --- a/src/org/traccar/database/ConnectionManager.java +++ b/src/org/traccar/database/ConnectionManager.java @@ -57,7 +57,9 @@ public class ConnectionManager { } public void addActiveDevice(long deviceId, Protocol protocol, Channel channel, SocketAddress remoteAddress) { - activeDevices.put(deviceId, new ActiveDevice(deviceId, protocol, channel, remoteAddress)); + ActiveDevice activeDevice = new ActiveDevice(deviceId, protocol, channel, remoteAddress); + activeDevices.put(deviceId, activeDevice); + Context.getCommandsManager().sendQueuedCommands(activeDevice); } public void removeActiveDevice(Channel channel) { @@ -122,6 +124,7 @@ public class ConnectionManager { public void run(Timeout timeout) throws Exception { if (!timeout.isCancelled()) { updateDevice(deviceId, Device.STATUS_UNKNOWN, null); + activeDevices.remove(deviceId); } } }, deviceTimeout, TimeUnit.MILLISECONDS)); diff --git a/swagger.json b/swagger.json index 120544798..82f34ac1d 100644 --- a/swagger.json +++ b/swagger.json @@ -108,11 +108,11 @@ "/commands/send": { "get": { "summary": "Fetch a list of Saved Commands supported by Device at the moment", - "description": "Return a list of saved commands linked to Device and its groups, filtered by current Device state and protocol support", + "description": "Return a list of saved commands linked to Device and its groups, filtered by current Device protocol support", "parameters": [ { "$ref": "#/parameters/deviceId" - }, + } ], "responses": { "200": { @@ -144,13 +144,19 @@ ], "responses": { "200": { - "description": "OK", + "description": "Command sent", + "schema": { + "$ref": "#/definitions/Command" + } + }, + "202": { + "description": "Command queued", "schema": { "$ref": "#/definitions/Command" } }, "400": { - "description": "Could happen when dispatching to a device that is offline, the user doesn't have permission or an incorrect command _type_ for the device" + "description": "Could happen when the user doesn't have permission or an incorrect command _type_ for the device" } } } -- cgit v1.2.3