Skip to content

Commit

Permalink
Added py-csv-ip stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
CreepPork committed Apr 29, 2020
1 parent 70c5975 commit d28b8c3
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BIND_IP=0.0.0.0
BIND_PORT=5002

MESSAGE_RELAY_ADDR=
MESSAGE_RELAY_BEARER_TOKEN=
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [CreepPork]
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,50 @@
# py-sia-dc-07
A Python script which will listen on a TCP/IP port and forward messages to a HTTP endpoint from a SIA DC-07 protocol supported device

A simple Python script that listens on a given TCP ip/port and awaits messages from a SIA DC-07 protocol supported device.

The script will handle all device communication with the supported device.

After parsing the contents of the TCP message then it will relay the JSON-formatted message to a web server endpoint of your choice. There, you can act upon the contents of the message itself.

## Payload

```json
{
"username": "",
"password": "",
"asd_id": "1002",
"event_qualifier": "3",
"event_code": "602",
"group_number": "000",
"device_or_sensor_number": "01"
}
```

## Installation

You can install the package via Git and the dependencies via pip. You'll need to configure the `.env` file too:

```bash
git clone https://github.com/CreepPork/py-sia-dc-07
pip install -r ./requirements.txt
cp .env.example .env
```

## Usage

```bash
python3 app.py
```

### Security

If you discover any security related issues, please email security@garkaklis.com instead of using the issue tracker.

## Credits

- [Ralfs Garkaklis](https://github.com/CreepPork)
- [All Contributors](https://github.com/CreepPork/py-sia-dc-07/contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
122 changes: 122 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import json
import socket
import threading
import os
import requests
import sys

from dotenv import load_dotenv, find_dotenv

load_dotenv(find_dotenv())

BIND_IP = os.getenv('BIND_IP')
BIND_PORT = int(os.getenv('BIND_PORT'))
MESSAGE_RELAY_ADDR = os.getenv('MESSAGE_RELAY_ADDR')
MESSAGE_RELAY_BEARER_TOKEN = os.getenv('MESSAGE_RELAY_BEARER_TOKEN')


def main():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((BIND_IP, BIND_PORT))
server.listen(5)

print('Listening on {}:{}'.format(BIND_IP, BIND_PORT))

accept_connections(server)

server.close()


def accept_connections(server):
while True:
client_sock = None

try:
client_sock, address = server.accept()

print('')
print('Accepted connection from {}:{}'.format(
address[0], address[1]))

client_handler = threading.Thread(
target=handle_client_connection,
args=(client_sock,)
)

client_handler.start()
except Exception as e:
print('{} in accept_connections'.format(e))
if client_sock:
print('Closing client socket in accept_connections.')
client_sock.close()
break


def handle_client_connection(client_socket):
try:
request = client_socket.recv(1024)

process_request_data(request)

# client_socket.send(b'ACK')
client_socket.close()
except Exception as e:
print('{} in handle_client_connection'.format(e))
if client_socket:
print('Closing client socket in handle_client_connection')
client_socket.close()

raise Exception


def process_request_data(request: bytes):
print(request)
contents = request.decode('ASCII').split(',')

username, password, asd_id, message = contents
message = message.rstrip('\x00')

message_format = message[0:2]

if message_format != '18':
print('Message format not supported, need 18, got {}'.format(message_format))
return

# 1 = new event or opening, 3 = new restore or closing, 6 = previous event
event_qualifier = message[2]

# 3 hex digits
event_code = message[3:6]

# 2 hex digits
group_number = message[6:9]

# 3 hex digits or can be 0 for no info
device_or_sensor_number = message[9:13]

csv_data = {
'username': username,
'password': password,
'asd_id': asd_id,
'event_qualifier': event_qualifier,
'event_code': event_code,
'group_number': group_number,
'device_or_sensor_number': device_or_sensor_number,
}

relay_message_contents(csv_data)


def relay_message_contents(csv_data: dict):
print('Received {}'.format(csv_data))

headers = {'Authorization': 'Bearer {}'.format(MESSAGE_RELAY_BEARER_TOKEN)}

r = requests.post(MESSAGE_RELAY_ADDR, json=csv_data,
headers=headers)

print('Sent HTTP request to relay, got {} status'.format(r.status_code))


if __name__ == "__main__":
sys.exit(main())
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-dotenv>=0.10.1

0 comments on commit d28b8c3

Please sign in to comment.