Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

win app: ifttt first light, slight refactoring (moving input methods …

…into Blink1Input), small changes to Blink1Lib API
  • Loading branch information...
commit 13b8f26cd8982e57134be8e8043acd31c5cf0756 1 parent f7165a6
@todbot authored
View
3  windows/Blink1Control/Blink1Control/App.config
@@ -13,9 +13,6 @@
<setting name="hostId" serializeAs="String">
<value>00000000</value>
</setting>
- <setting name="todtest" serializeAs="String">
- <value>hello</value>
- </setting>
</Blink1Control.Properties.Settings>
</userSettings>
</configuration>
View
2  windows/Blink1Control/Blink1Control/Blink1Control.csproj
@@ -55,8 +55,8 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
+ <Compile Include="Blink1Event.cs" />
<Compile Include="MySettings.cs" />
- <Compile Include="Settings.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
View
24 windows/Blink1Control/Blink1Control/Blink1Event.cs
@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Blink1Control
+{
+ // thanks to http://json2csharp.com/ for this
+ public class IftttEvent
+ {
+ public string blink1_id { get; set; }
+ public string name { get; set; }
+ public string source { get; set; }
+ public string date { get; set; }
+ }
+
+ public class IftttResponse
+ {
+ public List<IftttEvent> events { get; set; }
+ public int event_count { get; set; }
+ public string status { get; set; }
+ }
+}
View
223 windows/Blink1Control/Blink1Control/Blink1Input.cs
@@ -4,12 +4,18 @@
using System.Text;
using System.Threading.Tasks;
//using System.Runtime.Serialization;
+using System.Text.RegularExpressions;
+using System.Net;
+using System.IO;
+using Newtonsoft.Json;
namespace Blink1Control
{
// [Serializable]
public class Blink1Input
{
+
+ public Blink1Server blink1Server { private get; set; }
/// <summary>
/// Name of the input
/// </summary>
@@ -26,21 +32,236 @@ public class Blink1Input
public string arg2 { get; set; }
public string arg3 { get; set; }
public string lastVal { get; set; }
+ public string possibleVals { get; set; }
+
+ // each input can have an independent update interval that's greater than master interval
+ public DateTime lastTime;
+ private int updateInterval;
+ // holder of last valid response/content
+ private string lastContent;
+ // FIXME: hacks
+ public static DateTime iftttLastTime;
+ public static int iftttUpdateInterval;
+ public static string iftttLastContent;
+
+ /// <summary>
+ /// Constructor
+ /// </summary>
+ /// <param name="name"></param>
+ /// <param name="type"></param>
+ /// <param name="arg1"></param>
+ /// <param name="arg2"></param>
+ /// <param name="arg3"></param>
public Blink1Input(string name, string type, string arg1, string arg2, string arg3)
{
this.iname = name; this.type = type;
this.arg1 = arg1; this.arg2 = arg2; this.arg3 = arg3;
}
+ /// <summary>
+ ///
+ /// </summary>
+ public void stop()
+ {
+ }
+ /// <summary>
+ /// Called periodically by blink1Server
+ /// </summary>
public void update()
{
+ if (type.Equals("ifttt")) { // FIXME: there's totally a better way of doing this
+ updateIftttInput();
+ }
+ else if (type.Equals("url")) {
+ updateUrlInput();
+ }
+ else if (type.Equals("file")) {
+ updateFileInput();
+ }
+ else if (type.Equals("script")) {
+ updateScriptInput();
+ }
+
}
- public void stop()
+ // periodically fetch events from IFTTT gateway
+ public void updateIftttInput()
+ {
+ if (iftttLastContent==null) return;
+
+ // add interval checking
+
+ string rulename = arg1;
+
+ IftttResponse iftttResponse = JsonConvert.DeserializeObject<IftttResponse>( iftttLastContent );
+ if (iftttResponse.event_count > 0) {
+ foreach (IftttEvent ev in iftttResponse.events) {
+ possibleVals += ev.name;
+ lastVal = ev.source;
+ }
+ }
+ }
+
+ /// <summary>
+ /// Periodically fetch URL, looking for color patern name or color code
+ /// </summary>
+ public void updateUrlInput()
+ {
+ string url = arg1;
+ string resp = getContentsOfUrl(url);
+ lastContent = resp;
+ if (resp == null) {
+ lastVal = "bad url";
+ return;
+ }
+ string patternstr = parsePatternOrColorString(resp);
+
+ if (patternstr != null && !patternstr.Equals(lastVal)) {
+ lastVal = patternstr;
+ blink1Server.playPattern(patternstr);
+ }
+ }
+
+ /// <summary>
+ /// Periodically check file, looking for color pattern name or color code
+ ///
+ /// </summary>
+ public void updateFileInput()
+ {
+ string filepath = arg1;
+ string resp = getContentsOfFile(filepath);
+ lastContent = resp;
+ if (resp == null) {
+ lastVal = "bad filename";
+ return;
+ }
+ string patternstr = parsePatternOrColorString(resp);
+ if (patternstr != null && !patternstr.Equals(lastVal)) {
+ lastVal = patternstr;
+ blink1Server.playPattern(patternstr);
+ }
+ }
+
+ /// <summary>
+ /// Periodically excecute script, looking for color pattern name or color cod
+ /// </summary>
+ public void updateScriptInput()
+ {
+ string scriptname = arg1;
+ }
+
+ // ---------------------------------------------------------------------------------------
+ // static utility methods
+
+
+ /// <summary>
+ /// This is called peridically, but only once for all IFTTT inputs.
+ /// FIXE: This whole method is a super hack
+ /// </summary>
+ /// <param name="input"></param>
+ /// <param name="normalmode"></param>
+ public static void getIftttResponse(Boolean normalmode)
{
+ // only update URLs every 30 secs
+ DateTime now = DateTime.Now; ; //[[NSDate date] timeIntervalSince1970];
+ TimeSpan diff = now - iftttLastTime;
+ if( normalmode && (diff.Seconds < iftttUpdateInterval) ) {
+ return;
+ }
+ iftttLastTime = now;
+
+ string eventUrl = Blink1Server.iftttEventUrl +"/"+ Blink1Server.blink1Id; // FIXME: hack
+ iftttLastContent = getContentsOfUrl(eventUrl);
+ }
+
+ /// <summary>
+ /// Look for a color pattern designator or a HTML color hex code in a string
+ /// </summary>
+ /// <param name="str"></param>
+ /// <returns></returns>
+ public static string parsePatternOrColorString(string str)
+ {
+ // read color pattern, can be of form: pattern: "my pattern"
+ Regex patternregex = new Regex(@"\""*pattern\""*:\s*\""(.+?)\""");
+ Match matchp = patternregex.Match(str);
+ if (matchp.Success) {
+ return matchp.Groups[1].Value;
+ }
+ //
+ Regex hexcoderegex = new Regex(@"(#[A-Fa-f0-9]{6})");
+ Match matchc = hexcoderegex.Match(str.ToUpper());
+ if (matchc.Success) {
+ return matchc.Groups[1].Value;
+ }
+
+ //Color c = ColorTranslator.FromHtml(str);
+ return null;
}
+ /// <summary>
+ /// Get the contents of a URL into string
+ /// </summary>
+ /// <param name="url">the url to fetch</param>
+ /// <returns>contents of url or null</returns>
+ public static string getContentsOfUrl(string url)
+ {
+ // stolen from: http://stackoverflow.com/questions/9961220/webclient-read-content-of-error-page
+ Console.WriteLine("getContentsOfUrl:" + url); // FIXME: how to do debug logging better?
+ string content = null;
+ WebClient webclient = new WebClient();
+ try {
+ content = webclient.DownloadString(url);
+ }
+ catch (WebException we) {
+ // WebException.Status holds useful information
+ Console.WriteLine(we.Message + "\n" + we.Status.ToString()); // FIXME:
+ Stream receiveStream = we.Response.GetResponseStream();
+ Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
+ StreamReader readStream = new StreamReader(receiveStream, encode);
+ content = readStream.ReadToEnd();
+ }
+ catch (NotSupportedException ne) {
+ Console.WriteLine(ne.Message); // other errors
+ }
+ return content;
+ }
+ /// <summary>
+ /// Get contents of file into string
+ /// </summary>
+ /// <param name="filepath">full filepath of file to read</param>
+ /// <returns>contents of file or null</returns>
+ public static string getContentsOfFile(string filepath)
+ {
+ try {
+ using (StreamReader sr = new StreamReader(filepath)) {
+ String contents = sr.ReadToEnd();
+ Console.WriteLine("file contents:" + contents);
+ return contents;
+ }
+ }
+ catch (Exception e) {
+ Console.WriteLine("The file could not be read:");
+ Console.WriteLine(e.Message);
+ }
+ return null;
+ }
+
+ // stolen from http://stackoverflow.com/questions/3354893/how-can-i-convert-a-datetime-to-the-number-of-seconds-since-1970
+ public static DateTime ConvertFromUnixTimestamp(double timestamp)
+ {
+ DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
+ return origin.AddSeconds(timestamp);
+ }
+
+ public static double ConvertToUnixTimestamp(DateTime date)
+ {
+ DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
+ TimeSpan diff = date.ToUniversalTime() - origin;
+ return Math.Floor(diff.TotalSeconds);
+ }
+
+
}
}
View
106 windows/Blink1Control/Blink1Control/MySettings.cs
@@ -0,0 +1,106 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Xml.Serialization;
+using System.IO;
+
+namespace Blink1Control
+{
+ /*
+ using System.Collections.Generic;
+ using System.Configuration;
+
+ /// <summary>
+ /// Persistent store for my parameters.
+ /// </summary>
+ public class MySettings : ApplicationSettingsBase
+ {
+ /// <summary>
+ /// The instance lock.
+ /// </summary>
+ private static readonly object InstanceLock = new object();
+
+ /// <summary>
+ /// The instance.
+ /// </summary>
+ private static MySettings instance;
+
+ /// <summary>
+ /// Prevents a default instance of the <see cref="MySettings"/> class
+ /// from being created.
+ /// </summary>
+ private MySettings()
+ {
+ // don't need to do anything
+ }
+
+ /// <summary>
+ /// Gets the singleton.
+ /// </summary>
+ public static MySettings Instance
+ {
+ get
+ {
+ lock (InstanceLock)
+ {
+ if (instance == null)
+ {
+ instance = new MySettings();
+ }
+ }
+
+ return instance;
+ }
+ }
+
+ /// <summary>
+ /// Gets or sets the parameters.
+ /// </summary>
+ [UserScopedSetting]
+ [SettingsSerializeAs(SettingsSerializeAs.Binary)]
+ public Dictionary<string, string> Parameters
+ {
+ get
+ {
+ return (Dictionary<string, string>)this["Parameters"];
+ }
+
+ set
+ {
+ this["Parameters"] = value;
+ }
+ }
+ }
+ */
+
+ public class MySettings
+ {
+ public Dictionary<string, string> Parameters { get; set; }
+
+ public MySettings()
+ {
+ Parameters = new Dictionary<string,string>();
+ }
+ public static void saveSettings(MySettings settings)
+ {
+ Console.WriteLine("saveSettings!");
+ XmlSerializer mySerializer = new XmlSerializer(typeof(MySettings));
+ StreamWriter myWriter = new StreamWriter("c:/prefs.xml");
+ mySerializer.Serialize(myWriter, settings);
+ myWriter.Close();
+ }
+
+ public static MySettings loadSettings()
+ {
+ Console.WriteLine("loadSettings!");
+ XmlSerializer mySerializer = new XmlSerializer(typeof(MySettings));
+ FileStream myFileStream = new FileStream("c:/prefs.xml", FileMode.Open);
+ MySettings settings = (MySettings)mySerializer.Deserialize(myFileStream);
+ return settings;
+ }
+ }
+
+}
+
View
12 windows/Blink1Control/Blink1Control/Properties/Settings.Designer.cs
@@ -46,17 +46,5 @@ internal sealed partial class Settings : global::System.Configuration.Applicatio
this["hostId"] = value;
}
}
-
- [global::System.Configuration.UserScopedSettingAttribute()]
- [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
- [global::System.Configuration.DefaultSettingValueAttribute("hello")]
- public string todtest {
- get {
- return ((string)(this["todtest"]));
- }
- set {
- this["todtest"] = value;
- }
- }
}
}
View
3  windows/Blink1Control/Blink1Control/Properties/Settings.settings
@@ -8,8 +8,5 @@
<Setting Name="hostId" Type="System.String" Scope="User">
<Value Profile="(Default)">00000000</Value>
</Setting>
- <Setting Name="todtest" Type="System.String" Scope="User">
- <Value Profile="(Default)">hello</Value>
- </Setting>
</Settings>
</SettingsFile>
View
263 windows/Blink1Control/Blink1Control/blink1ControlServer.cs
@@ -11,38 +11,38 @@
using System.Diagnostics;
using System.Configuration;
using System.Globalization;
-using System.Net;
+using System.Collections.ObjectModel;
using MiniHttpd;
using Newtonsoft.Json;
using Blink1Lib;
-using System.Collections.ObjectModel;
-using System.Text.RegularExpressions;
namespace Blink1Control
{
- class Blink1Server
+ public class Blink1Server
{
static int httpPortDefault = 8934;
- static string iftttEventUrl = "http://api.thingm.com/blink1/events";
- static float iftttUpdateInterval = 15.0F;
- static float urlUpdateInterval = 15.0F;
+ public static string iftttEventUrl = "http://api.thingm.com/blink1/events";
+ public static float iftttUpdateInterval = 15.0F;
+ public static float urlUpdateInterval = 15.0F;
HttpWebServer bhI = new HttpWebServer( httpPortDefault );
//JTokenWriter jtw = new JTokenWriter();
- Blink1 blink1 = new Blink1();
+ static Blink1 Sblink1 = new Blink1();
Dictionary<string, Blink1Input> inputs = new Dictionary<string, Blink1Input>();
Dictionary<string, Blink1Pattern> patterns = new Dictionary<string, Blink1Pattern>();
- // constructor
- public Blink1Server()
- {
- inputs = new Dictionary<string, Blink1Input>();
- patterns = new Dictionary<string, Blink1Pattern>();
+ static JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
+ public void loadSettings()
+ {
+ // we assume hostId is always set, it defaults to "00000000" on fresh installation
+ // (or we could not set it in Properties and check for KeyNotFoundException)
blink1.hostId = (string)Properties.Settings.Default["hostId"];
-
+
+ Console.WriteLine("blink1.hostId:" + blink1.hostId);
+
blink1.regenerateBlink1Id();
//MySettings.Instance.Parameters["foo"] = "bar";
@@ -75,7 +75,27 @@ public Blink1Server()
Console.WriteLine(Properties.Settings.Default.TheInputs.Count.ToString());
}
*/
+ }
+
+ public void saveSettings()
+ {
+ Properties.Settings.Default["hostId"] = blink1.hostId;
+ Properties.Settings.Default.Save();
+ }
+
+ // constructor
+ public Blink1Server()
+ {
+ inputs = new Dictionary<string, Blink1Input>();
+ patterns = new Dictionary<string, Blink1Pattern>();
+
+ loadSettings();
+
Console.WriteLine("Blink1Server!");
+ Console.WriteLine("Running on port "+httpPortDefault);
+ Console.WriteLine("blink1Id:"+blink1Id);
+
+ saveSettings();
try {
VirtualDirectory root = new VirtualDirectory();
@@ -88,61 +108,68 @@ public Blink1Server()
// FIXME: the below is completely gross, how to do HTTP routing in .NET?
Blink1JSONFile id = new Blink1JSONFile("id", blink1dir, this);
- id.GetStringResponse = blink1Id;
+ id.GetStringResponse = Ublink1Id;
blink1dir.AddFile(id); //add a virtual file for each json method
Blink1JSONFile enumerate = new Blink1JSONFile("enumerate", blink1dir, this);
- enumerate.GetStringResponse = blink1Enumerate;
+ enumerate.GetStringResponse = Ublink1Enumerate;
blink1dir.AddFile(enumerate); //add a virtual file for each json method
Blink1JSONFile regen = new Blink1JSONFile("regenerateblink1id", blink1dir, this);
- regen.GetStringResponse = blink1RegenerateBlink1Id;
+ regen.GetStringResponse = Ublink1RegenerateBlink1Id;
blink1dir.AddFile(regen);
Blink1JSONFile fadeToRGB = new Blink1JSONFile("fadeToRGB", blink1dir, this);
- fadeToRGB.GetStringResponse = blink1FadeToRGB;
+ fadeToRGB.GetStringResponse = Ublink1FadeToRGB;
blink1dir.AddFile(fadeToRGB);
Blink1JSONFile on = new Blink1JSONFile("on", blink1dir, this);
- on.GetStringResponse = blink1On;
+ on.GetStringResponse = Ublink1On;
blink1dir.AddFile(on);
Blink1JSONFile off = new Blink1JSONFile("off", blink1dir, this);
- off.GetStringResponse = blink1Off;
+ off.GetStringResponse = Ublink1Off;
blink1dir.AddFile(off);
+ Blink1JSONFile lastColor = new Blink1JSONFile("lastColor", blink1dir, this);
+ lastColor.GetStringResponse = Ublink1LastColor;
+ blink1dir.AddFile(lastColor);
+
Blink1JSONFile pattern = new Blink1JSONFile("pattern", blink1dir, this);
- pattern.GetStringResponse = blink1Pattern;
+ pattern.GetStringResponse = Ublink1Pattern;
blink1dir.AddFile(pattern);
Blink1JSONFile pattadd = new Blink1JSONFile("patternadd", blink1dir, this);
- pattadd.GetStringResponse = blink1PatternAdd;
+ pattadd.GetStringResponse = Ublink1PatternAdd;
blink1dir.AddFile(pattadd);
Blink1JSONFile pattdel = new Blink1JSONFile("patterndel", blink1dir, this);
- pattdel.GetStringResponse = blink1PatternDel;
+ pattdel.GetStringResponse = Ublink1PatternDel;
blink1dir.AddFile(pattdel);
Blink1JSONFile pattdelall = new Blink1JSONFile("patterndelall", blink1dir, this);
- pattdelall.GetStringResponse = blink1PatternDelAll;
+ pattdelall.GetStringResponse = Ublink1PatternDelAll;
blink1dir.AddFile(pattdelall);
Blink1JSONFile input = new Blink1JSONFile("input", blink1dir, this);
- input.GetStringResponse = blink1Input;
+ input.GetStringResponse = Ublink1Input;
blink1dir.AddFile(input);
Blink1JSONFile inputdel = new Blink1JSONFile("inputdel", blink1dir, this);
- inputdel.GetStringResponse = blink1InputDel;
+ inputdel.GetStringResponse = Ublink1InputDel;
blink1dir.AddFile(inputdel);
Blink1JSONFile inputdelall = new Blink1JSONFile("inputdelall", blink1dir, this);
- inputdelall.GetStringResponse = blink1InputDelAll;
+ inputdelall.GetStringResponse = Ublink1InputDelAll;
blink1dir.AddFile(inputdelall);
Blink1JSONFile inputurl = new Blink1JSONFile("inputurl", blink1dir, this);
- inputurl.GetStringResponse = blink1InputUrl;
+ inputurl.GetStringResponse = Ublink1InputUrl;
blink1dir.AddFile(inputurl);
+ Blink1JSONFile inputifttt = new Blink1JSONFile("inputifttt", blink1dir, this);
+ inputifttt.GetStringResponse = Ublink1InputIfttt;
+ blink1dir.AddFile(inputifttt);
/*
Blink1JSONFile id2 = new Blink1JSONFile("id2", blink1dir, blink1);
id2.GetStringResponse = blink1Id2;
@@ -170,11 +197,11 @@ public Blink1Server()
//
// /blink1/id -- Display blink1_id and blink1 serial numbers (if any)
- static string blink1Id(HttpRequest request, Blink1Server blink1Server)
+ static string Ublink1Id(HttpRequest request, Blink1Server blink1Server)
{
Blink1 blink1 = blink1Server.blink1;
Dictionary<string, object> result = new Dictionary<string, object>();
- result.Add("blink1_id", blink1.getBlink1Id());
+ result.Add("blink1_id", blink1.blink1Id);
List<string> serialnums = new List<string>();
for( int i=0; i < blink1.getCachedCount(); i++ ) { // FIXME: surely a smarter way to do this
serialnums.Add( blink1.getCachedSerial(i) );
@@ -185,17 +212,17 @@ static string blink1Id(HttpRequest request, Blink1Server blink1Server)
}
// /blink1/enumerate -- Re-enumerate and List available blink(1) devices
- static string blink1Enumerate(HttpRequest request, Blink1Server blink1Server)
+ static string Ublink1Enumerate(HttpRequest request, Blink1Server blink1Server)
{
Blink1 blink1 = blink1Server.blink1;
- string blink1Id_old = blink1.getBlink1Id();
+ string blink1Id_old = blink1.blink1Id;
blink1.enumerate();
blink1.regenerateBlink1Id();
Dictionary<string, object> result = new Dictionary<string, object>();
result.Add("blink1_id_old", blink1Id_old);
- result.Add("blink1_id", blink1.getBlink1Id());
+ result.Add("blink1_id", blink1.blink1Id);
List<string> serialnums = new List<string>();
for (int i = 0; i < blink1.getCachedCount(); i++)
{ // FIXME: surely a smarter way to do this
@@ -207,10 +234,10 @@ static string blink1Enumerate(HttpRequest request, Blink1Server blink1Server)
}
// /blink1/regenerateblinkid -- Generate, save, and return new blink1_id
- static string blink1RegenerateBlink1Id(HttpRequest request, Blink1Server blink1Server)
+ static string Ublink1RegenerateBlink1Id(HttpRequest request, Blink1Server blink1Server)
{
Blink1 blink1 = blink1Server.blink1;
- string blink1Id_old = blink1.getBlink1Id();
+ string blink1Id_old = blink1.blink1Id;
blink1.hostId = null;
blink1.enumerate();
@@ -218,7 +245,7 @@ static string blink1RegenerateBlink1Id(HttpRequest request, Blink1Server blink1S
Dictionary<string, object> result = new Dictionary<string, object>();
result.Add("blink1_id_old", blink1Id_old);
- result.Add("blink1_id", blink1.getBlink1Id());
+ result.Add("blink1_id", blink1.blink1Id);
List<string> serialnums = new List<string>();
for (int i = 0; i < blink1.getCachedCount(); i++)
{ // FIXME: surely a smarter way to do this
@@ -230,12 +257,13 @@ static string blink1RegenerateBlink1Id(HttpRequest request, Blink1Server blink1S
}
// /blink1/fadeToRGB -- Send fadeToRGB command to blink(1) with hex color & fade time
- static string blink1FadeToRGB(HttpRequest request, Blink1Server blink1Server)
+ static string Ublink1FadeToRGB(HttpRequest request, Blink1Server blink1Server)
{
// FIXME: stop pattern player
//NameValueCollection query = request.Query;
string rgbstr = request.Query.Get("rgb");
string timestr = request.Query.Get("time");
+ Console.WriteLine("rgb: " + rgbstr);
if (rgbstr == null) rgbstr = "#000000";
if (timestr == null) timestr = "0.1";
Color colr = ColorTranslator.FromHtml(rgbstr);
@@ -245,13 +273,13 @@ static string blink1FadeToRGB(HttpRequest request, Blink1Server blink1Server)
Dictionary<string, object> result = new Dictionary<string, object>();
result.Add("status", "fadeToRGB");
- result.Add("rgb", ColorTranslator.ToHtml(colr));
+ result.Add("rgb", Blink1.colorToHexCode(colr) );
result.Add("time", secs.ToString("F2", CultureInfo.InvariantCulture));
return JsonConvert.SerializeObject(result, Formatting.Indented);
}
// /blink1/on -- Stop pattern playback and send fadeToRGB command to blink(1) with #FFFFFF & 0.1 sec fade time
- static string blink1On(HttpRequest request, Blink1Server blink1Server)
+ static string Ublink1On(HttpRequest request, Blink1Server blink1Server)
{
blink1Server.fadeToRGB(0.1, Color.White);
@@ -261,7 +289,7 @@ static string blink1On(HttpRequest request, Blink1Server blink1Server)
}
// /blink1/off -- Stop pattern playback and send fadeToRGB command to blink(1) with #000000 & 0.1 sec fade time
- static string blink1Off(HttpRequest request, Blink1Server blink1Server)
+ static string Ublink1Off(HttpRequest request, Blink1Server blink1Server)
{
blink1Server.fadeToRGB(0.1, Color.Black);
@@ -270,12 +298,22 @@ static string blink1Off(HttpRequest request, Blink1Server blink1Server)
return JsonConvert.SerializeObject(result, Formatting.Indented);
}
+ // /blink1/lastColor -- Return the last color command sent to blink(1)
+ static string Ublink1LastColor(HttpRequest request, Blink1Server blink1Server)
+ {
+ Dictionary<string, object> result = new Dictionary<string, object>();
+ result.Add("status", "lastColor");
+ result.Add("lastColor", blink1Server.blink1.lastColorString);
+ return JsonConvert.SerializeObject(result, Formatting.Indented);
+ }
+
+
// -----------------------------------------------------------------------------------------------
// color patterns url handling
//
// /blink1/pattern/ -- List saved color patterns
- static string blink1Pattern(HttpRequest request, Blink1Server blink1Server)
+ static string Ublink1Pattern(HttpRequest request, Blink1Server blink1Server)
{
Dictionary<string, object> result = new Dictionary<string, object>();
result.Add("status", "pattern results");
@@ -284,7 +322,7 @@ static string blink1Pattern(HttpRequest request, Blink1Server blink1Server)
}
// /blink1/pattern/add -- Add color pattern to color pattern list
- static string blink1PatternAdd(HttpRequest request, Blink1Server blink1Server)
+ static string Ublink1PatternAdd(HttpRequest request, Blink1Server blink1Server)
{
string pname = request.Query.Get("pname");
string patternstr = request.Query.Get("pattern");
@@ -299,7 +337,7 @@ static string blink1PatternAdd(HttpRequest request, Blink1Server blink1Server)
}
// /blink1/pattern/del -- Remove color pattern from color pattern list
- static string blink1PatternDel(HttpRequest request, Blink1Server blink1Server)
+ static string Ublink1PatternDel(HttpRequest request, Blink1Server blink1Server)
{
string pname = request.Query.Get("pname");
string statusstr = "no pattern by that name";
@@ -317,7 +355,7 @@ static string blink1PatternDel(HttpRequest request, Blink1Server blink1Server)
}
// /blink1/pattern/delall -- Remove all color patterns from color pattern list
- static string blink1PatternDelAll(HttpRequest request, Blink1Server blink1Server)
+ static string Ublink1PatternDelAll(HttpRequest request, Blink1Server blink1Server)
{
foreach (KeyValuePair<string, Blink1Pattern> kvp in blink1Server.patterns) {
kvp.Value.stop();
@@ -329,7 +367,7 @@ static string blink1PatternDelAll(HttpRequest request, Blink1Server blink1Server
}
// /blink1/pattern/play -- Play/test a specific color pattern
- static string blink1PatternPlay(HttpRequest request, Blink1Server blink1Server)
+ static string Ublink1PatternPlay(HttpRequest request, Blink1Server blink1Server)
{
string pname = request.Query.Get("pname");
string statusstr = "no pattern by that name";
@@ -347,7 +385,7 @@ static string blink1PatternPlay(HttpRequest request, Blink1Server blink1Server)
}
// /blink1/pattern/stop -- Stop a pattern playback, for a given pattern or all patterns
- static string blink1PatternStop(HttpRequest request, Blink1Server blink1Server)
+ static string Ublink1PatternStop(HttpRequest request, Blink1Server blink1Server)
{
string pname = request.Query.Get("pname");
string statusstr = "no pattern by that name";
@@ -368,7 +406,7 @@ static string blink1PatternStop(HttpRequest request, Blink1Server blink1Server)
//
// /blink1/input/ -- List configured inputs, enable or disable input watching
- static string blink1Input(HttpRequest request, Blink1Server blink1Server)
+ static string Ublink1Input(HttpRequest request, Blink1Server blink1Server)
{
Dictionary<string, object> result = new Dictionary<string, object>();
result.Add("status", "input results");
@@ -376,7 +414,7 @@ static string blink1Input(HttpRequest request, Blink1Server blink1Server)
return JsonConvert.SerializeObject(result, Formatting.Indented);
}
// /blink1/input/del -- Remove a configured input
- static string blink1InputDel(HttpRequest request, Blink1Server blink1Server)
+ static string Ublink1InputDel(HttpRequest request, Blink1Server blink1Server)
{
string iname = request.Query.Get("iname");
string statusstr = "no input by that name";
@@ -394,7 +432,7 @@ static string blink1InputDel(HttpRequest request, Blink1Server blink1Server)
}
// /blink1/input/delall -- Remove all configured inputs
- static string blink1InputDelAll(HttpRequest request, Blink1Server blink1Server)
+ static string Ublink1InputDelAll(HttpRequest request, Blink1Server blink1Server)
{
foreach (KeyValuePair<string, Blink1Input> kvp in blink1Server.inputs) {
kvp.Value.stop();
@@ -406,7 +444,7 @@ static string blink1InputDelAll(HttpRequest request, Blink1Server blink1Server)
}
// /blink1/input/url -- Add and Start URL watcher on given URL
- static string blink1InputUrl(HttpRequest request, Blink1Server blink1Server)
+ static string Ublink1InputUrl(HttpRequest request, Blink1Server blink1Server)
{
string pname = request.Query.Get("pname");
string iname = request.Query.Get("iname");
@@ -421,8 +459,10 @@ static string blink1InputUrl(HttpRequest request, Blink1Server blink1Server)
if( url != null && iname != null ) {
statusstr = "input url";
input = new Blink1Input(iname, "url", url, null, null);
+ input.blink1Server = blink1Server;
input.pname = pname;
- blink1Server.updateUrlInput(input);
+
+ input.updateUrlInput();
if (!testmode) {
blink1Server.inputs[iname] = input; // NOTE: this replaces input if already exists
}
@@ -431,23 +471,43 @@ static string blink1InputUrl(HttpRequest request, Blink1Server blink1Server)
Dictionary<string, object> result = new Dictionary<string, object>();
result.Add("status", statusstr);
result.Add("input", input);
- return JsonConvert.SerializeObject(result, Formatting.Indented);
+ return JsonConvert.SerializeObject(result, Formatting.Indented, jsonSerializerSettings);
}
// /blink1/input/ifttt -- Add and Start watching messages from IFTTT webservice
- static string blink1InputIfttt(HttpRequest request, Blink1Server blink1Server)
+ static string Ublink1InputIfttt(HttpRequest request, Blink1Server blink1Server)
{
string pname = request.Query.Get("pname");
string iname = request.Query.Get("iname");
- string url = request.Query.Get("url");
+ string rulename = request.Query.Get("arg1");
string test = request.Query.Get("test");
if (pname == null) pname = iname;
Boolean testmode = (test == null) ? false : (test.Equals("on") || test.Equals("true"));
- // FIXME: insert magic here
+ string statusstr = "must specifiy 'iname' and 'arg1' (rulename)";
+
+ Blink1Input input = null;
+ if (rulename != null && iname != null) {
+ input = new Blink1Input(iname, "ifttt", rulename, null, null);
+ input.blink1Server = blink1Server;
+ input.pname = pname;
+
+ // FIXME: insert magic here
+ //if (testmode) {
+ Blink1Input.getIftttResponse(false);
+ //}
+
+ input.updateIftttInput();
+ if (!testmode) {
+ blink1Server.inputs[iname] = input; // NOTE: this replaces input if already exists
+ }
+
+ statusstr = "input ifttt";
+ }
Dictionary<string, object> result = new Dictionary<string, object>();
- result.Add("status", "not implemented yet");
+ result.Add("status", statusstr);
+ result.Add("input", input);
return JsonConvert.SerializeObject(result, Formatting.Indented);
}
@@ -463,62 +523,10 @@ public void updateInputs()
{
foreach (var pair in inputs) {
Blink1Input input = pair.Value;
- if (input.type.Equals("ifttt")) { // FIXME: there's totally a better way of doing this
- updateIftttInput(input);
- }
- else if (input.type.Equals("url")) {
- updateUrlInput(input);
- }
- else if (input.type.Equals("file")) {
- }
- else if (input.type.Equals("script")) {
- }
- }
- }
-
- public void updateIftttInput(Blink1Input input)
- {
-
- }
-
- public void updateUrlInput(Blink1Input input)
- {
- string url = input.arg1;
-
- string resp = getContentOfUrl(url);
- if (resp == null) {
- input.lastVal = "bad url";
- return;
- }
- string patternstr = parsePatternOrColorString(resp);
-
- if (patternstr != null && !patternstr.Equals(input.lastVal)) {
- input.lastVal = patternstr;
- playPattern(patternstr);
+ input.update();
}
}
- /// <summary>
- ///
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- public string getContentOfUrl(string url)
- {
- string content = null;
- WebClient webclient = new WebClient();
- try {
- content = webclient.DownloadString(url);
- }
- catch (WebException we) {
- // WebException.Status holds useful information
- Console.WriteLine(we.Message + "\n" + we.Status.ToString());
- }
- catch (NotSupportedException ne) {
- Console.WriteLine(ne.Message); // other errors
- }
- return content;
- }
// ----------------------------------------------------------------------------------------
// pattern handling
@@ -551,30 +559,6 @@ public Boolean playPattern(string pname)
}
/// <summary>
- ///
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public string parsePatternOrColorString(string str)
- {
- // read color pattern, can be of form: pattern: "my pattern"
- Regex patternregex = new Regex(@"\""*pattern\""*:\s*\""(.+?)\""");
- Match matchp = patternregex.Match(str);
- if( matchp.Success ) {
- return matchp.Groups[1].Value;
- }
- //
- Regex hexcoderegex = new Regex(@"(#[A-Fa-f0-9]{6})");
- Match matchc = hexcoderegex.Match(str.ToUpper());
- if( matchc.Success ) {
- return matchc.Groups[1].Value;
- }
-
- //Color c = ColorTranslator.FromHtml(str);
- return null;
- }
-
- /// <summary>
/// Same as Blink1.fadeToRGB, but does an open/close around it.
/// Called by Blink1Patterns to send colors to blink(1)
/// </summary>
@@ -586,8 +570,9 @@ public void fadeToRGB(double secs, Color c)
blink1.close();
}
+ public Blink1 blink1 { get { return Blink1Server.Sblink1; } private set { } }
+ public static string blink1Id { get { return Blink1Server.Sblink1.blink1Id; } }
-// /blink1/lastColor -- Return the last color command sent to blink(1)
// /blink1/input/file -- Add and Start file watcher on given filepath
@@ -601,18 +586,20 @@ public void fadeToRGB(double secs, Color c)
// /blink1/input/netload -- Start network load watching input
-
-
// testing
- static string blink1Id2(HttpRequest request, Blink1 blink1)//example
+ static string Ublink1Id2(HttpRequest request, Blink1Server blink1Server)//example
{
- Properties.Settings.Default["blink1Id"] = blink1.getBlink1Id();
+ //Properties.Settings.Default["blink1Id"] = blink1Server.blink1Id;
Properties.Settings.Default.Save();
//Settings.Default["blink1Id"] = blink1.getBlink1Id();
//Settings.Default.Save(); // Saves settings in application configuration file
return @"\n\n{ suck it }\n\n";
}
+ //
+ // ---------------------------------------------------------------------------------------
+ //
+
/// <summary>
/// Weirdass wrapper for binding urls to funcblocks, why do we need this?
/// </summary>
View
49 windows/Blink1Lib/Blink1Lib/Blink1.cs
@@ -10,7 +10,21 @@ namespace Blink1Lib
{
public class Blink1
{
- string blink1Id;
+ /// <summary>
+ /// Returns the blink(1) uid (aka IFTTT key)
+ /// </summary>
+ /// <returns>blink1_uid</returns>
+ public string blink1Id { get; private set; }
+ /// <summary>
+ /// hostid is a component of the blink1Id (aka IFTT key)
+ /// </summary>
+ public string hostId { get; set; }
+ /// <summary>
+ ///
+ /// </summary>
+ public Color lastColor { get; private set; }
+
+ public string lastColorString { get { return Blink1.colorToHexCode(lastColor); } }
static void Main(string[] args)
{
@@ -23,25 +37,7 @@ static void Main(string[] args)
public Blink1()
{
enumerate();
- }
-
- /// <summary>
- /// hostid is a component of the blink1Id (aka IFTT key)
- /// </summary>
- public string hostId { get; set; }
-
- /// <summary>
- /// Returns the blink(1) uid (aka IFTTT key)
- /// </summary>
- /// <returns>blink1_uid</returns>
- public string getBlink1Id()
- {
- return blink1Id; // FIXME: how to make this get-only?
- }
-
- public Color getLastColor()
- {
- return Color.Black;
+ lastColor = Color.Black;
}
/// <summary>
@@ -51,6 +47,7 @@ public Color getLastColor()
/// <returns>blink1_id</returns>
public string regenerateBlink1Id()
{
+ Console.WriteLine("hostId:" + hostId);
if (hostId == null || hostId.Equals("00000000") )
{
Random rand = new Random();
@@ -105,6 +102,7 @@ public void close()
/// <param name="b">blue component</param>
public void setRGB(int r, int g, int b)
{
+ lastColor = Color.FromArgb(r, g, b);
blink1_setRGB(dev, r, g, b);
}
@@ -117,10 +115,21 @@ public void setRGB(int r, int g, int b)
/// <param name="b">blue component</param>
public void fadeToRGB(int millis, int r, int g, int b)
{
+ lastColor = Color.FromArgb(r, g, b);
blink1_fadeToRGB(dev, millis, r, g, b);
}
/// <summary>
+ /// Convert a Color to a HTML hex code (e.g."#FFCC33")
+ /// </summary>
+ /// <param name="c">color to convert</param>
+ /// <returns>hex code string</returns>
+ public static String colorToHexCode(Color c)
+ {
+ return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
+ }
+
+ /// <summary>
/// FIXME: make more like Blink1.m enumerate
/// </summary>
/// <returns>number of blink1 devices</returns>
Please sign in to comment.
Something went wrong with that request. Please try again.