Created
April 12, 2020 11:18
-
-
Save GluTbl/d7e64eb6c933748885fcc02e00a57343 to your computer and use it in GitHub Desktop.
TempMonitor over TCP #raspberry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.pi4j.system.SystemInfo | |
import java.io.BufferedReader | |
import java.io.IOException | |
import java.io.InputStreamReader | |
import java.net.InetAddress | |
import java.net.ServerSocket | |
import java.net.Socket | |
import java.text.ParseException | |
object SystemInfoTCP { | |
@Throws(InterruptedException::class, IOException::class, ParseException::class) | |
@JvmStatic | |
fun main(args: Array<String>) { | |
try { | |
val addr = ByteArray(4) | |
addr[0] = 0 | |
addr[1] = 0 | |
addr[2] = 0 | |
addr[3] = 0 | |
val MySocket = ServerSocket(6789,8, InetAddress.getByAddress(addr)) | |
while (true) { | |
val connectionSocket = MySocket.accept()// TODO - Make a new thread after the connection is accepted | |
val socket_thread = MyThread(connectionSocket) | |
socket_thread.start() | |
} | |
} catch (t: Throwable) { | |
println(t.message) | |
} | |
} | |
} | |
class MyThread(val connectionSocket: Socket) : Thread() { | |
override fun run() { | |
println("Connection accepted!") | |
val iS = connectionSocket.getInputStream() | |
val iSR = InputStreamReader(iS) | |
val bR = BufferedReader(iSR) | |
val recvdata = bR.readLine() | |
println("Recieved: $recvdata") | |
val bArray = getSystemInfo().toByteArray() | |
println("Okay, Sending the reply") | |
//declare new output strea object | |
val os = connectionSocket.getOutputStream() | |
os.write(bArray, 0, bArray.size) | |
os.flush() | |
connectionSocket.close() | |
} | |
private fun getSystemInfo(): String { | |
var cpuTemp="-1" | |
try { | |
cpuTemp= SystemInfo.getCpuTemperature().toString() | |
} catch (ex: Throwable) { | |
} | |
val currentsecond=(System.currentTimeMillis()/1000) | |
return """{"temp":"$cpuTemp","time":"$currentsecond"}""" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment