/// <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(); Blink1Server.Log("file contents:" + contents); return(contents); } } catch (Exception e) { Blink1Server.Log("The file could not be read:"); Blink1Server.Log(e.Message); } return(null); }
private void notifyIcon1_Click(object sender, EventArgs e) { Blink1Server.Log("notifyIcon1 SingleClick! " + e); stripMenuBlink1Status.Text = "blink1 status: hello"; if (blink1Server.blink1.getCachedCount() > 0) { stripMenuBlink1Status.Text = "blink(1) found"; stripMenuBlink1Id.Text = "serial: " + blink1Server.blink1.getCachedSerial(0); stripMenuKey.Text = "key: " + blink1Server.blink1.blink1Id; } else { stripMenuBlink1Status.Text = "blink(1) not found"; stripMenuBlink1Id.Text = "serial: -none-"; stripMenuKey.Text = "key: " + blink1Server.blink1.blink1Id; } }
/// <summary> /// periodically fetch events from IFTTT gateway /// </summary> /// <returns>true if pattern was played from input match</returns> public Boolean updateIftttInput() { if (iftttLastContent == null) { lastVal = "[couldn't connect]"; lastDateTime = DateTime.Now.ToUniversalTime(); return(false); } string rulename = arg1; IftttResponse iftttResponse = JsonConvert.DeserializeObject <IftttResponse>(iftttLastContent); if (iftttResponse.event_count > 0) { long lastsecs = (long)ConvertToUnixTimestamp(lastDateTime); //long lastsecs = epochSecs(lastDateTime); possibleVals.Clear(); foreach (IftttEvent ev in iftttResponse.events) // FIXME: loop overwrites per-obj props { long evdate = long.Parse(ev.date); //Blink1Server.Log("evdate: " + evdate + ", lastsecs:" + lastsecs); lastDateTime = ConvertFromUnixTimestamp(evdate); string evname = ev.name; possibleVals.Add(ev.name); lastVal = ev.source; if (rulename.Equals(evname)) { Blink1Server.Log("---ifttt match: evdate:" + evdate + ", lastsecs:" + lastsecs + ", dt:" + (evdate - lastsecs)); if (evdate > lastsecs) { blink1Server.playPattern(pname); return(true); } } } } else { lastVal = "[no events]"; lastDateTime = DateTime.Now.ToUniversalTime(); } return(false); }
/// <summary> /// Execute a script /// </summary> /// <param name="scriptpath"></param> /// <returns></returns> public static string execScript(string scriptpath) { //stolen from: http://stackoverflow.com/questions/878632/best-way-to-call-external-program-in-c-sharp-and-parse-output System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.Arguments = "/c " + scriptpath; // dir *.cs"; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.RedirectStandardOutput = true; p.Start(); string output = p.StandardOutput.ReadToEnd(); p.WaitForExit(); Blink1Server.Log("Output:" + output); return(output); }
/// <summary> /// Called whenever a new color needs to be played, via timer /// </summary> public void update(Object stateInfo) { if (!playing) { return; } Blink1Server.Log("update! " + name); Boolean scheduleNext = true; playpos++; if (playpos == times.Count()) { playpos = 0; if (repeats != 0) // infinite { playcount++; if (playcount == repeats) { scheduleNext = false; } } } if (scheduleNext) { float nextTime = times[playpos]; Color color = colors[playpos]; Blink1Server.Log("update: scheduleNext: " + nextTime); //DLog(@"%@ updt p:%d c:%d %@ nextTime:%f",name,playpos,playcount,[Blink1 hexStringFromColor:color],nextTime); blink1Server.fadeToRGB(nextTime / 2, color); timer = new Timer(update, null, (int)(nextTime * 1000), 0); } else { playing = false; } }
/// <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 Blink1Server.Log("getContentsOfUrl:" + url); // FIXME: how to do debug logging better? string content = null; if (url == null) { return(null); } WebClient webclient = new WebClient(); try { content = webclient.DownloadString(url); } catch (WebException we) { // WebException.Status holds useful information Blink1Server.Log("webex:" + 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(); * Blink1Server.Log("content: " + content); */ content = "{\"lastVal\":\"url not found\"}"; } catch (System.ArgumentException sae) { Blink1Server.Log("sae:" + sae.Message); } catch (NotSupportedException ne) { Blink1Server.Log("ne:" + ne.Message); // other errors } return(content); }
private void Form1_FormClosed(object sender, FormClosedEventArgs e) { Blink1Server.Log("FormClosed!"); doExit(); }