@@ -8,6 +8,7 @@ const ModelError = require('../modelError');
88const { inArray, arrayContains, sql, eq, asc, ilike } = require ( 'drizzle-orm' ) ;
99const { insertMeasurement, insertMeasurements } = require ( '../measurement' ) ;
1010const SketchTemplater = require ( '@sensebox/sketch-templater' ) ;
11+ const { utcNow } = require ( '../utils' ) ;
1112
1213const { max_boxes : pagination_max_boxes } = require ( 'config' ) . get ( 'openSenseMap-API-models.pagination' ) ;
1314
@@ -274,41 +275,6 @@ const saveMeasurements = async function saveMeasurements (device, measurements)
274275 await insertMeasurements ( measurements ) ;
275276} ;
276277
277- async function updateLocation ( deviceId , coords , timestamp = utcNow ( ) ) {
278- if ( ! coords || coords . length !== 2 ) {
279- throw new Error ( 'Invalid coordinates provided' ) ;
280- }
281-
282- const [ longitude , latitude ] = coords ;
283-
284- // Check if the location already exists
285- let existingLocation = await db . query . location . findFirst ( {
286- where : eq ( location . location , `POINT(${ longitude } ${ latitude } )` ) ,
287- } ) ;
288-
289- if ( ! existingLocation ) {
290- // Insert new location if not found
291- const newLocation = await db . insert ( location ) . values ( {
292- location : `POINT(${ longitude } ${ latitude } )` ,
293- } ) . returning ( ) ;
294- existingLocation = newLocation [ 0 ] ;
295- }
296-
297- // Insert into device_to_location linking table
298- await db . insert ( deviceToLocation ) . values ( {
299- deviceId,
300- locationId : existingLocation . id ,
301- time : timestamp ,
302- } ) ;
303-
304- // Update the device's current location
305- await db . update ( device )
306- . set ( { latitude, longitude, updatedAt : timestamp } )
307- . where ( eq ( device . id , deviceId ) ) ;
308-
309- return existingLocation ;
310- }
311-
312278const updateDevice = async function updateDevice ( deviceId , args ) {
313279 const {
314280 mqtt : {
@@ -410,15 +376,43 @@ const updateDevice = async function updateDevice (deviceId, args) {
410376 // box.addAddon(addonToAdd);
411377 // }
412378
413- // TODO: run location update logic, if a location was provided.
414- // const locPromise = location
415- // ? box
416- // .updateLocation(location)
417- // .then((loc) => box.set({ currentLocation: loc }))
418- // : Promise.resolve();
419- // update location
379+ // run location update logic, if a location was provided.
380+ // if the provided location already exists, just update the relation
381+ // if the location does not exist, create it and update
420382 if ( location ) {
421- await updateLocation ( deviceId , location ) ;
383+ const [ geometry ] = await db
384+ . insert ( locationTable )
385+ . values ( {
386+ location : sql `ST_SetSRID(ST_MakePoint(${ location [ 1 ] } , ${ location [ 0 ] } ), 4326)`
387+ } )
388+ . onConflictDoNothing ( )
389+ . returning ( { id : locationTable . id } ) ;
390+
391+ if ( geometry ) {
392+ // update location relation (update location id of device)
393+ await db
394+ . update ( deviceToLocationTable )
395+ . set ( { locationId : geometry . id } )
396+ . where ( eq ( deviceToLocationTable . deviceId , deviceId ) ) ;
397+ } else {
398+ // Get location id
399+ const geom = await db . query . locationTable . findFirst ( {
400+ columns : {
401+ id : true
402+ } ,
403+ where : sql `ST_Equals(${ locationTable . location } , ST_SetSRID(ST_MakePoint(${ location [ 1 ] } , ${ location [ 0 ] } ), 4326))`
404+ } ) ;
405+
406+ // update location relation (update location id of device)
407+ await db
408+ . update ( deviceToLocationTable )
409+ . set ( { locationId : geom . id } )
410+ . where ( eq ( deviceToLocationTable . deviceId , deviceId ) ) ;
411+ }
412+
413+ // also set latitude and longitude of device
414+ setColumns [ 'longitude' ] = location [ 0 ] ;
415+ setColumns [ 'latitude' ] = location [ 1 ] ;
422416 }
423417
424418 const device = await db
0 commit comments