Skip to content

Commit

Permalink
Daily reports for door unlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiaspalmer committed Mar 19, 2022
1 parent 54daa09 commit 0fa7c0c
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 1 deletion.
1 change: 1 addition & 0 deletions .meteor/packages
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ reactive-dict@1.3.0
accounts-ui@1.3.1
useraccounts:bootstrap
accounts-password@1.6.0
littledata:synced-cron
1 change: 1 addition & 0 deletions .meteor/versions
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ kadira:blaze-layout@2.3.0
kadira:flow-router@2.12.1
launch-screen@1.2.0
less@2.8.0
littledata:synced-cron@1.5.1
livedata@1.0.18
localstorage@1.2.0
logging@1.1.20
Expand Down
5 changes: 5 additions & 0 deletions lib/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ export const models = {
readonly: true,
}},
},
unlocks: {
timestamp: {label: 'Timestamp', type: Date},
username: {label: 'Username', type: String, max: 50},
user: {label: 'User', type: String, max: 25}
},
comment: {
text: {label: 'Text', type: String, max: 2000, optional: true},
created: {label: 'Date', type: Date},
Expand Down
1 change: 1 addition & 0 deletions lib/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export const schemas = {
lockusers: new SimpleSchema(models.lockusers),
mails: new SimpleSchema(models.mail),
comments: new SimpleSchema(models.comment),
unlocks: new SimpleSchema(models.unlocks)
};
96 changes: 96 additions & 0 deletions server/cronjob/syncAndMailUnlocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import {HTTP} from 'meteor/http';
import { Unlocks } from '/collections/unlocks';
import { Email } from 'meteor/email'

let credentials;
let assumeUser;
let lockid;
let groupid;
let authTime;
let options;
const authenticate = () => {
if (!authTime || (new Date().getTime() - authTime.getTime()) > 3600000) {
authTime = new Date();
const params = {
'grant_type': 'password',
'client_id': 'danalock-web',
'username': Meteor.settings.lockUsername,
'password': Meteor.settings.lockPassword,
};
try {
credentials = JSON.parse(HTTP.post('https://api.danalock.com/oauth2/token', { params }).content);
const Authorization = `${credentials.token_type} ${credentials.access_token}`;
const identities = JSON.parse(HTTP.get('https://api.danalock.com/user/v1/identities', { headers: { Authorization } }).content);
assumeUser = identities.find(obj => obj.domain.indexOf(Meteor.settings.lockAssumeUser) !== -1).id;
options = {
headers: {
Authorization,
'X-Assume-User': assumeUser,
}
};
const locks = JSON.parse(HTTP.get('https://api.danalock.com/locks/v1?page=0', options).content);
lockid = locks.find(l => l.name.indexOf(Meteor.settings.lockName) !== -1).id;
const groups = JSON.parse(HTTP.get('https://api.danalock.com/groups/v1?page=0', options).content);
groupid = groups.find(l => l.name.indexOf(Meteor.settings.groupLockName) !== -1).id;
} catch (e) {
console.log(e);
}
}
};

const syncUnlocks = () => {
const res = HTTP.get(`https://api.danalock.com/log/v1/lock/${lockid}?page=0&perpage=50`, options);
let importedCount = 0;
res.data.forEach((event) => {
const timestamp = new Date(event.timestamp);
const unlock = Unlocks.findOne({ timestamp });
if (!unlock) {
const colonlocation = event.username.lastIndexOf(':')
const username = colonlocation === -1 ? event.username : event.username.substr(0, colonlocation + 1);
Unlocks.insert({timestamp: timestamp, username: username, user: event.user});
importedCount += 1;
}
});
return importedCount;
};

if (Meteor.isServer) {
SyncedCron.add({
name: 'Sync unlocks and send a mail',
schedule: function (parser) {
// return parser.recur().on(0).hour();
return parser.recur().on(3).hour();
},
job: function () {
authenticate();
syncUnlocks();
const today = new Date();
const yesterday = new Date();
yesterday.setHours(-23);
const unlocks = Unlocks.find({
'timestamp': {
$gte: yesterday,
$lt: today
}
}).fetch();

const log = unlocks.map(t => `${t.timestamp.toISOString()} ${t.user}`).join("\n");
const message = `${unlocks.length} låsöppningar av ytterdörren från ${yesterday.toISOString()} till ${today.toISOString()}\n\n${log}`;
Email.send({
to: 'pass@ekebyindustrihus.com',
from: 'kansliet@uppsalamakerspace.se',
subject: 'Låsöppningar UMS',
text: message
});
Email.send({
to: 'mpalmer@gmail.com',
from: 'kansliet@uppsalamakerspace.se',
subject: 'Låsöppningar UMS',
text: message
});
return message;
}
});

SyncedCron.start();
}
10 changes: 9 additions & 1 deletion server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import { Messages } from '/collections/messages';
import { Payments } from '/collections/payments';
import { Mails } from '/collections/mails';
import { Comments} from "/collections/comments";
import { Unlocks } from '/collections/unlocks';
import './methods/import';
import './methods/update';
import './methods/mail';
import './methods/bank';
import './methods/lock';
import './methods/check';
import './methods/settings';
import './cronjob/syncAndMailUnlocks';

Meteor.startup(() => {
// code to run on server at startup
Expand Down Expand Up @@ -112,4 +114,10 @@ if (Meteor.isServer) {
return Comments.find();
}
});
}
Meteor.publish('unlocks', function() {
if (this.userId) {
return Unlocks.find();
}
});
}

43 changes: 43 additions & 0 deletions server/methods/lock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {HTTP} from 'meteor/http';
import { Unlocks } from '/collections/unlocks';
import { Email } from 'meteor/email'

let credentials;
let assumeUser;
Expand Down Expand Up @@ -36,7 +38,48 @@ const authenticate = () => {
}
};

const synkaUnlocks = () => {
const res = HTTP.get(`https://api.danalock.com/log/v1/lock/${lockid}?page=0&perpage=50`, options);
let importedCount = 0;
res.data.forEach((event) => {
const timestamp = new Date(event.timestamp);
const unlock = Unlocks.findOne({ timestamp });
if (!unlock) {
const colonlocation = event.username.lastIndexOf(':')
const username = colonlocation === -1 ? event.username : event.username.substr(0, colonlocation + 1);
Unlocks.insert({timestamp: timestamp, username: username, user: event.user});
importedCount += 1;
}
});
return importedCount;
};

Meteor.methods({
'syncLockHistory': () => {
authenticate();
return synkaUnlocks();
},
'syncAndMailLockHistory': () => {
if (!this.userId) {
throw new Meteor.Error('Not authorized');
}
authenticate();
synkaUnlocks();
const today = new Date();
const yesterday = new Date();
yesterday.setHours(-25);
const unlocks = Unlocks.find({
'timestamp': {
$gte: yesterday,
$lt: today
}
}).fetch();

const log = unlocks.map(t => `${t.timestamp.toISOString()} ${t.user}`).join("\n");
const message = `${unlocks.length} låsöppningar av ytterdörren från ${yesterday.toISOString()} till ${today.toISOString()}\n\n${log}`;
Email.send({ to: 'mpalmer@gmail.com', from: 'ums@uppsalamakerspace.se', subject: 'Låsöppningar UMS', text: message });
return message;
},
'lockHistory': () => {
authenticate();
const afterDate = new Date();
Expand Down

0 comments on commit 0fa7c0c

Please # to comment.