using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Web.Routing;
namespace Common
{
///
/// FreeGo对接
///
public static class FreeGoOperation
{
private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(FreeGoOperation));
private static string freego_url = System.Configuration.ConfigurationManager.AppSettings["freego_url"];
private static string freego_codes = System.Configuration.ConfigurationManager.AppSettings["freego_codes"];
private static string freego_appid = System.Configuration.ConfigurationManager.AppSettings["freego_appid"];
private static string freego_appsecret = System.Configuration.ConfigurationManager.AppSettings["freego_appsecret"];
///
/// 调用freego获取设备密钥等信息
///
/// 主机mac地址,如34-D0-00-00-00-01格式
///
public static DeviceRegisterResult DeviceRegister(string mac)
{
string deviceName = mac.Replace("-", "").ToUpper();
DeviceRegisterPostData postData = new DeviceRegisterPostData()
{
appid = freego_appid,
timestamp = TimeHelper.DateTimeToStamp(DateTime.Now).ToString(),
nonce_str = Guid.NewGuid().ToString(),
device_name = deviceName
};
var dic = new RouteValueDictionary();
dic["appid"] = postData.appid;
dic["timestamp"] = postData.timestamp;
dic["nonce_str"] = postData.nonce_str;
dic["device_name"] = postData.device_name;
postData.sign = HttpWebRequestHelper.GetFreeGoSignature(dic, freego_appsecret);
try
{
string result = HttpWebRequestHelper.PostWebRequest(freego_url, Newtonsoft.Json.JsonConvert.SerializeObject(postData));
return JsonConvert.DeserializeObject(result);
}
catch (Exception ex)
{
logger.Error("调用FreeGo接口失败:" + ex.ToString());
return new DeviceRegisterResult() { errcode = "-1", msg = ex.Message }; ;
}
}
///
/// 给FreeGo(福瑞狗)上报SOS和DND状态
///
///
///
///
///
public static void UploadService(string hotelCode, string roomNumber, string key, string value)
{
foreach (string freego_code in freego_codes.Split(','))
{
if (freego_code == hotelCode)
{
UploadServicePostData postData = new UploadServicePostData()
{
appid = freego_appid,
appsecret = freego_appsecret,
code = freego_code,
room_number = roomNumber,
timestamp = TimeHelper.DateTimeToStamp(DateTime.Now).ToString(),
key = key,
value = value
};
try
{
HttpWebRequestHelper.PostWebRequest(freego_url, Newtonsoft.Json.JsonConvert.SerializeObject(postData));
}
catch (Exception ex)
{
logger.Error("调用FreeGo接口失败:" + ex.ToString());
}
break;
}
}
}
}
internal class DeviceRegisterPostData
{
///
/// 分配的请求id
///
public string appid { get; set; }
///
/// 当前unix时间戳
///
public string timestamp { get; set; }
///
/// 随机字符串
///
public string nonce_str { get; set; }
///
/// 设备名称
///
public string device_name { get; set; }
///
/// 签名
///
public string sign { get; set; }
}
public class DeviceRegisterResult
{
///
/// 错误代码,0表示成功
///
public string errcode { get; set; }
///
/// 设备名称
///
public string device_name { get; set; }
///
/// 设备密钥
///
public string device_secret { get; set; }
///
/// 阿里云物联网id
///
public string iot_id { get; set; }
///
/// 产品key
///
public string product_key { get; set; }
///
/// 错误信息
///
public string msg { get; set; }
}
internal class UploadServicePostData
{
public string appid { get; set; }
public string appsecret { get; set; }
///
/// 酒店编码
///
public string code { get; set; }
///
/// 房号
///
public string room_number { get; set; }
///
/// 当前unix时间戳
///
public string timestamp { get; set; }
///
/// SOS/DND
///
public string key { get; set; }
///
/// 当前服务值
///
public string value { get; set; }
}
}