private void LoadModbus() { bool GetIp = true; string IPModbusInput = ""; string IPModbusOutput = ""; while (GetIp) { if (IPModbusInput == "") { try { if (GlobalProperties.DatabaseStatus) { // GET Modbus Input & Output IP Address IPModbusInput = DB_SFDB.IpDigitalInput(); IPModbusOutput = DB_SFDB.IpDigitalOutput(); GetIp = false; } } catch { } } Thread.Sleep(500); } // Check This Application Is Debugging OR Not // AND Add Modbus Device(s) bool mode = App.Default.IsDebugging; if (mode) { GlobalProperties.DevicesInput.Add(new ModbusInput(Modbusconfig.Default.ModbusDebugInput, Modbusconfig.Default.ModbusDebugInputPort, 0)); GlobalProperties.DevicesOutput.Add(new ModbusOutput(Modbusconfig.Default.ModbusDebugOutput, Modbusconfig.Default.ModbusDebugOutputPort, 0)); } else { GlobalProperties.DevicesInput.Add(new ModbusInput(IPModbusInput)); GlobalProperties.DevicesOutput.Add(new ModbusOutput(IPModbusOutput)); } Application.DoEvents(); // Do Connect Modbus Input foreach (ModbusInput device in GlobalProperties.DevicesInput) { device.StartLogging(); } // Do Connect Modbus Output foreach (ModbusOutput device in GlobalProperties.DevicesOutput) { device.StartLogging(); } // Set Device to Actions Lamp Actions.DeviceOutput = GlobalProperties.DevicesOutput[0]; // Start Logging TaskLogging.Start(); }
private void BgwSubmit_DoWork(object sender, DoWorkEventArgs e) { IsOnceSubmit = true; string userid = GlobalProperties.UserID; char coolanttype = GlobalProperties.CoolantType; retry = true; while (retry) { try { if (GlobalProperties.DatabaseStatus) { string datenow = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); DB_SFDB.InsertHistoryLoading(userid, coolanttype, datenow); retry = false; } } catch (Exception x) { Core.Log.Error.Collect(x.StackTrace.ToString()); } Thread.Sleep(100); } }
private void BgworkHistory_DoWork(object sender, DoWorkEventArgs e) { while (true) { try { if (PublicProperties.DatabaseStatus) { var dtHistory = DB_SFDB.PopulateHistory(typeCoolant); BeginInvoke((MethodInvoker) delegate { try { dgvHistory.Rows.Clear(); int row = 0; foreach (DataRow dr in dtHistory.Rows) { dgvHistory.Rows.Add(dr.ItemArray); dgvHistory.Rows[row].Cells[0].Value = row + 1; row += 1; } } catch (Exception x) { Error.Collect(x.StackTrace.ToString()); } }); } } catch { } Thread.Sleep(3000); } }
private void BgwMaintenance_DoWork(object sender, DoWorkEventArgs e) { try { if (GlobalProperties.DatabaseStatus) { FLAG_MAINTENANCE = DB_SFDB.DailyLoadingStationCheck(); } } catch (Exception x) { Core.Log.Error.Collect(x.StackTrace.ToString()); } }
private void BgwTasklist_DoWork(object sender, DoWorkEventArgs e) { try { if (GlobalProperties.DatabaseStatus) { dtTasklist = DB_SFDB.PopulateTasklist(); } } catch (Exception m) { Error.Collect(m.StackTrace.ToString()); Debug.WriteLine(m); } }
private void BgwSubmit_DoWork(object sender, DoWorkEventArgs e) { while (retry) { try { if (PublicProperties.DatabaseStatus) { DB_SFDB.DailyLoadingStationReady(); retry = false; panelNotification.Visible = false; } } catch { } Thread.Sleep(100); } }
private void BgwSubmit_DoWork(object sender, DoWorkEventArgs e) { while (retry) { try { if (GlobalProperties.DatabaseStatus) { DB_SFDB.DailyLoadingStationReady(); retry = false; } } catch (Exception x) { Base.Log.Error.Collect(x.StackTrace.ToString()); } Thread.Sleep(100); } }
private static void StatusServiceWorker_DoWork(object sender, DoWorkEventArgs e) { while (true) { try { if (GlobalProperties.DatabaseStatus) { DB_SFDB.CurrentStatusState(); } } catch (Exception x) { Log.Error.Collect(x.StackTrace.ToString()); Debug.WriteLine(x); } Thread.Sleep(1000); } }
private void btnSave_Click(object sender, EventArgs e) { if (GlobalProperties.DatabaseStatus) { CoreLS.Default.WaterFillingValue = int.Parse(txtWaterValue.Text); // by percentage CoreLS.Default.CoolantFillingValue = int.Parse(txtCoolantValue.Text); // by pulse CoreLS.Default.Save(); CoreLS.Default.Upgrade(); double CoolantPulse = double.Parse(txtPulseCoolant.Text); DB_SFDB.UpdatePulse(CoolantPulse); } else { MessageBox.Show("Data Unsaved Cause Reconnecting to Server", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error); } Close(); }
private void BgwSubmit_DoWork(object sender, DoWorkEventArgs e) { IsOnceSubmit = true; string userid = PublicProperties.UserID; char coolanttype = PublicProperties.CoolantType; retry = true; while (retry) { try { if (PublicProperties.DatabaseStatus) { string datenow = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); DB_SFDB.InsertHistoryLoading(userid, coolanttype, datenow); retry = false; } } catch { } Thread.Sleep(50); } }
private void BgwAuth_DoWork(object sender, DoWorkEventArgs e) { try { if (txtRfid.Text != "") { _AuthenticationResult = false; if (GlobalProperties.DatabaseStatus && GlobalProperties.ModbusInputStatus && GlobalProperties.ModbusOutputStatus) { _AuthenticationResult = DB_SFDB.LoginAuthentication(ID); } else { MessageBox.Show("Cannot Login While Reconnecting!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } catch (Exception m) { Error.Collect(m.StackTrace.ToString()); } }
private void BgwTasklist_DoWork(object sender, DoWorkEventArgs e) { while (true) { try { if (PublicProperties.DatabaseStatus) { dtTasklist = DB_SFDB.PopulateTasklist(); BeginInvoke((MethodInvoker) delegate { try { dgvTasklist.Rows.Clear(); int row = 0; foreach (DataRow dr in dtTasklist.Rows) { dgvTasklist.Rows.Add(dr.ItemArray); dgvTasklist.Rows[row].Cells[0].Value = row + 1; row += 1; } } catch { } }); } } catch (Exception m) { Error.Collect(m.StackTrace.ToString()); Debug.WriteLine(m); } Thread.Sleep(2000); } }
public static bool CheckDevice(int SerialNumber) { /* * Function Serial Number Validator * Flow : SerialNumber --> Binary --> Byte --> SHA256 --> Validation DB */ bool Result = false; string ResultBinary = ""; byte[] ResultBytes = null; string ResultByteString = ""; string ResultEncrypt256 = ""; try { // SerialNumber (Decimal) --> Binary while (SerialNumber > 1) { int remainder = SerialNumber % 2; ResultBinary = Convert.ToString(remainder) + ResultBinary; SerialNumber /= 2; } ResultBinary = Convert.ToString(SerialNumber) + ResultBinary; // Binary --> Byte int numOfBytes = ResultBinary.Length / 8; ResultBytes = new byte[numOfBytes]; for (int i = 0; i < numOfBytes; ++i) { ResultBytes[i] = Convert.ToByte(ResultBinary.Substring(8 * i, 8), 2); } // Byte --> String int ResultBytesLength = ResultBytes.Length; for (int i = 0; i < ResultBytesLength; i++) { ResultByteString += ResultBytes[i].ToString(); } // String --> SHA256 --> String using (SHA256 encrypt = SHA256.Create()) { byte[] bytes = encrypt.ComputeHash(Encoding.UTF8.GetBytes(ResultByteString)); StringBuilder strBuilder = new StringBuilder(); for (int i = 0; i < bytes.Length; i++) { strBuilder.Append(bytes[i].ToString("x2")); } ResultEncrypt256 = strBuilder.ToString(); } // Check SHA256.String --> Database.mst_serialnumber var CheckSerialFromDB = DB_SFDB.CheckSerialNumber(ResultEncrypt256); if (CheckSerialFromDB) { Result = true; } Debug.WriteLine(string.Format("SerialNumber: {0} Binary: {1} , Byte: {2}, SHA256: {3}", SerialNumber.ToString(), ResultBinary.ToString(), ResultByteString, ResultEncrypt256)); } catch { } return(Result); }
private void DoLogging() { var DeviceOutput = GlobalProperties.DevicesOutput[0]; var DeviceInput = GlobalProperties.DevicesInput[0]; // GET Max Min value Mixing int MaxMixing = CoreLS.Default.MaxMixing; int MinMixing = CoreLS.Default.MinMixing; // GET Max Min int MaxDrum = CoreLS.Default.MaxDrum; int MinDrum = CoreLS.Default.MinDrum; // GET Max Min value Measure int MaxMeasure = CoreLS.Default.MaxMeasure; int MinMeasure = CoreLS.Default.MinMeasure; // Default Channel dikurang 1 const int ValveCoolant = 14; const int ValveWater = 13; const int PumpDrum = 11; const int PumpDist = 10; const int Propeler = 12; const int Interlock = 9; // Addr Lamp const int LampGreen = 7; const int LampOrange = 6; const int LampRed = 8; const int LampWhite = 5; // Value Modbus Input (Feedback) int StatusValveWater; int StatusValveCoolant; Task TaskPropeler = new Task(() => { while (Logging) { Actions.SetIndicator(GlobalProperties.Valve.Open, GlobalProperties.Type.Propeler); DeviceOutput.SetBit(Propeler); Thread.Sleep(PropelerCycleTime); Actions.SetIndicator(GlobalProperties.Valve.Close, GlobalProperties.Type.Propeler); DeviceOutput.ResetBit(Propeler); Thread.Sleep(PropelerCycleTime); } }); TaskPropeler.Start(); Task TaskEmergency = new Task(() => { while (Logging) { DeviceInput.GetData("CHA7", out int Feedback); if (Feedback == 1) { Actions.SetIndicator(4); Emergency("EMERGENCY\nMODE", "Emergency Alert", true); } Thread.Sleep(EmergencyBtnCycleTime); } }); TaskEmergency.Start(); Task TaskRealtime = new Task(() => { byte CounterError = 0; while (Logging) { try { // SET Modbus Status if (DeviceInput.ConnectionStatus && DeviceOutput.ConnectionStatus) { bool result = (DeviceInput.IsLogging && DeviceOutput.IsLogging); GlobalProperties.ModbusInputStatus = (result) ? true : false; GlobalProperties.ModbusOutputStatus = (result) ? true : false; } else { GlobalProperties.ModbusInputStatus = false; GlobalProperties.ModbusOutputStatus = false; } if (GlobalProperties.DatabaseStatus) { // GET Value from Modbus Input (Drum) DeviceInput.GetData("CHA14", out MixingValue); DeviceInput.GetData("CHA15", out MeasureValue); DeviceInput.GetData("CHA16", out DrumValue); // GET Value from Modbus Input (Feedback) DeviceInput.GetData("CHA11", out StatusValveWater); DeviceInput.GetData("CHA13", out StatusValveCoolant); // Get Max Min Volt Level Sensor DB_SFDB.GetLoadingMaxMin("DRUM", out int ValueMaxDrum, out int ValueMinDrum); DB_SFDB.GetLoadingMaxMin("MEASSURE", out int ValueMaxMeasure, out int ValueMinMeasure); DB_SFDB.GetLoadingMaxMin("MIXING", out int ValueMaxMixing, out int ValueMinMixing); // Real Input Value --> publicproperties.value GlobalProperties.Value[0] = (int)Conversion.DrumPercentage(MixingValue, ValueMaxMixing, ValueMinMixing); GlobalProperties.Value[1] = (int)Conversion.DrumPercentage(MeasureValue, ValueMaxMeasure, ValueMinMeasure); GlobalProperties.Value[2] = (int)Conversion.DrumPercentage(DrumValue, ValueMaxDrum, ValueMinDrum); } else { GlobalProperties.Value[0] = GlobalProperties.Value[1] = GlobalProperties.Value[2] = 0; } CounterError = 0; } catch (Exception e) { Error.Collect(e.StackTrace.ToString()); Debug.WriteLine(e.StackTrace); if (!ApplicationModeDebug) { if (CounterError > ValueNumberTimeout) { Emergency("Internal Error \nDrum Unknown Value Number", "Unknown Value", true); } else { CounterError++; } } } Thread.Sleep(100); } }); TaskRealtime.Start(); Task TaskLampStatus = new Task(() => { DeviceOutput.SetBit(LampWhite); while (Logging) { try { if (DeviceOutput.IsLogging) { if (GlobalProperties.CurrentStatus == GlobalProperties.Status.Ready) { DeviceOutput.SetBit(LampGreen); DeviceOutput.ResetBit(LampRed); DeviceOutput.ResetBit(LampOrange); } else if (GlobalProperties.CurrentStatus == GlobalProperties.Status.Filling) { DeviceOutput.SetBit(LampGreen); DeviceOutput.ResetBit(LampOrange); } else if (GlobalProperties.CurrentStatus == GlobalProperties.Status.Error) { DeviceOutput.SetBit(LampOrange); DeviceOutput.ResetBit(LampGreen); } else if (GlobalProperties.CurrentStatus == GlobalProperties.Status.Emergency) { DeviceOutput.SetBit(LampRed); DeviceOutput.ResetBit(LampGreen); } } } catch (Exception e) { Core.Log.Error.Collect(e.StackTrace.ToString()); } Thread.Sleep(1000); } }); TaskLampStatus.Start(); Task TaskCheckPump = new Task(() => { while (Logging) { try { if (GlobalProperties.DatabaseStatus && DeviceInput.IsLogging && DeviceOutput.IsLogging) { int result = DB_SFDB.GetPumpStatus(); if (result == 1) { DeviceOutput.SetBit(PumpDist); Actions.SetIndicator(GlobalProperties.Valve.Open, GlobalProperties.Type.PumpDist); } else { DeviceOutput.ResetBit(PumpDist); Actions.SetIndicator(GlobalProperties.Valve.Close, GlobalProperties.Type.PumpDist); } } } catch (Exception e) { Debug.WriteLine(e.ToString()); } Thread.Sleep(1000); } }); TaskCheckPump.Start(); // COUNTER ERROR byte DBCounter = 0; byte SDCounter = 0; while (Logging) { Thread.Sleep(500); try { if ((DeviceInput.ConnectionStatus && DeviceInput.IsLogging) && (DeviceOutput.ConnectionStatus && DeviceOutput.IsLogging)) { SDCounter = 0; if (GlobalProperties.DatabaseStatus) { // ONLY EXECUTE AT START (ONCE) if (!FLAG_ATSTART) { DeviceOutput.SetBit(Interlock); FLAG_ATSTART = true; } DBCounter = 0; if (MixingValue <= MinMixing) { if (DrumValue <= MinDrum) { DeviceOutput.ResetBit(PumpDrum); DeviceOutput.ResetBit(ValveWater); DeviceOutput.ResetBit(ValveCoolant); FLAG_COUNTER_WATER = false; FLAG_COUNTER_COOLANT = false; Actions.SetIndicator(GlobalProperties.Valve.Close, GlobalProperties.Type.ValveWater); Actions.SetIndicator(GlobalProperties.Valve.Close, GlobalProperties.Type.ValveCoolant); Actions.SetIndicator(GlobalProperties.Valve.Close, GlobalProperties.Type.PumpDrum); // Alert Change Drum GlobalProperties.ChangeDrumNotify = GlobalProperties.Drum.Alert; } else { // START FILLING WATER GlobalProperties.ChangeDrumNotify = (DrumValue <= MinDrum) ? GlobalProperties.Drum.Alert : GlobalProperties.Drum.None; StartCounterFilling(); int i = 0; bool FirstFillingWater = false; while (i < FillingWaterDuration) { if (!FirstFillingWater) { DeviceOutput.SetBit(ValveWater); StartCounterWater(); StartElapsedFillingWater(); Actions.SetIndicator(GlobalProperties.Valve.Open, GlobalProperties.Type.ValveWater); FirstFillingWater = true; } if (MixingValue >= MaxMixing) { i = 100000; } Thread.Sleep(1000); i++; } DeviceOutput.ResetBit(ValveWater); FLAG_COUNTER_WATER = false; // STOP COUNTER WATER Actions.SetIndicator(GlobalProperties.Valve.Close, GlobalProperties.Type.ValveWater); // START FILLING COOLANT int j = 0; bool FirstFillingCoolant = false; while (j < FillingCoolantDuration) { if (!FirstFillingCoolant) { DeviceOutput.SetBit(PumpDrum); DeviceOutput.SetBit(ValveCoolant); StartCounterCoolant(); StartElapsedFillingCoolant(); Actions.SetIndicator(GlobalProperties.Valve.Open, GlobalProperties.Type.ValveCoolant); Actions.SetIndicator(GlobalProperties.Valve.Open, GlobalProperties.Type.PumpDrum); FirstFillingCoolant = true; } GlobalProperties.ChangeDrumNotify = (DrumValue <= MinDrum) ? GlobalProperties.Drum.Alert : GlobalProperties.Drum.None; while (GlobalProperties.ChangeDrumNotify == GlobalProperties.Drum.Alert) { DeviceOutput.ResetBit(PumpDrum); DeviceOutput.ResetBit(ValveCoolant); FLAG_COUNTER_COOLANT = false; Actions.SetIndicator(GlobalProperties.Valve.Close, GlobalProperties.Type.ValveCoolant); Actions.SetIndicator(GlobalProperties.Valve.Close, GlobalProperties.Type.PumpDrum); if (DrumValue <= MinDrum) { GlobalProperties.ChangeDrumNotify = GlobalProperties.Drum.Alert; } else if (DrumValue >= MaxDrum) { GlobalProperties.ChangeDrumNotify = GlobalProperties.Drum.None; DeviceOutput.SetBit(PumpDrum); DeviceOutput.SetBit(ValveCoolant); StartCounterCoolant(); StartElapsedFillingCoolant(); Actions.SetIndicator(GlobalProperties.Valve.Open, GlobalProperties.Type.ValveCoolant); Actions.SetIndicator(GlobalProperties.Valve.Open, GlobalProperties.Type.PumpDrum); } //PublicProperties.ChangeDrumNotify = (DrumValue <= MinDrum) ? PublicProperties.Drum.Alert : PublicProperties.Drum.None; Thread.Sleep(1000); } if (MixingValue >= MaxMixing) { j = 10000; } j++; Thread.Sleep(1000); } // STOP COUNTER FILLING (WATER/COOLANT) FLAG_COUNTER_FILLING = false; Thread.Sleep(100); DeviceOutput.ResetBit(PumpDrum); DeviceOutput.ResetBit(ValveCoolant); FLAG_COUNTER_COOLANT = false; Actions.SetIndicator(GlobalProperties.Valve.Close, GlobalProperties.Type.ValveCoolant); Actions.SetIndicator(GlobalProperties.Valve.Close, GlobalProperties.Type.PumpDrum); } } GlobalProperties.ChangeDrumNotify = (DrumValue <= MinDrum) ? GlobalProperties.Drum.Alert : GlobalProperties.Drum.None; } else { if (DBCounter > 19) { if (!FLAG_EMERGENCY) { Emergency("Can't Connect to Server", "Database Timeout " + DBCounter, true); DeviceOutput.ResetAllBit(); } } else { DBCounter++; } } } else { if (SDCounter > 10) { if (!FLAG_EMERGENCY) { Emergency("Smartdevice Disconnected", DeviceInput.IpAddress + "/" + DeviceOutput.IpAddress + " Disconnected"); DeviceOutput.ResetAllBit(); } } else { SDCounter++; } } } catch (Exception e) { Debug.WriteLine(e.StackTrace); Error.Collect(e.StackTrace.ToString()); } } }