Selaa lähdekoodia

command exec

tags/0.0.5
Robin Thoni 9 vuotta sitten
vanhempi
commit
683b88f6c0

+ 30
- 0
app/src/main/java/com/rthoni/camotion/business/CommandsBusiness.java Näytä tiedosto

@@ -0,0 +1,30 @@
1
+package com.rthoni.camotion.business;
2
+
3
+import android.graphics.Bitmap;
4
+import android.graphics.BitmapFactory;
5
+import android.util.Base64;
6
+
7
+import com.luticate.utils.business.LuBusinessManager;
8
+import com.luticate.utils.business.LuPromise;
9
+import com.luticate.utils.dbo.LuBoolDbo;
10
+import com.luticate.utils.dbo.LuDataAccessConfigDbo;
11
+import com.rthoni.camotion.dataaccess.CamerasDataAccess;
12
+import com.rthoni.camotion.dataaccess.CommandsDataAccess;
13
+import com.rthoni.camotion.dbo.CameraDbo;
14
+import com.rthoni.camotion.dbo.CameraImageDbo;
15
+import com.rthoni.camotion.dbo.CommandDbo;
16
+
17
+/**
18
+ * Created by robin on 12/3/15.
19
+ */
20
+public class CommandsBusiness extends LuBusinessManager {
21
+    public static LuPromise<CommandDbo.MultipleCommandDbo> getAll(LuDataAccessConfigDbo config, int page, int perPage)
22
+    {
23
+        return CommandsDataAccess.getAll(config, page, perPage);
24
+    }
25
+
26
+    public static LuPromise<LuBoolDbo> exec(LuDataAccessConfigDbo config, CommandDbo command)
27
+    {
28
+        return CommandsDataAccess.exec(config, command.getId());
29
+    }
30
+}

+ 31
- 0
app/src/main/java/com/rthoni/camotion/dataaccess/CommandsDataAccess.java Näytä tiedosto

@@ -0,0 +1,31 @@
1
+package com.rthoni.camotion.dataaccess;
2
+
3
+import com.luticate.utils.business.LuPromise;
4
+import com.luticate.utils.business.LuRequest;
5
+import com.luticate.utils.dataaccess.LuDataAccess;
6
+import com.luticate.utils.dbo.LuBoolDbo;
7
+import com.luticate.utils.dbo.LuDataAccessConfigDbo;
8
+import com.rthoni.camotion.dbo.CommandDbo;
9
+import com.rthoni.camotion.dbo.SensorDbo;
10
+import com.rthoni.camotion.dbo.SensorValueDbo;
11
+
12
+import java.util.HashMap;
13
+
14
+/**
15
+ * Created by robin on 12/3/15.
16
+ */
17
+public class CommandsDataAccess extends LuDataAccess {
18
+
19
+    public static LuPromise<CommandDbo.MultipleCommandDbo> getAll(LuDataAccessConfigDbo config, int page, int perPage)
20
+    {
21
+        HashMap<String, String> params = new HashMap<>();
22
+        params.put("page", "" + page);
23
+        params.put("perPage", "" + perPage);
24
+        return LuRequest.get(config, CommandDbo.MultipleCommandDbo.class, "/api/commands", params);
25
+    }
26
+
27
+    public static LuPromise<LuBoolDbo> exec(LuDataAccessConfigDbo config, int id)
28
+    {
29
+        return LuRequest.post(config, LuBoolDbo.class, "/api/commands/" + id + "/exec");
30
+    }
31
+}

+ 64
- 0
app/src/main/java/com/rthoni/camotion/dbo/CommandDbo.java Näytä tiedosto

@@ -0,0 +1,64 @@
1
+package com.rthoni.camotion.dbo;
2
+
3
+import com.luticate.utils.dbo.LuDbo;
4
+import com.luticate.utils.dbo.LuMultipleDbo;
5
+
6
+import org.json.JSONException;
7
+import org.json.JSONObject;
8
+
9
+import java.util.HashMap;
10
+
11
+/**
12
+ * Created by robin on 12/3/15.
13
+ */
14
+public class CommandDbo extends LuDbo {
15
+
16
+    private int _id;
17
+
18
+    private String _name;
19
+
20
+    private String _description;
21
+
22
+    public String getDescription() {
23
+        return _description;
24
+    }
25
+
26
+    public void setDescription(String description) {
27
+        _description = description;
28
+    }
29
+
30
+    public String getName() {
31
+        return _name;
32
+    }
33
+
34
+    public void setName(String name) {
35
+        _name = name;
36
+    }
37
+
38
+    public int getId() {
39
+        return _id;
40
+    }
41
+
42
+    public void setId(int id) {
43
+        _id = id;
44
+    }
45
+
46
+    public static class MultipleCommandDbo extends LuMultipleDbo<CommandDbo> {
47
+    }
48
+
49
+    @Override
50
+    public void fromJson(JSONObject json) throws JSONException {
51
+        _id = json.getInt("Id");
52
+        _description = json.getString("Description");
53
+        _name = json.getString("Name");
54
+    }
55
+
56
+    @Override
57
+    public HashMap<String, Object> toArray() {
58
+        HashMap<String, Object> array = new HashMap<>();
59
+        array.put("Id", _id);
60
+        array.put("Description", _description);
61
+        array.put("Name", _name);
62
+        return array;
63
+    }
64
+}

+ 1
- 0
app/src/main/java/com/rthoni/camotion/dbo/SensorDbo.java Näytä tiedosto

@@ -12,6 +12,7 @@ import java.util.HashMap;
12 12
  * Created by robin on 12/4/15.
13 13
  */
14 14
 public class SensorDbo extends LuDbo {
15
+
15 16
     private int _id;
16 17
 
17 18
     private String _name;

+ 3
- 4
app/src/main/java/com/rthoni/camotion/ui/MainActivity.java Näytä tiedosto

@@ -25,6 +25,7 @@ import com.rthoni.camotion.dbo.LocationDbo;
25 25
 import com.rthoni.camotion.ui.dialogs.LoginDialog;
26 26
 import com.rthoni.camotion.ui.fragments.CamerasFragment;
27 27
 import com.rthoni.camotion.ui.fragments.CamotionFragment;
28
+import com.rthoni.camotion.ui.fragments.CommandsFragment;
28 29
 import com.rthoni.camotion.ui.fragments.DefaultFragment;
29 30
 import com.rthoni.camotion.ui.fragments.SensorsFragment;
30 31
 
@@ -40,8 +41,6 @@ public class MainActivity extends AppCompatActivity
40 41
     private SubMenu _locationsMenu;
41 42
     private NavigationView _navigationView;
42 43
 
43
-    private int _lastView = -1;
44
-
45 44
     private static final int REQUEST_CODE_ADD_ACCOUNT = 0;
46 45
 
47 46
     @Override
@@ -168,9 +167,9 @@ public class MainActivity extends AppCompatActivity
168 167
             fragment = new CamerasFragment();
169 168
         } else if (view == R.id.nav_sensors) {
170 169
             fragment = new SensorsFragment();
171
-        } /*else if (view == R.id.nav_commands) {
170
+        } else if (view == R.id.nav_commands) {
172 171
             fragment = new CommandsFragment();
173
-        } else if (view == -1) {
172
+        } /*else if (view == -1) {
174 173
             fragment = new DefaultFragment();
175 174
         } */else {
176 175
             return;

+ 72
- 0
app/src/main/java/com/rthoni/camotion/ui/dialogs/CommandExecDialog.java Näytä tiedosto

@@ -0,0 +1,72 @@
1
+package com.rthoni.camotion.ui.dialogs;
2
+
3
+import android.app.Dialog;
4
+import android.app.ProgressDialog;
5
+import android.content.Context;
6
+import android.content.DialogInterface;
7
+import android.support.v7.app.AlertDialog;
8
+
9
+import com.luticate.auth.business.LuticatePermissionsBusiness;
10
+import com.luticate.auth.business.LuticateUsersBusiness;
11
+import com.luticate.auth.dbo.LuFullLoginDbo;
12
+import com.luticate.auth.dbo.LuLoginDbo;
13
+import com.luticate.auth.dbo.LuPermissionDbo;
14
+import com.luticate.utils.business.LuPromise;
15
+import com.luticate.utils.dbo.LuBoolDbo;
16
+import com.luticate.utils.dbo.LuDataAccessConfigDbo;
17
+import com.rthoni.camotion.R;
18
+import com.rthoni.camotion.business.CommandsBusiness;
19
+import com.rthoni.camotion.dbo.CommandDbo;
20
+import com.rthoni.camotion.dbo.LocationDbo;
21
+
22
+/**
23
+ *
24
+ * Created by robin on 11/28/15.
25
+ */
26
+public abstract class CommandExecDialog {
27
+
28
+    public static void showDialog(final LuDataAccessConfigDbo config, final Context context, final CommandDbo command)
29
+    {
30
+        AlertDialog.Builder builder = new AlertDialog.Builder(context);
31
+        builder.setMessage(context.getString(R.string.command_confirm, command.getName()))
32
+                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
33
+                    @Override
34
+                    public void onClick(DialogInterface dialog, int which) {
35
+                        showLoadingDialog(config, context, command);
36
+                    }
37
+                })
38
+                .setNegativeButton(android.R.string.no, null)
39
+                .show();
40
+    }
41
+
42
+    protected static void showLoadingDialog(final LuDataAccessConfigDbo config, final Context context, final CommandDbo command)
43
+    {
44
+        final ProgressDialog dlg = new ProgressDialog(context);
45
+        dlg.setIndeterminate(true);
46
+        dlg.setTitle(R.string.loading);
47
+        dlg.setMessage(context.getString(R.string.command_loading, command.getName()));
48
+        dlg.setCancelable(false);
49
+        dlg.show();
50
+        CommandsBusiness.exec(config, command).then(new LuPromise.LuConsumer<LuBoolDbo>() {
51
+            @Override
52
+            public void execute(LuBoolDbo data) {
53
+                dlg.hide();
54
+                showEndDialog(context, context.getString(R.string.command_success, command.getName()));
55
+            }
56
+        }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
57
+            @Override
58
+            public void execute(LuPromise.LuPromiseError error) {
59
+                dlg.hide();
60
+                showEndDialog(context, context.getString(R.string.error, error.toString()));
61
+            }
62
+        });
63
+    }
64
+
65
+    protected static void showEndDialog(Context context, String text)
66
+    {
67
+        AlertDialog.Builder builder = new AlertDialog.Builder(context);
68
+        builder.setMessage(text);
69
+        builder.setPositiveButton(android.R.string.ok, null);
70
+        builder.show();
71
+    }
72
+}

+ 41
- 0
app/src/main/java/com/rthoni/camotion/ui/fragments/AbstractCommandsFragment.java Näytä tiedosto

@@ -0,0 +1,41 @@
1
+package com.rthoni.camotion.ui.fragments;
2
+
3
+import com.luticate.utils.business.LuPromise;
4
+import com.luticate.utils.dbo.LuMultipleDbo;
5
+import com.rthoni.camotion.business.CommandsBusiness;
6
+import com.rthoni.camotion.dbo.CommandDbo;
7
+import com.rthoni.camotion.ui.views.CommandView;
8
+
9
+/**
10
+ *
11
+ * Created by robin on 11/29/15.
12
+ */
13
+public abstract class AbstractCommandsFragment extends CamotionFragment<CommandDbo, CommandView> {
14
+
15
+    @Override
16
+    protected LuPromise<LuMultipleDbo<CommandDbo>> getLoadPagePromise(int page, int perPage, String query) {
17
+        return CommandsBusiness.getAll(_currentLocation.getConfig(_fullLoginDbo.getLoginDbo()), page, perPage)
18
+                .map(new LuPromise.LuConverter<CommandDbo.MultipleCommandDbo, LuMultipleDbo<CommandDbo>>() {
19
+                    @Override
20
+                    public LuMultipleDbo<CommandDbo> convert(CommandDbo.MultipleCommandDbo data) {
21
+                        return data;
22
+                    }
23
+                });
24
+    }
25
+
26
+    @Override
27
+    protected CommandView getDboView(final CommandDbo command) {
28
+        CommandView commandView = new CommandView(getActivity());
29
+        commandView.setCamotionDbo(_fullLoginDbo, _currentLocation);
30
+        commandView.setCommand(command);
31
+        commandView.setOnClickedListener(new LuPromise.LuConsumer<CommandDbo>() {
32
+            @Override
33
+            public void execute(CommandDbo command) {
34
+                onCommandClicked(command);
35
+            }
36
+        });
37
+        return commandView;
38
+    }
39
+
40
+    protected abstract void onCommandClicked(CommandDbo command);
41
+}

+ 5
- 0
app/src/main/java/com/rthoni/camotion/ui/fragments/CamerasFragment.java Näytä tiedosto

@@ -12,6 +12,11 @@ import com.rthoni.camotion.ui.views.CameraView;
12 12
  */
13 13
 public class CamerasFragment extends CamotionFragment<CameraDbo, CameraView> {
14 14
 
15
+    public CamerasFragment() {
16
+        super();
17
+        _perPage = 6;
18
+    }
19
+
15 20
     @Override
16 21
     protected LuPromise<LuMultipleDbo<CameraDbo>> getLoadPagePromise(int page, int perPage, String query) {
17 22
         return CamerasBusiness.getAll(_currentLocation.getConfig(_fullLoginDbo.getLoginDbo()), page, perPage)

+ 16
- 0
app/src/main/java/com/rthoni/camotion/ui/fragments/CommandsFragment.java Näytä tiedosto

@@ -0,0 +1,16 @@
1
+package com.rthoni.camotion.ui.fragments;
2
+
3
+import com.rthoni.camotion.dbo.CommandDbo;
4
+import com.rthoni.camotion.ui.dialogs.CommandExecDialog;
5
+
6
+/**
7
+ *
8
+ * Created by robin on 11/29/15.
9
+ */
10
+public class CommandsFragment extends AbstractCommandsFragment {
11
+
12
+    @Override
13
+    protected void onCommandClicked(CommandDbo command) {
14
+        CommandExecDialog.showDialog(_currentLocation.getConfig(_fullLoginDbo.getLoginDbo()), getActivity(), command);
15
+    }
16
+}

+ 1
- 1
app/src/main/java/com/rthoni/camotion/ui/views/CameraView.java Näytä tiedosto

@@ -131,7 +131,7 @@ public class CameraView extends RelativeLayout {
131 131
                 }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
132 132
                     @Override
133 133
                     public void execute(LuPromise.LuPromiseError data) {
134
-
134
+                        pause();
135 135
                     }
136 136
                 });
137 137
     }

+ 74
- 0
app/src/main/java/com/rthoni/camotion/ui/views/CommandView.java Näytä tiedosto

@@ -0,0 +1,74 @@
1
+package com.rthoni.camotion.ui.views;
2
+
3
+import android.content.Context;
4
+import android.util.AttributeSet;
5
+import android.view.View;
6
+import android.widget.RelativeLayout;
7
+import android.widget.TextView;
8
+
9
+import com.luticate.auth.dbo.LuFullLoginDbo;
10
+import com.luticate.utils.business.LuPromise;
11
+import com.rthoni.camotion.R;
12
+import com.rthoni.camotion.dbo.CommandDbo;
13
+import com.rthoni.camotion.dbo.LocationDbo;
14
+import com.rthoni.camotion.ui.dialogs.CommandExecDialog;
15
+
16
+/**
17
+ * Created by robin on 12/10/15.
18
+ */
19
+public class CommandView extends RelativeLayout {
20
+    private LuFullLoginDbo _fullLoginDbo = null;
21
+    private LocationDbo _currentLocation = null;
22
+
23
+    private CommandDbo _command;
24
+
25
+    private LuPromise.LuConsumer<CommandDbo> _onClicked = null;
26
+
27
+    public CommandView(Context context) {
28
+        super(context);
29
+        init();
30
+    }
31
+
32
+    public CommandView(Context context, AttributeSet attrs) {
33
+        super(context, attrs);
34
+        init();
35
+    }
36
+
37
+    public CommandView(Context context, AttributeSet attrs, int defStyleAttr) {
38
+        super(context, attrs, defStyleAttr);
39
+        init();
40
+    }
41
+
42
+    private void init()
43
+    {
44
+        inflate(getContext(), R.layout.command_view, this);
45
+        findViewById(R.id.textViewName).setOnClickListener(new OnClickListener() {
46
+            @Override
47
+            public void onClick(View v) {
48
+                if (_onClicked != null) {
49
+                    _onClicked.execute(_command);
50
+                }
51
+            }
52
+        });
53
+    }
54
+
55
+    public void setCamotionDbo(LuFullLoginDbo fullLoginDbo, LocationDbo currentLocation)
56
+    {
57
+        _fullLoginDbo = fullLoginDbo;
58
+        _currentLocation = currentLocation;
59
+    }
60
+
61
+    public CommandDbo getCommand() {
62
+        return _command;
63
+    }
64
+
65
+    public void setCommand(CommandDbo command) {
66
+        _command = command;
67
+        ((TextView)findViewById(R.id.textViewName)).setText(command.getName());
68
+    }
69
+
70
+    public void setOnClickedListener(LuPromise.LuConsumer<CommandDbo> onClicked)
71
+    {
72
+        _onClicked = onClicked;
73
+    }
74
+}

+ 7
- 0
app/src/main/res/layout/command_exec_dialog_layout.xml Näytä tiedosto

@@ -0,0 +1,7 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
+              android:orientation="vertical"
4
+              android:layout_width="match_parent"
5
+              android:layout_height="match_parent">
6
+
7
+</LinearLayout>

+ 19
- 0
app/src/main/res/layout/command_view.xml Näytä tiedosto

@@ -0,0 +1,19 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
+              android:layout_width="match_parent"
4
+              android:layout_height="match_parent">
5
+
6
+    <TextView
7
+        android:layout_width="wrap_content"
8
+        android:layout_height="wrap_content"
9
+        android:textAppearance="?android:attr/textAppearanceLarge"
10
+        android:text=""
11
+        android:id="@+id/textViewName"
12
+        android:layout_alignParentTop="true"
13
+        android:layout_alignParentLeft="true"
14
+        android:layout_alignParentStart="true"
15
+        android:layout_alignParentRight="true"
16
+        android:layout_alignParentEnd="true"
17
+        android:padding="15dp"/>
18
+
19
+</RelativeLayout>

+ 7
- 2
app/src/main/res/values/strings.xml Näytä tiedosto

@@ -1,5 +1,8 @@
1 1
 <resources>
2 2
     <string name="app_name">Camotion</string>
3
+    <string name="account_type">com.rthoni.camotion.account</string>
4
+
5
+    <string name="error">Error : %1$s</string>
3 6
 
4 7
     <string name="add_location_title">Add Camotion Account</string>
5 8
 
@@ -16,17 +19,19 @@
16 19
     <string name="username">Username</string>
17 20
     <string name="password">Password</string>
18 21
     <string name="domain">Domain</string>
19
-    <string name="check_account">Check account</string>
20 22
 
21 23
     <string name="error_name">Location name is required</string>
22 24
     <string name="error_username">Username is required</string>
23 25
     <string name="error_password">Password is required</string>
24 26
     <string name="error_domain">Domain is required</string>
25
-    <string name="account_type">com.rthoni.camotion.account</string>
26 27
 
27 28
     <string name="loading">Loading…</string>
28 29
     <string name="login_progress">Logging in…</string>
29 30
 
31
+    <string name="command_confirm">Do you really want to execute \'%1$s\' ?</string>
32
+    <string name="command_loading">Executing \'%1$s\'&#8230;</string>
33
+    <string name="command_success">Successfully executed \'%1$s\'</string>
34
+
30 35
     <string name="camera">Cameras</string>
31 36
     <string name="sensors">Sensors</string>
32 37
     <string name="commands">Commands</string>

+ 9
- 1
luticateutils/src/main/java/com/luticate/utils/business/LuRequest.java Näytä tiedosto

@@ -41,7 +41,15 @@ public class LuRequest {
41 41
                     T dbo = type.newInstance();
42 42
                     try {
43 43
                         JSONObject json = new JSONObject(response);
44
-                        dbo.fromJson(json.getJSONObject("Data"));
44
+                        JSONObject obj = null;
45
+                        if (!json.isNull("Data")) {
46
+                            obj = json.optJSONObject("Data");
47
+                            if (obj == null) {
48
+                                obj = new JSONObject();
49
+                                obj.put("Value", json.opt("Data"));
50
+                            }
51
+                        }
52
+                        dbo.fromJson(obj);
45 53
                         promise.resolve(dbo);
46 54
                     } catch (Exception e)
47 55
                     {

+ 13
- 2
luticateutils/src/main/java/com/luticate/utils/dbo/LuBoolDbo.java Näytä tiedosto

@@ -9,14 +9,25 @@ import java.util.HashMap;
9 9
  * Created by robin on 11/27/15.
10 10
  */
11 11
 public class LuBoolDbo extends LuDbo {
12
-    private boolean _bool;
12
+    public boolean isValue() {
13
+        return _value;
14
+    }
15
+
16
+    public void setValue(boolean value) {
17
+        _value = value;
18
+    }
19
+
20
+    private boolean _value;
13 21
 
14 22
     @Override
15 23
     public void fromJson(JSONObject json) throws JSONException {
24
+        _value = json.getBoolean("Value");
16 25
     }
17 26
 
18 27
     @Override
19 28
     public HashMap<String, Object> toArray() {
20
-        return null;
29
+        HashMap<String, Object> array = new HashMap<>();
30
+        array.put("Value", _value);
31
+        return array;
21 32
     }
22 33
 }

+ 1
- 1
luticateutils/src/main/java/com/luticate/utils/ui/fragments/AbstractPaginationFragment.java Näytä tiedosto

@@ -23,7 +23,7 @@ public abstract class AbstractPaginationFragment<Dbo extends LuDbo> extends Frag
23 23
 
24 24
     protected int _page = 0;
25 25
 
26
-    protected int _perPage = 6;
26
+    protected int _perPage = 15;
27 27
 
28 28
     protected int _maxPage = 1;
29 29
 

+ 14
- 2
luticateutils/src/main/java/com/luticate/utils/ui/views/DefaultPaginationView.java Näytä tiedosto

@@ -30,6 +30,18 @@ public class DefaultPaginationView extends AbstractPaginationView {
30 30
     public void init()
31 31
     {
32 32
         inflate(getContext(), R.layout.default_pagination_view_layout, this);
33
+        findViewById(R.id.previousPageView).setOnClickListener(new OnClickListener() {
34
+            @Override
35
+            public void onClick(View v) {
36
+                loadPreviousPage();
37
+            }
38
+        });
39
+        findViewById(R.id.nextPageView).setOnClickListener(new OnClickListener() {
40
+            @Override
41
+            public void onClick(View v) {
42
+                loadNextPage();
43
+            }
44
+        });
33 45
     }
34 46
 
35 47
     @Override
@@ -43,8 +55,8 @@ public class DefaultPaginationView extends AbstractPaginationView {
43 55
             tv.setOnClickListener(new View.OnClickListener() {
44 56
                     @Override
45 57
                     public void onClick(View v) {
46
-                            loadPage(page);
47
-                        }
58
+                        loadPage(page);
59
+                    }
48 60
                 });
49 61
             tv.setTextAppearance(getContext(), android.R.style.TextAppearance_DeviceDefault_Large);
50 62
             layout.addView(tv);

Loading…
Peruuta
Tallenna