Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System.Data.SqlClient
  2. Imports System.Net.Sockets
  3. Imports System.IO
  4. Imports System.Text
  5. Imports System.Threading
  6. Imports System.Net
  7. Imports System.Net.NetworkInformation
  8. Public Class Form1
  9.     Public StartProcess, I, Count As Integer
  10.     'Public port As Int32 = My.Settings.Port
  11.    Public Enabled_IOT As Boolean
  12.     Public port As Int32 = My.Settings.Port
  13.     Public DataRequest, SizeAck, DOEAuthenticationCodeRespond As String
  14.     Public con As New SqlConnection
  15.     Public cmd As New SqlCommand
  16.     Public adaptor As New SqlDataAdapter
  17.     Public DbReader As SqlDataReader
  18.     Public networkStream As NetworkStream
  19.     Public exitpass, Counter, SenDataToDOE_Accept As Integer
  20.     Public tcpClient As TcpClient
  21.     Public tcpListener As New TcpListener(IPAddress.Any, port)
  22.  
  23.  
  24.     Public PublicTcpClient As TcpClient
  25.     Private serverRuns As Boolean
  26.     Private server As TcpListener
  27.     Private sc As SynchronizationContext
  28.     Private isConnected, receiveStatus, sendStatus As Boolean
  29.     Private iRemote, pLocal As EndPoint
  30.     Public Thread_1 As System.Threading.Thread
  31.     Public Thread_2 As System.Threading.Thread
  32.  
  33.     Private Sub Listen()
  34.  
  35.  
  36.         tcpListener.Start()
  37.         tcpListener.BeginAcceptTcpClient(AddressOf Accept, tcpListener)
  38.  
  39.  
  40.     End Sub
  41.  
  42.     Private Sub Accept(ByVal ar As IAsyncResult)
  43.         Dim server = DirectCast(ar.AsyncState, TcpListener)
  44.         Dim client = server.EndAcceptTcpClient(ar)
  45.  
  46.         networkStream = client.GetStream()
  47.         Dim bytes(client.ReceiveBufferSize) As Byte
  48.         networkStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))
  49.  
  50.         Dim clientdata As String = Encoding.ASCII.GetString(bytes)
  51.         DataRequest = clientdata
  52.         ErrorMsg_TB.Text = clientdata
  53.         System.IO.File.Create("C:\CEMS\ComModuleSettings\InformationFromDOE.ini").Dispose()
  54.         Dim file As System.IO.StreamWriter
  55.         file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\InformationFromDOE.ini", True)
  56.         file.WriteLine(DataRequest)
  57.         file.Close()
  58.  
  59.         Threading.Thread.Sleep(1000)
  60.         Call ArrangeDataFromDOE_P1()
  61.  
  62.         If SenDataToDOE_Accept = 1 Then
  63.             System.IO.File.Create("C:\CEMS\ComModuleSettings\AuthenticationCode.ini").Dispose() ' Clear textfile content first
  64.  
  65.             Try
  66.                 Status_TB.Text = "Sending data to DOE.."
  67.                 Dim Authentication As String
  68.  
  69.                 Dim file2 As System.IO.StreamWriter
  70.  
  71.  
  72.                 Dim cpt As Integer = 0
  73.  
  74.  
  75.                 If networkStream.CanWrite And networkStream.CanRead Then
  76.                     'Do a simple write
  77.  
  78.                     Dim SendAuthenticationToDOE As String
  79.  
  80.                     Dim Parameterlines() As String = IO.File.ReadAllLines("c:\CEMS\ComModuleSettings\ArrangedData.ini")
  81.                     Authentication = Parameterlines(0)
  82.                     SendAuthenticationToDOE = Authentication
  83.  
  84.                     'Write Authentication code to text file
  85.  
  86.                     'System.Threading.Thread.Sleep(5000)
  87.                    System.Threading.Thread.Sleep(2000)
  88.                     Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(SendAuthenticationToDOE)
  89.                     networkStream.Write(sendBytes, 0, sendBytes.Length) ' Send Data To DOE
  90.                    ' Read the NetworkStream into a byte buffer.
  91.  
  92.                     System.Threading.Thread.Sleep(2000)
  93.                     Dim bytes2(client.ReceiveBufferSize) As Byte
  94.                     networkStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))
  95.                     DOEAuthenticationCodeRespond = Encoding.ASCII.GetString(bytes)  ' Respond from DOE
  96.  
  97.                     Threading.Thread.Sleep(2000)
  98.  
  99.                     file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\AuthenticationCode.ini", True)
  100.                     file.WriteLine(DOEAuthenticationCodeRespond)
  101.                     file.Close()
  102.                 End If
  103.  
  104.                 Dim CheckAuthenticationString() As String = IO.File.ReadAllLines("c:\CEMS\ComModuleSettings\AuthenticationCode.ini")
  105.                 Authentication = CheckAuthenticationString(0)
  106.  
  107.                 If networkStream.CanWrite And networkStream.CanRead Then
  108.                     'Do a simple write
  109.                    Dim infoReader As System.IO.FileInfo
  110.                     Dim FileSize As String
  111.                     System.Threading.Thread.Sleep(1000)
  112.  
  113.                     infoReader = My.Computer.FileSystem.GetFileInfo("C:\CEMS\ComModuleSettings\PreparedData.txt")
  114.                     FileSize = (infoReader.Length)
  115.                     System.Threading.Thread.Sleep(1000)
  116.  
  117.                     Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(FileSize)
  118.                     networkStream.Write(sendBytes, 0, sendBytes.Length)
  119.                     ' Read the NetworkStream into a byte buffer.
  120.                    Dim bytes2(client.ReceiveBufferSize) As Byte
  121.                     networkStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))
  122.                     ' Output the data received from the host to the console.
  123.                    Dim DOEFileSizeRespond As String = Encoding.ASCII.GetString(bytes) 'DOE respond file size with "Size Received"
  124.  
  125.  
  126.                 Else
  127.  
  128.                 End If
  129.  
  130.                 If networkStream.CanWrite And networkStream.CanRead Then   '' Do checking after DOE respond...
  131.  
  132.                     Try
  133.                         System.Threading.Thread.Sleep(1000)
  134.  
  135.                         Dim Myfile As FileInfo = New System.IO.FileInfo("C:\CEMS\ComModuleSettings\PreparedData.txt")
  136.                         Dim MyFileStream As FileStream = Myfile.Open(FileMode.Open)
  137.                         Dim MyBinaryReader As BinaryReader = New BinaryReader(MyFileStream)
  138.                         Dim MySendBuffer(client.SendBufferSize) As Byte
  139.                         While (MyBinaryReader.Read(MySendBuffer, 0, MySendBuffer.Length))
  140.                             networkStream.Write(MySendBuffer, 0, MySendBuffer.Length)
  141.                             cpt = cpt + MySendBuffer.Length
  142.  
  143.                         End While
  144.                         MyBinaryReader.Close()
  145.                         MyFileStream.Close()
  146.  
  147.                         System.Threading.Thread.Sleep(1000)
  148.  
  149.                         file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\Log.ini", True)
  150.                         file.WriteLine("All data send successfully" + " " + Date.Now) 'Complete Status
  151.                        file.Close()
  152.  
  153.                         Dim text As String = My.Computer.FileSystem.ReadAllText("C:\CEMS\ComModuleSettings\ArrangedParameter.ini")
  154.                         Dim index As Integer = text.IndexOf("so2")
  155.                         If index >= 0 Then
  156.                             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 & ";"
  157.                             con.Open()
  158.                             cmd.Connection = con
  159.                             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 & "')"
  160.                             cmd.ExecuteNonQuery()
  161.                             con.Close()
  162.                         End If
  163.  
  164.  
  165.                     Catch ex As Exception
  166.  
  167.                         networkStream.Close()
  168.  
  169.  
  170.                         ErrorMsg_TB.Text = ex.Message
  171.                         Dim ErrorMsg As String
  172.                         ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 605 Edit"
  173.                         Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  174.                         Writer.WriteLine(ErrorMsg)
  175.                         Writer.Close()
  176.  
  177.                     End Try
  178.  
  179.                     'tcpClient.Close()
  180.                    'tcpListener.Stop()
  181.                    Status_TB.Text = "Sending Data Completed."
  182.                     Successfull_Connect_TB.Text = System.DateTime.Now
  183.  
  184.  
  185.  
  186.  
  187.  
  188.                     Thread.Sleep(2000)
  189.                     Call UpdateLogWindow()
  190.  
  191.                 End If
  192.  
  193.             Catch ex As Exception
  194.                 ErrorMsg_TB.Text = ex.Message
  195.                 Dim ErrorMsg As String
  196.                 ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 630 Edit"
  197.                 Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  198.                 Writer.WriteLine(ErrorMsg)
  199.                 Writer.Close()
  200.  
  201.  
  202.  
  203.  
  204.             End Try
  205.         End If
  206.     End Sub
  207.  
  208.     Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
  209.         ShowInTaskbar = True
  210.         Me.WindowState = FormWindowState.Normal
  211.         NotifyIcon1.Visible = False
  212.     End Sub
  213.  
  214.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  215.         Timer1.Start()
  216.         SITE_NAME_TB.Text = My.Settings.SITE_NAME
  217.         Me.CheckForIllegalCrossThreadCalls = False
  218.         Dim Lic As String
  219.         Dim nics() As NetworkInterface =
  220.              NetworkInterface.GetAllNetworkInterfaces
  221.         Lic = nics(0).GetPhysicalAddress.ToString
  222.  
  223.         TabControl1.TabPages(2).Enabled = False
  224.         TabControl1.TabPages.Remove(TabPage3)
  225.  
  226.         TabControl1.TabPages(3).Enabled = False
  227.         TabControl1.TabPages.Remove(TabPage5)
  228.  
  229.  
  230.         If Lic = My.Settings.License Then
  231.             Call UpdateLogWindow()
  232.  
  233.  
  234.         Else
  235.             Status_TB.Text = "System Halt.."
  236.             ErrorMsg_TB.Text = "Invalid License"
  237.         End If
  238.         exitpass = 0
  239.  
  240.     End Sub
  241.  
  242.     Sub UpdateLogWindow()
  243.         ErrorMsg_TB.Text = ""
  244.  
  245.         Status_TB.Text = "Updating startup files.."
  246.  
  247.         Thread.Sleep(2000)
  248.  
  249.         Status_TB.Text = "Start Listening..."
  250.  
  251.         Try
  252.             Dim ClearDataLog As String = "c:\CEMS\ComModuleSettings\Clearlog.ini"
  253.             Dim path As String = "c:\CEMS\ComModuleSettings\log.ini"
  254.             Thread.Sleep(2000)
  255.  
  256.  
  257.  
  258.             Try
  259.                 Thread_1 = New System.Threading.Thread(AddressOf Listen)
  260.                 Thread_1.Start()
  261.  
  262.  
  263.             Catch ex As Exception
  264.                 ErrorMsg_TB.Text = ex.Message
  265.                 Dim ErrorMsg As String
  266.                 ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 1001 Edit"
  267.                 Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  268.                 Writer.WriteLine(ErrorMsg)
  269.                 Writer.Close()
  270.  
  271.  
  272.  
  273.             End Try
  274.  
  275.             Try
  276.                 Thread_2 = New System.Threading.Thread(AddressOf Watchdog)
  277.                 Thread_2.Start()
  278.  
  279.  
  280.             Catch ex As Exception
  281.                 ErrorMsg_TB.Text = ex.Message
  282.                 Dim ErrorMsg As String
  283.                 ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 1001 Watchdog Error"
  284.                 Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  285.                 Writer.WriteLine(ErrorMsg)
  286.                 Writer.Close()
  287.                 Thread_2.Abort()
  288.  
  289.  
  290.  
  291.  
  292.  
  293.             End Try
  294.  
  295.  
  296.         Catch ex As Exception
  297.             ErrorMsg_TB.Text = ex.Message
  298.             Dim ErrorMsg As String
  299.             ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 84 Edit"
  300.             Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  301.             Writer.WriteLine(ErrorMsg)
  302.             Writer.Close()
  303.             Thread_1.Abort()
  304.  
  305.         End Try
  306.     End Sub
  307.  
  308.     Private Sub Save_BT_Click(sender As Object, e As EventArgs) Handles Save_BT.Click
  309.         My.Settings.SQL_Server = SQL_Server_TB.Text
  310.         My.Settings.SQL_DB = SQL_DB_TB.Text
  311.         My.Settings.SQL_User = SQL_User_TB.Text
  312.         My.Settings.SQL_Pass = SQL_Pass_TB.Text
  313.         My.Settings.Port = PortNo_TB.Text
  314.  
  315.  
  316.         My.Settings.Save()
  317.  
  318.         TabControl1.TabPages(1).Enabled = False
  319.         TabControl1.TabPages.Remove(TabPage3)
  320.  
  321.  
  322.         TabControl1.TabPages.Remove(TabPage5)
  323.  
  324.     End Sub
  325.  
  326.  
  327.  
  328.     Private Sub Reg_BT_Click(sender As Object, e As EventArgs) Handles Reg_BT.Click
  329.         Try
  330.             Dim nics() As NetworkInterface =
  331.                   NetworkInterface.GetAllNetworkInterfaces
  332.             My.Settings.License = nics(0).GetPhysicalAddress.ToString
  333.             My.Settings.Save()
  334.         Catch ex As Exception
  335.             ErrorMsg_TB.Text = ex.Message
  336.             Dim ErrorMsg As String
  337.             ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 166 Edit"
  338.             Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  339.             Writer.WriteLine(ErrorMsg)
  340.             Writer.Close()
  341.         End Try
  342.  
  343.     End Sub
  344.  
  345.  
  346.  
  347.     Private Sub UpdateLog_BT_Click(sender As Object, e As EventArgs) Handles UpdateLog_BT.Click
  348.         ListBox1.Items.Clear()
  349.         Dim lines() As String = IO.File.ReadAllLines("C:\CEMS\ComModuleSettings\Log.ini")
  350.  
  351.         ListBox1.Items.AddRange(lines)
  352.     End Sub
  353.  
  354.     Private Sub EnableExit_BT_Click(sender As Object, e As EventArgs) Handles EnableExit_BT.Click
  355.         exitpass = 1
  356.  
  357.     End Sub
  358.  
  359.     Private Sub Login_Click(sender As Object, e As EventArgs) Handles LogOn.Click
  360.         Login.Show()
  361.  
  362.     End Sub
  363.  
  364.     Private Sub TCS_Save_BT_Click(sender As Object, e As EventArgs) Handles TCS_Save_BT.Click
  365.         My.Settings.TCS_SQL_Server = TCS_Server_TB.Text
  366.         My.Settings.TCS_SQL_DB = TCS_DB_TB.Text
  367.         My.Settings.TCS_SQL_User = TCS_User_TB.Text
  368.         My.Settings.TCS_SQL_Pass = TCS_Pass_TB.Text
  369.         My.Settings.SITE_ID = TCS_SITE_ID_TB.Text
  370.         My.Settings.SITE_NAME = TCS_SITENAME_TB.Text
  371.  
  372.         My.Settings.Save()
  373.  
  374.         TabControl1.TabPages(4).Enabled = False
  375.         TabControl1.TabPages.Remove(TabPage5)
  376.  
  377.         TabControl1.TabPages(1).Enabled = False
  378.         TabControl1.TabPages.Remove(TabPage3)
  379.     End Sub
  380.  
  381.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  382.         My.Settings.Enabled_IOT = 1
  383.         Dim con As New SqlConnection
  384.         Dim cmd As New SqlCommand
  385.         Try
  386.             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 & ";"
  387.             con.Open()
  388.             cmd.Connection = con
  389.             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')"
  390.             cmd.ExecuteNonQuery()
  391.  
  392.         Catch ex As Exception
  393.             ErrorMsg_TB.Text = ex.Message
  394.             Dim ErrorMsg As String
  395.             ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 999 SQL Insert Error Edit"
  396.             Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  397.             Writer.WriteLine(ErrorMsg)
  398.             Writer.Close()
  399.         Finally
  400.             con.Close()
  401.         End Try
  402.     End Sub
  403.  
  404.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  405.         My.Settings.Enabled_IOT = 0
  406.     End Sub
  407.  
  408.     Sub ArrangeDataFromDOE_P1()
  409.  
  410.         Try
  411.             If File.Exists("C:\CEMS\ComModuleSettings\PreparedData.txt") = False Then
  412.                 File.CreateText("C:\CEMS\ComModuleSettings\PreparedData.txt").Close()
  413.  
  414.             End If
  415.         Catch ex As Exception
  416.             ErrorMsg_TB.Text = ex.Message
  417.             Dim ErrorMsg As String
  418.             ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 191 Edit"
  419.             Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  420.             Writer.WriteLine(ErrorMsg)
  421.             Writer.Close()
  422.         End Try
  423.  
  424.         Try
  425.             Status_TB.Text = "Arranging Data Step 1.."
  426.             System.IO.File.Create("C:\CEMS\ComModuleSettings\ArrangedData.ini").Dispose() ' Clear textfile content first
  427.            System.IO.File.Create("C:\CEMS\ComModuleSettings\ArrangedParameter.ini").Dispose() ' Clear textfile content first
  428.            System.IO.File.Create("C:\CEMS\ComModuleSettings\TemporaryCacheFile.ini").Dispose() ' Clear textfile content first
  429.            System.IO.File.Create("C:\CEMS\ComModuleSettings\ToDate.ini").Dispose() ' Clear textfile content first
  430.            System.IO.File.Create("C:\CEMS\ComModuleSettings\CurrentDate.ini").Dispose() ' Clear textfile content first
  431.            System.IO.File.Create("C:\CEMS\ComModuleSettings\PreparedData.txt").Dispose() ' Clear textfile content first
  432.            System.IO.File.Create("C:\CEMS\ComModuleSettings\SizeReceive.ini").Dispose() ' Clear textfile content first
  433.        Catch ex As Exception
  434.             ErrorMsg_TB.Text = ex.Message
  435.             Dim ErrorMsg As String
  436.             ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 192 Edit"
  437.             Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  438.             Writer.WriteLine(ErrorMsg)
  439.             Writer.Close()
  440.  
  441.         End Try
  442.  
  443.         Try
  444.  
  445.  
  446.  
  447.             Using MyReader As New Microsoft.VisualBasic.
  448.                         FileIO.TextFieldParser(
  449.                           "C:\CEMS\ComModuleSettings\InformationFromDOE.ini")
  450.                 MyReader.TextFieldType = FileIO.FieldType.Delimited
  451.                 MyReader.SetDelimiters("|")
  452.                 Dim currentRow As String()
  453.                 While Not MyReader.EndOfData
  454.                     Try
  455.                         currentRow = MyReader.ReadFields()
  456.                         Dim currentField As String
  457.                         For Each currentField In currentRow
  458.  
  459.                             Dim file As System.IO.StreamWriter
  460.                             file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\ArrangedData.ini", True)
  461.                             file.WriteLine(currentField)
  462.                             file.Close()
  463.                         Next
  464.                     Catch ex As Microsoft.VisualBasic.
  465.                     FileIO.MalformedLineException
  466.                         ErrorMsg_TB.Text = ex.Message
  467.                         Dim ErrorMsg As String
  468.                         ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 220 Edit"
  469.                         Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  470.                         Writer.WriteLine(ErrorMsg)
  471.                         Writer.Close()
  472.                         exitpass = 1
  473.                         Application.Restart()
  474.  
  475.  
  476.  
  477.  
  478.                     End Try
  479.                 End While
  480.             End Using
  481.         Catch ex As Exception
  482.             ErrorMsg_TB.Text = ex.Message
  483.             Dim ErrorMsg As String
  484.             ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 220 Edit"
  485.             Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  486.             Writer.WriteLine(ErrorMsg)
  487.             Writer.Close()
  488.             exitpass = 1
  489.             Application.Restart()
  490.  
  491.         End Try
  492.         Thread.Sleep(1000)
  493.         Call ArrangeDataFromDOE_P2()
  494.     End Sub
  495.  
  496.     Sub ArrangeDataFromDOE_P2()
  497.         Try
  498.             'Procedure to Arrange parameters data
  499.            Status_TB.Text = "Arranging Data Step 2.."
  500.  
  501.  
  502.             Dim Parameter, DateTime As String
  503.             Dim Parameterlines() As String = IO.File.ReadAllLines("c:\CEMS\ComModuleSettings\ArrangedData.ini")
  504.             Parameter = Parameterlines(3)
  505.             Dim file As System.IO.StreamWriter
  506.             file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\TemporaryCacheFile.ini", True)
  507.             file.WriteLine(Parameter)
  508.             file.Close()
  509.  
  510.  
  511.             Using MyReader As New Microsoft.VisualBasic.
  512.                            FileIO.TextFieldParser(
  513.                              "C:\CEMS\ComModuleSettings\TemporaryCacheFile.ini")
  514.                 MyReader.TextFieldType = FileIO.FieldType.Delimited
  515.                 MyReader.SetDelimiters(";")
  516.                 Dim currentRow As String()
  517.                 While Not MyReader.EndOfData
  518.                     Try
  519.                         currentRow = MyReader.ReadFields()
  520.                         Dim currentField As String
  521.                         For Each currentField In currentRow
  522.                             file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\ArrangedParameter.ini", True)
  523.                             file.WriteLine(currentField)
  524.  
  525.                             file.Close()
  526.                         Next
  527.                     Catch ex As Microsoft.VisualBasic.
  528.                                 FileIO.MalformedLineException
  529.                         ErrorMsg_TB.Text = ex.Message
  530.                         Dim ErrorMsg As String
  531.                         ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 267 Edit"
  532.                         Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  533.                         Writer.WriteLine(ErrorMsg)
  534.                         Writer.Close()
  535.                         exitpass = 1
  536.                         Application.Restart()
  537.  
  538.                     End Try
  539.                 End While
  540.             End Using
  541.  
  542.             Try
  543.                 'Procedure to Arrange FromDate Request data
  544.  
  545.                 System.IO.File.Create("C:\CEMS\ComModuleSettings\TemporaryCacheFile.ini").Dispose() ' Clear textfile content first
  546.                System.IO.File.Create("C:\CEMS\ComModuleSettings\ToDateTemporary.ini").Dispose() ' Clear textfile content first
  547.  
  548.                 Dim DateTimelines() As String = IO.File.ReadAllLines("c:\CEMS\ComModuleSettings\ArrangedData.ini")
  549.                 DateTime = DateTimelines(10)
  550.  
  551.                 file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\TemporaryCacheFile.ini", True)
  552.                 file.WriteLine(DateTime)
  553.                 file.Close()
  554.             Catch ex As Exception
  555.                 ErrorMsg_TB.Text = ex.Message
  556.                 Dim ErrorMsg As String
  557.                 ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 290 Edit"
  558.                 Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  559.                 Writer.WriteLine(ErrorMsg)
  560.                 Writer.Close()
  561.                 exitpass = 1
  562.                 'tcpClient.Close()
  563.  
  564.                 'Application.Restart()
  565.                Call UpdateLogWindow()
  566.  
  567.             End Try
  568.  
  569.  
  570.             Using MyReader As New Microsoft.VisualBasic.
  571.                            FileIO.TextFieldParser(
  572.                              "C:\CEMS\ComModuleSettings\TemporaryCacheFile.ini")
  573.                 MyReader.TextFieldType = FileIO.FieldType.Delimited
  574.                 MyReader.SetDelimiters(",")
  575.                 Dim currentRow As String()
  576.                 While Not MyReader.EndOfData
  577.                     Try
  578.                         currentRow = MyReader.ReadFields()
  579.                         Dim currentField As String
  580.                         For Each currentField In currentRow
  581.  
  582.                             file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\ToDateTemporary.ini", True)
  583.                             file.WriteLine(currentField)
  584.  
  585.                             file.Close()
  586.                         Next
  587.                     Catch ex As Microsoft.VisualBasic.
  588.                                 FileIO.MalformedLineException
  589.                         ErrorMsg_TB.Text = ex.Message
  590.                         Dim ErrorMsg As String
  591.                         ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 318 Edit"
  592.                         Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  593.                         Writer.WriteLine(ErrorMsg)
  594.                         Writer.Close()
  595.                         exitpass = 1
  596.                         Application.Restart()
  597.                     End Try
  598.                 End While
  599.             End Using
  600.  
  601.             'Procedure to Arrange FromDate Data
  602.  
  603.             Using MyReader As New Microsoft.VisualBasic.
  604.                            FileIO.TextFieldParser(
  605.                              "C:\CEMS\ComModuleSettings\ToDateTemporary.ini")
  606.                 MyReader.TextFieldType = FileIO.FieldType.Delimited
  607.                 MyReader.SetDelimiters("#")
  608.                 Dim currentRow As String()
  609.                 While Not MyReader.EndOfData
  610.                     Try
  611.                         currentRow = MyReader.ReadFields()
  612.                         Dim currentField As String
  613.                         For Each currentField In currentRow
  614.  
  615.                             file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\ToDate.ini", True)
  616.                             file.WriteLine(currentField)
  617.  
  618.                             file.Close()
  619.                         Next
  620.                     Catch ex As Microsoft.VisualBasic.
  621.                                 FileIO.MalformedLineException
  622.                         ErrorMsg_TB.Text = ex.Message
  623.                         Dim ErrorMsg As String
  624.                         ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 349 Edit"
  625.                         Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  626.                         Writer.WriteLine(ErrorMsg)
  627.                         Writer.Close()
  628.                         exitpass = 1
  629.                         Application.Restart()
  630.                     End Try
  631.                 End While
  632.             End Using
  633.  
  634.             Try
  635.                 'Procedure to Arrange CurrentDate Data
  636.  
  637.                 System.IO.File.Create("C:\CEMS\ComModuleSettings\TemporaryCacheFile.ini").Dispose() ' Clear textfile content first
  638.                System.IO.File.Create("C:\CEMS\ComModuleSettings\ToDateTemporary.ini").Dispose() ' Clear textfile content first
  639.  
  640.                 Dim CurrentDatelines() As String = IO.File.ReadAllLines("c:\CEMS\ComModuleSettings\ArrangedData.ini")
  641.                 DateTime = CurrentDatelines(9)
  642.  
  643.                 file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\TemporaryCacheFile.ini", True)
  644.                 file.WriteLine(DateTime)
  645.                 file.Close()
  646.             Catch ex As Exception
  647.                 ErrorMsg_TB.Text = ex.Message
  648.                 Dim ErrorMsg As String
  649.                 ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 372 Edit"
  650.                 Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  651.                 Writer.WriteLine(ErrorMsg)
  652.                 Writer.Close()
  653.                 exitpass = 1
  654.                 'Application.Restart()
  655.                'tcpClient.Close()
  656.                Call UpdateLogWindow()
  657.  
  658.             End Try
  659.  
  660.  
  661.             Using MyReader As New Microsoft.VisualBasic.
  662.                            FileIO.TextFieldParser(
  663.                              "C:\CEMS\ComModuleSettings\TemporaryCacheFile.ini")
  664.                 MyReader.TextFieldType = FileIO.FieldType.Delimited
  665.                 MyReader.SetDelimiters(",")
  666.                 Dim currentRow As String()
  667.                 While Not MyReader.EndOfData
  668.                     Try
  669.                         currentRow = MyReader.ReadFields()
  670.                         Dim currentField As String
  671.                         For Each currentField In currentRow
  672.  
  673.                             file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\ToDateTemporary.ini", True)
  674.                             file.WriteLine(currentField)
  675.  
  676.                             file.Close()
  677.                         Next
  678.                     Catch ex As Microsoft.VisualBasic.
  679.                                 FileIO.MalformedLineException
  680.                         ErrorMsg_TB.Text = ex.Message
  681.                         Dim ErrorMsg As String
  682.                         ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 400 Edit"
  683.                         Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  684.                         Writer.WriteLine(ErrorMsg)
  685.                         Writer.Close()
  686.                         exitpass = 1
  687.                         Application.Restart()
  688.                     End Try
  689.                 End While
  690.             End Using
  691.  
  692.             'Procedure to arrange current Date from temporary memory bank
  693.  
  694.             Using MyReader As New Microsoft.VisualBasic.
  695.                            FileIO.TextFieldParser(
  696.                              "C:\CEMS\ComModuleSettings\ToDateTemporary.ini")
  697.                 MyReader.TextFieldType = FileIO.FieldType.Delimited
  698.                 MyReader.SetDelimiters("#")
  699.                 Dim currentRow As String()
  700.                 While Not MyReader.EndOfData
  701.                     Try
  702.                         currentRow = MyReader.ReadFields()
  703.                         Dim currentField As String
  704.                         For Each currentField In currentRow
  705.  
  706.                             file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\CurrentDate.ini", True)
  707.                             file.WriteLine(currentField)
  708.  
  709.                             file.Close()
  710.                         Next
  711.                     Catch ex As Microsoft.VisualBasic.
  712.                                 FileIO.MalformedLineException
  713.                         ErrorMsg_TB.Text = ex.Message
  714.                         Dim ErrorMsg As String
  715.                         ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 431 Edit"
  716.                         Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  717.                         Writer.WriteLine(ErrorMsg)
  718.                         Writer.Close()
  719.                         exitpass = 1
  720.                         Application.Restart()
  721.  
  722.                     End Try
  723.                 End While
  724.             End Using
  725.  
  726.             System.Threading.Thread.Sleep(1000)
  727.  
  728.             Call DataPreparation() 'Call DataPreparation procedure to execute data preparation from database according to information collected
  729.        Catch ex As Exception
  730.             ErrorMsg_TB.Text = ex.Message
  731.             Dim ErrorMsg As String
  732.             ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 446 Edit"
  733.             Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  734.             Writer.WriteLine(ErrorMsg)
  735.             Writer.Close()
  736.             'exitpass = 1
  737.            'Application.Restart()
  738.            'tcpClient.Close()
  739.            Call UpdateLogWindow()
  740.  
  741.         End Try
  742.     End Sub
  743.     Sub DataPreparation()
  744.         Try
  745.             Status_TB.Text = "Preparing data.."
  746.  
  747.  
  748.             Dim DataPreparationDataset As New DataTable
  749.             Dim LastRequestDate, PreparedData, FactoryID, AuthenticationCode As String
  750.             Dim Path As String = "C:\CEMS\ComModuleSettings\PreparedData.txt"
  751.             Dim Dr As DataRow
  752.             Dim file As System.IO.StreamWriter
  753.  
  754.             file = My.Computer.FileSystem.OpenTextFileWriter("c:\CEMS\ComModuleSettings\PreparedData.txt", True) ' Open PreparedData to write data from database
  755.  
  756.             Dim CurrentDatelines() As String = IO.File.ReadAllLines("c:\CEMS\ComModuleSettings\CurrentDate.ini") ' Get Last Request Date CurrentDate
  757.            LastRequestDate = CDate(CurrentDatelines(1)).ToString("yyyy-MM-dd")
  758.             'LastRequestDate = Format((CurrentDatelines(1)).ToString, "yyyy-dd-MM")
  759.            Dim RequestFactoryID() As String = IO.File.ReadAllLines("c:\CEMS\ComModuleSettings\ArrangedData.ini") ' Get Factory ID from Arranged Data
  760.            FactoryID = RequestFactoryID(1)
  761.  
  762.             Dim RequestAuthenticationCode() As String = IO.File.ReadAllLines("c:\CEMS\ComModuleSettings\ArrangedData.ini") ' Get Authentication Code from Arranged Data
  763.            AuthenticationCode = RequestAuthenticationCode(0)
  764.  
  765.             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 & ";"
  766.             con.Open()
  767.             cmd.Connection = con
  768.             '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")
  769.            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")
  770.  
  771.  
  772.  
  773.             adaptor.SelectCommand = cmd
  774.             adaptor.Fill(DataPreparationDataset)
  775.             file.WriteLine(AuthenticationCode + ";" + Format(Date.Now, "MM-dd-yyyy") + ";" + Format(Date.Now, "HH:mm:ss"))
  776.  
  777.             Try
  778.                 For Each Dr In DataPreparationDataset.Rows 'Each data row will be write into text file with a new line for every new data.
  779.  
  780.                     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
  781.                     file.WriteLine(PreparedData)
  782.  
  783.                 Next Dr
  784.                 con.Close()
  785.                 file.Close()
  786.             Catch ex As Exception
  787.                 ErrorMsg_TB.Text = ex.Message
  788.                 Dim ErrorMsg As String
  789.                 ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 558 Edit"
  790.                 Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  791.                 Writer.WriteLine(ErrorMsg)
  792.                 Writer.Close()
  793.                 exitpass = 1
  794.                 Application.Restart()
  795.  
  796.             End Try
  797.  
  798.  
  799.  
  800.             System.Threading.Thread.Sleep(1000)
  801.  
  802.             Call SendDataToDOE() 'Call procedure to send data to DOE after data preparation.
  803.        Catch ex As Exception
  804.             'MsgBox("DataPreparation:" + ex.Message)
  805.            ErrorMsg_TB.Text = ex.Message
  806.             Dim ErrorMsg As String
  807.             ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 502 Edit"
  808.             Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  809.             Writer.WriteLine(ErrorMsg)
  810.             Writer.Close()
  811.             'exitpass = 1
  812.            'Application.Restart()
  813.            'tcpClient.Close()
  814.            Call UpdateLogWindow()
  815.  
  816.  
  817.  
  818.         End Try
  819.     End Sub
  820.  
  821.     Sub SendDataToDOE()
  822.         SenDataToDOE_Accept = 1
  823.     End Sub
  824.  
  825.     Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
  826.         If Me.WindowState = FormWindowState.Minimized Then
  827.             NotifyIcon1.Visible = True
  828.             NotifyIcon1.Icon = SystemIcons.Application
  829.             NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
  830.  
  831.             NotifyIcon1.ShowBalloonTip(50000)
  832.             'Me.Hide()
  833.            ShowInTaskbar = False
  834.         End If
  835.     End Sub
  836.  
  837.     Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
  838.  
  839.  
  840.         If exitpass = 1 Then
  841.  
  842.             e.Cancel = False
  843.         Else
  844.             e.Cancel = True
  845.  
  846.  
  847.         End If
  848.  
  849.  
  850.  
  851.     End Sub
  852.  
  853.     Sub Watchdog()
  854.         Dim i As Integer = 0
  855.         Thread.Sleep(1000)
  856.         i = i + 1
  857.     End Sub
  858.     Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  859.         Label17.Text = My.Settings.Enabled_IOT
  860.         If My.Settings.Enabled_IOT = True Then
  861.             Dim con As New SqlConnection
  862.             Dim cmd As New SqlCommand
  863.             Try
  864.                 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 & ";"
  865.                 con.Open()
  866.                 cmd.Connection = con
  867.                 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 & "'"
  868.                 cmd.ExecuteNonQuery()
  869.  
  870.             Catch ex As Exception
  871.                 ErrorMsg_TB.Text = ex.Message
  872.                 Dim ErrorMsg As String
  873.                 ErrorMsg = System.DateTime.Now + " |>>> " + ex.Message.ToString + " Error 998 SQL Insert Error Edit"
  874.                 Dim Writer As New System.IO.StreamWriter("C:\CEMS\ComModuleSettings\Log.ini", True)
  875.                 Writer.WriteLine(ErrorMsg)
  876.                 Writer.Close()
  877.             Finally
  878.                 con.Close()
  879.             End Try
  880.         End If
  881.  
  882.  
  883.     End Sub
  884. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement