Пример #1
0
        public void sendToIoTHub(string trackerId, string data)
        {
            //string deviceId = ConfigurationManager.AppSettings["deviceId"];
            //string deviceKey = ConfigurationManager.AppSettings["deviceKey"];
            string     deviceId  = getDeviceId(trackerId);
            string     deviceKey = getDeviceKey(trackerId);
            IoTProcess process   = new IoTProcess(deviceId, deviceKey, data);

            log.Debug(" sendToIoTHub: " + trackerId + "," + deviceId + "," + deviceKey + "," + data);
            process.sendMessage();
        }
Пример #2
0
        public void parserDATA(string data, Socket handler)
        {
            //將資料轉回原始狀態
            data = data.Replace("7D02", "7E");
            data = data.Replace("7D01", "7D");
            //parser 基本資訊,以下均為切割字串後,對應文件上的型態做轉碼
            string[] positioningData = new string[18];
            DateTime date = DateTime.MinValue, time = DateTime.MinValue;
            string   latitude = null, longitude = null, eg_load = null, eg_close = null;
            int      start = 2, i = 0, j = 0;
            string   str = null;

            int[]         cutTimes = { 4, 4, 12, 2, 6, 6, 8, 9, 1, 2, 2, 2, 2, 8, 2, 8, 4, 10 };
            string[]      cutName = { "id", "attributes", "deviceid", "serial_number", "date", "time", "latitude", "longitude", "位指示", "speed", "direction", "GSM", "GPS", "mileage", "sensor_power", "CELL ID", "LAC ID", "Reserved" };
            int[]         cutType = { 16, 2, 16, 10, 16, 16, 16, 16, 2, 10, 10, 10, 10, 10, 10, 10, 16, 16, 16 };
            double        latitudenumber = 0.0, longitudenumber = 0.0;
            StringBuilder messageString = new StringBuilder("");
            Random        test          = new Random();

            foreach (var ct in cutTimes)
            {
                if (cutType[i] != 16)
                {
                    str = hexToConvert(data.Substring(start, cutTimes[i]), cutType[i]);
                }
                //
                if (i == 4)
                {
                    if (DateTime.TryParseExact(data.Substring(start, cutTimes[i]), "ddMMyy", null, System.Globalization.DateTimeStyles.None, out date))
                    {
                        //TryParseExact轉換成功
                        //positioningData[j++] = data.Substring(start, cutTimes[i]);
                        positioningData[j++] = date.ToString("yyyy/MM/dd");
                        messageString.Append("\"" + cutName[i] + "\":\"" + date.ToString("yyyy/MM/dd") + "\",");
                    }
                }
                else if (i == 5)
                {
                    if (DateTime.TryParseExact(data.Substring(start, cutTimes[i]), "HHmmss", null, System.Globalization.DateTimeStyles.None, out time))
                    {
                        //TryParseExact轉換成功
                        //positioningData[j++] = data.Substring(start, cutTimes[i]);
                        positioningData[j++] = time.ToString("HH:mm:ss");
                        messageString.Append("\"" + cutName[i] + "\":\"" + time.ToString("HH:mm:ss") + "\",");
                    }
                }
                else if (i == 6)
                {
                    if (double.Parse(data.Substring(start, cutTimes[i])) != 0)
                    {
                        latitude = data.Substring(start, cutTimes[i]);
                    }
                    else
                    {
                        positioningData[j++] = null;
                    }
                }
                else if (i == 7)
                {
                    if (double.Parse(data.Substring(start, cutTimes[i])) != 0)
                    {
                        longitude = data.Substring(start, cutTimes[i]);
                    }
                    else
                    {
                        positioningData[j++] = null;
                    }
                }
                else if (i == 8 && longitude != null && latitude != null)
                {
                    latitudenumber  = int.Parse(latitude.Substring(0, 2)) + ((int.Parse(latitude.Substring(2, 6)) * 0.0001) / 60);
                    longitudenumber = int.Parse(longitude.Substring(0, 3)) + ((int.Parse(longitude.Substring(3, 6)) * 0.0001) / 60);
                    //
                    str = hexToConvert(data.Substring(start, cutTimes[i]), cutType[i]);
                    str = str.PadLeft(4, '0');

                    if (str.Substring(1, 1) == "1")
                    {
                        positioningData[j++] = longitudenumber.ToString("#0.000000");
                        messageString.Append("\"longitude\":\"").Append(longitudenumber.ToString("#0.000000") + "\",");
                    }
                    else if (str.Substring(1, 1) == "0")
                    {
                        positioningData[j++] = "-" + longitudenumber.ToString("#0.000000");
                        messageString.Append("\"longitude\":\"-").Append(longitudenumber.ToString("#0.000000") + "\",");
                    }
                    //
                    if (str.Substring(2, 1) == "1")
                    {
                        positioningData[j++] = latitudenumber.ToString("#0.000000");
                        messageString.Append("\"latitude\":\"").Append(latitudenumber.ToString("#0.000000") + "\",");
                    }
                    else if (str.Substring(2, 1) == "0")
                    {
                        positioningData[j++] = "-" + latitudenumber.ToString("#0.000000");
                        messageString.Append("\"latitude\":\"-").Append(latitudenumber.ToString("#0.000000") + "\",");
                    }
                }
                else if (i == 9)
                {
                    double spend = int.Parse(str) * 1.85;
                    positioningData[j++] = spend.ToString();
                    messageString.Append("\"" + cutName[i] + "\":\"" + spend + "\",");
                }
                else if (i == 10)
                {
                    int direction = int.Parse(str) * 2;
                    positioningData[j++] = direction.ToString();
                    messageString.Append("\"" + cutName[i] + "\":\"" + direction + "\",");
                }
                else
                {
                    if (cutType[i] != 16)
                    {
                        if (i == 1)
                        {
                            str = str.PadLeft(16, '0');
                        }
                        positioningData[j++] = str;
                        messageString.Append("\"" + cutName[i] + "\":\"" + str + "\",");
                    }
                    else
                    {
                        try
                        {
                            positioningData[j++] = data.Substring(start, cutTimes[i]);
                            messageString.Append("\"" + cutName[i] + "\":\"" + data.Substring(start, cutTimes[i]) + "\",");
                        }
                        catch (Exception e)
                        {
                            log.Fatal(e.ToString());
                            log.Fatal(data + "->" + start + "," + cutTimes[i]);
                        }
                    }
                }
                start += cutTimes[i++];
            }
            if (positioningData[1].Substring(0, 1) == "1")
            {
                int    NumberChars = 0;
                string msg         = "";
                try
                {
                    int number = int.Parse(positioningData[3]) + 1;
                    if (number.ToString("X").Length > 2)
                    {
                        number = 0;
                    }
                    msg = "44010003" + positioningData[2] + number.ToString("X").PadLeft(2, '0') + positioningData[0] + "00";
                    string XOR = checkEORCode(msg);

                    msg = msg + XOR.Substring(XOR.Length - 2, 2);
                    msg = msg.Replace("7D", "7D01");
                    msg = msg.Replace("7E", "7D02");
                    msg = "7E" + msg + "7E";

                    NumberChars = msg.Length;
                    byte[] bytes = new byte[NumberChars / 2];
                    for (int ii = 0; ii < NumberChars; ii += 2)
                    {
                        bytes[ii / 2] = Convert.ToByte(msg.Substring(ii, 2), 16);
                    }
                    Program.Send(handler, bytes);
                    //Program.Send(handler, "(700160818000,1,001,BASE,11,123)");
                }
                catch (Exception e)
                {
                    log.Fatal("NumberChars:" + NumberChars);
                    log.Fatal("msg:" + msg);
                    log.Fatal(e.ToString());
                }
                //Program.Send(handler, "(700160818000,1,001,BASE,11,123)");
            }
            //下指令
            if (positioningData[2] == getIdKey[0].trackerId && instruction9201)
            {
                instruction9201 = false;
                log.Debug("send 9201, " + getIdKey[0].trackerId);
                Program.Send(handler, "(700160818000,1,001,GSENS,2,0,500,80)");
            }
            else if (positioningData[2] == getIdKey[1].trackerId && instruction0516)
            {
                instruction0516 = false;
                log.Debug("send 0516, " + getIdKey[1].trackerId);
                Program.Send(handler, "(700160818000,1,001,GSENS,2,0,500,80)");
            }
            else if (positioningData[2] == getIdKey[2].trackerId && instruction9195)
            {
                instruction9195 = false;
                log.Debug("send 9195, " + getIdKey[2].trackerId);
                Program.Send(handler, "(700160818000,1,001,GSENS,2,0,500,80)");
            }
            else if (positioningData[2] == getIdKey[3].trackerId && instruction8792)
            {
                instruction8792 = false;
                log.Debug("send 8792, " + getIdKey[3].trackerId);
                Program.Send(handler, "(700160818000,1,001,GSENS,2,0,500,100)");
            }

            if (positioningData[0] == "5501")
            {
                //string[] extendedData = new string[6];
                messageString.Append(parserSplitData(data.Substring(start), eg_load, eg_close));
                //parser 其他擴充內容
                messageString.Remove(messageString.Length - 1, 1);
                Console.WriteLine("parser sensor data:" + messageString);
                Random rnd = new Random();

                obdData.Reverse();
                //由舊至新
                foreach (string[] obd in obdData)
                {
                    try
                    {
                        if (int.Parse(obd[6]) > 0)//轉速大於0表示引擎運作中
                        {
                            eg_load = "on";
                        }
                        else
                        {
                            eg_load = "off";
                        }
                    }
                    catch (Exception e)
                    {
                        eg_load = "error";
                        log.Fatal(e.ToString());
                    }
                    if (positioningData[6] == null)
                    {
                        positioningData[6] = "null";
                    }

                    if (positioningData[7] == null)
                    {
                        positioningData[7] = "null";
                    }

                    string location = "fail";
                    if (positioningData[0] == "5501")
                    {
                        location = "success";
                    }

                    //如果OBD時間為0,改傳裝置速度
                    if (double.Parse(obd[8]) < 0)
                    {
                        log.Debug("really Intake Air Temperature:" + obd[8]);
                        obd[8] = "0.0";
                    }
                    if (int.Parse(obd[3]) < 0)
                    {
                        log.Debug("really Engine Coolant Temperature:" + obd[3]);
                        obd[3] = "0";
                    }

                    string telemetry = "{" +
                                       "\"Device Speed\":\"" + positioningData[8] +
                                       "\",\"EG_RPM\":\"" + obd[6] +
                                       "\",\"mileage\":\"" + positioningData[12] +
                                       "\",\"GPS Location\":\"" + location +
                                       "\",\"DeviceID\":\"" + positioningData[2] +
                                       "\",\"direction\":\"" + positioningData[9] +
                                       "\",\"GSM\":\"" + positioningData[10] +
                                       "\",\"GPS\":\"" + positioningData[11] +
                                       "\",\"sensor_power\":\"" + positioningData[13] +
                                       //"\",\"CELL ID\":\"" + positioningData[14] +
                                       //"\",\"LAC ID\":\"" + positioningData[15] +
                                       "\",\"Fuel System Status\":\"" + obd[1] +
                                       "\",\"Calculated Engine Load\":\"" + obd[2] +
                                       "\",\"Engine Coolant Temperature\":\"" + obd[3] +
                                       "\",\"Fuel Pressure\":\"" + obd[4] +
                                       "\",\"Intake Manifold Pressure\":\"" + obd[5] +
                                       "\",\"Vehicle Speed\":\"" + obd[7] +
                                       "\",\"Intake Air Temperature\":\"" + obd[8] +
                                       "\",\"Air Flow Rate\":\"" + obd[9] +
                                       "\",\"Throttle Position\":\"" + obd[10] +
                                       "\",\"Battery voltage\":\"" + obd[11] +
                                       //"\",\"SS\":\"" + obd[12] +
                                       "\",\"Distance\":\"" + obd[13] +
                                       "\",\"LONGITUDE\":\"" + (positioningData[6] ?? "0") +
                                       "\",\"LATITUDE\":\"" + (positioningData[7] ?? "0") +
                                       "\",\"Device Date\":\"" + positioningData[4] +
                                       "\",\"Device Time\":\"" + positioningData[5] +
                                       "\",\"Voltage Connect\":\"" + (extendedData[5] ?? null) +
                                       "\"}";
                    //
                    Console.WriteLine("parser obd data:" + telemetry);
                    log.Debug("parser sensor data:" + messageString);
                    log.Debug("parser obd data:" + telemetry);

                    //DateTime now = DateTime.UtcNow;
                    DateTime deviceTime = DateTime.Parse(positioningData[4] + " " + positioningData[5]);
                    //TimeSpan ts = now - deviceTime;

                    //log.Debug("Minutes:" + ts.TotalMinutes);
                    if (!string.IsNullOrEmpty(positioningData[6]) && !string.IsNullOrEmpty(positioningData[7]))
                    {
                        string deviceId = getDeviceId(positioningData[2]);

                        if (checkDeviceTime(deviceId, deviceTime))
                        {
                            log.Debug("move:" + deviceId + "->" + positioningData[2] + "," + positioningData[6] + "," + positioningData[7]);
                            IoTProcess.moveLocation(deviceId, double.Parse(positioningData[6]), double.Parse(positioningData[7]));
                        }
                    }
                    //改變DocumentDB

                    long tt = Convert.ToInt64((DateTime.Parse(positioningData[4] + " " + positioningData[5])).ToString("yyyyMMddHHmmss"));
                    if (timeDic.ContainsKey(positioningData[2]))
                    {
                        long nn = timeDic[positioningData[2]];
                        if (tt > nn)
                        {
                            if (obd[6] != "0" || (obd[6] == "0" && obd[7] == "0"))
                            {
                                //if (ts.TotalMinutes <= int.Parse(ConfigurationManager.AppSettings["timedeviation"]))
                                {
                                    sendToIoTHub(positioningData[2], telemetry);
                                    // 傳送data至iot hub
                                    Console.WriteLine("sendToIoTHub.");
                                }
                            }
                            timeDic.Remove(positioningData[2]);
                            timeDic.Add(positioningData[2], tt);
                        }
                    }
                    else
                    {
                        timeDic.Add(positioningData[2], tt);
                    }
                }
            }
        }