Beispiel #1
0
        /// <summary>
        /// 查询设备的运转时间
        /// </summary>
        /// <returns></returns>
        private string HandleEquipment6004()
        {
            var ret = "{}";
            var id  = ParseInt(Utility.Decrypt(data));
            var obj = EquipmentInstance.Find(f => f.id == id && f.Deleted == false);

            if (null != obj)
            {
                var date     = DateTime.Parse(GetParamenter("date") + " 23:59:59");
                var runtimes = DataInstance.FindList(f => f.mac_id.Equals(EquipmentInstance.GetFullNumber(obj)) &&
                                                     f.command_id.Equals("0x6004") && f.receive_time < date).OrderByDescending(o => o.receive_time).FirstOrDefault();
                if (null != runtimes)
                {
                    _0x6004DX x6004 = new _0x6004DX();
                    x6004.Command     = "0x6004";
                    x6004.HexContent  = runtimes.message_content;
                    x6004.DataType    = doosan.DX;
                    x6004.MacID       = obj.TB_EquipmentModel.Code + obj.Number;
                    x6004.ReceiveTime = runtimes.receive_time.Value;
                    x6004.SimNo       = runtimes.terminal_id;
                    x6004.Unpackage();
                    ret = JsonConverter.ToJson(x6004);
                }
            }
            return(ret);
        }
        /// <summary>
        /// 查询按终端号码的历史纪录
        /// </summary>
        /// <returns></returns>
        private string QueryTerminalHistiry()
        {
            string ret;
            var    time = GetParamenter("time");

            time = string.IsNullOrEmpty(time) ? "0" : time;
            long t = long.Parse(time);

            if (t <= 0)
            {
                ret = "{\"Time\":" + DateTime.Now.Ticks + ",\"Data\":[]}";
            }
            else
            {
                var dt = new DateTime(t);
                // 查询指定时间之后的通讯记录
                var tid = data.Length > 10 ? data : data + "000";
                var his = DataInstance.FindList(f => f.terminal_id.Equals(tid) && f.receive_time >= dt);
                ret = "{\"Time\":" + time + ",\"Data\":" + JsonConverter.ToJson(his) + "}";
            }
            return(ret);
        }
        /// <summary>
        /// 查询指定日期范围内的运转时间并补偿相应的数量
        /// </summary>
        /// <param name="averagable"></param>
        /// <returns></returns>
        private string HandleQueryEquipmentWorktime(bool averagable = true)
        {
            var ret = "{}";
            var id  = ParseInt(Utility.Decrypt(data));
            var obj = EquipmentInstance.Find(f => f.id == id && f.Deleted == false);

            if (null != obj)
            {
                var date = DateTime.Parse(GetParamenter("date") + " 00:00:00");
                // 如果不是求平均值则将日期往前推一天
                //if (!averagable) { date = date.AddDays(-1); }
                var date1 = DateTime.Parse(GetParamenter("date1") + " 23:59:59");
                List <WorktimeChart> avg  = new List <WorktimeChart>();
                List <WorktimeChart> work = new List <WorktimeChart>();
                DateTime             dt   = date;
                // 循环每天一个节点
                while (dt.Ticks < date1.Ticks)
                {
                    avg.Add(new WorktimeChart()
                    {
                        date = dt.ToString("yyyy/MM/dd"), x = Utility.DateTimeToJavascriptDate(dt.Date), y = 0, min = 0
                    });
                    work.Add(new WorktimeChart()
                    {
                        date = dt.ToString("yyyy/MM/dd"), x = Utility.DateTimeToJavascriptDate(dt.Date), y = 0, min = 0
                    });
                    dt = dt.AddDays(1);
                }
                var macid    = EquipmentInstance.GetFullNumber(obj);
                var cmds     = new string[] { "0x1000", "0x600B" };
                var runtimes = DataInstance.FindList(f => f.mac_id.Equals(macid) && cmds.Contains(f.command_id) &&
                                                     f.receive_time >= date && f.receive_time <= date1).OrderBy(o => o.receive_time);
                var list = new List <Worktime>();
                if (null != runtimes)
                {
                    long today        = 0;
                    long timeInterval = 0;
                    foreach (var r in runtimes)
                    {
                        bool gps = r.command_id.Equals("0x1000");
                        if (today == 0)
                        {
                            dt = r.receive_time.Value;
                        }
                        else
                        {
                            // 计算与上一条数据的实际接收时间之间的时间差
                            timeInterval = (long)(r.receive_time.Value - dt).TotalSeconds;
                        }
                        var t = Utility.DateTimeToJavascriptDate(r.receive_time.Value.Date);
                        // 日期不同则重置日期和运转时间
                        if (today != t)
                        {
                            today = t;
                        }

                        byte[] temp  = null;
                        int    index = 0;
                        if (gps)
                        {
                            if (r.protocol_type == ProtocolTypes.SATELLITE)
                            {
                                temp  = CustomConvert.GetBytes(r.message_content);
                                index = 13;
                            }
                        }
                        else
                        {
                            temp  = CustomConvert.GetBytes(r.message_content);
                            index = 0;
                        }

                        // 增加一个节点
                        Worktime wt = new Worktime();
                        wt.date     = r.receive_time.Value.ToString("yyyy/MM/dd HH:mm:ss");
                        wt.worktime = null == temp ? 0 : BitConverter.ToUInt32(temp, index);
                        var bin = !gps ? "00000000" : CustomConvert.IntToDigit(temp[4], CustomConvert.BIN, 8);
                        // 如果list已经有数据则计算与上一条数据之间的差值
                        var cnt = list.Count;
                        if (cnt > 0)
                        {
                            if (wt.worktime < list[cnt - 1].worktime)
                            {
                                // 当前运转时间小于前一条时,小计为0
                                wt.interval = 0;
                            }
                            else
                            {
                                // 差值
                                wt.interval = !gps ? 0 : (wt.worktime - list[cnt - 1].worktime);
                                // 计算差值与实际两条数据的接收时间差值的对比
                                if (wt.interval > timeInterval)
                                {
                                    // 如果运转时间差超过了这两条数据接收时间的差值,则记为0
                                    wt.interval = 0;
                                }
                                // 计算运转时间之间的差值  2016/08/15 15:00
                                if (wt.interval > 0)
                                {
                                    uint hour = (uint)r.receive_time.Value.Hour, min = (uint)r.receive_time.Value.Minute;
                                    // 凌晨0点的运转时间需要分开到两个日期里
                                    if (hour == 0)
                                    {
                                        // 时间间隔大于0点已过的分钟数,则需要把时间差分开到两个日期里
                                        if (wt.interval > min)
                                        {
                                            list[cnt - 1].interval += wt.interval - min;
                                            wt.interval             = min;
                                        }
                                    }
                                }
                                // 补偿的分钟
                                if (wt.interval > 0)
                                {
                                    uint ad = 0;
                                    if (wt.interval > 60)
                                    {
                                        ad = wt.interval / 60;
                                    }
                                    else
                                    {
                                        ad = 1;
                                    }
                                    if (bin[6] == '1')
                                    {
                                        ad += 1;
                                    }
                                    wt.added = ad;
                                }
                                else
                                {
                                    wt.interval = 0;
                                }
                                // 所用的小时
                                wt.hours = wt.interval > 0 ? (wt.interval > 60 ? wt.interval / 60 : 1) : 0;
                            }
                        }
                        else
                        {
                            // 第一条数据小计为0
                            wt.interval = 0;
                        }
                        list.Add(wt);

                        // 更新本日最后的运转时间
                        var wk = work.FirstOrDefault(f => f.x == today);
                        if (null != wk)
                        {
                            //if (wk.min == 0) { wk.min = run; }
                            wk.min = wt.worktime;
                        }
                    }// end of foreach

                    // 工作时间总计
                    var totalWorkMins = list.Sum(s => s.interval);
                    // 补偿的分钟
                    var totalAddMins = list.Sum(s => s.added);
                    // 所用的小时数
                    var totalUsedHours = list.Sum(s => s.hours);
                    // 工作效率
                    var compensate = totalWorkMins / 60.0 / totalUsedHours;
                    // 最终增加的小时数
                    var finalAdded = totalAddMins / 60.0 * compensate;

                    // 计算每日运转时间
                    foreach (var f in work)
                    {
                        f.y   = Math.Round(list.Where(w => w.date.Contains(f.date)).Sum(s => s.interval) / 60.0, 2);
                        f.add = finalAdded;
                    }
                    // 有工作时间的天数里平均加入补偿的小时数
                    var per = finalAdded / work.Count(c => c.y > 0);
                    //per = Math.Round(per, 2);
                    // 加入补偿
                    foreach (var w in work)
                    {
                        //if (!averagable)
                        {
                            w.y += w.y > 0 ? per : 0;
                            // 如果超过24小时则直接设为24小时  2016/08/15 15:16
                            if (w.y >= 24)
                            {
                                w.y = 24;
                            }
                        }
                        if (averagable)
                        {
                            w.y = Math.Round(w.y, 2);
                        }
                    }

                    // 计算平均值
                    var avgg = Math.Round(work.Sum(s => s.y) * 1.0 / work.Count, 2);
                    foreach (var a in avg)
                    {
                        a.y = avgg;
                    }
                }
                if (averagable)
                {
                    ret = string.Format("{0}\"Average\":{1},\"Worktime\":{2}{3}", "{", JsonConverter.ToJson(avg), JsonConverter.ToJson(work), "}");
                }
                else
                {
                    ret = JsonConverter.ToJson(work);
                }
            }
            return(ret);
        }
Beispiel #4
0
        /// <summary>
        /// 查询指定设备在指定日期范围内每日运转时间
        /// </summary>
        /// <returns></returns>
        private string HandleEquipmentWorktime(bool averagable = true)
        {
            var ret = "{}";
            var id  = ParseInt(Utility.Decrypt(data));
            var obj = EquipmentInstance.Find(f => f.id == id && f.Deleted == false);

            if (null != obj)
            {
                var date = DateTime.Parse(GetParamenter("date") + " 00:00:00");
                // 如果不是求平均值则将日期往前推一天
                //if (!averagable) { date = date.AddDays(-1); }
                var date1 = DateTime.Parse(GetParamenter("date1") + " 23:59:59");
                List <WorktimeChart> avg  = new List <WorktimeChart>();
                List <WorktimeChart> work = new List <WorktimeChart>();
                DateTime             dt   = date;
                // 循环每天一个节点
                while (dt.Ticks < date1.Ticks)
                {
                    avg.Add(new WorktimeChart()
                    {
                        date = dt.ToString("yyyy/MM/dd"), x = Utility.DateTimeToJavascriptDate(dt.Date), y = 0, min = 0
                    });
                    work.Add(new WorktimeChart()
                    {
                        date = dt.ToString("yyyy/MM/dd"), x = Utility.DateTimeToJavascriptDate(dt.Date), y = 0, min = 0
                    });
                    dt = dt.AddDays(1);
                }
                var macid    = EquipmentInstance.GetFullNumber(obj);
                var cmds     = new string[] { "0x1000", "0x1001", "0x5000", "0x6004", "0x600B", "0xCC00" };
                var runtimes = DataInstance.FindList(f => f.mac_id.Equals(macid) && cmds.Contains(f.command_id) &&
                                                     f.receive_time >= date.AddDays(-1) && f.receive_time <= date1.AddDays(1)).OrderBy(o => o.receive_time);
                var list = new List <WorkTime>();
                if (null != runtimes)
                {
                    long today = 0;
                    foreach (var r in runtimes)
                    {
                        var t = Utility.DateTimeToJavascriptDate(r.receive_time.Value.Date);
                        // 日期不同则重置日期和运转时间
                        if (today != t)
                        {
                            today = t;
                        }

                        byte[] temp  = null;
                        int    index = 0;
                        if (r.command_id.Equals("0x1000"))
                        {
                            if (r.protocol_type == ProtocolTypes.SATELLITE)
                            {
                                temp  = CustomConvert.GetBytes(r.message_content);
                                index = 13;
                            }
                        }
                        else if (r.command_id.Equals("0x1001"))
                        {
                            temp  = CustomConvert.GetBytes(r.message_content);
                            index = 37;
                        }
                        else if (r.command_id.Equals("0x5000"))
                        {
                            // 只有装载机和电装的挖掘机才能有5000命令的总运转时间
                            if (r.terminal_type >= TerminalTypes.DXE)
                            {
                                temp  = CustomConvert.GetBytes(r.message_content);
                                index = 0;
                            }
                        }
                        else if (r.command_id.Equals("0x600B"))
                        {
                            temp  = CustomConvert.GetBytes(r.message_content);
                            index = 0;
                        }
                        else if (r.command_id.Equals("0xCC00"))
                        {
                            temp  = CustomConvert.GetBytes(r.message_content);
                            index = 12;
                        }
                        else
                        {
                            temp = CustomConvert.GetBytes(r.message_content);

                            byte tp = r.terminal_type.Value;
                            index = tp == TerminalTypes.DH ? 2 : (tp == TerminalTypes.DX ? 5 : 1);
                        }

                        // 增加一个节点
                        WorkTime wt = new WorkTime();
                        wt.date = r.receive_time.Value.ToString("yyyy/MM/dd HH:mm:ss");
                        wt.time = null == temp ? 0 : BitConverter.ToUInt32(temp, index);
                        // 如果list已经有数据则计算与上一条数据之间的差值
                        var cnt = list.Count;
                        if (cnt > 0)
                        {
                            if (wt.time < list[cnt - 1].time)
                            {
                                // 当前运转时间小于前一条时,小计为0
                                wt.subtotal = 0;
                            }
                            else
                            {
                                // 否则小计为差值
                                wt.subtotal = wt.time - list[cnt - 1].time;
                                // 每日凌晨1点之前,如果计算的时间差超过了当前时间的分钟数,则只计算分钟数
                                if (r.receive_time.Value.Hour < 1 && wt.subtotal > r.receive_time.Value.Minute)
                                {
                                    // 每日1时之前计算的时间差大于已经过去了的分钟数则将差值算到前一天最后一条数据里
                                    if (cnt > 0)
                                    {
                                        list[cnt - 1].subtotal += (uint)(wt.subtotal - r.receive_time.Value.Minute);
                                    }
                                    wt.subtotal = (uint)r.receive_time.Value.Minute;
                                }
                                // 小于0时算作0
                                if (wt.subtotal < 0)
                                {
                                    wt.subtotal = 0;
                                }
                                // 如果与上一条日期的分钟数相差12个小时以上则记为0
                                DateTime lst = DateTime.Parse(list[cnt - 1].date);
                                if ((lst - r.receive_time.Value).Duration().TotalMinutes >= DAY_MINUTES / 2)
                                {
                                    wt.subtotal = 0;
                                }
                                // 大于24小时算作0
                                if (wt.subtotal >= DAY_MINUTES)
                                {
                                    wt.subtotal = 0;
                                }
                            }
                        }
                        else
                        {
                            // 第一条数据小计为0
                            wt.subtotal = 0;
                        }
                        list.Add(wt);

                        // 更新本日最后的运转时间
                        var wk = work.FirstOrDefault(f => f.x == today);
                        if (null != wk)
                        {
                            //if (wk.min == 0) { wk.min = run; }
                            wk.min = wt.time;
                        }
                    }// end of foreach

                    // 计算每日运转时间
                    foreach (var f in work)
                    {
                        f.y = Math.Round(list.Where(w => w.date.Contains(f.date)).Sum(s => s.subtotal) / 60.0, 2);
                    }

                    // 计算平均值
                    var avgg = Math.Round(work.Sum(s => s.y) * 1.0 / work.Count, 2);
                    foreach (var a in avg)
                    {
                        a.y = avgg;
                    }
                }
                if (averagable)
                {
                    ret = string.Format("{0}\"Average\":{1},\"Worktime\":{2}{3}", "{", JsonConverter.ToJson(avg), JsonConverter.ToJson(work), "}");
                }
                else
                {
                    ret = JsonConverter.ToJson(work);
                }
            }
            return(ret);
        }