From 22a885b3ae5b86ea0fd158b9f9fe80f213f2ffa8 Mon Sep 17 00:00:00 2001 From: marvin78 Date: Mon, 13 Jan 2020 20:41:13 +0000 Subject: [PATCH] 98_todoist: new command reorderTasks/widget reorder repaired git-svn-id: https://svn.fhem.de/fhem/trunk@20965 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/98_todoist.pm | 128 ++++++++++++++++++++++++++++++++++++++- fhem/www/pgm2/todoist.js | 8 ++- 3 files changed, 135 insertions(+), 2 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index 448c2b8dd..1637d9ca0 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,6 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. + - change: 98_todoist: reorder tasks - widget reorder - change: 73_NUKIBridge: 74_NUKIDevice use packages - change: 98_Heating_Control: move to contrib - bugfix: 98_livetracking: DoTrigger after readingsEndUpdate, decimals diff --git a/fhem/FHEM/98_todoist.pm b/fhem/FHEM/98_todoist.pm index 2f6dc3d81..a6d88f18e 100644 --- a/fhem/FHEM/98_todoist.pm +++ b/fhem/FHEM/98_todoist.pm @@ -17,7 +17,7 @@ eval "use Date::Parse;1" or $missingModule .= "Date::Parse "; ####################### # Global variables -my $version = "1.2.13"; +my $version = "1.3.0"; my $srandUsed; @@ -250,6 +250,111 @@ sub todoist_ErrorReadings($;$$) { } +# reorderTasks +sub todoist_ReorderTasks ($$) { + my ($hash,$cmd) = @_; + + my $name=$hash->{NAME}; + + Log3 $name,4, "$name: cmd: ".$cmd; + + my $pwd=""; + + my $param; + + my %commands=(); + + # some random string for UUID + my $uuid = todoist_genUUID(); + + # JSON String start- and endpoint + my $commandsStart="[{"; + my $commandsEnd="}]"; + + my $tType; + + my $argsStart = "{\"items\": ["; + my $argsEnd = "]}"; + + my $args = ""; + + ## if no token is needed and device is not disabled, check token and get list vom todoist + if (!$hash->{helper}{PWD_NEEDED} && !IsDisabled($name)) { + + ## get password + $pwd=todoist_GetPwd($hash); + + if ($pwd) { + Log3 $name,5, "$name: hash: ".Dumper($hash); + + # get Task - IDs in order + my $tids = $cmd; + my @taskIds = split(",",$tids); + + $tType = "item_reorder"; + + my $i=0; + + foreach my $taskId (@taskIds) { + $i++; + $args .= "," if ($i>1); + $args .= "{\"id\":".$taskId.",\"child_order\":".$i."}"; + } + + $args = $argsStart.$args.$argsEnd; + + Log3 $name,5, "todoist ($name): Data sent to todoist API: ".$args; + + my $dataArr=$commandsStart.'"type":"'.$tType.'","uuid":"'.$uuid.'","args":'.$args.$commandsEnd; + + Log3 $name,4, "todoist ($name): Data Array sent to todoist API: ".$dataArr; + + my $data= { + token => $pwd, + commands => $dataArr + }; + + Log3 $name,4, "todoist ($name): JSON sent to todoist API: ".Dumper($data); + + my $method="POST"; + + $param = { + url => "https://api.todoist.com/sync/v8/sync", + data => $data, + method => $method, + wType => "reorder", + timeout => 7, + header => "Content-Type: application/x-www-form-urlencoded", + hash => $hash, + callback => \&todoist_HandleTaskCallback, ## call callback sub to work with the data we get + }; + + Log3 $name,5, "todoist ($name): Param: ".Dumper($param); + + ## non-blocking access to todoist API + InternalTimer(gettimeofday()+0.1, "HttpUtils_NonblockingGet", $param, 0); + + + } + else { + todoist_ErrorReadings($hash,"access token empty"); + } + } + else { + if (!IsDisabled($name)) { + todoist_ErrorReadings($hash,"no access token set"); + } + else { + todoist_ErrorReadings($hash,"device is disabled"); + } + + } + + + + return undef; +} + # update Task sub todoist_UpdateTask($$$) { my ($hash,$cmd, $type) = @_; @@ -1679,6 +1784,7 @@ sub todoist_Set ($@) { push @sets, "getTasks:noArg"; push @sets, "cChildProjects:noArg"; push @sets, "getUsers:noArg"; + push @sets, "reorderTasks"; } push @sets, "accessToken" if ($hash->{helper}{PWD_NEEDED}); push @sets, "newAccessToken" if (!$hash->{helper}{PWD_NEEDED}); @@ -1753,6 +1859,14 @@ sub todoist_Set ($@) { } return "in order to complete a task, we need it's ID" if ($count==0); } + elsif ($cmd eq "reorderTasks") { + my $count=@args; + if ($count!=0) { + my $exp=$args[0]; + todoist_ReorderTasks ($hash,$exp); + } + return "in order to delete a task, we need it's ID" if ($count==0); + } elsif ($cmd eq "deleteTask") { my $count=@args; if ($count!=0) { @@ -2366,6 +2480,18 @@ sub todoist_genUUID() {
  • 1: show order number

  • +
  • showParent +
  • +
    +
  • showSection +
  • +
  • showChecked