44using System . Collections . Specialized ;
55using System . Linq ;
66using System . Net ;
7+ using System . Net . Http ;
8+ using System . Net . Http . Headers ;
79using System . Text ;
810
911namespace AssistantComputerControl {
1012 class AnalyticsSettings {
1113 //private static string requestKey = null; // < version 1.1
1214 const string getKeyUrl = "https://acc.albe.pw/functions/ReceiveAnalyticsData.php" ;
1315 const string sendDataUrl = "https://acc.albe.pw/functions/ReceiveAnalyticsData.php" ;
16+ private static readonly HttpClient client = new HttpClient ( ) ;
1417
15- public static readonly string [ ] actions = new String [ 12 ] { //No changing this order!
18+ public static readonly string [ ] actions = new String [ 13 ] { //No changing this order!
1619 "shutdown" , //0
1720 "restart" , //1
1821 "open" , //2
@@ -24,37 +27,69 @@ class AnalyticsSettings {
2427 "previous_song" , //8
2528 "play_pause" , //9
2629 "next_song" , //10
27- "die" //11
30+ "die" , //11
31+ "hibernate" , //12
32+ } ;
33+
34+ public static readonly string [ ] assistants = new String [ 4 ] { //No changing this order!
35+ "google" ,
36+ "alexa" ,
37+ "cortana" ,
38+ "unknown" ,
2839 } ;
2940
3041 class KeyHandler {
3142 public bool Status { get ; set ; }
3243 public string Message { get ; set ; }
3344 public string Key { get ; set ; }
3445 }
35-
46+
3647 public static void SetupAnalyticsAsync ( ) {
48+ //Unique user-ID
49+ if ( Properties . Settings . Default . UID == "" || Properties . Settings . Default . UID == null ) {
50+ string newUID = Guid . NewGuid ( ) . ToString ( "N" ) ;
51+ Properties . Settings . Default . UID = newUID ;
52+ Properties . Settings . Default . Save ( ) ;
53+
54+ MainProgram . DoDebug ( "Set UID to " + newUID ) ;
55+ } else {
56+ MainProgram . DoDebug ( "UID; " + Properties . Settings . Default . UID ) ;
57+ }
58+
3759 if ( Properties . Settings . Default . SendAnonymousAnalytics ) {
60+ MainProgram . DoDebug ( "Setting up annonymous analytics..." ) ;
61+ //Execution amounts
3862 if ( Properties . Settings . Default . TotalActionsExecuted == null ) {
3963 Properties . Settings . Default . TotalActionsExecuted = new int [ actions . Length - 1 ] ;
4064 Properties . Settings . Default . Save ( ) ;
4165 }
4266
43- MainProgram . DoDebug ( "Annonymous analytics setup..." ) ;
67+ int [ ] actionsExecuted = Properties . Settings . Default . TotalActionsExecuted ;
68+ if ( actions . Length != actionsExecuted . Length ) {
69+ MainProgram . DoDebug ( actions . Length + "!= " + actionsExecuted . Length ) ;
4470
45- //Request analytics-key < For version 1.1
46- //RequestKey();
71+ //New action most likely added
72+ int [ ] oldSettings = actionsExecuted
73+ , newSettings = new int [ actions . Length ] ;
4774
48- //Setup array
49- int [ ] actionsExecuted = Properties . Settings . Default . TotalActionsExecuted ;
50- if ( actionsExecuted . Length != actions . Length ) {
51- int i = actionsExecuted . Length - 1 ;
52- while ( i < actions . Length - 1 ) {
53- actionsExecuted [ i ] = 0 ;
75+ //Populate new analytics array with old values
76+ int i = 0 ;
77+ foreach ( int ac in oldSettings ) {
78+ newSettings [ i ] = ac ;
5479 i ++ ;
5580 }
81+
82+ Properties . Settings . Default . TotalActionsExecuted = newSettings ;
83+ Properties . Settings . Default . Save ( ) ;
84+ }
85+
86+ //Assistant type
87+ if ( Properties . Settings . Default . AssistantType == null ) {
88+ Properties . Settings . Default . AssistantType = new int [ actions . Length - 1 ] ;
5689 Properties . Settings . Default . Save ( ) ;
5790 }
91+
92+ MainProgram . DoDebug ( "Annonymous analytics setup done" ) ;
5893 } else {
5994 MainProgram . DoDebug ( "Annonymous analytics are not being shared" ) ;
6095 }
@@ -65,7 +100,7 @@ public static void PrintAnalytics() {
65100 , totalCount = 0 ;
66101 if ( Properties . Settings . Default . TotalActionsExecuted != null ) {
67102 foreach ( int count in Properties . Settings . Default . TotalActionsExecuted ) {
68- MainProgram . DoDebug ( actions [ i ] + ": executed " + count + " times" ) ;
103+ MainProgram . DoDebug ( i + ". " + actions [ i ] + ": executed " + count + " times" ) ;
69104 i ++ ;
70105
71106 totalCount += count ;
@@ -75,7 +110,8 @@ public static void PrintAnalytics() {
75110 MainProgram . DoDebug ( "\n Total executions; " + totalCount ) ;
76111 }
77112
78- public static void AddCount ( string action ) {
113+ public static void AddCount ( string action , string type ) {
114+ AddTypeCount ( type ) ;
79115 int pos = Array . IndexOf ( actions , action ) ;
80116 if ( pos > - 1 ) {
81117 Properties . Settings . Default . TotalActionsExecuted [ pos ] ++ ;
@@ -84,7 +120,8 @@ public static void AddCount(string action) {
84120 MainProgram . DoDebug ( "Could not find action \" " + action + "\" in action-array (analytics)" ) ;
85121 }
86122 }
87- public static void AddCount ( int action ) {
123+ public static void AddCount ( int action , string type ) {
124+ AddTypeCount ( type ) ;
88125 if ( actions [ action ] != null ) {
89126 Properties . Settings . Default . TotalActionsExecuted [ action ] ++ ;
90127 Properties . Settings . Default . Save ( ) ;
@@ -93,48 +130,36 @@ public static void AddCount(int action) {
93130 }
94131 }
95132
96- /*private static void RequestKey() {
97- using (var wb = new WebClient()) {
98- string actionsExecutedString = string.Join(",", Properties.Settings.Default.TotalActionsExecuted);
99-
100- var data = new NameValueCollection {
101- ["key"] = requestKey,
102- ["actions_executed"] = actionsExecutedString
103- };
104-
105- var response = wb.UploadValues(getKeyUrl, "POST", data);
106- KeyHandler jsonResponse = JsonConvert.DeserializeObject<KeyHandler>(Encoding.UTF8.GetString(response));
107- if (jsonResponse.Status) {
108- requestKey = jsonResponse.Key;
109- MainProgram.DoDebug("Successfully requested analytics key. Got \"" + requestKey + "\"");
133+ private static void AddTypeCount ( string type ) {
134+ if ( type == null ) {
135+ Properties . Settings . Default . AssistantType [ 3 ] ++ ;
136+ Properties . Settings . Default . Save ( ) ;
137+ } else {
138+ int pos = Array . IndexOf ( assistants , type ) ;
139+ if ( pos > - 1 ) {
140+ Properties . Settings . Default . AssistantType [ pos ] ++ ;
141+ Properties . Settings . Default . Save ( ) ;
110142 } else {
111- MainProgram.DoDebug("Failed to get analytics key; " + jsonResponse.Message );
143+ MainProgram . DoDebug ( "Assistant type \" " + type + " \" doesn't exist in assistant-array (analytics)" ) ;
112144 }
113145 }
114- }*/
146+ }
115147
116- /*public static void SendData() { // < for version 1.1
117- if (requestKey != null) {
118- if (ACC_Updater.RemoteFileExists(sendDataUrl)) {
119- using (var wb = new WebClient()) {
120- string actionsExecutedString = string.Join(",", Properties.Settings.Default.TotalActionsExecuted);
121-
122- var data = new NameValueCollection {
123- ["key"] = requestKey,
124- ["actions_executed"] = actionsExecutedString
125- };
126-
127- var response = wb.UploadValues(sendDataUrl, "POST", data);
128- string responseInString = Encoding.UTF8.GetString(response);
129-
130- ... deserialize json
131- }
132- }
133- } else {
134- MainProgram.DoDebug("Attempted to send analytics data, but the key was null, trying to request new");
135- RequestKey();
148+ /*public static async System.Threading.Tasks.Task SendDataAsync() { // < for version 1.1
149+ if (ACC_Updater.RemoteFileExists(sendDataUrl)) {
150+ using (var wb = new WebClient()) {
136151
137- ...
152+ var values = new Dictionary<string, string> {
153+ { "actions_executed", JsonConvert.SerializeObject(Properties.Settings.Default.TotalActionsExecuted) },
154+ { "thing2", "world" }
155+ };
156+ var content = new FormUrlEncodedContent(values);
157+ var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);
158+
159+ client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "Your Oauth token");
160+
161+ var responseString = await response.Content.ReadAsStringAsync();
162+ }
138163 }
139164 }*/
140165 }
0 commit comments