def send_deny(boxNum, pin): padding = get_padding(config.OP_DENY_SIZE) packet = bytearray(config.OP_DENY_SIZE + padding) packet[0] = config.OP_DENY packet[1:9] = crypto.get_checksum(packet) packet[13:15] = struct.pack('>H', pin) send(packet, boxNum)
def loop(): try: packet = lora.recv() if not packet: return info('loop', 'recv packet {}'.format(lora.packet_str(packet))) base_name = '{}_'.format(get_channel()) data = [] op = packet[0] size = len(packet) if op == config.OP_ACK: data.append({'bn': base_name, 'n': 'Flag', 'u': 'Flag', 'v': packet[9]}) data.append({'n': 'Lock', 'u': 'Lock', 'v': packet[10]}) data.append({'n': 'Package', 'u': 'Package', 'v': packet[11]}) data.append({'n': 'Power', 'u': 'Power', 'v': packet[12]}) data.append({'n': 'Error', 'u': 'Error', 'v': packet[13]}) box_checksum = packet[1:9] info('loop', 'box checksum {}'.format(lora.packet_str(box_checksum))) elif op == config.OP_CONNECT: data.append({'bn': base_name, 'n': 'Flag', 'u': 'Flag', 'v': packet[0]}) data.append({'n': 'Lock', 'u': 'Lock', 'v': packet[1]}) data.append({'n': 'Package', 'u': 'Package', 'v': packet[2]}) data.append({'n': 'Power', 'u': 'Power', 'v': packet[3]}) data.append({'n': 'Error', 'u': 'Error', 'v': packet[4]}) box_key = packet[5:] info('loop', 'box key {}'.format(lora.packet_str(box_key))) elif op == config.OP_STATUS: data.append({'bn': base_name, 'n': 'Flag', 'u': 'Flag', 'v': packet[9]}) data.append({'n': 'Lock', 'u': 'Lock', 'v': packet[10]}) data.append({'n': 'Package', 'u': 'Package', 'v': packet[11]}) data.append({'n': 'Power', 'u': 'Power', 'v': packet[12]}) box_checksum = packet[1:9] info('loop', 'box checksum {}'.format(lora.packet_str(box_checksum))) else: raise Exception('invalid OP packet {}'.format(op)) if len(data) > 1: info('send', 'ACK') packet = bytearray(config.OP_ACK_SIZE) packet[0] = config.OP_ACK packet[1:9] = crypto.get_checksum(packet) packet[9] = 1 packet[10] = 1 packet[11] = 1 packet[12] = 100 packet[13] = config.E_OK lora.send(packet) info('send', 'ACK {}'.format(lora.packet_str(packet))) msg = Message(get_topic(), data) info('loop', 'message {}'.format(msg)) publish(msg) except Exception as ex: error('loop', str(ex)) finally: time.sleep(0)
def setup(): print('Program is starting ... ') print('Press Ctrl-C to exit.') lora.init() #boxNum = box #print('Mailbox Number: ' boxNum) info('send', 'STATUS') packet = bytearray(config.OP_STATUS_SIZE) packet[0] = config.OP_STATUS packet[1:9] = crypto.get_checksum(packet) packet[9] = 1 packet[10] = 1 packet[11] = 1 packet[12] = 1 i = 0 for x in os.environ['GATE_PUB_KEYS'].split(','): if i == 0: i += 1 else: lora.send(packet, i) info('send', 'ACK {}'.format(lora.packet_str(packet))) i += 1
def setup(box): print('Program is starting ... ') print('Press Ctrl-C to exit.') lora.init() print('Mailbox Number: ' + str(box)) info('send', 'STATUS') packet = bytearray(config.OP_STATUS_SIZE) packet[0] = config.OP_STATUS packet[1:9] = crypto.get_checksum(packet) packet[9] = 1 packet[10] = 1 packet[11] = 1 packet[12] = 1 lora.send(packet) info('send', 'ACK {}'.format(lora.packet_str(packet)))
def send_unlock(boxNum): padding = get_padding(config.OP_UNLOCK_SIZE) packet = bytearray(config.OP_UNLOCK_SIZE + padding) packet[0] = config.OP_UNLOCK packet[5:] = crypto.get_checksum(packet) send(packet, boxNum)
def send_status(boxNum): padding = get_padding(config.OP_STATUS_SIZE) packet = bytearray(config.OP_STATUS_SIZE + padding) packet[0] = config.OP_STATUS packet[1:9] = crypto.get_checksum(packet) send(packet, boxNum)
def send_register(boxNum): padding = get_padding(config.OP_REGISTER_SIZE) packet = bytearray(config.OP_REGISTER_SIZE + padding) packet[0] = config.OP_REGISTER packet[5:] = crypto.get_checksum(packet) send(packet, boxNum)
import config import crypto import db import lora status = bytearray(config.OP_STATUS_SIZE) status[0] = config.OP_STATUS status[1:9] = crypto.get_checksum(status) if __name__ == "__main__": try: print('Init') db.init() print('Create Box') db.create_box('SOME_TEST_KEY') print('Read Box') print(db.read_box()) print('Create Packet') db.create_packet(status) print('Read Packet') print(db.read_packet()) print('Create PIN') db.create_pin('CONTACT_1', 1111) db.create_pin('CONTACT_2', 2222) db.create_pin('CONTACT_3', 3333) print('Read PIN') print(db.read_pin())
import time box_key = bytearray(32) sync_word = 0x13 register = bytearray(config.OP_REGISTER_SIZE) register[0] = config.OP_REGISTER register[3] = sync_word connect = bytearray(config.OP_CONNECT_SIZE) connect[0] = config.OP_CONNECT connect[5:] = crypto.get_hash(connect) status = bytearray(config.OP_STATUS_SIZE) status[0] = config.OP_STATUS status[1:9] = crypto.get_checksum(status) unlock = bytearray(config.OP_UNLOCK_SIZE) unlock[0] = config.OP_UNLOCK unlock[1:9] = crypto.get_checksum(unlock) lock = bytearray(config.OP_LOCK_SIZE) lock[0] = config.OP_LOCK lock[1:9] = crypto.get_checksum(lock) allow = bytearray(config.OP_ALLOW_SIZE) allow[0] = config.OP_ALLOW allow[1:9] = crypto.get_checksum(allow) allow[13] = 0x0b # 2827 allow[14] = 0x0b