Skip to content
This repository
Browse code

win app: added basics of input handling, trying to figure out setting…

…s files
  • Loading branch information...
commit 71849144ae4d0e320930b47ab229a6e7cb60e9d7 1 parent 826a3c2
Tod E. Kurt authored January 03, 2013
2  windows/Blink1Control/Blink1Control/Blink1Control.csproj
@@ -55,6 +55,7 @@
55 55
       <Generator>MSBuild:Compile</Generator>
56 56
       <SubType>Designer</SubType>
57 57
     </ApplicationDefinition>
  58
+    <Compile Include="Settings.cs" />
58 59
     <Page Include="MainWindow.xaml">
59 60
       <Generator>MSBuild:Compile</Generator>
60 61
       <SubType>Designer</SubType>
@@ -64,6 +65,7 @@
64 65
       <SubType>Code</SubType>
65 66
     </Compile>
66 67
     <Compile Include="blink1ControlServer.cs" />
  68
+    <Compile Include="Blink1Input.cs" />
67 69
     <Compile Include="Blink1Pattern.cs" />
68 70
     <Compile Include="MainWindow.xaml.cs">
69 71
       <DependentUpon>MainWindow.xaml</DependentUpon>
46  windows/Blink1Control/Blink1Control/Blink1Input.cs
... ...
@@ -0,0 +1,46 @@
  1
+using System;
  2
+using System.Collections.Generic;
  3
+using System.Linq;
  4
+using System.Text;
  5
+using System.Threading.Tasks;
  6
+using System.Runtime.Serialization;
  7
+
  8
+namespace Blink1Control
  9
+{
  10
+    [Serializable]
  11
+    public class Blink1Input
  12
+    {
  13
+        /// <summary>
  14
+        /// Name of the input 
  15
+        /// </summary>
  16
+        public string iname { get; set; }
  17
+        /// <summary>
  18
+        /// name of the color pattern to play if this input is triggered
  19
+        /// </summary>
  20
+        public string pname { get; set; }
  21
+        /// <summary>
  22
+        ///  type of input, can be: "ifttt", "url", "file", "script"
  23
+        /// </summary>
  24
+        public string type { get; set; }
  25
+        public string arg1 { get; set; }
  26
+        public string arg2 { get; set; }
  27
+        public string arg3 { get; set; }
  28
+        public string lastVal { get; set; }
  29
+
  30
+        public Blink1Input(string name, string type, string arg1, string arg2, string arg3)
  31
+        {
  32
+            this.iname = name; this.type = type;
  33
+            this.arg1 = arg1; this.arg2 = arg2; this.arg3 = arg3;
  34
+        }
  35
+
  36
+
  37
+        public void update()
  38
+        {
  39
+        }
  40
+
  41
+        public void stop()
  42
+        {
  43
+        }
  44
+
  45
+    }
  46
+}
2  windows/Blink1Control/Blink1Control/Properties/Settings.settings
... ...
@@ -1,4 +1,4 @@
1  
-<?xml version='1.0' encoding='utf-8'?>
  1
+<?xml version='1.0' encoding='utf-8'?>
2 2
 <SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="Blink1Control.Properties" GeneratedClassName="Settings">
3 3
   <Profiles />
4 4
   <Settings>
304  windows/Blink1Control/Blink1Control/blink1ControlServer.cs
@@ -11,9 +11,12 @@
11 11
 using System.Diagnostics;
12 12
 using System.Configuration;
13 13
 using System.Globalization;
  14
+using System.Net;
14 15
 using MiniHttpd;
15 16
 using Newtonsoft.Json;
16 17
 using Blink1Lib;
  18
+using System.Collections.ObjectModel;
  19
+
17 20
 
18 21
 namespace Blink1Control
19 22
 {
@@ -24,35 +27,49 @@ class Blink1Server
24 27
         //JTokenWriter jtw = new JTokenWriter();
25 28
         Blink1 blink1 = new Blink1();
26 29
 
27  
-        Dictionary<string, object> inputs   = new Dictionary<string, object>();
  30
+        Dictionary<string, Blink1Input> inputs   = new Dictionary<string, Blink1Input>();
28 31
         Dictionary<string, Blink1Pattern> patterns = new Dictionary<string, Blink1Pattern>();
29 32
 
30 33
         // constructor
31 34
         public Blink1Server()
32 35
         {
33  
-            inputs   = new Dictionary<string, object>();
  36
+            inputs = new Dictionary<string, Blink1Input>();
34 37
             patterns = new Dictionary<string, Blink1Pattern>();
35  
-           
36  
-            blink1.hostId = (string) Properties.Settings.Default["hostId"];
  38
+
  39
+            blink1.hostId = (string)Properties.Settings.Default["hostId"];
  40
+            
37 41
             blink1.regenerateBlink1Id();
  42
+
  43
+            
  44
+            /*
  45
+            if (Properties.Settings.Default.TheInputs == null) {
  46
+                Console.WriteLine("*** New Settings! ****\n");
  47
+                Properties.Settings.Default.TheInputs = new ObservableCollection<Blink1Input>
  48
+                {
  49
+                    new Blink1Input("bob","url",null,null,null),
  50
+                    new Blink1Input("sue","file",null,null,null),
  51
+                    new Blink1Input("joe","script",null,null,null)
  52
+                };
  53
+                Properties.Settings.Default.Save();
  54
+            }
  55
+            else {
  56
+                Console.WriteLine("****\n\n found TheInputs Settings!\n\n **********");
  57
+                Console.WriteLine(Properties.Settings.Default.TheInputs.Count.ToString());
  58
+            }
  59
+*/
38 60
             Console.WriteLine("Blink1Server!");
39 61
 
40  
-            try
41  
-            {
  62
+            try {
42 63
                 VirtualDirectory root = new VirtualDirectory();
43 64
                 DriveDirectory homeDir = new DriveDirectory(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
44  
-                foreach (DriveDirectory d in homeDir.GetDirectories())
45  
-                {
  65
+                foreach (DriveDirectory d in homeDir.GetDirectories()) {
46 66
                     root.AddDirectory(d.Path);
47 67
                 }
48 68
 
49 69
                 VirtualDirectory blink1dir = new VirtualDirectory("blink1", root);
50 70
 
51  
-                //Blink1IDir b1dir = new Blink1IDir("test",blink1dir);
52  
-                //blink1dir.AddDirectory(b1dir);
53  
-
54 71
                 // FIXME: the below is completely gross, how to do HTTP routing in .NET?
55  
-                Blink1JSONFile id        = new Blink1JSONFile("id", blink1dir, this);
  72
+                Blink1JSONFile id = new Blink1JSONFile("id", blink1dir, this);
56 73
                 id.GetStringResponse = blink1Id;
57 74
                 blink1dir.AddFile(id);   //add a virtual file for each json method
58 75
 
@@ -61,7 +78,7 @@ public Blink1Server()
61 78
                 blink1dir.AddFile(enumerate);   //add a virtual file for each json method
62 79
 
63 80
                 Blink1JSONFile regen = new Blink1JSONFile("regenerateblink1id", blink1dir, this);
64  
-                regen.GetStringResponse  = blink1RegenerateBlink1Id;
  81
+                regen.GetStringResponse = blink1RegenerateBlink1Id;
65 82
                 blink1dir.AddFile(regen);
66 83
 
67 84
                 Blink1JSONFile fadeToRGB = new Blink1JSONFile("fadeToRGB", blink1dir, this);
@@ -92,14 +109,21 @@ public Blink1Server()
92 109
                 pattdelall.GetStringResponse = blink1PatternDelAll;
93 110
                 blink1dir.AddFile(pattdelall);
94 111
 
95  
-                Blink1JSONFile pattplay = new Blink1JSONFile("patternplay", blink1dir, this);
96  
-                pattplay.GetStringResponse = blink1PatternPlay;
97  
-                blink1dir.AddFile(pattplay);
  112
+                Blink1JSONFile input = new Blink1JSONFile("input", blink1dir, this);
  113
+                input.GetStringResponse = blink1Input;
  114
+                blink1dir.AddFile(input);
98 115
 
99  
-                Blink1JSONFile pattstop = new Blink1JSONFile("patternstop", blink1dir, this);
100  
-                pattstop.GetStringResponse = blink1PatternStop;
101  
-                blink1dir.AddFile(pattstop);
  116
+                Blink1JSONFile inputdel = new Blink1JSONFile("inputdel", blink1dir, this);
  117
+                inputdel.GetStringResponse = blink1InputDel;
  118
+                blink1dir.AddFile(inputdel);
102 119
 
  120
+                Blink1JSONFile inputdelall = new Blink1JSONFile("inputdelall", blink1dir, this);
  121
+                inputdelall.GetStringResponse = blink1InputDelAll;
  122
+                blink1dir.AddFile(inputdelall);
  123
+
  124
+                Blink1JSONFile inputurl = new Blink1JSONFile("inputurl", blink1dir, this);
  125
+                inputurl.GetStringResponse = blink1InputUrl;
  126
+                blink1dir.AddFile(inputurl);
103 127
 
104 128
                 /*
105 129
                                 Blink1JSONFile id2       = new Blink1JSONFile("id2", blink1dir, blink1);
@@ -112,26 +136,22 @@ public Blink1Server()
112 136
                 bhI.Root = root;
113 137
                 bhI.Start();
114 138
             }
115  
-            catch (Exception e)
116  
-            {
  139
+            catch (Exception e) {
117 140
                 Console.WriteLine(e.ToString());
118 141
             }
119 142
         }
120 143
 
121  
-        /// <summary>
122  
-        /// Same as Blink1.fadeToRGB, but does an open/close around it
123  
-        /// </summary>
124  
-        public void fadeToRGB(double secs, Color c)
125  
-        {
126  
-            Console.WriteLine("fadeToRGB: rgb:" + ColorTranslator.ToHtml(c) + " secs:" + secs);
127  
-            blink1.open();
128  
-            blink1.fadeToRGB((int)(secs * 1000), c.R, c.G, c.B);
129  
-            blink1.close();
130  
-        }
  144
+        // -----------------------------------------------------------------------------------------------
  145
+        // url methods
  146
+        //
131 147
 
132 148
         // why do we need this?
133 149
         public delegate string GetJSONStringResponse(HttpRequest request, Blink1Server aBlink1Server);
134 150
 
  151
+        //
  152
+        // direct blink(1) control url handling
  153
+        //
  154
+        
135 155
         //    /blink1/id -- Display blink1_id and blink1 serial numbers (if any)
136 156
         static string blink1Id(HttpRequest request, Blink1Server blink1Server)
137 157
         {
@@ -201,14 +221,14 @@ static string blink1FadeToRGB(HttpRequest request, Blink1Server blink1Server)
201 221
             string timestr = request.Query.Get("time");
202 222
             if (rgbstr == null) rgbstr = "#000000";
203 223
             if (timestr == null) timestr = "0.1";   
204  
-            Color colr = System.Drawing.ColorTranslator.FromHtml(rgbstr);
  224
+            Color colr = ColorTranslator.FromHtml(rgbstr);
205 225
             float secs = float.Parse(timestr, CultureInfo.InvariantCulture);
206 226
 
207 227
             blink1Server.fadeToRGB(secs, colr);
208 228
 
209 229
             Dictionary<string, object> result = new Dictionary<string, object>();
210 230
             result.Add("status", "fadeToRGB");
211  
-            result.Add("rgb", System.Drawing.ColorTranslator.ToHtml(colr));
  231
+            result.Add("rgb", ColorTranslator.ToHtml(colr));
212 232
             result.Add("time", secs.ToString("F2", CultureInfo.InvariantCulture));
213 233
             return JsonConvert.SerializeObject(result, Formatting.Indented);
214 234
         }
@@ -233,8 +253,8 @@ static string blink1Off(HttpRequest request, Blink1Server blink1Server)
233 253
             return JsonConvert.SerializeObject(result, Formatting.Indented);
234 254
         }
235 255
 
236  
-        //
237  
-        // color patterns
  256
+        // -----------------------------------------------------------------------------------------------
  257
+        // color patterns url handling
238 258
         //
239 259
 
240 260
         //    /blink1/pattern/ -- List saved color patterns
@@ -297,7 +317,7 @@ static string blink1PatternPlay(HttpRequest request, Blink1Server blink1Server)
297 317
             string pname = request.Query.Get("pname");
298 318
             string statusstr = "no pattern by that name";
299 319
             if (pname != null) {
300  
-                Blink1Pattern patt = null ;     
  320
+                Blink1Pattern patt = null ;
301 321
                 if( blink1Server.patterns.TryGetValue(pname, out patt)) {
302 322
                     patt.blink1Server = blink1Server; // justin case
303 323
                     patt.play();
@@ -326,22 +346,216 @@ static string blink1PatternStop(HttpRequest request, Blink1Server blink1Server)
326 346
             return JsonConvert.SerializeObject(result, Formatting.Indented);
327 347
         }
328 348
 
329  
-        
330  
-//    /blink1/lastColor -- Return the last color command sent to blink(1)
  349
+        // 
  350
+        // inputs
  351
+        //
  352
+
  353
+        //    /blink1/input/ -- List configured inputs, enable or disable input watching
  354
+        static string blink1Input(HttpRequest request, Blink1Server blink1Server)
  355
+        {
  356
+            Dictionary<string, object> result = new Dictionary<string, object>();
  357
+            result.Add("status", "input results");
  358
+            result.Add("inputs", blink1Server.inputs);
  359
+            return JsonConvert.SerializeObject(result, Formatting.Indented);
  360
+        }
  361
+        //    /blink1/input/del -- Remove a configured input
  362
+        static string blink1InputDel(HttpRequest request, Blink1Server blink1Server)
  363
+        {
  364
+            string iname = request.Query.Get("iname");
  365
+            string statusstr = "no input by that name";
  366
+            Blink1Input input = null;
  367
+            if (iname != null) {
  368
+                input = blink1Server.inputs[iname];
  369
+                input.stop();
  370
+                blink1Server.inputs.Remove(iname);
  371
+                statusstr = "input '" + iname + "' removed";
  372
+            }
  373
+
  374
+            Dictionary<string, object> result = new Dictionary<string, object>();
  375
+            result.Add("status", statusstr);
  376
+            return JsonConvert.SerializeObject(result, Formatting.Indented);
  377
+        }
  378
+
  379
+        //    /blink1/input/delall -- Remove all configured inputs
  380
+        static string blink1InputDelAll(HttpRequest request, Blink1Server blink1Server)
  381
+        {
  382
+            foreach (KeyValuePair<string, Blink1Input> kvp in blink1Server.inputs) {
  383
+                kvp.Value.stop();
  384
+            }
  385
+            blink1Server.patterns.Clear();
  386
+            Dictionary<string, object> result = new Dictionary<string, object>();
  387
+            result.Add("status", "all patterns removed");
  388
+            return JsonConvert.SerializeObject(result, Formatting.Indented);
  389
+        }
  390
+
  391
+        //    /blink1/input/url -- Add and Start URL watcher on given URL
  392
+        static string blink1InputUrl(HttpRequest request, Blink1Server blink1Server)
  393
+        {
  394
+            string pname = request.Query.Get("pname");
  395
+            string iname = request.Query.Get("iname");
  396
+            string url   = request.Query.Get("url");
  397
+            string test  = request.Query.Get("test");
  398
+            if (iname == null) iname = pname;
  399
+            Boolean testmode = (test.Equals("on") || test.Equals("true"));
  400
+
  401
+            string statusstr = "must specifiy 'iname' and 'arg1' (url)";
  402
+
  403
+            if( url != null && iname != null ) {
  404
+                Blink1Input input = new Blink1Input(iname, "url", url, null, null);
  405
+                input.pname = pname;
  406
+                blink1Server.updateUrlInput(input);
  407
+                if (!testmode) {
  408
+                    blink1Server.inputs[iname] = input; // NOTE: this replaces input if already exists
  409
+                }
  410
+            }
  411
+
  412
+            Dictionary<string, object> result = new Dictionary<string, object>();
  413
+            result.Add("status", statusstr);
  414
+            return JsonConvert.SerializeObject(result, Formatting.Indented);
  415
+        }
  416
+
  417
+        //    /blink1/input/ifttt -- Add and Start watching messages from IFTTT webservice
  418
+        static string blink1InputIfttt(HttpRequest request, Blink1Server blink1Server)
  419
+        {
  420
+            string pname = request.Query.Get("pname");
  421
+            string iname = request.Query.Get("iname");
  422
+            string url = request.Query.Get("url");
  423
+            string test = request.Query.Get("test");
  424
+            if (iname == null) iname = pname;
  425
+
  426
+            // FIXME: insert magic here
  427
+
  428
+            Dictionary<string, object> result = new Dictionary<string, object>();
  429
+            result.Add("status", "not implemented yet");
  430
+            return JsonConvert.SerializeObject(result, Formatting.Indented);
  431
+        }
  432
+
  433
+        // ----------------------------------------------------------------------------------------
  434
+        // input update url handling
  435
+        //
  436
+
  437
+        /// <summary>
  438
+        /// Periodically update the inputs, triggering color patterns if needed
  439
+        /// Runs every 15(?) seconds
  440
+        /// </summary>
  441
+        public void updateInputs()
  442
+        {
  443
+            foreach (var pair in inputs) {
  444
+                Blink1Input input = pair.Value;
  445
+                if (input.type.Equals("ifttt")) { // FIXME: there's totally a better way of doing this
  446
+                    updateIftttInput(input);
  447
+                }
  448
+                else if (input.type.Equals("url")) {
  449
+                    updateUrlInput(input);
  450
+                }
  451
+                else if (input.type.Equals("file")) {
  452
+                }
  453
+                else if (input.type.Equals("script")) {
  454
+                }
  455
+            }
  456
+        }
  457
+
  458
+        public void updateIftttInput(Blink1Input input)
  459
+        {
  460
+
  461
+        }
  462
+
  463
+        public void updateUrlInput(Blink1Input input)
  464
+        {
  465
+            string url = input.arg1;
  466
+
  467
+            string resp = getContentOfUrl(url);
  468
+            if (resp == null) {
  469
+                input.lastVal = "bad url";
  470
+                return;
  471
+            }
  472
+            string patternstr = parsePatternOrColorString(resp);
  473
+            if (patternstr != null && !patternstr.Equals(input.lastVal)) {
  474
+                input.lastVal = patternstr;
  475
+                playPattern(patternstr);
  476
+            }
  477
+        }
  478
+
  479
+        /// <summary>
  480
+        /// 
  481
+        /// </summary>
  482
+        /// <param name="url"></param>
  483
+        /// <returns></returns>
  484
+        public string getContentOfUrl(string url)
  485
+        {
  486
+            string content = null;
  487
+            WebClient webclient = new WebClient();
  488
+            try {
  489
+                content = webclient.DownloadString(url);
  490
+            }
  491
+            catch (WebException we) {
  492
+                // WebException.Status holds useful information
  493
+                Console.WriteLine(we.Message + "\n" + we.Status.ToString());
  494
+            }
  495
+            catch (NotSupportedException ne) {
  496
+                Console.WriteLine(ne.Message);   // other errors
  497
+            } 
  498
+            return content;
  499
+        }
331 500
 
332  
-//Input Selection  //interface methods to patterns and watchers
  501
+        // ----------------------------------------------------------------------------------------
  502
+        // pattern handling
  503
+        //
333 504
 
334  
-//    /blink1/input/ -- List configured inputs, enable or disable input watching
  505
+        /// <summary>
  506
+        // Play a color pattern.
  507
+        // Can restart if already playing, or just leave be an already playing pattern
  508
+        //
  509
+        /// </summary>
  510
+        /// <param name="pname">pattern name / color code to play</param>
  511
+        /// <returns> true if pattern was played, false if not</returns>
  512
+        public Boolean playPattern(string pname)
  513
+        {
  514
+            if (pname == null) return false;
  515
+            // if rgb hex code, just do it
  516
+            if (pname.StartsWith("#")) {
  517
+                Color c = ColorTranslator.FromHtml(pname);
  518
+                fadeToRGB(0.1, c);
  519
+                return true;
  520
+            }
  521
+            // otherwise treat it as a color pattern name
  522
+            Blink1Pattern patt = null;
  523
+            if (patterns.TryGetValue(pname, out patt)) {
  524
+                patt.blink1Server = this; // just in case
  525
+                patt.play();
  526
+                return true;
  527
+            }
  528
+            return false;
  529
+        }
335 530
 
336  
-//    /blink1/input/del -- Remove a configured input
  531
+        /// <summary>
  532
+        /// 
  533
+        /// </summary>
  534
+        /// <param name="str"></param>
  535
+        /// <returns></returns>
  536
+        public string parsePatternOrColorString(string str)
  537
+        {
  538
+            return str;
  539
+        }
337 540
 
338  
-//    /blink1/input/delall -- Remove all configured inputs
  541
+        /// <summary>
  542
+        /// Same as Blink1.fadeToRGB, but does an open/close around it.
  543
+        /// Called by Blink1Patterns to send colors to blink(1)
  544
+        /// </summary>
  545
+        public void fadeToRGB(double secs, Color c)
  546
+        {
  547
+            Console.WriteLine("fadeToRGB: rgb:" + ColorTranslator.ToHtml(c) + " secs:" + secs);
  548
+            blink1.open();
  549
+            blink1.fadeToRGB((int)(secs * 1000), c.R, c.G, c.B);
  550
+            blink1.close();
  551
+        }
  552
+
  553
+
  554
+//    /blink1/lastColor -- Return the last color command sent to blink(1)
339 555
 
340  
-//    /blink1/input/ifttt -- Add and Start watching messages from IFTTT webservice
341 556
 
342 557
 //    /blink1/input/file -- Add and Start file watcher on given filepath
343 558
 
344  
-//    /blink1/input/url -- Add and Start URL watcher on given URL
345 559
 
346 560
 //    /blink1/input/script -- Add and Start command-line script executer
347 561
 
@@ -353,8 +567,6 @@ static string blink1PatternStop(HttpRequest request, Blink1Server blink1Server)
353 567
 
354 568
 
355 569
 
356  
-
357  
-
358 570
         // testing
359 571
         static string blink1Id2(HttpRequest request, Blink1 blink1)//example
360 572
         {

0 notes on commit 7184914

Please sign in to comment.
Something went wrong with that request. Please try again.