/// <summary> /// Reset this instance. /// </summary> public void reset() { try { resetMutex.WaitOne(); CRCFails = 0; Bugs = 0; Packets = 0; LengthFails = 0; FrameLoss = 0; FrameJumps = 0; productsReceived = new Dictionary <int, long>(); lock (demuxers) { var ks = demuxers.Keys.ToList(); foreach (var k in ks) { demuxers[k] = new Demuxer(this); } } bool lastState = RecordToFile; RecordToFile = false; RecordToFile = lastState; resetMutex.ReleaseMutex(); } catch (Exception e) { CrashReport.Report(e); throw e; } }
private void constellationDataLoop() { try { UIConsole.Log("Constellation Data Loop started"); var data = new float[1024]; for (var i = 0; i < 1024; i++) { data[i] = 0; } var ipHostInfo = Dns.GetHostEntry(ConstellationServerName); var ipAddress = new IPAddress(new byte[] { 127, 0, 0, 1 }); foreach (var ip in ipHostInfo.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetworkV6) { continue; } ipAddress = ip; break; } var remoteEP = new IPEndPoint(ipAddress, ConstellationServerPort); var udpClient = new UdpClient(ConstellationServerPort) { Client = { ReceiveTimeout = 200 } }; while (constellationDataThreadRunning) { try { var buffer = udpClient.Receive(ref remoteEP); if (buffer.Length != 1024) { continue; } for (var i = 0; i < 1024; i++) { var t = (sbyte)buffer[i]; data[i] = t; data[i] /= 128f; } postConstellationData(data); } catch (SocketException) { // Do nothing, timeout on UDP } } UIConsole.Log("Constellation Thread closed."); } catch (Exception e) { CrashReport.Report(e); } }
private void ThreadLoop() { try { while (running) { SingleThreadRun(); Thread.Sleep(200); } } catch (Exception e) { CrashReport.Report(e); throw; } }
private void constellationDataLoop() { try { UIConsole.Log("Constellation Data Loop started"); byte[] buffer = null; float[] data = new float[1024]; for (int i = 0; i < 1024; i++) { data[i] = 0; } IPHostEntry ipHostInfo = Dns.GetHostEntry(ConstellationServerName); IPAddress ipAddress = new IPAddress(new byte[] { 127, 0, 0, 1 }); foreach (IPAddress ip in ipHostInfo.AddressList) { if (ip.AddressFamily != AddressFamily.InterNetworkV6) { ipAddress = ip; break; } } IPEndPoint remoteEP = new IPEndPoint(ipAddress, ConstellationServerPort); UdpClient udpClient = new UdpClient(ConstellationServerPort); udpClient.Client.ReceiveTimeout = 200; while (constellationDataThreadRunning) { try { buffer = udpClient.Receive(ref remoteEP); if (buffer != null && buffer.Length == 1024) { for (int i = 0; i < 1024; i++) { sbyte t = (sbyte)buffer[i]; data[i] = t; data[i] /= 128f; } this.postConstellationData(data); } } catch (SocketException) { // Do nothing, timeout on UDP } } UIConsole.Log("Constellation Thread closed."); } catch (Exception e) { CrashReport.Report(e); } }
public void parseBytes(byte[] data) { try { int scid = ((data[0] & 0x3F) << 2) | ((data[1] & 0xC0) >> 6); int vcid = (data[1] & 0x3F); int vcnt = (data[2] << 16 | data[3] << 8 | data[4]); // UIConsole.Log($"Satellite ID: {scid}"); EventMaster.Post(EventTypes.FrameEvent, new FrameEventData { ChannelID = vcid, PacketNumber = vcnt }); if (vcid != FILL_VCID) { resetMutex.WaitOne(); lock (demuxers) { if (!demuxers.ContainsKey(vcid)) { UIConsole.Log($"I don't have a demuxer for VCID {vcid}. Creating..."); demuxers.Add(vcid, new Demuxer(this)); } } recordMutex.WaitOne(); if (RecordToFile) { try { fStream.Write(data, 0, data.Length); } catch (Exception e) { UIConsole.Error($"Error writting demuxdump file: {e}"); } } recordMutex.ReleaseMutex(); demuxers[vcid].ParseBytes(data); resetMutex.ReleaseMutex(); } } catch (Exception e) { CrashReport.Report(e); throw e; } }
public DemuxManager() { try { demuxers = new Dictionary <int, Demuxer>(); productsReceived = new Dictionary <int, long>(); CRCFails = 0; Bugs = 0; Packets = 0; LengthFails = 0; FrameLoss = 0; FrameJumps = 0; recordMutex = new Mutex(); resetMutex = new Mutex(); if (RecordToFile) { fileName = string.Format("demuxdump-{0}.bin", LLTools.Timestamp()); UIConsole.Log(string.Format("Demux Dump filename: {0}", Path.GetFileName(fileName))); fStream = File.OpenWrite(fileName); } } catch (Exception e) { CrashReport.Report(e); throw e; } }
private void channelDataLoop() { try { UIConsole.Log("Channel Data Loop started"); byte[] buffer = new byte[892]; IPHostEntry ipHostInfo = Dns.GetHostEntry(ChannelDataServerName); IPAddress ipAddress = new IPAddress(new byte[] { 127, 0, 0, 1 }); foreach (IPAddress ip in ipHostInfo.AddressList) { if (ip.AddressFamily != AddressFamily.InterNetworkV6) { ipAddress = ip; break; } } IPEndPoint remoteEP = new IPEndPoint(ipAddress, ChannelDataServerPort); Socket sender = null; while (channelDataThreadRunning) { bool isConnected = true; UIConsole.Log("Channel Data Thread connect"); try { sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sender.ReceiveTimeout = 3000; sender.Connect(remoteEP); isConnected = true; UIConsole.Log(String.Format("Socket connected to {0}", sender.RemoteEndPoint.ToString())); int nullReceive = 0; while (isConnected) { try { var receivedBytes = sender.Receive(buffer); if (receivedBytes < buffer.Length && receivedBytes != 0) { UIConsole.Error("Received less bytes than channel data!"); Thread.Sleep(200); nullReceive = 0; } else if (receivedBytes == 0) { nullReceive++; if (nullReceive == 5) { UIConsole.Error("Cannot reach server. Dropping connection!"); isConnected = false; sender.Shutdown(SocketShutdown.Both); sender.Disconnect(false); sender.Close(); } } else { nullReceive = 0; this.postChannelData(buffer); } } catch (ArgumentNullException ane) { UIConsole.Error(String.Format("ArgumentNullException : {0}", ane.ToString())); isConnected = false; } catch (SocketException se) { // That's usually timeout. I would say that is best to handle and show some message // But for now, that would make it confusing for the users. So let's keep without a notice. //UIConsole.GlobalConsole.Error(String.Format("SocketException : {0}", se.ToString())); isConnected = false; } catch (Exception e) { UIConsole.Error(String.Format("Unexpected exception : {0}", e.ToString())); isConnected = false; } DataConnected = isConnected; if (!channelDataThreadRunning) { break; } } sender.Shutdown(SocketShutdown.Both); sender.Disconnect(false); sender.Close(); } catch (ArgumentNullException ane) { UIConsole.Error(String.Format("ArgumentNullException : {0}", ane.ToString())); } catch (SocketException se) { UIConsole.Error(String.Format("SocketException : {0}", se.ToString())); } catch (Exception e) { UIConsole.Error(String.Format("Unexpected exception : {0}", e.ToString())); } if (channelDataThreadRunning) { UIConsole.Warn("Socket closed. Waiting 1s before trying again."); Thread.Sleep(1000); } } UIConsole.Debug("Requested to close Channel Data Thread!"); try { if (sender != null) { sender.Shutdown(SocketShutdown.Both); sender.Disconnect(false); sender.Close(); } } catch (Exception e) { UIConsole.Debug(String.Format("Exception thrown when closing socket: {0} Ignoring.", e.ToString())); } UIConsole.Log("Channel Data Thread closed."); } catch (Exception e) { CrashReport.Report(e); } }
private void statisticsLoop() { try { UIConsole.Log("Statistics Thread Started"); var buffer = new byte[4167]; var ipHostInfo = Dns.GetHostEntry(StatisticsServerName); var ipAddress = new IPAddress(new byte[] { 127, 0, 0, 1 }); foreach (var ip in ipHostInfo.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetworkV6) { continue; } ipAddress = ip; break; } var remoteEP = new IPEndPoint(ipAddress, StatisticsServerPort); Socket sender = null; while (statisticsThreadRunning) { try { UIConsole.Log("Statistics Thread connect"); sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { ReceiveTimeout = 5000 }; sender.Connect(remoteEP); var isConnected = true; UIConsole.Log($"Socket connected to {sender.RemoteEndPoint}"); var nullReceive = 0; while (isConnected) { try { var receivedBytes = sender.Receive(buffer); if (receivedBytes < buffer.Length && receivedBytes != 0) { nullReceive = 0; UIConsole.Error("Received less than Statistics Packet size!"); Thread.Sleep(200); } else if (receivedBytes == 0) { nullReceive++; if (nullReceive == 5) { UIConsole.Error("Cannot reach server. Dropping connection!"); isConnected = false; sender.Shutdown(SocketShutdown.Both); sender.Disconnect(false); sender.Close(); } } else { nullReceive = 0; var sst = Statistics_st.fromByteArray(buffer); this.postStatistics(sst); } } catch (ArgumentNullException ane) { UIConsole.Error($"ArgumentNullException : {ane}"); isConnected = false; } catch (SocketException se) { // That's usually timeout. I would say that is best to handle and show some message // But for now, that would make it confusing for the users. So let's keep without a notice. //UIConsole.GlobalConsole.Error(String.Format("SocketException : {0}", se.ToString())); isConnected = false; } catch (Exception e) { UIConsole.Error($"Unexpected exception : {e}"); isConnected = false; } StatisticsConnected = isConnected; if (!statisticsThreadRunning) { break; } Thread.Sleep(1); } sender.Shutdown(SocketShutdown.Both); sender.Disconnect(false); sender.Close(); } catch (ArgumentNullException ane) { UIConsole.Error($"ArgumentNullException : {ane}"); } catch (SocketException se) { UIConsole.Error($"SocketException : {se}"); } catch (Exception e) { UIConsole.Error($"Unexpected exception : {e}"); } if (!statisticsThreadRunning) { continue; } UIConsole.Warn("Socket closed. Waiting 1s before trying again."); Thread.Sleep(1000); } Console.WriteLine("Requested to close Statistics Thread!"); try { if (sender != null) { sender.Shutdown(SocketShutdown.Both); sender.Disconnect(false); sender.Close(); } } catch (Exception e) { UIConsole.Debug($"Exception thrown when closing socket: {e} Ignoring."); } UIConsole.Log("Statistics Thread closed"); } catch (Exception e) { CrashReport.Report(e); } }
void ThreadLoop() { try { while (running) { organizer.Update(); var data = organizer.GroupData; var clist = data.ToList(); foreach (var z in clist) { var mData = z.Value; if (!running) { break; } string ImageName = string.Format("{0}-{1}-{2}", z.Key, mData.SatelliteName, mData.RegionName); if (!mData.IsProcessed) { try { if (ImageManager.GenerateVisible && mData.Visible.IsComplete && mData.Visible.MaxSegments != 0 && !mData.IsVisibleProcessed) { string ofilename = Path.Combine(folder, GenFilename(mData.SatelliteName, mData.RegionName, "VIS", z.Key, mData.Visible.Segments [mData.Visible.FirstSegment])); if (File.Exists(ofilename)) { UIConsole.Debug($"Skipping generating Visible for {Path.GetFileName(ofilename)}. Image already exists."); mData.IsVisibleProcessed = true; } else { UIConsole.Debug(string.Format("Starting Generation of Visible for {0}.", Path.GetFileName(ofilename))); var bmp = ImageTools.GenerateFullImage(mData.Visible, mData.CropImage); if (GenerateMapOverlays || GenerateLatLonOverlays || GenerateLabels) { if (SaveNonOverlay) { string orgFileName = Path.Combine(folder, $"{Path.GetFileNameWithoutExtension(ofilename)}-original.png"); bmp.Save(orgFileName, ImageFormat.Png); } UIConsole.Debug(string.Format("Generating Overlays Visible for {0}.", Path.GetFileName(ofilename))); GenerateImageOverlay(ref bmp, mData, mData.Visible); } bmp.Save(ofilename, ImageFormat.Png); bmp.Dispose(); UIConsole.Log($"New Visible Image: {Path.GetFileName(ofilename)}"); EventMaster.Post("newFile", new NewFileReceivedEventData() { Name = Path.GetFileName(ofilename), Path = ofilename, Metadata = { { "channel", "visible" }, { "satelliteName", mData.SatelliteName },{ "regionName", mData.RegionName },{ "timestamp", z.Key.ToString() } } }); } mData.IsVisibleProcessed = true; mData.Visible.OK = true; } else if (mData.Visible.MaxSegments == 0) { mData.IsVisibleProcessed = true; mData.Visible.OK = true; } if (ImageManager.GenerateInfrared && mData.Infrared.IsComplete && mData.Infrared.MaxSegments != 0 && !mData.IsInfraredProcessed) { string ofilename = Path.Combine(folder, GenFilename(mData.SatelliteName, mData.RegionName, "IR", z.Key, mData.Infrared.Segments [mData.Infrared.FirstSegment])); if (File.Exists(ofilename)) { UIConsole.Debug($"Skipping generating Infrared for {Path.GetFileName(ofilename)}. Image already exists."); } else { UIConsole.Debug($"Starting Generation of Infrared for {Path.GetFileName(ofilename)}."); var bmp = ImageTools.GenerateFullImage(mData.Infrared, mData.CropImage); if (GenerateMapOverlays || GenerateLatLonOverlays || GenerateLabels) { if (SaveNonOverlay) { string orgFileName = Path.Combine(folder, $"{Path.GetFileNameWithoutExtension(ofilename)}-original.png"); bmp.Save(orgFileName, ImageFormat.Png); } UIConsole.Debug(string.Format("Generating Overlays Infrared for {0}.", Path.GetFileName(ofilename))); GenerateImageOverlay(ref bmp, mData, mData.Infrared); } bmp.Save(ofilename, ImageFormat.Png); bmp.Dispose(); UIConsole.Log($"New Infrared Image: {Path.GetFileName(ofilename)}"); EventMaster.Post("newFile", new NewFileReceivedEventData() { Name = Path.GetFileName(ofilename), Path = ofilename, Metadata = { { "channel", "infrared" }, { "satelliteName", mData.SatelliteName },{ "regionName", mData.RegionName },{ "timestamp", z.Key.ToString() } } }); } mData.IsInfraredProcessed = true; mData.Infrared.OK = true; } else if (mData.Infrared.MaxSegments == 0) { mData.IsInfraredProcessed = true; mData.Infrared.OK = true; } if (ImageManager.GenerateWaterVapour && mData.WaterVapour.IsComplete && mData.WaterVapour.MaxSegments != 0 && !mData.IsWaterVapourProcessed) { string ofilename = Path.Combine(folder, GenFilename(mData.SatelliteName, mData.RegionName, "WV", z.Key, mData.WaterVapour.Segments [mData.WaterVapour.FirstSegment])); if (File.Exists(ofilename)) { UIConsole.Debug($"Skipping generating Water Vapour for {Path.GetFileName(ofilename)}. Image already exists."); } else { UIConsole.Debug($"Starting Generation of Water Vapour for {Path.GetFileName(ofilename)}."); var bmp = ImageTools.GenerateFullImage(mData.WaterVapour, mData.CropImage); if (GenerateMapOverlays || GenerateLatLonOverlays || GenerateLabels) { if (SaveNonOverlay) { string orgFileName = Path.Combine(folder, $"{Path.GetFileNameWithoutExtension(ofilename)}-original.png"); bmp.Save(orgFileName, ImageFormat.Png); } UIConsole.Debug(string.Format("Generating Overlays WaterVapour for {0}.", Path.GetFileName(ofilename))); GenerateImageOverlay(ref bmp, mData, mData.WaterVapour); } bmp.Save(ofilename, ImageFormat.Png); bmp.Dispose(); UIConsole.Log($"New Water Vapour Image: {Path.GetFileName(ofilename)}"); EventMaster.Post("newFile", new NewFileReceivedEventData() { Name = Path.GetFileName(ofilename), Path = ofilename, Metadata = { { "channel", "watervapour" }, { "satelliteName", mData.SatelliteName },{ "regionName", mData.RegionName },{ "timestamp", z.Key.ToString() } } }); } mData.IsWaterVapourProcessed = true; mData.WaterVapour.OK = true; } if (GenerateFalseColor && !mData.IsFalseColorProcessed && ImageTools.CanGenerateFalseColor(mData)) { string filename = GenFilename( mData.SatelliteName, mData.RegionName, "FSCLR", z.Key, mData.Visible.Segments [mData.Visible.FirstSegment] .Replace("VS", "FC") .Replace("VIS", "FC") ); filename = Path.Combine(folder, filename); if (File.Exists(filename)) { UIConsole.Debug($"Skipping generating FLSCLR for {Path.GetFileName(filename)}. Image already exists."); } else { UIConsole.Debug($"Starting Generation of FSLCR for {Path.GetFileName(filename)}."); var bmp = ImageTools.GenerateFalseColor(mData); if (GenerateMapOverlays || GenerateLatLonOverlays || GenerateLabels) { if (SaveNonOverlay) { string orgFileName = Path.Combine(folder, $"{Path.GetFileNameWithoutExtension(filename)}-original.png"); bmp.Save(orgFileName, ImageFormat.Png); } UIConsole.Debug(string.Format("Generating Overlays False Colour for {0}.", Path.GetFileName(filename))); GenerateImageOverlay(ref bmp, mData, mData.Visible); // Using visible coordinates } bmp.Save(filename, ImageFormat.Png); bmp.Dispose(); UIConsole.Log($"New False Colour Image: {Path.GetFileName(filename)}"); EventMaster.Post("newFile", new NewFileReceivedEventData() { Name = Path.GetFileName(filename), Path = filename, Metadata = { { "channel", "filename" }, { "satelliteName", mData.SatelliteName },{ "regionName", mData.RegionName },{ "timestamp", z.Key.ToString() } } }); } mData.IsFalseColorProcessed = true; } if (GenerateOtherImages && !mData.IsOtherDataProcessed && mData.OtherData.Count > 0) { mData.OtherData.Keys.ToList().ForEach(k => { var gd = mData.OtherData [k]; if (gd.IsComplete && gd.MaxSegments != 0 && !gd.OK) { string ofilename = GenFilename(mData.SatelliteName, mData.RegionName, gd.Code, gd.Timestamp, gd.Segments [0]); ofilename = Path.Combine(folder, ofilename); if (File.Exists(ofilename)) { UIConsole.Debug($"Skipping generating {Path.GetFileName(ofilename)}. Image already exists."); } else { UIConsole.Debug($"Starting Generation of {Path.GetFileName(ofilename)}."); var bmp = ImageTools.GenerateFullImage(gd, false); if (GenerateMapOverlays || GenerateLatLonOverlays || GenerateLabels) { if (SaveNonOverlay) { string orgFileName = Path.Combine(folder, $"{Path.GetFileNameWithoutExtension(ofilename)}-original.png"); bmp.Save(orgFileName, ImageFormat.Png); } UIConsole.Debug(string.Format("Generating Overlays for {0}.", Path.GetFileName(ofilename))); GenerateImageOverlay(ref bmp, mData, gd); } bmp.Save(ofilename, ImageFormat.Png); bmp.Dispose(); UIConsole.Log($"New Image: {Path.GetFileName(ofilename)}"); EventMaster.Post("newFile", new NewFileReceivedEventData() { Name = Path.GetFileName(ofilename), Path = ofilename, Metadata = { { "channel", "otherimages" },{ "satelliteName", mData.SatelliteName },{ "regionName", mData.RegionName },{ "timestamp", z.Key.ToString() } } }); } gd.OK = true; } }); } else if (mData.OtherData.Count == 0) { if (mData.ReadyToMark) { mData.IsOtherDataProcessed = true; } } if (mData.ReadyToMark) { mData.IsProcessed = (!GenerateFalseColor || (GenerateFalseColor && mData.IsFalseColorProcessed)) && (!GenerateVisible || (GenerateVisible && mData.IsVisibleProcessed)) && (!GenerateInfrared || (GenerateInfrared && mData.IsInfraredProcessed)) && (!GenerateWaterVapour || (GenerateWaterVapour && mData.IsWaterVapourProcessed)) && (!GenerateOtherImages || (GenerateOtherImages && mData.IsOtherDataProcessed)); } if (mData.Timeout) { // Timeout completing, so let's erase the files. mData.ForceComplete(); } if (EraseFiles) { TryEraseGroupDataFiles(z.Key, mData); } } catch (SystemException e) { UIConsole.Error($"Error processing image (SysExcpt) {ImageName}: {e}"); mData.RetryCount++; if (mData.RetryCount == ImageManager.MaxRetryCount) { mData.IsProcessed = true; } } catch (Exception e) { UIConsole.Error($"Error processing image {ImageName}: {e}"); mData.RetryCount++; if (mData.RetryCount == ImageManager.MaxRetryCount) { mData.IsProcessed = true; } } } } Thread.Sleep(200); } } catch (Exception e) { CrashReport.Report(e); throw e; } }