summaryrefslogtreecommitdiff
path: root/www/js/StateCtrl.js
blob: 4c44b6b59b3803525cee2de88b4e6713819f19aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* jshint -W041 */
/* jslint browser: true*/
/* global cordova,StatusBar,angular,console */

// controller for State View

angular.module('zmApp.controllers').controller('zmApp.StateCtrl', ['$ionicPopup', '$scope', 'ZMDataModel', '$ionicSideMenuDelegate', '$ionicLoading', '$ionicModal', '$state', '$http', function ($ionicPopup, $scope, ZMDataModel, $ionicSideMenuDelegate, $ionicLoading, $ionicModal, $state, $http, $rootScope) {

    $scope.zmRun = "loading...";
    $scope.zmLoad = "loading...";
    $scope.zmDisk = "loading...";
    $scope.color = "";
    $scope.showDanger = false;
    $scope.dangerText = ["Show ZoneMinder Controls", "Hide ZoneMinder Controls"];
    $scope.dangerButtonColor = ["button-positive", "button-assertive"];

    var loginData = ZMDataModel.getLogin();

    var apiRun = loginData.apiurl + "/host/daemonCheck.json";
    var apiLoad = loginData.apiurl + "/host/getLoad.json";
    var apiDisk = loginData.apiurl + "/host/getDiskPercent.json";

    var apiExec = loginData.apiurl + "/states/change/";

    var inProgress = 0;
    getRunStatus();
    getLoadStatus();
    getDiskStatus();

    function getDiskStatus() {
        $http.get(apiDisk)
            .then(
                function (success) {
                    var obj = success.data.usage;
                    var du = 0;
                    console.log("DISK:" + JSON.stringify(success));
                    for (var p in obj) {
                        if (obj.hasOwnProperty(p)) {
                            du += parseFloat(obj[p].space);

                        }
                    }
                    $scope.zmDisk = du.toFixed(1).toString() + "G";

                },
                function (error) {
                    $scope.zmDisk = "unknown";
                    console.log("ERROR:" + JSON.stringify(error));
                }
            );
    }

    function getRunStatus() {
        $http.get(apiRun)
            .then(
                function (success) {
                    switch (success.data.result) {
                    case 1:
                        $scope.zmRun = 'running';
                        $scope.color = 'color:green;';
                        break;
                    case 0:
                        $scope.zmRun = 'stopped';
                        $scope.color = 'color:red;';
                        break;
                    default:
                        $scope.zmRun = 'undetermined';
                        $scope.color = 'color:orange;';
                        break;
                    }


                    // console.log("X"+success.data.result+"X");
                },
                function (error) {
                    console.log("ERROR in getRun: " + JSON.stringify(error));
                    $scope.color = 'color:red;';
                    $scope.zmRun = 'undetermined';
                }
            );

    }


    function getLoadStatus() {
        $http.get(apiLoad)
            .then(
                function (success) {
                    //console.log(JSON.stringify(success));
                    // load returns 3 params - one in the middle is avg.
                    $scope.zmLoad = success.data.load[1];


                    // console.log("X"+success.data.result+"X");
                },
                function (error) {
                    console.log("ERROR in getLoad: " + JSON.stringify(error));
                    $scope.zmLoad = 'undetermined';
                }
            );
    }




    $scope.controlZM = function (str) {
        if (inProgress) {
            $ionicPopup.alert({
                title: "Operation in Progress",
                template: "The previous operation is still in progress. Please wait..."
            });
            return;
        }


        $ionicPopup.show({
            title: 'Please Confirm',
            template: 'Are you sure you want to ' + str + ' Zoneminder?',
            buttons: [
                {
                    text: 'Cancel',
                    type: 'button-positive'
                        },
                {
                    text: 'Yes, ' + str + ' Zoneminder',
                    type: 'button-assertive',
                    onTap: function (e) {
                        $scope.zmRun = "please wait...";
                        $scope.color = 'color:orange;';
                        console.log("Control command is " + apiExec + str + ".json");
                        inProgress = 1;
                        $http.post(apiExec + str + ".json")
                            .then(
                                function (success) {
                                    switch (str) {
                                    case "stop":
                                        $scope.zmRun = 'stopped';
                                        $scope.color = 'color:red;';
                                        break;
                                    case "start":
                                    case "restart":
                                        $scope.zmRun = 'running';
                                        $scope.color = 'color:green;';
                                        break;

                                    }
                                    inProgress = 0;
                                },
                                function (error) {
                                    //if (error.status) // it seems to return error with status 0 if ok
                                    // {
                                    console.log("ERROR in Change State:" + JSON.stringify(error));
                                    $scope.zmRun = 'undetermined';
                                    $scope.color = 'color:orange;';
                                    inProgress = 0;
                                    //   }
                                    /*else
                                    {
                                        switch (str)
                                        {
                                            case "stop":$scope.zmRun = 'stopped';
                                                $scope.color='color:red;'; break;
                                            case "start":
                                            case "restart":$scope.zmRun = 'running';
                                                $scope.color = 'color:green;';break;

                                        }
                                        inProgress = 0;
                                    }*/

                                }

                            );

                    }
                        }
                    ]
        });

    };

    $scope.openMenu = function () {
        $ionicSideMenuDelegate.toggleLeft();
    };

    $scope.$on('$ionicView.leave', function () {
        console.log("**VIEW ** State Ctrl Left");
        // FIXME not the best way...
        inProgress = 0;
    });

    $scope.reloadView = function () {
        console.log("*** Refreshing Modal view ***");
        inProgress = 0;
        $scope.rand = Math.floor(Math.random() * (999999 - 111111 + 1)) + 111111;
        $ionicLoading.show({
            template: "refreshed view",
            noBackdrop: true,
            duration: 2000
        });
    };

    $scope.doRefresh = function () {
        console.log("***Pull to Refresh");
        getRunStatus();
        getLoadStatus();
        getDiskStatus();
        $scope.$broadcast('scroll.refreshComplete');

    };

}]);