| 
									
										
										
										
											2018-08-07 11:40:02 -04:00
										 |  |  | from __future__ import print_function | 
					
						
							| 
									
										
										
										
											2018-08-07 23:11:11 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-08 17:15:49 -04:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2018-08-07 11:40:02 -04:00
										 |  |  | from socket import socket, AF_INET, SOCK_DGRAM | 
					
						
							| 
									
										
										
										
											2018-08-07 23:11:11 -04:00
										 |  |  | import time | 
					
						
							| 
									
										
										
										
											2018-08-07 11:40:02 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | listen = ('127.0.0.1', 2237) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-08 17:15:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | def from_message(content): | 
					
						
							| 
									
										
										
										
											2018-08-07 11:40:02 -04:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2018-08-08 17:15:49 -04:00
										 |  |  |         return json.loads(content) | 
					
						
							|  |  |  |     except ValueError: | 
					
						
							|  |  |  |         return {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def to_message(typ, value='', params=None): | 
					
						
							|  |  |  |     if params is None: | 
					
						
							|  |  |  |         params = {} | 
					
						
							|  |  |  |     return json.dumps({'type': typ, 'value': value, 'params': params}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Server(object): | 
					
						
							| 
									
										
										
										
											2018-08-09 22:44:25 -04:00
										 |  |  |     first = True | 
					
						
							| 
									
										
										
										
											2018-08-08 17:15:49 -04:00
										 |  |  |     def process(self, message): | 
					
						
							|  |  |  |         typ = message.get('type', '') | 
					
						
							|  |  |  |         value = message.get('value', '') | 
					
						
							|  |  |  |         params = message.get('params', {}) | 
					
						
							|  |  |  |         if not typ: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         print('->', typ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if value: | 
					
						
							|  |  |  |             print('-> value', value) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if params: | 
					
						
							|  |  |  |             print('-> params: ', params) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-09 22:44:25 -04:00
										 |  |  |         #### if typ == 'PING': | 
					
						
							|  |  |  |         ####     if self.first: | 
					
						
							|  |  |  |         ####         self.send('RX.GET_BAND_ACTIVITY') | 
					
						
							|  |  |  |         ####         self.send('TX.SET_TEXT', 'HERE WE GO') | 
					
						
							|  |  |  |         ####         time.sleep(1) | 
					
						
							|  |  |  |         ####         self.send('WINDOW.RAISE') | 
					
						
							|  |  |  |         ####         self.first = False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         #### if typ == 'PING': | 
					
						
							|  |  |  |         ####     self.send('STATION.GET_GRID') | 
					
						
							|  |  |  |         ####     self.send('RIG.GET_FREQ') | 
					
						
							|  |  |  |         ####     self.send('STATION.GET_CALLSIGN') | 
					
						
							|  |  |  |         ####     self.send('RX.GET_CALL_ACTIVITY') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         #### elif typ == 'STATION.GRID': | 
					
						
							| 
									
										
										
										
											2018-08-08 17:15:49 -04:00
										 |  |  |         ####     if value != 'EM73TU49TQ': | 
					
						
							| 
									
										
										
										
											2018-08-09 22:44:25 -04:00
										 |  |  |         ####         self.send('STATION.SET_GRID', 'EM73TU49TQ') | 
					
						
							| 
									
										
										
										
											2018-08-07 11:40:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-09 22:44:25 -04:00
										 |  |  |         #### elif typ == 'RIG.FREQ': | 
					
						
							| 
									
										
										
										
											2018-08-08 17:15:49 -04:00
										 |  |  |         ####     if params.get('DIAL', 0) != 14064000: | 
					
						
							| 
									
										
										
										
											2018-08-09 22:44:25 -04:00
										 |  |  |         ####         self.send('RIG.SET_FREQ', '', {"DIAL": 14064000, "OFFSET": 977}) | 
					
						
							|  |  |  |         ####         self.send('TX.SEND_MESSAGE', 'HELLO WORLD') | 
					
						
							| 
									
										
										
										
											2018-08-08 17:15:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         elif typ == 'CLOSE': | 
					
						
							|  |  |  |             self.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def send(self, *args, **kwargs): | 
					
						
							|  |  |  |         message = to_message(*args, **kwargs) | 
					
						
							|  |  |  |         print('outgoing message:', message) | 
					
						
							|  |  |  |         self.sock.sendto(message, self.reply_to) | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     def listen(self): | 
					
						
							|  |  |  |         print('listening on', ':'.join(map(str, listen))) | 
					
						
							|  |  |  |         self.sock = socket(AF_INET, SOCK_DGRAM) | 
					
						
							|  |  |  |         self.sock.bind(listen) | 
					
						
							|  |  |  |         self.listening = True | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             while self.listening: | 
					
						
							|  |  |  |                 content, addr = self.sock.recvfrom(65500) | 
					
						
							|  |  |  |                 print('incoming message:', ':'.join(map(str, addr))) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     message = json.loads(content) | 
					
						
							|  |  |  |                 except ValueError: | 
					
						
							|  |  |  |                     message = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if not message: | 
					
						
							|  |  |  |                     continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 self.reply_to = addr | 
					
						
							|  |  |  |                 self.process(message) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             self.sock.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def close(self): | 
					
						
							|  |  |  |         self.listening = False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							|  |  |  |     s = Server() | 
					
						
							|  |  |  |     s.listen() | 
					
						
							| 
									
										
										
										
											2018-08-07 11:40:02 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     main() |