Windows Home Server Twitter通知插件

A while back, the very wise Brendan Grant sent me some sample code that would use Twitter to report the health of one's Windows Home Server. I always meant to update the code to use TweetSharp to talk to Twitter, as well as add some robustness for connected/disconnected scenarios, but I'm just never going to get around to it. Instead, here it is as he sent to me.

不久之前,非常明智的Brendan Grant给我发送了一些示例代码,这些示例代码将使用Twitter报告一个Windows Home Server的运行状况。 我一直打算更新代码以使用TweetSharp与Twitter交谈,并为已连接/已断开连接的场景添加一些健壮性,但是我永远都不会去解决它。 相反,这是他送给我的。

There's a REALLY vibrant community around Windows Home Server plugins and if you've got a WHS and you want it to do something that it doesn't do, I'd encourage you to jump in.

Windows Home Server插件周围有一个非常活跃的社区,如果您拥有WHS,并且希望它做一些它没有做的事情,我鼓励您加入。

Even as I'm posting this, I'm sure there are better and more interesting implementations. However, I like what Brendan has done to abstract away the core COM-based API of WHS for use in managed code.

即使在发布此内容时,我也肯定有更好,更有趣的实现。 但是,我喜欢Brendan为提取WHS的基于COM的核心API以便在托管代码中使用所做的工作。

Here's the full program...note again that the PostTweet() method is hacked together and should use a more robust technique:

这是完整的程序...请再次注意,PostTweet()方法被黑在一起,应使用更可靠的技术:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml.Serialization;
using Microsoft.HomeServer.SDK.Interop.v1;

namespace Twitter_Test
{
class Program
{
static string username = "";
static string password = "";

static void Main(string[] args)
{
IWHSInfo info = new WHSInfoClass();
//Register application name
info.Init("WHS Twitter Client");

NotificationCallbackClass notificationClass = new NotificationCallbackClass();
//Register notification callback class
info.RegisterForNotifications(notificationClass);

//Check current state
Console.WriteLine("Current State: " + notificationClass.GetHealthState().ToString());

notificationClass.HealthChanged += new EventHandler (notificationClass_HealthChanged);
Console.WriteLine("Monitoring for health changes. Press to exit.");
Console.ReadLine();
}
static void notificationClass_HealthChanged(object sender, HealthChangedEventArgs e)
{
Console.WriteLine("Current State " + e.Health.ToString());
PostTweet(username, password, "Your Windows Home Server's health is now: " + e.Health.ToString());
}
private static Status PostTweet(string username, string password, string message)
{
string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
// determine what we want to upload as a status
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + message);
// connect with the update page
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
// set the method to POST
request.Method = "POST";
// set the authorisation levels
request.Headers.Add("Authorization", "Basic " + user);
request.ContentType = "application/x-www-form-urlencoded";
// set the length of the content
request.ContentLength = bytes.Length;
request.ServicePoint.Expect100Continue = false;
// set up the stream
Stream reqStream = request.GetRequestStream();
// write to the stream
reqStream.Write(bytes, 0, bytes.Length);
// close the stream
reqStream.Close();
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
StreamReader sr = new StreamReader(response.GetResponseStream());
string s = sr.ReadToEnd();
XmlSerializer ser = new XmlSerializer(typeof(Status));
object o = ser.Deserialize(new StringReader(s));
Status status = o as Status;
return status;
}
}
}

There interesting part is the Eventing part where he makes changes in your Home Server turn into .NET Events via callbacks. Check the code for details. You can get events when Physical Disks are changed, when Backup States change, or when basically anything happens. There's a number of folks on Twitter already who have their Windows Home Servers tweeting.

有一个有趣的部分是“事件处理”部分,他通过回调将您的Home Server中的更改转换为.NET事件。 检查代码以获取详细信息。 当更改物理磁盘,更改备份状态或发生任何事情时,您都可以获取事件。 Twitter上已经有很多人在发布他们的Windows Home Server。

If you've got, or you're using a plugin to report your Home Server status on Twitter (or SMS or whatever) leave a comment and I'll update the post! I'm sure there are better solutions than this little sample.

如果有,或者您正在使用插件在Twitter(或SMS或其他)上报告您的Home Server状态,请发表评论,我将更新该帖子! 我敢肯定有比这个小样本更好的解决方案。

Here's the code if you want it, and remember, it may kill your pets. If so, don't blame me as I'll deny everything. It's a sample you found on the Internet, what did you expect?

如果需要的话这里是代码,请记住,它可能会杀死您的宠物。 如果是这样,请不要怪我,因为我会否认一切。 这是您在Internet上找到的样本,您期望得到什么?

翻译自: https://www.hanselman.com/blog/windows-home-server-twitter-notification-plugin

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值