Skip to content

Commit

Permalink
Umut new features (#18)
Browse files Browse the repository at this point in the history
* stuff

* broken phenomena-list

* promise try

* rov in phenomena list

* feat-image-upload

* imageupload

* devices and phenomena lists

* package-lock

Co-authored-by: Umut Tas <[email protected]>
Co-authored-by: fbruc03 <[email protected]>
  • Loading branch information
3 people authored Aug 20, 2021
1 parent 5d06e4a commit 6dc3257
Show file tree
Hide file tree
Showing 14 changed files with 5,397 additions and 94 deletions.
11 changes: 7 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var sensorsRouter = require('./routes/sensors');
var phenomenaRouter = require('./routes/phenomena');
var devicesRouter = require('./routes/devices');
var domainsRouter = require('./routes/domains');
var imageRouter = require('./routes/image');
var AuthController = require('./controllers/auth-controller');
var cors = require('cors')

Expand Down Expand Up @@ -43,16 +44,18 @@ app.use('/*', cors(corsOptions), function (req, res, next) {
next();
});

app.post('*', AuthController.isAuthenticated, function (req, res, next) {
console.log("LOCALS", res.locals.user.role);
next();
});
// app.post('*', AuthController.isAuthenticated, function (req, res, next) {
// console.log("LOCALS", res.locals.user.role);
// next();
// });

app.use('/', indexRouter);
app.use('/queries', queriesRouter);
app.use('/sensors', sensorsRouter);
app.use('/phenomena', phenomenaRouter);
app.use('/devices', devicesRouter);
app.use('/domains', domainsRouter);
app.use('/image', imageRouter);
app.use(express.static('owl'));


Expand Down
119 changes: 118 additions & 1 deletion controllers/devices-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ module.exports.getDevice = function (iri) {
`)
.execute()
.then(res => {
console.log(res);
res.results.bindings.push({
'iri':
{
Expand Down Expand Up @@ -222,6 +221,124 @@ module.exports.getHistoricDevice = function (iri) {
}


module.exports.getSensorsOfDevice = function (iri) {
var senphurl = 'http://www.opensensemap.org/SENPH#';
console.log("IRI",iri);
return client
.query(SPARQL`
SELECT ?sensor ?label ?description ?sensorElement
WHERE {
?sensor s:isSensorOf ${{ s: iri }}.
?sensor rdfs:label ?label.
?sensor rdfs:comment ?description.
?sensor s:hasElement ?sensorElement.
}`)
.execute({ format: { resource: 'sensor' } })
.then(async res => {

let mappedRes = [];
for (let binding of res.results.bindings) {
let allsensorElement = [];
if(binding.sensorElement){
for(let sensorElement of binding.sensorElement){
let sensorElementValue = await this.getSensorElement(sensorElement.value);
sensorElementValue.forEach(value => {
if (value){
let sensorElement = {phenomenon: value.phenomenon.value, unit: value.unit.value, acc: value.accuracy ? value.accuracy.value : undefined };
allsensorElement.push(sensorElement);
}
})
}
binding.sensorElement = allsensorElement;
}
mappedRes.push(binding);
}
return mappedRes;

}
)
.catch(function (error) {
console.dir(arguments, { depth: null })
console.log("Oh no, error!")
console.log(error)
});
}


//TODO: maybe move this function to sensors
module.exports.getAllSensorsOfAllDevices = function () {
var senphurl = 'http://www.opensensemap.org/SENPH#';
return client
.query(SPARQL`
SELECT ?sensor ?label ?description ?sensorElement
WHERE {
?sensor rdf:type s:sensor.
?sensor rdfs:label ?label.
?sensor rdfs:comment ?description.
?sensor s:hasElement ?sensorElement.
}`)
.execute({ format: { resource: 'sensor' } })
.then(async res => {

let mappedRes = [];
for (let binding of res.results.bindings) {
let allsensorElement = [];
if(binding.sensorElement){
for(let sensorElement of binding.sensorElement){
let sensorElementValue = await this.getSensorElement(sensorElement.value);
sensorElementValue.forEach(value => {
if (value){
let sensorElement = {phenomenon: value.phenomenon.value, unit: value.unit.value, acc: value.accuracy ? value.accuracy.value : undefined };
allsensorElement.push(sensorElement);
}
})
}
binding.sensorElement = allsensorElement;
}
mappedRes.push(binding);
}
return mappedRes;

}
)
.catch(function (error) {
console.dir(arguments, { depth: null })
console.log("Oh no, error!")
console.log(error)
});
}


module.exports.getSensorElement = function (iri) {
console.log(iri)
//still missing: ?domains rdfs:label ?domainsLabel.
// var senphurl = 'http://www.opensensemap.org/SENPH#';

var bindingsText = `
Select Distinct ?phenomenon ?unit ?accuracy
WHERE {
?iri s:canMeasure ?phenomenon.
?iri s:hasAccuracyUnit ?unit.
?iri s:accuracyValue ?accVal.
}
`;
return client
.query(bindingsText)
.bind({iri: {value: iri, type: 'uri'}})
.execute()
.then(res => {
console.log("BINDINGS", res.results.bindings)
return res.results.bindings;
})
.catch(function (error) {
console.dir(arguments, { depth: null })
console.log("Oh no, error!")
console.log(error)
});
}



// //update/add a new device @inputs required: label + language, description + language; optional: website, image, contact, compatible sensor
// module.exports.updateDevice = function (device) {
// var senphurl = 'http://www.opensensemap.org/SENPH#';
Expand Down
78 changes: 67 additions & 11 deletions controllers/phenomena-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const history_endpoint = `${fuseki_endpoint}/senph-history/sparql`;
const history_updatepoint = `${fuseki_endpoint}/senph-history/update`;

const Phenomenon = require('../models/Phenomenon');
const Phenomena = require('../models/Phenomena');
// const unitpoint = 'http://localhost:3030/uo/sparql';


Expand Down Expand Up @@ -54,18 +55,44 @@ const historyClient = new SparqlClient(history_endpoint, {
module.exports.getPhenomena = function () {
return client
.query(SPARQL`
SELECT ?phenomenonLabel ?phenomenon ?validation
SELECT ?phenomenonLabel ?phenomenon ?validation ?rovs ?unit
WHERE {
?phenomenon rdf:type s:phenomenon.
?phenomenon rdfs:label ?phenomenonLabel.
OPTIONAL{
?phenomenon rdf:type s:phenomenon.
?phenomenon rdfs:label ?phenomenonLabel.
OPTIONAL {
?phenomenon s:describedBy ?rovs.
?rovs s:describedBy ?unit.
}
OPTIONAL {
?phenomenon s:isValid ?validation.
}
}`)
} GROUP BY ?phenomenonLabel ?phenomenon ?validation ?rovs ?unit`)
.execute({ format: { resource: 'phenomenon' } })
.then(res => res.results.bindings)
.then(async res => {
let mappedRes = [];
for (let binding of res.results.bindings) {
let allrov = [];
for(let rov of binding.rovs){
let rovValue = await this.getROV(rov.value);
rovValue.forEach(value => {
if (value.unit){
let rov = {unit: value.unit.value, min: value.min.value, max: value.max.value};
console.log("ROV", rov);
allrov.push(rov);
}
})
}
binding.rovs = allrov;
mappedRes.push(binding);
}
return mappedRes;
})
.catch(function (error) {
console.log("Oh no, error!")
console.log("Oh no, error!", error)
});
}

Expand Down Expand Up @@ -201,7 +228,6 @@ module.exports.getPhenomenon = function (iri) {
Group BY ?sensors ?domain ?rov ?min ?max ?unit ?label ?description ?sensorlabel ?domainLabel ?unitLabel ?validation
ORDER BY ?sensors ?domain ?unit
`;
console.log(bindingsText)
return client
.query(bindingsText)
.bind('iri', { s: iri })
Expand All @@ -225,6 +251,34 @@ module.exports.getPhenomenon = function (iri) {
});
}

module.exports.getROV = function (iri) {
console.log(iri)
//still missing: ?domains rdfs:label ?domainsLabel.
// var senphurl = 'http://www.opensensemap.org/SENPH#';

var bindingsText = `
Select Distinct ?unit ?min ?max
WHERE {
?iri s:describedBy ?unit.
?iri s:min ?min.
?iri s:max ?max.
}
`;
return client
.query(bindingsText)
.bind({iri: {value: iri, type: 'uri'}})
.execute()
.then(res => {
console.log("BINDINGS", res.results.bindings)
return res.results.bindings;
})
.catch(function (error) {
console.dir(arguments, { depth: null })
console.log("Oh no, error!")
console.log(error)
});
}


//get a single historic phenomenon entry identified by its iri @returns the phenomenon's labels, descriptions, units it is described by, domains, sensors it can be measured by
module.exports.getHistoricPhenomenon = function (iri) {
Expand Down Expand Up @@ -518,6 +572,8 @@ module.exports.convertPhenomenonToJson = function (pheno) {

module.exports.convertPhenomenaToJson = function (phenos) {
//TODO: IMPLEMENT IF NEEDED
// return new Phenomenon(pheno);
return phenos
}
console.log("PHENOS", phenos)


return phenos.map(pheno => new Phenomena(pheno));
}
Loading

0 comments on commit 6dc3257

Please sign in to comment.