forked from tidalvirus/alphaess-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfake-battery.py
More file actions
executable file
·50 lines (36 loc) · 1.32 KB
/
fake-battery.py
File metadata and controls
executable file
·50 lines (36 loc) · 1.32 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
#!/usr/bin/env python3
#Fake Battery (client)
import binascii
import socket
import struct
import sys
import re
import time
from crccheck.crc import Crc16Modbus
# Server details
HOST, PORT = "localhost", 7778
#filename = "1-1-16.direct.json"
# Create a socket (SOCK_STREAM means a TCP socket)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
# Connect to server and send data
sock.connect((HOST, PORT))
files = ['1-1-2.direct.json', '1-1-9.direct.json', '1-1-16.direct.json', '1-2-3.direct.json', '1-1-4.direct.json']
for filename in files:
with open(filename, 'rb') as f:
data = f.read()
f.close()
length = len(data)
values = re.split(r'[.-]', filename)
val1, val2, val3 = int(values[0]), int(values[1]), int(values[2])
#count the length, and mimic the data the battery might actually send
check = struct.pack("!3bi", val1, val2, val3, length) + bytes(data)
crc = Crc16Modbus.calc(check)
check = check + struct.pack('!H',crc)
sock.sendall(check)
print("Sent: {}".format(check))
print("Sleeping...")
time.sleep(1)
# Receive data from the server and shut down
#received = str(sock.recv(1024), "utf-8")
#print("Sent: {}".format(check))
#print("Received: {}".format(received))