Skip to content

Commit

Permalink
Merge pull request #17 from WLAN-Kabel/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
maxblome authored Mar 3, 2020
2 parents dd55bbd + 5a74b56 commit 4cdd64a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 52 deletions.
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@

![Logo](admin/calendar.png)
# ioBroker.calendar

[![NPM version](http://img.shields.io/npm/v/iobroker.calendar.svg)](https://www.npmjs.com/package/iobroker.calendar)
[![Downloads](https://img.shields.io/npm/dm/iobroker.calendar.svg)](https://www.npmjs.com/package/iobroker.calendar)
![Number of Installations (latest)](http://iobroker.live/badges/calendar-installed.svg)
![Number of Installations (stable)](http://iobroker.live/badges/calendar-stable.svg)
[![NPM version](http://img.shields.io/npm/v/iobroker.calendar.svg?logo=npm)](https://www.npmjs.com/package/iobroker.calendar)
[![Downloads](https://img.shields.io/npm/dm/iobroker.calendar?logo=npm)](https://www.npmjs.com/package/iobroker.calendar)
![Installations](http://iobroker.live/badges/calendar-installed.svg)
![Stable](http://iobroker.live/badges/calendar-stable.svg)
[![Dependency Status](https://img.shields.io/david/WLAN-Kabel/ioBroker.calendar.svg)](https://david-dm.org/WLAN-Kabel/iobroker.calendar)
[![Known Vulnerabilities](https://snyk.io/test/github/WLAN-Kabel/ioBroker.calendar/badge.svg)](https://snyk.io/test/github/WLAN-Kabel/ioBroker.calendar)

[![NPM](https://nodei.co/npm/iobroker.calendar.png?downloads=true)](https://nodei.co/npm/iobroker.calendar/)

**Tests:** [![Travis-CI](http://img.shields.io/travis/WLAN-Kabel/ioBroker.calendar/master.svg)](https://travis-ci.org/WLAN-Kabel/ioBroker.calendar) [![AppVeyor](https://ci.appveyor.com/api/projects/status/github/WLAN-Kabel/ioBroker.calendar?branch=master&svg=true)](https://ci.appveyor.com/project/WLANKabel/ioBroker-calendar/)
**Tests:** [![Travis-CI](http://img.shields.io/travis/WLAN-Kabel/ioBroker.calendar/master.svg?logo=travis)](https://travis-ci.org/WLAN-Kabel/ioBroker.calendar) [![AppVeyor](https://img.shields.io/appveyor/build/WLANKabel/ioBroker-calendar/master?logo=appveyor)](https://ci.appveyor.com/project/WLANKabel/ioBroker-calendar/)

## Calendar adapter for ioBroker

Expand All @@ -27,7 +28,7 @@ The following step is only needed if your ioBroker is installed on another compu
### Windows:

Run ```nodepad.exe``` with admin right and open the ```C:\Windows\System32\drivers\etc\hosts``` file.
Add a entry like ```192.168.0.10 example.com``` (<IP-Adress ioBroker> <FQDN>)
Add a entry like ```192.168.0.10 example.com``` (\<IP-Adress ioBroker\> \<FQDN\>)
Save the file and open the webinterface via the <FQDN> you have written in the hosts file. Example: http://example.com:8081

### Linux:
Expand Down Expand Up @@ -60,7 +61,7 @@ You can add your caldav calendar in the adapter config.
Enter your access data and the host name in the config.

### Baseurl list
* Nextcloud: https://<Hostname>/remote.php/dav/principals
* Nextcloud: https://\<Hostname\>/remote.php/dav/principals
* Web.de: https://caldav.web.de
* mail.de: https://kalender.mail.de
* Posteo: https://posteo.de:8443
Expand All @@ -69,12 +70,16 @@ If you know more, please let me know so that I can include them.

## iCal File Calendar

You can add your ical calendar in the adapter config.
You can add your iCal calendar in the adapter config.

Enter the file path on the CalDav tab in the hostname field.

## Changelog

### 1.1.2 (2020-03-03)
* (WLAN-Kabel) #15 - Fixed a serious bug that caused incorrect credentials for CalDav accounts
* (WLAN-Kabel) #15 - Fixed a bug that caused a 'TypeError' message

### 1.1.1 (2020-02-26)
* (WLAN-Kabel) Password encryption added
* (WLAN-Kabel) Error messages for caldav lib extended
Expand Down
14 changes: 13 additions & 1 deletion io-package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
{
"common": {
"name": "calendar",
"version": "1.1.1",
"version": "1.1.2",
"news": {
"1.1.2": {
"en": "#15 - Fixed a serious bug that caused incorrect credentials for CalDav accounts\n#15 - Fixed a bug that caused a 'TypeError' message",
"de": "#15 - Ein schwerwiegender Fehler wurde behoben, der zu falschen Anmeldeinformationen für CalDav-Konten führte\n#15 - Ein Fehler wurde behoben, der eine 'TypeError'-Nachricht verursachte",
"ru": "#15 - Исправлена серьезная ошибка, которая приводила к неверным учетным данным для учетных записей CalDav\n#15 - Исправлена ошибка, приводившая к сообщению «TypeError»",
"pt": "#15 - Corrigido um bug sério que causava credenciais incorretas nas contas CalDav\n#15 - Corrigido um erro que causava uma mensagem 'TypeError'",
"nl": "#15 - Een ernstige bug opgelost die onjuiste inloggegevens voor CalDav-accounts veroorzaakte\n#15 - Een bug gerepareerd die een 'TypeError' bericht veroorzaakte",
"fr": "#15 - Correction d'un bug grave qui provoquait des informations d'identification incorrectes pour les comptes CalDav\n#15 - Correction d'un bug qui provoquait un message 'TypeError'",
"it": "#15 - Risolto un bug grave che causava credenziali errate per gli account CalDav\n#15 - Risolto un bug che causava un messaggio \"TypeError\"",
"es": "#15 - Se corrigió un error grave que causaba credenciales incorrectas para las cuentas de CalDav\n#15 - Se corrigió un error que causaba un mensaje 'TypeError'",
"pl": "#15 - Naprawiono poważny błąd, który powodował nieprawidłowe dane uwierzytelniające dla kont CalDav\n#15 - Naprawiono błąd, który powodował komunikat „TypeError”",
"zh-cn": "#15-修复了一个严重的错误,该错误导致CalDav帐户的凭据不正确\n#15-修复了导致“ TypeError”消息的错误"
},
"1.1.1": {
"en": "Password encryption added\nError messages for caldav lib extended\nFixed an issue that caused errors when reading null events",
"de": "Passwortverschlüsselung hinzugefügt\nFehlermeldungen für caldav lib erweitert\nEs wurde ein Problem behoben, das beim Lesen von Nullereignissen zu Fehlern führte",
Expand Down
110 changes: 69 additions & 41 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ let cronJob;
let server;

let adapter;
let systemConfig;

let oauth2;
const googleScope = 'https://www.googleapis.com/auth/calendar';
Expand Down Expand Up @@ -60,8 +61,6 @@ class Calendar extends utils.Adapter {
server = initServer(adapter.config);
}

let systemConfig;

try {
systemConfig = await adapter.getForeignObjectAsync('system.config');
} catch(error) {
Expand Down Expand Up @@ -89,7 +88,7 @@ class Calendar extends utils.Adapter {
}

if(ids) {
updateConfig({
await updateConfig({
caldav: handleCaldavCalendarIds(adapter.config, i, ids)
});
}
Expand All @@ -106,7 +105,7 @@ class Calendar extends utils.Adapter {
adapter.config.caldav[i].color = calendar.color|| '#000000';
adapter.config.caldav[i].listIsLoaded = true;

updateConfig({
await updateConfig({
caldav: adapter.config.caldav
});
}
Expand Down Expand Up @@ -170,6 +169,14 @@ class Calendar extends utils.Adapter {
}
}

function encrypt(key, value) {
let result = '';
for(let i = 0; i < value.length; ++i) {
result += String.fromCharCode(key[i % key.length].charCodeAt(0) ^ value.charCodeAt(i));
}
return result;
}

function decrypt(key, value) {
let result = '';
for(let i = 0; i < value.length; ++i) {
Expand Down Expand Up @@ -205,9 +212,11 @@ async function updateConfig(newConfig) {
...adapter.config,
...newConfig,
};

// Update the adapter object
const adapterObj = await adapter.getForeignObjectAsync(`system.adapter.${adapter.namespace}`);
adapterObj.native = config;

await adapter.setForeignObjectAsync(`system.adapter.${adapter.namespace}`, adapterObj);
}

Expand All @@ -230,18 +239,23 @@ async function startCalendarSchedule(config, auth = null) {
}

for(let i = 0; i < caldavCalendars.length; i++) {

adapter.log.info('CALDAV ' + caldavCalendars[i].name);

if(caldavCalendars[i].active) {

try {
const events = await getCaldavCalendarEvents(caldavCalendars[i]);

handleCalendarEvents(caldavCalendars[i], events);
} catch(error) {
adapter.log.error(error);
adapter.log.error('ERROR ' + error);
}
}
}

adapter.log.info('NACH LADEN');

cronJob = cron.schedule('*/10 * * * *', async () => {
for(let i = 0; i < googleCalendars.length; i++) {
if(googleCalendars[i].active) {
Expand Down Expand Up @@ -270,6 +284,8 @@ async function startCalendarSchedule(config, auth = null) {
}
});

adapter.log.info('NACH CRON');

adapter.log.debug('Cron job started');
}

Expand Down Expand Up @@ -321,16 +337,18 @@ function handleCaldavCalendarIds(config, index, ids) {
id = id.substring(id.length - 31, id.length - 1);

adapter.log.info(`Set calendar name "${index}": Old name => "${configCaldav[index].name}" New name "${calendar.name}"`);

configCaldav[index].active = true;
configCaldav[index].path = calendar.path;
configCaldav[index].name = calendar.name;
configCaldav[index].id = id;
configCaldav[index].ctag = calendar.ctag || '';
configCaldav[index].color = calendar.color|| '#000000';
configCaldav[index].listIsLoaded = true;
configCaldav[index].password = encrypt(systemConfig.native.secret, configCaldav[index].password);

firstIsSet = true;

} else {

const configCalendar = {};
Expand All @@ -352,11 +370,13 @@ function handleCaldavCalendarIds(config, index, ids) {
configCalendar.color = calendar.color || '#000000';
configCalendar.path = calendar.path;
configCalendar.listIsLoaded = true;

configCaldav.push(configCalendar);
}


}

return configCaldav;
}

Expand All @@ -370,19 +390,24 @@ function sameDate(targetDate, calendarDate) {
} else return false;
}

function addChannel(id, name) {
async function addChannel(id, name) {

//adapter.createChannel(device, id, {name: name});

adapter.setObjectNotExistsAsync(id, {
type: 'channel',
common: {
name: name,
},
native: {},
}).then((prom) => {
if(prom) adapter.log.debug('Channel added => ' + prom.id);
});
try {
const result = await adapter.setObjectNotExistsAsync(id, {
type: 'channel',
common: {
name: name,
},
native: {},
});

if(result) adapter.log.debug('Channel added => ' + result.id);

} catch(error) {
adapter.log.error(error);
}
}

function addDevice(id, name) {
Expand Down Expand Up @@ -452,34 +477,37 @@ function addState(id, name, type, role, value = null) {
*/
function removeDeleted(oldList, newList, calendarId) {

for(let i = oldList.length - 1; i >= 0; i--) {
if(oldList) {

if(oldList[i]._id.split('.')[2] == calendarId && oldList[i]._id.split('.')[4]) {
for(let i = oldList.length - 1; i >= 0; i--) {

let event = false;
if(oldList[i]._id.split('.')[2] == calendarId && oldList[i]._id.split('.')[4]) {

let event = false;

for(const j of newList.keys()) {
for(const j of newList.keys()) {

if(oldList[i]._id.split('.')[3] == j) {
if(oldList[i]._id.split('.')[3] == j) {

for(let k = 0; k < newList.get(j); k++) {
if(oldList[i]._id.split('.')[4] == k) {
for(let k = 0; k < newList.get(j); k++) {
if(oldList[i]._id.split('.')[4] == k) {

event = true;
event = true;
}
}
}
}
}

if(event == false) {
adapter.log.debug(`Delete channel => ${oldList[i]._id}`);
adapter.delObject(oldList[i]._id);
adapter.getStates(oldList[i]._id + '*', (err, states) => {
for (const id in states) {
adapter.log.debug(`Delete state => ${id}`);
adapter.delObject(id);
}
});
if(event == false) {
adapter.log.debug(`Delete channel => ${oldList[i]._id}`);
adapter.delObject(oldList[i]._id);
adapter.getStates(oldList[i]._id + '*', (err, states) => {
for (const id in states) {
adapter.log.debug(`Delete state => ${id}`);
adapter.delObject(id);
}
});
}
}
}
}
Expand All @@ -502,7 +530,7 @@ async function getCaldavCalendarEvents(calendar) {
return;
}

for(let i = 0; i < events.length; i++) {
for(const i in events) {

let calendar;

Expand All @@ -526,7 +554,7 @@ async function getCaldavCalendarEvents(calendar) {

const parsedEvents = ical.parse(events);

for(let i = 0; i < parsedEvents.events.length; i++) {
for(const i in parsedEvents.events) {
list.push(util.normalizeEvent(parsedEvents.events[i].summary, parsedEvents.events[i].description,
parsedEvents.events[i].dtstart.val, parsedEvents.events[i].dtend.val));
}
Expand All @@ -545,9 +573,9 @@ async function getGoogleCalendarEvents(calendar, auth, index) {
return new Promise((resolve, reject) => {

if(calendar.accessToken && calendar.accessToken != '' && calendar.refreshToken && calendar.refreshToken != '' && calendar.id != '') {

const oauth2 = auth;

oauth2.setCredentials({
access_token: calendar.accessToken,
refresh_token: calendar.refreshToken
Expand Down Expand Up @@ -600,7 +628,7 @@ async function handleCalendarEvents(calendar, events) {
for(let i = 0; i <= ((calendar.days > 0) ? calendar.days : 7); i++) {
dayEvents.set(i, []);
}

for (let i = 0; i < events.length; i++) {

for(let j = 0; j <= ((calendar.days > 0) ? calendar.days : 7); j++) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iobroker.calendar",
"version": "1.1.1",
"version": "1.1.2",
"description": "Connect to google, caldav and ical calendar",
"author": {
"name": "WLAN-Kabel",
Expand Down

0 comments on commit 4cdd64a

Please sign in to comment.