//---------------------推撥LOG---------------------------------------------------------
 private void CreatePushNoticeLog(PhoneGCMInfo info, string sendValue, string functionName)
 {
     ArrayList insertDataList = new ArrayList();
     insertDataList.Add(info.WorkListAlertID);
     insertDataList.Add(info.DeviceID);
     insertDataList.Add(info.AlertRuleID);
     insertDataList.Add(info.RegID);            
     insertDataList.Add(DateTime.UtcNow);
     insertDataList.Add(sendValue);
     insertDataList.Add(functionName);
     insertDataList.Add(string.Empty);
     DataProvider.Instance().InsertPushNoticeLog(insertDataList);
 }
        public HttpResponseMessage SendPhoneGCM(string DeviceID, string AlertRuleID, string EventTime, string WorkListAlertID)
        {
            try
            {
                AlarmRuleSetController CreatePhoneRecordCtrl = new AlarmRuleSetController();
                PhoneGCMInfo Info = new PhoneGCMInfo();//宣告一個物件來接
                //UserInfo UInfo = UserController.GetCurrentUserInfo();
                //string InfoHours = PortalSettings.TimeZone.BaseUtcOffset.TotalHours.ToString();
                //return Request.CreateResponse(HttpStatusCode.OK, InfoHours);
                DateTime dt = Convert.ToDateTime(EventTime);
                dt = dt.AddHours(PortalSettings.TimeZone.BaseUtcOffset.TotalHours);
                Info.DeviceID = DeviceID;
                Info.AlertRuleID = AlertRuleID;
                Info.EventTime = dt.ToString("yyyy-MM-dd HH:mm:ss");
                Info.userID = "";
                Info.RegID = "";
                Info.WorkListAlertID = WorkListAlertID;
                //Info.RegisterID = RegisterID;
                //Info.UserId = UInfo.UserID;
                 


                //int result = CreatePhoneRecordCtrl.ChkPhoneRecord(Info);//判斷是否可新增手機紀錄資料
                //string flag = "";
                //return Request.CreateResponse(HttpStatusCode.OK, result);

                //return Request.CreateResponse(HttpStatusCode.OK, result);

                //---------將資料放到物件------------------------------------------------------------------------               

                //if (result.Equals(1))
                //{
                //  flag = "Success";
                CreatePhoneRecordCtrl.ChkPhoneGCM(Info);//找出手機紀錄資料  2015/5/13 先暫停

                CreatePhoneRecordCtrl.SendPhoneAPNS(Info); //發送 IOS APNS 資料

                CreatePhoneRecordCtrl.SendPhoneBaidu(Info);  //發送 Baidue 資料

                //}
                //else
                //  flag = "Fail";
                var jsonToReturn = new
                {
                    InsertFillLog = "Success",
                };

                return Request.CreateResponse(HttpStatusCode.OK, jsonToReturn);
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.OK, ex.Message);
            }
        }
 public bool ChkPhoneGCM(BaseInfo baseInfo)
 {
     PhoneGCMInfo info = (PhoneGCMInfo)baseInfo;
     JSONInfo Info1 = new JSONInfo();
     subJSONInfo Info2 = new subJSONInfo();
     ArrayList al = new ArrayList();
     ArrayList a2 = new ArrayList();
     ArrayList a3 = new ArrayList();
     //ArrayList a3 = new ArrayList();
     al.Add(info.AlertRuleID);
     a2.Add(info.DeviceID);
     IDataReader reader1 = DataProvider.Instance().ChkPhoneDriverInfoDB(a2);//FMS_PhoneDriverInfo_Fetch;return CarNo,Driver
     if (reader1.Read())
     {
         Info1.type = "check";
         //Info1.data.CarNo = reader1.GetString(0);
         //Info1.data.Driver = reader1.GetString(1);
         Info2.CarNo = reader1.GetString(0);
         Info2.Driver = reader1.GetString(1);
     }
     CBO.CloseDataReader(reader1, true);
     a2.Clear();
     a2.Add(info.AlertRuleID);
     IDataReader reader2 = DataProvider.Instance().ChkPhoneRuleDB(a2);//FMS_PhoneRule_Fetch;return RuleName,AlarmType,Severity
     if (reader2.Read())
     {
         //Info1.data.AlertDate = info.EventTime;
         //Info1.data.RuleName = reader2.GetString(0);
         //Info1.data.AlarmType = reader2.GetString(1);
         //Info1.data.Severity = reader2.GetString(2);
         Info2.AlertDate = info.EventTime;
         Info2.RuleName = reader2.GetString(0);
         Info2.AlarmType = reader2.GetString(1);
         Info2.Severity = reader2.GetString(2);
     }
     CBO.CloseDataReader(reader2, true);
     a2.Clear();
     //string output2 = JsonConvert.SerializeObject(Info2);
     Info1.data = new string[] { JsonConvert.SerializeObject(Info2) };
     string output = JsonConvert.SerializeObject(Info1);
     output = output.Replace("\\", "").Replace("[\"", "[").Replace("\"]", "]");
     //Info1.type = "alert";
     string output1 = output.Replace("check", "alert");
     al.Add(info.DeviceID);
     IDataReader reader = DataProvider.Instance().ChkGCMDB(al);//FMS_PhoneGCM_Fetch;return UserId,RegisterID,Phone_Mail_Logic
     GCMSender gcm = null;
     string strResultJson, strResultJson1;
     while (reader.Read())
     {
         info.userID = reader.GetInt32(0).ToString();
         info.RegID = reader.GetString(1);
         info.Phone_Mail_Logic = reader.GetString(2);
         gcm = new GCMSender(info.RegID , "AIzaSyAI7vpx-Q3gQ1l-2R0jH00IqnVEbaQgSR8");
         strResultJson = gcm.Send(output);                
         if (strResultJson.IndexOf("registration_id") == -1)
         {
             strResultJson1 = gcm.Send(output1);
             //save log record
             CreatePushNoticeLog(info, output1, "Google");
         }
         else
         {
             JObject obj = (JObject)JsonConvert.DeserializeObject(strResultJson);
             obj = (JObject)obj["results"][0];
             if (obj["registration_id"] != null)
             {
                 string newRegId = obj["registration_id"].ToString();
                 PhoneGCMInfo newGcmInfo = new PhoneGCMInfo();
                 newGcmInfo.RegID = info.RegID;
                 newGcmInfo.newRegID = newRegId;
                 newGcmInfo.userID = info.userID;
                 newGcmInfo.Phone_Mail_Logic = info.Phone_Mail_Logic;
                 newGcmInfo.WorkListAlertID = info.WorkListAlertID;
                 newGcmInfo.DeviceID = info.DeviceID;
                 newGcmInfo.AlertRuleID = info.AlertRuleID;
                 a3.Add(newGcmInfo);
             }
         }
     }
     if (a3.Count > 0)
     {               
         for (int i = 0; i < a3.Count; i++)
         {
             PhoneGCMInfo newGcmInfo = a3[i] as PhoneGCMInfo;
             a2.Clear();
             a2.Add(newGcmInfo.RegID);
             a2.Add(newGcmInfo.newRegID);
             a2.Add(newGcmInfo.userID);
             a2.Add(newGcmInfo.Phone_Mail_Logic);
             int reader3 = DataProvider.Instance().ChkPhoneOverRegIDDB(a2);
             gcm = new GCMSender(newGcmInfo.newRegID, "AIzaSyAI7vpx-Q3gQ1l-2R0jH00IqnVEbaQgSR8");
             strResultJson1 = gcm.Send(output1);
             //save log record
             newGcmInfo.RegID = newGcmInfo.newRegID;
             CreatePushNoticeLog(info, output1, "Google");
         }
     }
     CBO.CloseDataReader(reader, true);
     return true;
 }