Skip to content
Snippets Groups Projects
Commit 60a2a880 authored by jo2-holdsworth's avatar jo2-holdsworth
Browse files

removed part 2

a few more bugs after unittest
parent bc57047c
No related branches found
No related tags found
No related merge requests found
...@@ -17,15 +17,16 @@ ...@@ -17,15 +17,16 @@
- [Getting Started](#getting-started) - [Getting Started](#getting-started)
- [Dependencies](#dependencies) - [Dependencies](#dependencies)
- [Installing](#installing) - [Installing](#installing)
- [Executing program](#executing-program)
- [Authors](#authors) - [Authors](#authors)
- [Version History](#version-history) - [Version History](#version-history)
- [License](#license) - [License](#license)
## Description ## Description
Morse code :: Worksheet(2 part 1) Morse code :: Worksheet(2 part 1)\
A program to encode and decode morse. This is a rep for only worksheet 2 part 1. Worksheet 2 part 2 can be found here [IOT - Worksheet 2 part 2 ](https://gitlab.uwe.ac.uk/jo2-holdsworth/iot-worksheet-2-part-2)
### Task 1: Getting to know morse code ### Task 1: Getting to know morse code
* *The CSCT cloud server has a port open on 10105 which contains a morse encoder and decoder. We are required to connect to the service and test its functionality*. * *The CSCT cloud server has a port open on 10105 which contains a morse encoder and decoder. We are required to connect to the service and test its functionality*.
* To connect to the port on the csct server we need to be able to access the port. Todo this we can port ford the port via VS Code. Using the command `Ctrl + Shift + P` we can bring up the command wind and open ports: ![image](src/screenshot1.PNG) if we then add the port 10105 ![image](src/screenshot2.PNG) should be able to access that port on our local machine using a browser to access `localhost:10105` as we can see it has worked and we can now connect to the 10105 port on the csct cloud server: ![image](src/screenshot3.PNG) * To connect to the port on the csct server we need to be able to access the port. Todo this we can port ford the port via VS Code. Using the command `Ctrl + Shift + P` we can bring up the command wind and open ports: ![image](src/screenshot1.PNG) if we then add the port 10105 ![image](src/screenshot2.PNG) should be able to access that port on our local machine using a browser to access `localhost:10105` as we can see it has worked and we can now connect to the 10105 port on the csct cloud server: ![image](src/screenshot3.PNG)
...@@ -86,20 +87,12 @@ morse.decode('..- ...') ...@@ -86,20 +87,12 @@ morse.decode('..- ...')
### Dependencies ### Dependencies
* None * unittest
### Installing ### Installing
* Nothing to install * Nothing to install
### Executing program
```
python3 main.py
# or
python3 morseunit.py
```
## Authors ## Authors
Contributors names and contact info Contributors names and contact info
......
tree = "-ETIANMSURWDKGOHVF*L*PJBXCYZQ**54*3*¿?2&*+****16=/***(*7***8*90*************_****\"**.********'**-********;!*)***¡*,****:****************$***********************************************************************************************************************"
def decode_bt(string):
output = ""
parent = 1
for i in range(len(string)):
if i < len(string) - 1 :
if string[i] == " ":
output += tree[parent - 1]
parent = 1
if string[i] == ".":
parent = 2 * parent
elif string[i] == "-":
parent = 2 * parent + 1
else:
if string[i] == ".":
parent = 2 * parent
if string[i] == "-":
parent = 2 * parent + 1
output += tree[parent - 1]
parent = 1
return output
print(decode_bt(".-. .---- -.. . ... .---- -...- .... .. -...- -.--."))
\ No newline at end of file
# encode_ham(sender: str, receiver: str, msg:str) -> str
import morse
import server
import asyncio
def encode_ham(sender, receiver, msg):
output = receiver + "de" + sender + "=" + msg + "=("
# print(output)
return morse.encode(output)
def decode_ham(str2):
print("hi")
ret = morse.decode(str2)
sender = ret.split("DE")
receiver = sender[1].split("=")
msg = receiver[1].split("=(")
return (sender[0],receiver[0],msg[0])
# print(encode_ham("jack","echo","its me"))
print(encode_ham("s","echo","msg"))
asyncio.run(server.main(encode_ham("jack","time","hello world")))
print(decode_ham(asyncio.run(server.main(encode_ham("jack","echo","hello how are you")))))
# print(decode_ham(".-. .---- -.. . ... .---- -...- .... .. -...- -.--."))
\ No newline at end of file
tree = "-ETIANMSURWDKGOHVF*L*PJBXCYZQ**54*3*¿?2&*+****16=/***(*7***8*90*************_****\"**.********'**-********;!*)***¡*,****:****************$***********************************************************************************************************************"
output = ""
for item in tree:
output += "\"" + item + "\"" + ","
print(output)
\ No newline at end of file
...@@ -52,7 +52,7 @@ class node: ...@@ -52,7 +52,7 @@ class node:
output.append(self.decode(y)) output.append(self.decode(y))
else: else:
output.append(" ") # add a space back in output if empty output.append(" ") # add a space back in output if empty
return "".join(output) return "".join(output).replace(" ", " ")
def decode(self,values): def decode(self,values):
# a method to decode morse suing recursion # a method to decode morse suing recursion
...@@ -143,4 +143,3 @@ def decode(value): ...@@ -143,4 +143,3 @@ def decode(value):
# the decoder # the decoder
tree = initTree() tree = initTree()
return tree.words(value) return tree.words(value)
\ No newline at end of file
import time
import morse
start = time.time()
morse.decode('.. --- - .. ... - .... . -... . ... -')
end = time.time()
print(f"morse(oop): {end-start}")
\ No newline at end of file
import asyncio
import websockets
import json
import time
async def main(str):
uri = "ws://localhost:10102"
async with websockets.connect(uri) as websocket:
# After joining server will send client unique id.
message = json.loads(await websocket.recv())
print(message)
# Get the client_id from the join message
if message['type'] == 'join_evt':
client_id = message['client_id']
# Send a ping to the server
await send_message(websocket, str, client_id)
# Wait for the 'ping' response from the server
response = await recv_message(websocket)
print("The Server Sent Back:")
print(response)
else:
# If first message is not the join message exit
print("Did not receive a correct join message")
return 0
return response
async def send_message(websocket, message, client_id):
outward_message = {
'type' : 'morse_evt',
'client_id': client_id,
'payload': message
}
await websocket.send(json.dumps(outward_message))
async def recv_message(websocket):
message = json.loads(await websocket.recv())
return message['payload']
if __name__ == "__main__":
print("Echo client")
while 1:
time.sleep(1)
asyncio.run(main())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment