forked from pavian57/qsosrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
192 lines (142 loc) · 4.79 KB
/
main.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: ruedi kneubuehler hb9fvk@gmx.net
import sys
import time
import socket
import binascii
from qsostate import State
from qsostate import Qsostate
from mopp import Moppm32
from callsign import CallGenerator
if sys.platform == 'esp8266':
import roger
from wifi_manager import WifiManager
WifiManager.setup_network()
import utelnetserver
utelnetserver.start()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
SERVER_IP = "0.0.0.0"
UDP_PORT = 7373
CLIENT_TIMEOUT = 300
MAX_CLIENTS = 10
KEEPALIVE = 10
DEBUG = False
# map the states to action functions
# map the states to action functions
action = {
State.CQ : 'cq',
State.DE : 'de',
State.CALLSIGN : 'callsign',
State.PSE : 'pse',
State.K : 'k',
State.CHASE: 'k',
State.OURCALLSIGN : 'ourcallsign',
State.UR : 'ur',
State.RST : 'rst',
State.REPORT :'report',
State.REPORTDE :'reportde',
State.REPORTCALL :'reportcall',
State.REPORTK : 'reportk',
State.REPORTCHASE : 'reportk',
State.BYERR : 'byerr',
State.BYETU : 'byetu',
State.BYE73 : 'bye73',
State.BYEDE : 'byede',
State.BYECALL : 'byecall',
State.BYEEE : 'byeee',
State.END : 'end'
}
qso = Qsostate(action)
receivers = {}
server = (SERVER_IP, UDP_PORT)
sock.bind(server)
print("Listening on " + SERVER_IP + ":" + str(UDP_PORT))
print("Python {} on {}\n".format(sys.version, sys.platform))
translator = Moppm32()
callsign = CallGenerator()
print('ready')
def log(s):
if DEBUG:
print(s)
'''
easier code int(value,2) does not work with micropython
'''
def sendmoppstr(adr, txtstr):
log(txtstr)
sendstr = translator.txttomopp(txtstr)
splits = [sendstr[x:x + 8] for x in range(0, len(sendstr), 8)]
frame = bytearray()
for split in splits:
value = 0
t = len(split)
for i in range (t):
value *= 2 # // double the result so far
if split[i] == '1':
value +=1 #++; //add 1 if needed
frame.append(value) #
time.sleep(0.2)
sock.sendto(frame,adr)
def main():
if sys.platform == 'esp8266':
roger.roger()
rc = 0
state = State.CQ
while True:
payload, client_address = sock.recvfrom(64)
b = bytearray(payload)
hexstr = binascii.hexlify(b)
morsecode = translator.mopptotxt(hexstr)
tlg = morsecode.strip()
# print ('From: ', end ='')
# print( client_address)
# print('Text: ', end ='')
log('<'+morsecode+'>')
# call the designated function and update the state
# <sk> break and go to end
if morsecode.strip() == '<sk>':
print(':<sk> bye')
state = State.END
if state == State.CQ:
if (morsecode.isdigit()):
i = int(morsecode)
if i >= 3 or i >=8:
callsign.set_call_length(i)
if sys.platform == 'esp8266':
roger.blink(i)
state = qso.run_func(state,tlg)
log('state: '+str(state))
if state == State.CHASE:
qso.urcallsign = callsign.get_call()
# Chaser calls activator
# VK3XAS/P de VK3BQ VK3BQ VK3BQ K
sendmoppstr(client_address, qso.callsignlist[0])
sendmoppstr(client_address, 'de')
sendmoppstr(client_address, qso.urcallsign)
sendmoppstr(client_address, qso.urcallsign)
sendmoppstr(client_address, qso.urcallsign)
sendmoppstr(client_address, 'k')
state = State.OURCALLSIGN
#Chaser confirms receipt of his report and gives one to activator
# R R UR RST 559 559 de VK3BQ K
if state == State.REPORTCHASE:
sendmoppstr(client_address, 'r')
sendmoppstr(client_address, 'r')
sendmoppstr(client_address, 'ur')
sendmoppstr(client_address, 'rst')
rst = callsign.get_rst()
sendmoppstr(client_address,rst )
sendmoppstr(client_address,rst )
sendmoppstr(client_address,rst )
sendmoppstr(client_address, 'de')
sendmoppstr(client_address, qso.urcallsign)
sendmoppstr(client_address, 'k')
state = State.BYERR
# Chaser may then send 73
if state == State.END:
sendmoppstr(client_address, '73')
if sys.platform == 'esp8266': roger.roger()
if sys.platform == 'esp8266': roger.roger()
state = State.CQ
if __name__=="__main__":
main()