-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayload_sender.py
More file actions
127 lines (91 loc) · 2.24 KB
/
payload_sender.py
File metadata and controls
127 lines (91 loc) · 2.24 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 10 22:42:29 2021.
@author: sophu
"""
from threading import Thread
import zmq
import time
class ethernet_sender(Thread):
"""
Ethernet transmitter.
Sends data to the API from the siutcase.
"""
def __init__(self, ip, storage_box, frequncy):
"""
Parameters.
----------
ip : TYPE
DESCRIPTION.
storage_box : TYPE
DESCRIPTION.
frequncy : TYPE
DESCRIPTION.
Returns
-------
None.
"""
Thread.__init__(self)
self.ip = ip
context = zmq.Context()
self.socket = context.socket(zmq.PUB)
self.connect()
self.box = storage_box
self.frequency = frequncy
def run(self):
"""
Return.
-------
None.
"""
while True:
start = time.process_time()
message = self.box.get_reduced_string()
self.publish(message)
end = time.process_time()
time.sleep(1/self.frequency - (end-start))
def publish(self, message):
"""
Parameters.
----------
message : TYPE
DESCRIPTION.
Returns
-------
None.
"""
self.socket.send_json(message)
def get_message(self, reduce=True):
"""
Get a message from the storage box.
Parameters.
----------
reduce : TYPE, optional
DESCRIPTION. The default is True.
If it's set to false return a string of all data is sent,
else only a reduced version of the string is sent.
Returns
-------
TYPE: String
a json string from the storage box.
"""
if reduce:
return self.box.get_reduced_string()
else:
return self.box.get_full_string()
def disconnect(self):
"""
Disconnect from the socket.
Returns.
-------
None.
"""
self.socket.disconnect()
def connect(self):
"""
Connect the socket to the spesific IP.
Returns.
-------
None.
"""
self.socket.bind(self.ip)