/// <summary> /// 同步所有已连接设备的用户信息 /// </summary> private void SyncUser() { output("同步用户信息"); DeviceManagement deviceManagement = new DeviceManagement(); PersonnelManagement personnelManagement = new PersonnelManagement(); PersonnelManagement.PersonInfo person = new PersonnelManagement.PersonInfo(5); Stm32Sync stm32_sync = new Stm32Sync(); Stm32_crc stm32_crc = new Stm32_crc(); UInt32 crc = 0; MacAddr dst_addr; UInt16 total; UInt16 count; UInt16 total_get = 0; byte[] user_crc_byte; UInt32[] user_crc = new UInt32[0]; bool err = true; bool is_change = false; stm32_sync.output_dlg = output; int row = dgvDeviceInfo.SelectedRows.Count; /* 获取选中总行数 */ int rowIndex = dgvDeviceInfo.CurrentRow.Index; /* 获取选择行号 */ for (int j = 0; j < row; j++) { if (dgvDeviceInfo.SelectedRows[j].Cells[1].Value.ToString() == "已连接") { string addr_str = dgvDeviceInfo.SelectedRows[j].Cells[2].Value.ToString(); dst_addr = new MacAddr(addr_str); /* 首先获取设备内所有用户信息的crc */ while (true) { /* 获取用户crc */ if (stm32_sync.user_crc_get(dst_addr.Mac, (UInt16)(total_get + 1), (UInt16)200, out total, out count, out user_crc_byte) != true) { err = false; break; } if (total_get == 0) { user_crc = new UInt32[total]; } for (int i = 0; i < count; i++) { crc = (UInt32)((user_crc_byte[i * 4 + 0] << 0) + (user_crc_byte[i * 4 + 1] << 8) + (user_crc_byte[i * 4 + 2] << 16) + (user_crc_byte[i * 4 + 3] << 24)); user_crc[i + total_get] = crc; } total_get += count; if (total_get == total) { output("已获取完成所有用户crc"); err = true; break; } } if (err == false) { output("crc获取错误"); continue; } /* 同步用户信息 */ for (UInt32 uID = 1; uID <= user_crc.Length; uID++) { /* 获取用户信息 */ person = personnelManagement.user_get(uID); /* 用户不存在上位机,删除用户 */ if ((person.uID != uID) && (user_crc[uID - 1] != 0)) { is_change = true; if (stm32_sync.user_del(dst_addr.Mac, uID) == false) { output("删除" + uID.ToString() + "号用户失败"); err = false; break; } continue; } if (person.uID != 0) { /* 计算用户crc */ crc = stm32_sync.user_crc_calc(person); /* crc不同,证明用户信息改变,添加用户 */ if (crc != user_crc[uID - 1]) { is_change = true; if (stm32_sync.user_add(dst_addr.Mac, person) == false) { output("添加" + uID.ToString() + "号用户失败"); err = false; break; } } } } UInt32 user_id_max_device = (UInt32)user_crc.Length; UInt32 user_id_max_db = personnelManagement.max_user_id_get(); if (user_id_max_device < user_id_max_db) { for (UInt32 uID = (UInt32)(user_id_max_device + 1); uID <= user_id_max_db; uID++) { /* 获取用户信息 */ person = personnelManagement.user_get(uID); if (person.uID == uID) { is_change = true; if (stm32_sync.user_add(dst_addr.Mac, person) == false) { output("添加" + uID.ToString() + "号用户失败"); err = false; } } } } if (is_change == true) { /* 重新加载卡号-用户号,指纹号-用户号列表 */ stm32_sync.list_reload(dst_addr.Mac); } if (err == false) { output("同步用户信息出错"); continue; } else { output("同步用户信息成功"); } } } is_child_thread_stop = 0; }
/// <summary> /// 同步所有已连接设备的考勤记录 /// </summary> private void SyncATT() { output("获取人员进出记录"); DeviceManagement deviceManagement = new DeviceManagement(); AttendanceInfo attendanceInfo = new AttendanceInfo(); AttendanceInfo.AttInfo attInfo = new AttendanceInfo.AttInfo(); Stm32Sync stm32_sync = new Stm32Sync(); Stm32_crc stm32_crc = new Stm32_crc(); UInt32 crc = 0; MacAddr dst_addr; UInt32 total; UInt16 count; byte[] att_data; byte[] student_id = new byte[16]; byte[] name = new byte[16]; stm32_sync.output_dlg = output; int row = dgvDeviceInfo.SelectedRows.Count; /* 获取选中总行数 */ int rowIndex = dgvDeviceInfo.CurrentRow.Index; /* 获取选择行号 */ for (int j = 0; j < row; j++) { if (dgvDeviceInfo.SelectedRows[j].Cells[1].Value.ToString() == "已连接") { string addr_str = dgvDeviceInfo.SelectedRows[j].Cells[2].Value.ToString(); dst_addr = new MacAddr(addr_str); while (true) { /* 获取考勤记录 */ if (stm32_sync.att_get(dst_addr.Mac, (UInt16)10, out total, out count, out att_data) != true) { break; } for (int i = 0; i < count; i++) { crc = stm32_crc.block_crc_calc(att_data, (UInt32)(i * 53), 53 - 4); if (crc != (UInt32)((att_data[i * 53 + 49] << 0) + (att_data[i * 53 + 50] << 8) + (att_data[i * 53 + 51] << 16) + (att_data[i * 53 + 52] << 24))) { output("crc failed"); continue; } Buffer.BlockCopy(att_data, i * 53 + 2, student_id, 0, student_id.Length); Buffer.BlockCopy(att_data, i * 53 + 18, name, 0, name.Length); attInfo.deviceID = att_data[i * 53 + 34]; /* 设备ID */ attInfo.deviceName = deviceManagement.device_name_get(attInfo.deviceID); /* 设备名称 */ attInfo.mac = att_data[i * 53 + 35].ToString("x2") + ":" + att_data[i * 53 + 36].ToString("x2") + ":" + att_data[i * 53 + 37].ToString("x2") + ":" + att_data[i * 53 + 38].ToString("x2") + ":" + att_data[i * 53 + 39].ToString("x2") + ":" + att_data[i * 53 + 40].ToString("x2"); /* 设备mac地址 */ attInfo.uID = (UInt32)(att_data[i * 53 + 0] + (att_data[i * 53 + 1] << 8)); /* 用户号 */ attInfo.studentID = System.Text.Encoding.Default.GetString(student_id).Replace("\0", ""); /* 学号 */ attInfo.name = System.Text.Encoding.UTF8.GetString(name).Replace("\0", ""); /* 姓名 */ attInfo.state = (char)att_data[i * 53 + 41]; /* 状态 0:出门 1:进门 */ attInfo.time = (att_data[i * 53 + 42] + (att_data[i * 53 + 43] << 8)).ToString("D4") + "年" + att_data[i * 53 + 44].ToString("D2") + "月" + att_data[i * 53 + 45].ToString("D2") + "日 " + att_data[i * 53 + 46].ToString("D2") + ":" + att_data[i * 53 + 47].ToString("D2") + ":" + att_data[i * 53 + 48].ToString("D2"); /* 时间 */ attendanceInfo.AddInfo(attInfo); } /* 删除考勤记录 */ if (count != 0) { if (stm32_sync.att_del(dst_addr.Mac, count) != true) { break; } } if (total == 0) { output("已获取完成所有考勤记录"); break; } } } } is_child_thread_stop = 0; }