private void NotifyCloudOfExternalPort(object nil) { logger.InfoFormat("Notify public port info to cloud: ext ip {0}, ext port {1} via heartbeat", this.externalIP, this.externalPort); try { Model.StationInfo stationInfo = Model.StationCollection.Instance.FindOne(); if (stationInfo == null) { return; } Cloud.StationApi api = new Cloud.StationApi(stationInfo.Id, stationInfo.SessionToken); Cloud.StationDetail detail = StatusChecker.GetDetail(); logger.Debug("detail: " + detail.ToFastJSON()); api.Heartbeat(new WebClient(), detail); } catch (Exception e) { logger.Warn("Unable to notify external ip/port to cloud", e); } }
/// <summary> /// 得到一个对象实体 /// </summary> public Model.StationInfo DataRowToModel(DataRow row) { Model.StationInfo model = new Model.StationInfo(); if (row != null) { if (row["id"] != null && row["id"].ToString() != "") { model.id = int.Parse(row["id"].ToString()); } if (row["subwayId"] != null && row["subwayId"].ToString() != "") { model.subwayId = int.Parse(row["subwayId"].ToString()); } if (row["subwayName"] != null) { model.subwayName = row["subwayName"].ToString(); } if (row["stationName"] != null) { model.stationName = row["stationName"].ToString(); } if (row["stationNumber"] != null && row["stationNumber"].ToString() != "") { model.stationNumber = int.Parse(row["stationNumber"].ToString()); } if (row["lineDirect"] != null) { model.lineDirect = row["lineDirect"].ToString(); } if (row["isTransfer"] != null && row["isTransfer"].ToString() != "") { model.isTransfer = int.Parse(row["isTransfer"].ToString()); } if (row["status"] != null && row["status"].ToString() != "") { model.status = int.Parse(row["status"].ToString()); } if (row["remark"] != null) { model.remark = row["remark"].ToString(); } if (row["infoType"] != null && row["infoType"].ToString() != "") { model.infoType = int.Parse(row["infoType"].ToString()); } } return(model); }
/// <summary> /// 增加一条数据 /// </summary> public static int Add(Model.StationInfo model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into StationInfo("); strSql.Append("subwayId,subwayName,stationName,stationNumber,lineDirect,isTransfer,status,remark,infoType)"); strSql.Append(" values ("); strSql.Append("@subwayId,@subwayName,@stationName,@stationNumber,@lineDirect,@isTransfer,@status,@remark,@infoType)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@subwayId", SqlDbType.Int, 4), new SqlParameter("@subwayName", SqlDbType.VarChar, 50), new SqlParameter("@stationName", SqlDbType.VarChar, 150), new SqlParameter("@stationNumber", SqlDbType.Int, 4), new SqlParameter("@lineDirect", SqlDbType.VarChar, 50), new SqlParameter("@isTransfer", SqlDbType.Int, 4), new SqlParameter("@status", SqlDbType.Int, 4), new SqlParameter("@remark", SqlDbType.VarChar, 150), new SqlParameter("@infoType", SqlDbType.Int, 4) }; parameters[0].Value = model.subwayId; parameters[1].Value = model.subwayName; parameters[2].Value = model.stationName; parameters[3].Value = model.stationNumber; parameters[4].Value = model.lineDirect; parameters[5].Value = model.isTransfer; parameters[6].Value = model.status; parameters[7].Value = model.remark; parameters[8].Value = model.infoType; object obj = DBHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }
public void Stop() { timer.Change(Timeout.Infinite, Timeout.Infinite); using (WebClient agent = new WebClient()) { Model.StationInfo sinfo = Model.StationCollection.Instance.FindOne(); if (sinfo != null) { try { Cloud.StationApi api = new Cloud.StationApi(sinfo.Id, sinfo.SessionToken); api.Offline(agent); } catch (Exception ex) { logger.Warn("cloud offline error", ex); } } } }
private static void LogOutStationFromCloud() { try { Model.StationInfo stationInfo = Model.StationCollection.Instance.FindOne(); if (stationInfo == null) { throw new InvalidOperationException("station is null in station collection"); } logger.DebugFormat("Station logout with stationId = {0}", stationInfo.Id); Cloud.StationApi stationApi = new Cloud.StationApi(stationInfo.Id, stationInfo.SessionToken); stationApi.Offline(new System.Net.WebClient()); logger.Debug("Station logout successfully"); } catch (Exception ex) { logger.Debug("Unable to logout station", ex); } }
/// <summary> /// 得到一个对象实体 /// </summary> public Model.StationInfo GetModel(int id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 id,subwayId,subwayName,stationName,stationNumber,lineDirect,isTransfer,status,remark,infoType from StationInfo "); strSql.Append(" where id=@id"); SqlParameter[] parameters = { new SqlParameter("@id", SqlDbType.Int, 4) }; parameters[0].Value = id; Model.StationInfo model = new Model.StationInfo(); DataSet ds = DBHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return(DataRowToModel(ds.Tables[0].Rows[0])); } else { return(null); } }
private void SendHeartbeat(Object obj) { StationDetail detail = GetDetail(); Model.StationInfo sinfo = Model.StationCollection.Instance.FindOne(); if (sinfo != null) { bool locChange = false; string baseurl = NetworkHelper.GetBaseURL(); if (baseurl != sinfo.Location) { // update location if baseurl changed logger.DebugFormat("station location changed: {0}", baseurl); sinfo.Location = baseurl; locChange = true; } try { WebClient agent = new WebClient(); Cloud.StationApi api = new Cloud.StationApi(sinfo.Id, sinfo.SessionToken); if (logon == false || DateTime.Now - sinfo.LastLogOn > TimeSpan.FromDays(1)) { logger.Debug("cloud logon start"); api.LogOn(agent, detail); logon = true; // update station info in database logger.Debug("update station information"); sinfo.LastLogOn = DateTime.Now; Model.StationCollection.Instance.Save(sinfo); } if (locChange) { // update station info in database logger.Debug("update station information"); Model.StationCollection.Instance.Save(sinfo); } api.Heartbeat(agent, detail); } catch (WammerCloudException ex) { WebException webex = (WebException)ex.InnerException; if (webex != null) { HttpWebResponse response = (HttpWebResponse)webex.Response; if (response != null) { if (response.StatusCode == HttpStatusCode.Unauthorized) { // station's session token expired, it might be caused by: // 1. server maintenance // 2. driver registered another station // in this situation, client has to re-login/re-register the station functionServer.BlockAuth(true); } } } logger.Warn("cloud send heartbeat error", ex); } catch (Exception ex) { logger.Warn("cloud send heartbeat error", ex); } } }