mirror of
https://github.com/traccar/traccar.git
synced 2025-01-09 04:07:38 +08:00
Add linking and unlinking of devices
This commit is contained in:
parent
6034374826
commit
843a370660
22
debug.xml
22
debug.xml
@ -101,7 +101,7 @@
|
||||
address VARCHAR(512),
|
||||
attributes VARCHAR(4096) NOT NULL,
|
||||
FOREIGN KEY (deviceId) REFERENCES device (id) ON DELETE CASCADE);
|
||||
|
||||
|
||||
CREATE INDEX position_deviceId_fixTime ON position (deviceId, fixTime);
|
||||
|
||||
CREATE TABLE server (
|
||||
@ -184,7 +184,7 @@
|
||||
<entry key='database.updateUserPassword'>
|
||||
UPDATE "user" SET hashedPassword = :hashedPassword, salt = :salt WHERE id = :id;
|
||||
</entry>
|
||||
|
||||
|
||||
<entry key='database.deleteUser'>
|
||||
DELETE FROM "user" WHERE id = :id;
|
||||
</entry>
|
||||
@ -192,33 +192,37 @@
|
||||
<entry key='database.getPermissionsAll'>
|
||||
SELECT userId, deviceId FROM user_device;
|
||||
</entry>
|
||||
|
||||
|
||||
<entry key='database.selectDevicesAll'>
|
||||
SELECT * FROM device;
|
||||
</entry>
|
||||
|
||||
|
||||
<entry key='database.selectDevices'>
|
||||
SELECT * FROM device d INNER JOIN user_device ud ON d.id = ud.deviceId WHERE ud.userId = :userId;
|
||||
</entry>
|
||||
|
||||
|
||||
<entry key='database.insertDevice'>
|
||||
INSERT INTO device (name, uniqueId) VALUES (:name, :uniqueId);
|
||||
</entry>
|
||||
|
||||
|
||||
<entry key='database.updateDevice'>
|
||||
UPDATE device SET name = :name, uniqueId = :uniqueId WHERE id = :id;
|
||||
</entry>
|
||||
|
||||
|
||||
<entry key='database.deleteDevice'>
|
||||
DELETE FROM device WHERE id = :id;
|
||||
</entry>
|
||||
|
||||
|
||||
<entry key='database.linkDevice'>
|
||||
INSERT INTO user_device (userId, deviceId) VALUES (:userId, :deviceId);
|
||||
</entry>
|
||||
|
||||
<entry key='database.unlinkDevice'>
|
||||
DELETE FROM user_device WHERE userId = :userId AND deviceId = :deviceId;
|
||||
</entry>
|
||||
|
||||
<entry key='database.selectPositions'>
|
||||
SELECT * FROM position WHERE deviceId = :deviceId AND fixTime BETWEEN :from AND :to ORDER BY fixTime;
|
||||
SELECT * FROM position WHERE deviceId = :deviceId AND fixTime BETWEEN :from AND :to ORDER BY fixTime;
|
||||
</entry>
|
||||
|
||||
<entry key='database.insertPosition'>
|
||||
|
@ -321,6 +321,14 @@ public class DataManager implements IdentityManager {
|
||||
AsyncServlet.sessionRefreshUser(userId);
|
||||
}
|
||||
|
||||
public void unlinkDevice(long userId, long deviceId) throws SQLException {
|
||||
QueryBuilder.create(dataSource, getQuery("database.unlinkDevice"))
|
||||
.setLong("userId", userId)
|
||||
.setLong("deviceId", deviceId)
|
||||
.executeUpdate();
|
||||
AsyncServlet.sessionRefreshUser(userId);
|
||||
}
|
||||
|
||||
public Collection<Position> getPositions(long userId, long deviceId, Date from, Date to) throws SQLException {
|
||||
return QueryBuilder.create(dataSource, getQuery("database.selectPositions"))
|
||||
.setLong("deviceId", deviceId)
|
||||
|
@ -38,6 +38,12 @@ public class DeviceServlet extends BaseServlet {
|
||||
case "/remove":
|
||||
remove(req, resp);
|
||||
break;
|
||||
case "/link":
|
||||
link(req, resp);
|
||||
break;
|
||||
case "/unlink":
|
||||
unlink(req, resp);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@ -87,4 +93,22 @@ public class DeviceServlet extends BaseServlet {
|
||||
sendResponse(resp.getWriter(), true);
|
||||
}
|
||||
|
||||
private void link(HttpServletRequest req, HttpServletResponse resp) throws Exception {
|
||||
Context.getPermissionsManager().checkAdmin(getUserId(req));
|
||||
Context.getDataManager().linkDevice(
|
||||
Long.parseLong(req.getParameter("userId")),
|
||||
Long.parseLong(req.getParameter("deviceId")));
|
||||
Context.getPermissionsManager().refresh();
|
||||
sendResponse(resp.getWriter(), true);
|
||||
}
|
||||
|
||||
private void unlink(HttpServletRequest req, HttpServletResponse resp) throws Exception {
|
||||
Context.getPermissionsManager().checkAdmin(getUserId(req));
|
||||
Context.getDataManager().unlinkDevice(
|
||||
Long.parseLong(req.getParameter("userId")),
|
||||
Long.parseLong(req.getParameter("deviceId")));
|
||||
Context.getPermissionsManager().refresh();
|
||||
sendResponse(resp.getWriter(), true);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -76,5 +76,24 @@ Ext.define('Traccar.Application', {
|
||||
|
||||
getPreference: function (key, defaultValue) {
|
||||
return this.getUser().get(key) || this.getServer().get(key) || defaultValue;
|
||||
},
|
||||
|
||||
getErrorHandler: function (scope, handler) {
|
||||
return function (options, success, response) {
|
||||
var result;
|
||||
if (success) {
|
||||
result = Ext.decode(response.responseText);
|
||||
if (!result.success) {
|
||||
Ext.Msg.alert(Strings.errorTitle, result.error);
|
||||
}
|
||||
handler.call(scope, options, success, response);
|
||||
} else {
|
||||
if (response.statusText) {
|
||||
Ext.Msg.alert(Strings.errorTitle, response.statusText);
|
||||
} else {
|
||||
Ext.Msg.alert(Strings.errorTitle, response.status.toString()); // TODO: text message
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -32,7 +32,8 @@ Ext.define('Traccar.view.UserDevices', {
|
||||
},
|
||||
|
||||
listeners: {
|
||||
selectionchange: 'onSelectionChange'
|
||||
beforedeselect: 'onBeforeDeselect',
|
||||
beforeselect: 'onBeforeSelect'
|
||||
},
|
||||
|
||||
columns: [{
|
||||
@ -43,4 +44,3 @@ Ext.define('Traccar.view.UserDevices', {
|
||||
dataIndex: 'uniqueId', flex: 1
|
||||
}]
|
||||
});
|
||||
32
|
@ -19,6 +19,7 @@ Ext.define('Traccar.view.UserDevicesController', {
|
||||
alias: 'controller.userDevices',
|
||||
|
||||
init: function () {
|
||||
this.userId = this.getView().user.getData().id;
|
||||
this.getView().getStore().load({
|
||||
scope: this,
|
||||
callback: function (records, operation, success) {
|
||||
@ -26,7 +27,7 @@ Ext.define('Traccar.view.UserDevicesController', {
|
||||
|
||||
userStore.load({
|
||||
params: {
|
||||
userId: this.getView().user.getData().id
|
||||
userId: this.userId
|
||||
},
|
||||
scope: this,
|
||||
callback: function (records, operation, success) {
|
||||
@ -43,7 +44,35 @@ Ext.define('Traccar.view.UserDevicesController', {
|
||||
});
|
||||
},
|
||||
|
||||
onSelectionChange: function (selected) {
|
||||
console.log(selected); // TODO
|
||||
onBeforeSelect: function (object, record, index) {
|
||||
Ext.Ajax.request({
|
||||
scope: this,
|
||||
url: '/api/device/link',
|
||||
params: {
|
||||
userId: this.userId,
|
||||
deviceId: record.getData().id
|
||||
},
|
||||
callback: Traccar.app.getErrorHandler(this, function (options, success, response) {
|
||||
if (!success) {
|
||||
// TODO deselect again
|
||||
}
|
||||
})
|
||||
});
|
||||
},
|
||||
|
||||
onBeforeDeselect: function (object, record, index) {
|
||||
Ext.Ajax.request({
|
||||
scope: this,
|
||||
url: '/api/device/unlink',
|
||||
params: {
|
||||
userId: this.userId,
|
||||
deviceId: record.getData().id
|
||||
},
|
||||
callback: Traccar.app.getErrorHandler(this, function (options, success, response) {
|
||||
if (!success) {
|
||||
// TODO select again
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user