-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiving_node_PLUM_v1.0.py
79 lines (66 loc) · 2.78 KB
/
receiving_node_PLUM_v1.0.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#------------------------------------------
#--- Author: Chanthujan Chandrakumar
#--- Date: 08th Feb 2022
#--- Version: 1.0
#--- Python Ver: 2.7
#------------------------------------------
import socket
import time
import json
import numpy as np
from datetime import datetime
from tcp_sender import tcp_Sender
###########################TCP_SOCKET_Details###########################
port = 5005
host_IP = raw_input("Please enter the IP address of the receiver RS4D:")
sock =socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((host_IP, port))
sock.listen(0)
#######################################################################
FMT = '%H:%M:%S:%f'
print "Listener" + host_IP + "started"
alert_Count = []
time_Count = []
def trigger_Alert(val, val2):
global alert_Count
global time_Count
if( val not in alert_Count):
alert_Count.append(val)
time_Count.append(val2)
if(len(alert_Count) == 2):
time= (datetime.today()).strftime("%d-%b-%Y %H:%M:%S:%f").split()
rec_time = (time[1][:-1])
print("Earthquake detected time at first sensor:", time_Count[0])
print("Alert generated time :", rec_time)
tdelta = datetime.strptime(rec_time, FMT) - datetime.strptime(time_Count[0] ,FMT)
print("Detection time is:", tdelta)
if(host_IP in alert_Count):
print("Generating the Alert and sending to the sensors within the 30km area !!!")
tcp_Sender(nodeList, host_IP, port, "Alarm", "Get Ready for a Shake !!!")
else:
print("Generating the Alert within the sensor and sends message to users connected to the sensor = GET READY FOR THE SHAKE !!!")
alert_Count =[]
time_Count = []
text_file = open("time_distributed_sys_latency.txt", "a+")
text_file.write(str(tdelta))
text_file.write("\n")
text_file.close()
nodeList =[]
nodeList = [u'10.241.1.5', u'10.241.1.7', u'10.241.1.9', u'10.241.1.25']
counter_1 = 0
while True:
conn, addr = sock.accept()
recieved = conn.recv(1024)
print recieved
json_obj = json.loads(recieved)
print("Message Type:", str(json_obj.get('AlertType')))
data = str(json_obj.get('Date'))
sent_time = data.split()
if( (str(json_obj.get('AlertType')) == "Alert")):
sensor_Data_Handler( str(json_obj.get('AlertType')), recieved)
print("Calling the Alert Function !!!")
trigger_Alert(str(json_obj.get('Sensor_ID')),sent_time[1])
text_file = open("receiver_marion.txt", "a+")
text_file.write(str(counter_1))
text_file.write("\n")
text_file.close()