private CBeiDouDevice parseBeiDouDevice(string deviceLine) { string[] deviceItems = deviceLine.Split(','); if (deviceItems.Length < 4) { message = string.Format("Device data format error,<{0}>.", deviceLine); _SystemLog.writeLog(LOG_LEVEL.ERR, message); return(null); } CBeiDouDevice beiDouDevice = new CBeiDouDevice(_SystemLog, _DbConnection); beiDouDevice.Id = deviceItems [0]; beiDouDevice.Name = deviceItems [1]; bool deviceHasCamer = false; if (!bool.TryParse(deviceItems [2], out deviceHasCamer)) { deviceHasCamer = false; } beiDouDevice.HasCamer = deviceHasCamer; beiDouDevice.RtspAddress = deviceItems [3]; if (deviceItems.Length == 5) { beiDouDevice.Description = deviceItems [4]; } return(beiDouDevice); }
public bool loadBeiDouDeviceFromFile(string deviceDataFile, Dictionary <string, CBeiDouDevice> beiDouDeviceTable, bool isStore2Db) { int savedCount = 0; if (!File.Exists(deviceDataFile)) { message = string.Format("Device data file<{0}> not exists.", deviceDataFile); _SystemLog.writeLog(LOG_LEVEL.ERR, message); return(false); } StreamReader fileReader = new StreamReader(deviceDataFile, Encoding.UTF8); if (fileReader == null) { message = string.Format("Open device data file<{0}> faild.", deviceDataFile); _SystemLog.writeLog(LOG_LEVEL.ERR, message); return(false); } string readLine = null; while ((readLine = fileReader.ReadLine()) != null) { readLine = readLine.Trim(); CBeiDouDevice beiDouDevice = parseBeiDouDevice(readLine); if (beiDouDeviceTable.ContainsKey(beiDouDevice.Id)) { message = string.Format("Device<{0}> exists.", beiDouDevice); _SystemLog.writeLog(LOG_LEVEL.ERR, message); continue; } beiDouDeviceTable.Add(beiDouDevice.Id, beiDouDevice); } fileReader.Close(); fileReader.Dispose(); fileReader = null; if (isStore2Db) { foreach (KeyValuePair <string, CBeiDouDevice> kvp in beiDouDeviceTable) { if (kvp.Value.save2Db()) { savedCount += 1; } else { message = string.Format("Device <{0}> save to db faild.", kvp.Value); _SystemLog.writeLog(LOG_LEVEL.ERR, message); } } } return(beiDouDeviceTable.Count == savedCount); }
private void processDbReaderDelegate(MySqlDataReader mySqlDataReader) { while (mySqlDataReader.Read()) { CBeiDouDevice beiDouDevice = new CBeiDouDevice(_SystemLog, _DbConnection); beiDouDevice.Id = mySqlDataReader.GetString(CBeiDouDevice.ID); beiDouDevice.Name = mySqlDataReader.GetString(CBeiDouDevice.NAME); beiDouDevice.HasCamer = mySqlDataReader.GetBoolean(CBeiDouDevice.HAS_CAMER); try { beiDouDevice.RtspAddress = mySqlDataReader.GetString(CBeiDouDevice.RTSP_ADDRESS); } catch { beiDouDevice.RtspAddress = ""; } try { beiDouDevice.LastModifyTime = mySqlDataReader.GetDateTime(CBeiDouDevice.LAST_MODIFY_TIME); } catch { beiDouDevice.LastModifyTime = DateTime.Now; } beiDouDevice.DeviceType = (E_DEVICE_TYPE)mySqlDataReader.GetInt16(CBeiDouDevice.DEVICE_TYPE); beiDouDevice.DeviceState = mySqlDataReader.GetInt16(CBeiDouDevice.DEVICE_STATE); beiDouDevice.Longitude = mySqlDataReader.GetFloat(CBeiDouDevice.LONGITUDE); beiDouDevice.Latitude = mySqlDataReader.GetFloat(CBeiDouDevice.LATITUDE); beiDouDevice.Altitude = mySqlDataReader.GetFloat(CBeiDouDevice.ALTITUDE); beiDouDevice.OrganizationCode = mySqlDataReader.GetString(CBeiDouDevice.ORGANIZATION_CODE); beiDouDevice.LastLocateTime = mySqlDataReader.GetDateTime(CBeiDouDevice.LAST_LOCATE_TIME); beiDouDevice.LongitudeDirection = mySqlDataReader.GetString(CBeiDouDevice.LONGITUDE_DIRECTION); beiDouDevice.LatitudeDirection = mySqlDataReader.GetString(CBeiDouDevice.LATITUDE_DIRECTION); beiDouDevice.AverageSpeed = mySqlDataReader.GetFloat(CBeiDouDevice.AVERAGE_SPEED); beiDouDevice.IsDeleted = mySqlDataReader.GetBoolean(CBeiDouDevice.IS_DELETED); try { beiDouDevice.Description = mySqlDataReader.GetString(CBeiDouDevice.DESCRIPTION); } catch { beiDouDevice.Description = ""; } if (!_DbBeiDouDeviceTable.ContainsKey(beiDouDevice.Id)) { _DbBeiDouDeviceTable.Add(beiDouDevice.Id, beiDouDevice); } } }
private void findUpdateDevice() { _UpdateDeviceArray.Clear(); foreach (KeyValuePair <string, CBeiDouDevice> kvp in _RemoteServerBeiDouDeviceTable) { string deviceId = kvp.Key; if (_DbBeiDouDeviceTable.ContainsKey(deviceId)) //not in db { CBeiDouDevice dbBeiDouDevice = _DbBeiDouDeviceTable[deviceId]; if (dbBeiDouDevice.IsDeleted) { dbBeiDouDevice.IsDeleted = false; _UpdateDeviceArray.Add(dbBeiDouDevice); } } } }
private E_ERROR_CODE insertDeviceToDb() { string message = string.Empty; List <List <CDbParameter> > recordTable = new List <List <CDbParameter> >(); foreach (CBeiDouDevice beiDevice in _InsertDeviceArray) { List <CDbParameter> recordParameter = beiDevice.convert2DbParameterList(); if (recordParameter != null) { recordTable.Add(recordParameter); } else { message = string.Format("Conver device<{0}> to db parameter list faild.", beiDevice.Id); _SystemLog.writeLog(LOG_LEVEL.ERR, message); } } int addDeviceCount = 0; bool dbConnectionsaveRecords = _DbConnection.saveRecords(CBeiDouDevice.INSERT_RECORD_SQL, recordTable, CBeiDouDevice.getFieldNameList(), out addDeviceCount); message = string.Format("Add <{0}> devices to db.", addDeviceCount); _SystemLog.writeLog(LOG_LEVEL.INFO, message); return(dbConnectionsaveRecords ? E_ERROR_CODE.OK : E_ERROR_CODE.ERROR); }
private bool parseDeviceRecord(JArray beiDouDeviceRecords) { string errorMessage = string.Empty; foreach (JToken beiDouRecord in beiDouDeviceRecords) { if (beiDouRecord == null) { errorMessage = string.Format("Convert to JObject faild, content<{0}>.", beiDouRecord); _SystemLog.writeLog(LOG_LEVEL.ERR, errorMessage); continue; } string deviceId = beiDouRecord[0].Value <string>(); if (_RemoteServerBeiDouDeviceTable.ContainsKey(deviceId)) { errorMessage = string.Format("Device <{0}> exist, this device record will be discard.", deviceId); _SystemLog.writeLog(LOG_LEVEL.WARNING, errorMessage); continue; } /* * 0 string √ 设备编号 * 1 integer √ 设备类型(0:指挥机,1:终端) * 2 float √ 当前位置,经度 * 3 float √ 当前位置,纬度 * 4 float √ 当前位置,高度 * 5 integer √ 设备状态(0:正常,1:故障) * 6 string × 备注 **/ CBeiDouDevice beiDouDevice = new CBeiDouDevice(_SystemLog, _DbConnection); beiDouDevice.Id = deviceId; try { beiDouDevice.DeviceType = (E_DEVICE_TYPE)beiDouRecord[1].Value <int>(); } catch { continue; } try { beiDouDevice.Longitude = beiDouRecord[2].Value <float>(); } catch { continue; } try { beiDouDevice.Latitude = beiDouRecord[3].Value <float>(); } catch { continue; } try { beiDouDevice.Altitude = beiDouRecord[4].Value <float>(); } catch { continue; } try { beiDouDevice.DeviceState = beiDouRecord[5].Value <int>(); } catch { continue; } try { beiDouDevice.Description = beiDouRecord[6].Value <string>(); } catch { beiDouDevice.Description = ""; } _RemoteServerBeiDouDeviceTable.Add(beiDouDevice.Id, beiDouDevice); } return(true); }