1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.pmc;
18 
19 import android.util.Log;
20 
21 import org.json.JSONArray;
22 import org.json.JSONException;
23 import org.json.JSONObject;
24 
25 import java.io.BufferedWriter;
26 import java.io.File;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 
30 
31 /**
32  * Logging class to log status so PMC can communicate the status back to client
33  */
34 public class PMCStatusLogger {
35     private File mFile;
36     public static String TAG;
37     public static String LOG_DIR = "/mnt/sdcard/Download";
38     public static JSONObject mJObject;
39     public static JSONArray mJArray;
40 
41     /**
42      * Construtor - check if the file exist. If it is delete and create a new.
43      *
44      * @param message - message to be logged
45      */
PMCStatusLogger(String fileName, String tag)46     public PMCStatusLogger(String fileName, String tag) {
47         TAG = tag;
48 
49         try {
50             mFile = new File(LOG_DIR + "/" + fileName);
51             if (mFile.exists()) mFile.delete();
52             mFile.createNewFile();
53         } catch (IOException e) {
54             Log.e(TAG, "Exception creating log file: " + fileName + " " + e);
55         }
56         mJObject = new JSONObject();
57         mJArray = new JSONArray();
58     }
59 
60     /**
61      * Function to log status message into log file
62      *
63      * @param message - message to be logged
64      */
logStatus(String message)65     public void logStatus(String message) {
66         try {
67             FileWriter fos = new FileWriter(mFile);
68             BufferedWriter bw = new BufferedWriter(fos);
69             bw.write(message);
70             bw.newLine();
71             bw.close();
72         } catch (IOException e) {
73             Log.e(TAG, "Exception writing log: " + message + " " + e);
74         }
75     }
76 
77     /**
78      * Function to add alarm times into JSONArray object
79      *
80      * @param startTime - Start time for the cycle
81      * @param endTime - End time for the cycle
82      */
logAlarmTimes(double startTime, double endTime)83     public void logAlarmTimes(double startTime, double endTime) {
84         JSONObject obj = new JSONObject();
85         try {
86             obj.put("StartTime", startTime);
87             obj.put("EndTime", endTime);
88             mJArray.put(obj);
89         } catch (JSONException e) {
90             Log.e(TAG, "Exception to put Alarm Times into JSONArray: " + e);
91         }
92     }
93 
94     /**
95      * Function to save Json object into log file
96      *
97      */
flash()98     public void flash() {
99         try {
100             mJObject.put("AlarmTimes", mJArray);
101 
102             FileWriter fos = new FileWriter(mFile);
103             BufferedWriter bw = new BufferedWriter(fos);
104             Log.v(TAG, "JSON: " + mJObject.toString());
105             bw.write(mJObject.toString());
106             bw.newLine();
107             bw.close();
108         } catch (JSONException e) {
109             Log.e(TAG, "Exception to put JSONArray into main JSON object: " + e);
110         } catch (IOException e) {
111             Log.e(TAG, "Exception writing JSON to log file: " + e);
112         }
113     }
114 
115 }
116 
117