Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Data.SqlClient
- Imports System.Net.Sockets
- Imports System.IO
- Imports System.Text
- Imports System.Threading
- Imports System.Net
- Imports System.Net.NetworkInformation
- Public Class Form1
- Public StartProcess, I, Count As Integer
- 'Public port As Int32 = My.Settings.Port
- Public Enabled_IOT As Boolean
- Public port As Int32 = My.Settings.Port
- Public DataRequest, SizeAck, DOEAuthenticationCodeRespond As String
- Public con As New SqlConnection
- Public cmd As New SqlCommand
- Public adaptor As New SqlDataAdapter
- Public DbReader As SqlDataReader
- Public networkStream As NetworkStream
- Public exitpass, Counter, SenDataToDOE_Accept As Integer
- Public tcpClient As TcpClient
- Public tcpListener As New TcpListener(IPAddress.Any, port)
- Public PublicTcpClient As TcpClient
- Private serverRuns As Boolean
- Private server As TcpListener
- Private sc As SynchronizationContext
- Private isConnected, receiveStatus, sendStatus As Boolean
- Private iRemote, pLocal As EndPoint
- Public Thread_1 As System.Threading.Thread
- Public Thread_2 As System.Threading.Thread
- Private Sub Listen()
- tcpListener.Start()
- tcpListener.BeginAcceptTcpClient(AddressOf Accept, tcpListener)
- End Sub
- Private Sub Accept(ByVal ar As IAsyncResult)
- Dim server = DirectCast(ar.AsyncState, TcpListener)
- Dim client = server.EndAcceptTcpClient(ar)
- networkStream = client.GetStream()
- Dim bytes(client.ReceiveBufferSize) As Byte
- networkStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))
- Dim clientdata As String = Encoding.ASCII.GetString(bytes)
- DataRequest = clientdata
- ErrorMsg_TB.Text = clientdata
- System.IO.File.Create("C:\CEMS\ComModuleSettings\InformationFromDOE.ini").Dispose()
- Dim file As System.IO.StreamWriter
- file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\InformationFromDOE.ini", True)
- file.WriteLine(DataRequest)
- file.Close()
- Threading.Thread.Sleep(1000)
- Call ArrangeDataFromDOE_P1()
- If SenDataToDOE_Accept = 1 Then
- System.IO.File.Create("C:\CEMS\ComModuleSettings\AuthenticationCode.ini").Dispose() ' Clear textfile content first
- Try
- Status_TB.Text = "Sending data to DOE.."
- Dim Authentication As String
- Dim file2 As System.IO.StreamWriter
- Dim cpt As Integer = 0
- If networkStream.CanWrite And networkStream.CanRead Then
- 'Do a simple write
- Dim SendAuthenticationToDOE As String
- Dim Parameterlines() As String = IO.File.ReadAllLines("c:\CEMS\ComModuleSettings\ArrangedData.ini")
- Authentication = Parameterlines(0)
- SendAuthenticationToDOE = Authentication
- 'Write Authentication code to text file
- 'System.Threading.Thread.Sleep(5000)
- System.Threading.Thread.Sleep(2000)
- Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(SendAuthenticationToDOE)
- networkStream.Write(sendBytes, 0, sendBytes.Length) ' Send Data To DOE
- ' Read the NetworkStream into a byte buffer.
- System.Threading.Thread.Sleep(2000)
- Dim bytes2(client.ReceiveBufferSize) As Byte
- networkStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))
- DOEAuthenticationCodeRespond = Encoding.ASCII.GetString(bytes) ' Respond from DOE
- Threading.Thread.Sleep(2000)
- file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\AuthenticationCode.ini", True)
- file.WriteLine(DOEAuthenticationCodeRespond)
- file.Close()
- End If
- Dim CheckAuthenticationString() As String = IO.File.ReadAllLines("c:\CEMS\ComModuleSettings\AuthenticationCode.ini")
- Authentication = CheckAuthenticationString(0)
- If networkStream.CanWrite And networkStream.CanRead Then
- 'Do a simple write
- Dim infoReader As System.IO.FileInfo
- Dim FileSize As String
- System.Threading.Thread.Sleep(1000)
- infoReader = My.Computer.FileSystem.GetFileInfo("C:\CEMS\ComModuleSettings\PreparedData.txt")
- FileSize = (infoReader.Length)
- System.Threading.Thread.Sleep(1000)
- Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(FileSize)
- networkStream.Write(sendBytes, 0, sendBytes.Length)
- ' Read the NetworkStream into a byte buffer.
- Dim bytes2(client.ReceiveBufferSize) As Byte
- networkStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))
- ' Output the data received from the host to the console.
- Dim DOEFileSizeRespond As String = Encoding.ASCII.GetString(bytes) 'DOE respond file size with "Size Received"
- Else
- End If
- If networkStream.CanWrite And networkStream.CanRead Then '' Do checking after DOE respond...
- Try
- System.Threading.Thread.Sleep(1000)
- Dim Myfile As FileInfo = New System.IO.FileInfo("C:\CEMS\ComModuleSettings\PreparedData.txt")
- Dim MyFileStream As FileStream = Myfile.Open(FileMode.Open)
- Dim MyBinaryReader As BinaryReader = New BinaryReader(MyFileStream)
- Dim MySendBuffer(client.SendBufferSize) As Byte
- While (MyBinaryReader.Read(MySendBuffer, 0, MySendBuffer.Length))
- networkStream.Write(MySendBuffer, 0, MySendBuffer.Length)
- cpt = cpt + MySendBuffer.Length
- End While
- MyBinaryReader.Close()
- MyFileStream.Close()
- System.Threading.Thread.Sleep(1000)
- file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\Log.ini", True)
- file.WriteLine("All data send successfully" + " " + Date.Now) 'Complete Status
- file.Close()
- Dim text As String = My.Computer.FileSystem.ReadAllText("C:\CEMS\ComModuleSettings\ArrangedParameter.ini")
- Dim index As Integer = text.IndexOf("so2")
- If index >= 0 Then
- con.ConnectionString = "Data Source=" & My.Settings.TCS_SQL_Server & ";Initial Catalog=" & My.Settings.TCS_SQL_DB & ";User Id=" & My.Settings.TCS_SQL_User & ";Password=" & My.Settings.TCS_SQL_Pass & ";"
- con.Open()
- cmd.Connection = con
- cmd.CommandText = "INSERT INTO CEMS_Connection (read_datetime,ConnectionStatus,Downtime,ClientName,SiteID,SubcriptionDate,SubscriptionDuration,DOE_Success) VALUES (getdate(),'111','0', '" & My.Settings.SITE_NAME & "','" & My.Settings.SITE_ID & "','2018-03-22 00:00:00.000','1','" & System.DateTime.Now & "')"
- cmd.ExecuteNonQuery()
- con.Close()
- End If
- Catch ex As Exception
- networkStream.Close()
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 605 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- End Try
- 'tcpClient.Close()
- 'tcpListener.Stop()
- Status_TB.Text = "Sending Data Completed."
- Successfull_Connect_TB.Text = System.DateTime.Now
- Thread.Sleep(2000)
- Call UpdateLogWindow()
- End If
- Catch ex As Exception
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 630 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- End Try
- End If
- End Sub
- Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
- ShowInTaskbar = True
- Me.WindowState = FormWindowState.Normal
- NotifyIcon1.Visible = False
- End Sub
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- Timer1.Start()
- SITE_NAME_TB.Text = My.Settings.SITE_NAME
- Me.CheckForIllegalCrossThreadCalls = False
- Dim Lic As String
- Dim nics() As NetworkInterface =
- NetworkInterface.GetAllNetworkInterfaces
- Lic = nics(0).GetPhysicalAddress.ToString
- TabControl1.TabPages(2).Enabled = False
- TabControl1.TabPages.Remove(TabPage3)
- TabControl1.TabPages(3).Enabled = False
- TabControl1.TabPages.Remove(TabPage5)
- If Lic = My.Settings.License Then
- Call UpdateLogWindow()
- Else
- Status_TB.Text = "System Halt.."
- ErrorMsg_TB.Text = "Invalid License"
- End If
- exitpass = 0
- End Sub
- Sub UpdateLogWindow()
- ErrorMsg_TB.Text = ""
- Status_TB.Text = "Updating startup files.."
- Thread.Sleep(2000)
- Status_TB.Text = "Start Listening..."
- Try
- Dim ClearDataLog As String = "c:\CEMS\ComModuleSettings\Clearlog.ini"
- Dim path As String = "c:\CEMS\ComModuleSettings\log.ini"
- Thread.Sleep(2000)
- Try
- Thread_1 = New System.Threading.Thread(AddressOf Listen)
- Thread_1.Start()
- Catch ex As Exception
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 1001 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- End Try
- Try
- Thread_2 = New System.Threading.Thread(AddressOf Watchdog)
- Thread_2.Start()
- Catch ex As Exception
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 1001 Watchdog Error"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- Thread_2.Abort()
- End Try
- Catch ex As Exception
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 84 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- Thread_1.Abort()
- End Try
- End Sub
- Private Sub Save_BT_Click(sender As Object, e As EventArgs) Handles Save_BT.Click
- My.Settings.SQL_Server = SQL_Server_TB.Text
- My.Settings.SQL_DB = SQL_DB_TB.Text
- My.Settings.SQL_User = SQL_User_TB.Text
- My.Settings.SQL_Pass = SQL_Pass_TB.Text
- My.Settings.Port = PortNo_TB.Text
- My.Settings.Save()
- TabControl1.TabPages(1).Enabled = False
- TabControl1.TabPages.Remove(TabPage3)
- TabControl1.TabPages.Remove(TabPage5)
- End Sub
- Private Sub Reg_BT_Click(sender As Object, e As EventArgs) Handles Reg_BT.Click
- Try
- Dim nics() As NetworkInterface =
- NetworkInterface.GetAllNetworkInterfaces
- My.Settings.License = nics(0).GetPhysicalAddress.ToString
- My.Settings.Save()
- Catch ex As Exception
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 166 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- End Try
- End Sub
- Private Sub UpdateLog_BT_Click(sender As Object, e As EventArgs) Handles UpdateLog_BT.Click
- ListBox1.Items.Clear()
- Dim lines() As String = IO.File.ReadAllLines("C:\CEMS\ComModuleSettings\Log.ini")
- ListBox1.Items.AddRange(lines)
- End Sub
- Private Sub EnableExit_BT_Click(sender As Object, e As EventArgs) Handles EnableExit_BT.Click
- exitpass = 1
- End Sub
- Private Sub Login_Click(sender As Object, e As EventArgs) Handles LogOn.Click
- Login.Show()
- End Sub
- Private Sub TCS_Save_BT_Click(sender As Object, e As EventArgs) Handles TCS_Save_BT.Click
- My.Settings.TCS_SQL_Server = TCS_Server_TB.Text
- My.Settings.TCS_SQL_DB = TCS_DB_TB.Text
- My.Settings.TCS_SQL_User = TCS_User_TB.Text
- My.Settings.TCS_SQL_Pass = TCS_Pass_TB.Text
- My.Settings.SITE_ID = TCS_SITE_ID_TB.Text
- My.Settings.SITE_NAME = TCS_SITENAME_TB.Text
- My.Settings.Save()
- TabControl1.TabPages(4).Enabled = False
- TabControl1.TabPages.Remove(TabPage5)
- TabControl1.TabPages(1).Enabled = False
- TabControl1.TabPages.Remove(TabPage3)
- End Sub
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- My.Settings.Enabled_IOT = 1
- Dim con As New SqlConnection
- Dim cmd As New SqlCommand
- Try
- con.ConnectionString = "Data Source=" & My.Settings.TCS_SQL_Server & ";Initial Catalog=" & My.Settings.TCS_SQL_DB & ";User Id=" & My.Settings.TCS_SQL_User & ";Password=" & My.Settings.TCS_SQL_Pass & ";"
- con.Open()
- cmd.Connection = con
- cmd.CommandText = "if not exists (select * from CEMS_STATUS where SiteID='" & My.Settings.SITE_ID & "') INSERT INTO [dbo].[CEMS_STATUS] ([read_datetime],[SiteName],[SiteID],[SiteStatus]) VALUES (getdate(),'" & My.Settings.SITE_NAME & "','" & My.Settings.SITE_ID & "','1')"
- cmd.ExecuteNonQuery()
- Catch ex As Exception
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 999 SQL Insert Error Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- Finally
- con.Close()
- End Try
- End Sub
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- My.Settings.Enabled_IOT = 0
- End Sub
- Sub ArrangeDataFromDOE_P1()
- Try
- If File.Exists("C:\CEMS\ComModuleSettings\PreparedData.txt") = False Then
- File.CreateText("C:\CEMS\ComModuleSettings\PreparedData.txt").Close()
- End If
- Catch ex As Exception
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 191 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- End Try
- Try
- Status_TB.Text = "Arranging Data Step 1.."
- System.IO.File.Create("C:\CEMS\ComModuleSettings\ArrangedData.ini").Dispose() ' Clear textfile content first
- System.IO.File.Create("C:\CEMS\ComModuleSettings\ArrangedParameter.ini").Dispose() ' Clear textfile content first
- System.IO.File.Create("C:\CEMS\ComModuleSettings\TemporaryCacheFile.ini").Dispose() ' Clear textfile content first
- System.IO.File.Create("C:\CEMS\ComModuleSettings\ToDate.ini").Dispose() ' Clear textfile content first
- System.IO.File.Create("C:\CEMS\ComModuleSettings\CurrentDate.ini").Dispose() ' Clear textfile content first
- System.IO.File.Create("C:\CEMS\ComModuleSettings\PreparedData.txt").Dispose() ' Clear textfile content first
- System.IO.File.Create("C:\CEMS\ComModuleSettings\SizeReceive.ini").Dispose() ' Clear textfile content first
- Catch ex As Exception
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 192 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- End Try
- Try
- Using MyReader As New Microsoft.VisualBasic.
- FileIO.TextFieldParser(
- "C:\CEMS\ComModuleSettings\InformationFromDOE.ini")
- MyReader.TextFieldType = FileIO.FieldType.Delimited
- MyReader.SetDelimiters("|")
- Dim currentRow As String()
- While Not MyReader.EndOfData
- Try
- currentRow = MyReader.ReadFields()
- Dim currentField As String
- For Each currentField In currentRow
- Dim file As System.IO.StreamWriter
- file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\ArrangedData.ini", True)
- file.WriteLine(currentField)
- file.Close()
- Next
- Catch ex As Microsoft.VisualBasic.
- FileIO.MalformedLineException
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 220 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- exitpass = 1
- Application.Restart()
- End Try
- End While
- End Using
- Catch ex As Exception
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 220 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- exitpass = 1
- Application.Restart()
- End Try
- Thread.Sleep(1000)
- Call ArrangeDataFromDOE_P2()
- End Sub
- Sub ArrangeDataFromDOE_P2()
- Try
- 'Procedure to Arrange parameters data
- Status_TB.Text = "Arranging Data Step 2.."
- Dim Parameter, DateTime As String
- Dim Parameterlines() As String = IO.File.ReadAllLines("c:\CEMS\ComModuleSettings\ArrangedData.ini")
- Parameter = Parameterlines(3)
- Dim file As System.IO.StreamWriter
- file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\TemporaryCacheFile.ini", True)
- file.WriteLine(Parameter)
- file.Close()
- Using MyReader As New Microsoft.VisualBasic.
- FileIO.TextFieldParser(
- "C:\CEMS\ComModuleSettings\TemporaryCacheFile.ini")
- MyReader.TextFieldType = FileIO.FieldType.Delimited
- MyReader.SetDelimiters(";")
- Dim currentRow As String()
- While Not MyReader.EndOfData
- Try
- currentRow = MyReader.ReadFields()
- Dim currentField As String
- For Each currentField In currentRow
- file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\ArrangedParameter.ini", True)
- file.WriteLine(currentField)
- file.Close()
- Next
- Catch ex As Microsoft.VisualBasic.
- FileIO.MalformedLineException
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 267 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- exitpass = 1
- Application.Restart()
- End Try
- End While
- End Using
- Try
- 'Procedure to Arrange FromDate Request data
- System.IO.File.Create("C:\CEMS\ComModuleSettings\TemporaryCacheFile.ini").Dispose() ' Clear textfile content first
- System.IO.File.Create("C:\CEMS\ComModuleSettings\ToDateTemporary.ini").Dispose() ' Clear textfile content first
- Dim DateTimelines() As String = IO.File.ReadAllLines("c:\CEMS\ComModuleSettings\ArrangedData.ini")
- DateTime = DateTimelines(10)
- file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\TemporaryCacheFile.ini", True)
- file.WriteLine(DateTime)
- file.Close()
- Catch ex As Exception
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 290 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- exitpass = 1
- 'tcpClient.Close()
- 'Application.Restart()
- Call UpdateLogWindow()
- End Try
- Using MyReader As New Microsoft.VisualBasic.
- FileIO.TextFieldParser(
- "C:\CEMS\ComModuleSettings\TemporaryCacheFile.ini")
- MyReader.TextFieldType = FileIO.FieldType.Delimited
- MyReader.SetDelimiters(",")
- Dim currentRow As String()
- While Not MyReader.EndOfData
- Try
- currentRow = MyReader.ReadFields()
- Dim currentField As String
- For Each currentField In currentRow
- file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\ToDateTemporary.ini", True)
- file.WriteLine(currentField)
- file.Close()
- Next
- Catch ex As Microsoft.VisualBasic.
- FileIO.MalformedLineException
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 318 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- exitpass = 1
- Application.Restart()
- End Try
- End While
- End Using
- 'Procedure to Arrange FromDate Data
- Using MyReader As New Microsoft.VisualBasic.
- FileIO.TextFieldParser(
- "C:\CEMS\ComModuleSettings\ToDateTemporary.ini")
- MyReader.TextFieldType = FileIO.FieldType.Delimited
- MyReader.SetDelimiters("#")
- Dim currentRow As String()
- While Not MyReader.EndOfData
- Try
- currentRow = MyReader.ReadFields()
- Dim currentField As String
- For Each currentField In currentRow
- file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\ToDate.ini", True)
- file.WriteLine(currentField)
- file.Close()
- Next
- Catch ex As Microsoft.VisualBasic.
- FileIO.MalformedLineException
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 349 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- exitpass = 1
- Application.Restart()
- End Try
- End While
- End Using
- Try
- 'Procedure to Arrange CurrentDate Data
- System.IO.File.Create("C:\CEMS\ComModuleSettings\TemporaryCacheFile.ini").Dispose() ' Clear textfile content first
- System.IO.File.Create("C:\CEMS\ComModuleSettings\ToDateTemporary.ini").Dispose() ' Clear textfile content first
- Dim CurrentDatelines() As String = IO.File.ReadAllLines("c:\CEMS\ComModuleSettings\ArrangedData.ini")
- DateTime = CurrentDatelines(9)
- file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\TemporaryCacheFile.ini", True)
- file.WriteLine(DateTime)
- file.Close()
- Catch ex As Exception
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 372 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- exitpass = 1
- 'Application.Restart()
- 'tcpClient.Close()
- Call UpdateLogWindow()
- End Try
- Using MyReader As New Microsoft.VisualBasic.
- FileIO.TextFieldParser(
- "C:\CEMS\ComModuleSettings\TemporaryCacheFile.ini")
- MyReader.TextFieldType = FileIO.FieldType.Delimited
- MyReader.SetDelimiters(",")
- Dim currentRow As String()
- While Not MyReader.EndOfData
- Try
- currentRow = MyReader.ReadFields()
- Dim currentField As String
- For Each currentField In currentRow
- file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\ToDateTemporary.ini", True)
- file.WriteLine(currentField)
- file.Close()
- Next
- Catch ex As Microsoft.VisualBasic.
- FileIO.MalformedLineException
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 400 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- exitpass = 1
- Application.Restart()
- End Try
- End While
- End Using
- 'Procedure to arrange current Date from temporary memory bank
- Using MyReader As New Microsoft.VisualBasic.
- FileIO.TextFieldParser(
- "C:\CEMS\ComModuleSettings\ToDateTemporary.ini")
- MyReader.TextFieldType = FileIO.FieldType.Delimited
- MyReader.SetDelimiters("#")
- Dim currentRow As String()
- While Not MyReader.EndOfData
- Try
- currentRow = MyReader.ReadFields()
- Dim currentField As String
- For Each currentField In currentRow
- file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\CurrentDate.ini", True)
- file.WriteLine(currentField)
- file.Close()
- Next
- Catch ex As Microsoft.VisualBasic.
- FileIO.MalformedLineException
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 431 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- exitpass = 1
- Application.Restart()
- End Try
- End While
- End Using
- System.Threading.Thread.Sleep(1000)
- Call DataPreparation() 'Call DataPreparation procedure to execute data preparation from database according to information collected
- Catch ex As Exception
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 446 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- 'exitpass = 1
- 'Application.Restart()
- 'tcpClient.Close()
- Call UpdateLogWindow()
- End Try
- End Sub
- Sub DataPreparation()
- Try
- Status_TB.Text = "Preparing data.."
- Dim DataPreparationDataset As New DataTable
- Dim LastRequestDate, PreparedData, FactoryID, AuthenticationCode As String
- Dim Path As String = "C:\CEMS\ComModuleSettings\PreparedData.txt"
- Dim Dr As DataRow
- Dim file As System.IO.StreamWriter
- file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\PreparedData.txt", True) ' Open PreparedData to write data from database
- Dim CurrentDatelines() As String = IO.File.ReadAllLines("c:\CEMS\ComModuleSettings\CurrentDate.ini") ' Get Last Request Date CurrentDate
- LastRequestDate = CDate(CurrentDatelines(1)).ToString("yyyy-MM-dd")
- 'LastRequestDate = Format((CurrentDatelines(1)).ToString, "yyyy-dd-MM")
- Dim RequestFactoryID() As String = IO.File.ReadAllLines("c:\CEMS\ComModuleSettings\ArrangedData.ini") ' Get Factory ID from Arranged Data
- FactoryID = RequestFactoryID(1)
- Dim RequestAuthenticationCode() As String = IO.File.ReadAllLines("c:\CEMS\ComModuleSettings\ArrangedData.ini") ' Get Authentication Code from Arranged Data
- AuthenticationCode = RequestAuthenticationCode(0)
- con.ConnectionString = "Data Source=" & My.Settings.SQL_Server & ";Initial Catalog=" & My.Settings.SQL_DB & ";User Id=" & My.Settings.SQL_User & ";Password=" & My.Settings.SQL_Pass & ";"
- con.Open()
- cmd.Connection = con
- 'cmd.CommandText = ("SELECT [reading_id],[stack_id],[read_datetime],round([so2],2)as so2,round([no2],2)as no2,round([co],2)as co,round([co2],2) as co2,round([hcl],2) as hcl,round([hf],2) as hf,round([h2o],2) as h2o,round([o2],2) as o2,round([nmvoc],2) as nmvoc,round([total_pm],2)as total_pm,round([opacity],2) as opacity FROM [" & My.Settings.SQL_DB & "].[dbo].[reading] where read_datetime between '" & LastRequestDate.ToString + " 00:00:00" & "' and '" & Format(Date.Now, "yyyy-MM-dd").ToString + " 23:59:59" & "' order by read_datetime asc")
- cmd.CommandText = ("SELECT [reading_id],[stack_id],[read_datetime],round([so2],2)as so2,round([no2],2)as no2,round([co],2)as co,round([co2],2) as co2,round([hcl],2) as hcl,round([hf],2) as hf,round([h2o],2) as h2o,round([o2],2) as o2,round([nmvoc],2) as nmvoc,round([total_pm],2)as total_pm,round([opacity],2) as opacity FROM [" & My.Settings.SQL_DB & "].[dbo].[reading_raw] where read_datetime between '" & LastRequestDate.ToString + " 00:00:00" & "' and '" & Format(Date.Now, "yyyy-MM-dd").ToString + " 23:59:59" & "' order by read_datetime asc")
- adaptor.SelectCommand = cmd
- adaptor.Fill(DataPreparationDataset)
- file.WriteLine(AuthenticationCode + ";" + Format(Date.Now, "MM-dd-yyyy") + ";" + Format(Date.Now, "HH:mm:ss"))
- Try
- For Each Dr In DataPreparationDataset.Rows 'Each data row will be write into text file with a new line for every new data.
- PreparedData = (Dr("stack_id").ToString() + ";" + Replace(LSet(Dr("read_datetime").ToString(), 9), "/", "-") + ";" + Microsoft.VisualBasic.Right(Format(Dr("read_datetime"), "HH:mm:ss").ToString, 10) + "|" + Dr("so2").ToString + "," + Dr("no2").ToString + "," + Dr("co").ToString + "," + Dr("co2").ToString + "," + Dr("hcl").ToString + "," + Dr("hf").ToString + "," + Dr("h2o").ToString + "," + Dr("o2").ToString + "," + Dr("nmvoc").ToString + "," + Dr("total_pm").ToString + "," + Dr("opacity").ToString).ToString
- file.WriteLine(PreparedData)
- Next Dr
- con.Close()
- file.Close()
- Catch ex As Exception
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 558 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- exitpass = 1
- Application.Restart()
- End Try
- System.Threading.Thread.Sleep(1000)
- Call SendDataToDOE() 'Call procedure to send data to DOE after data preparation.
- Catch ex As Exception
- 'MsgBox("DataPreparation:" + ex.Message)
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 502 Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- 'exitpass = 1
- 'Application.Restart()
- 'tcpClient.Close()
- Call UpdateLogWindow()
- End Try
- End Sub
- Sub SendDataToDOE()
- SenDataToDOE_Accept = 1
- End Sub
- Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
- If Me.WindowState = FormWindowState.Minimized Then
- NotifyIcon1.Visible = True
- NotifyIcon1.Icon = SystemIcons.Application
- NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
- NotifyIcon1.ShowBalloonTip(50000)
- 'Me.Hide()
- ShowInTaskbar = False
- End If
- End Sub
- Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
- If exitpass = 1 Then
- e.Cancel = False
- Else
- e.Cancel = True
- End If
- End Sub
- Sub Watchdog()
- Dim i As Integer = 0
- Thread.Sleep(1000)
- i = i + 1
- End Sub
- Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
- Label17.Text = My.Settings.Enabled_IOT
- If My.Settings.Enabled_IOT = True Then
- Dim con As New SqlConnection
- Dim cmd As New SqlCommand
- Try
- con.ConnectionString = "Data Source=" & My.Settings.TCS_SQL_Server & ";Initial Catalog=" & My.Settings.TCS_SQL_DB & ";User Id=" & My.Settings.TCS_SQL_User & ";Password=" & My.Settings.TCS_SQL_Pass & ";"
- con.Open()
- cmd.Connection = con
- cmd.CommandText = "UPDATE [dbo].[CEMS_STATUS] SET [read_datetime] = getdate(), [SiteName] = '" & My.Settings.SITE_NAME & "',[SiteID] = '" & My.Settings.SITE_ID & "',[SiteStatus] = '1' WHERE SiteID= '" & My.Settings.SITE_ID & "'"
- cmd.ExecuteNonQuery()
- Catch ex As Exception
- ErrorMsg_TB.Text = ex.Message
- Dim ErrorMsg As String
- ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 998 SQL Insert Error Edit"
- Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
- Writer.WriteLine(ErrorMsg)
- Writer.Close()
- Finally
- con.Close()
- End Try
- End If
- End Sub
- End Class
Add Comment
Please, Sign In to add comment