Exemple #1
0
    def pdm_loop(self):
        self.i_pod = Pod.Load("/home/pi/omnipy/data/pod.json", "/home/pi/omnipy/data/pod.db")
        self.i_pdm = Pdm(self.i_pod)
        self.g_pod = Pod.Load("/home/pi/glucopy/data/pod.json", "/home/pi/glucopy/data/pod.db")
        self.g_pdm = Pdm(self.g_pod)
        self.check_wait = 3600
        while(True):
            if self.rate_check_event.wait(self.check_wait):
                self.rate_check_event.clear()

            with self.rate_request_lock:
                i_requested, g_requested = self.rate_requested

            wait1 = 3600
            wait2 = 3600

            if self.i_pod.state_progress == 8 or self.i_pod.state_progress == 9:
                self.i_pdm.start_radio()
                self.rate_check(i_requested, 1.6, self.i_pod, self.i_pdm)
                wait1 = self.check_wait
                self.i_pdm.stop_radio()

            if self.g_pod.state_progress == 8 or self.g_pod.state_progress == 9:
                self.g_pdm.start_radio()
                self.rate_check(g_requested, 0.3, self.g_pod, self.g_pdm)
                wait2 = self.check_wait
                self.g_pdm.stop_radio()

            self.check_wait = min(wait1, wait2)
Exemple #2
0
def activate_pod():
    verify_auth(request)

    pod = Pod()
    archive_pod()
    pod.radio_address2 = get_next_pod_address()
    pod.Save(POD_FILE + POD_FILE_SUFFIX)

    pdm = get_pdm()
    pdm.activate_pod()
    save_activated_pod_address(pod.radio_address)
    return pod
Exemple #3
0
def get_pod() -> Pod:
    global g_pod
    try:
        if g_pod is None:
            g_pod = Pod.Load(POD_FILE + POD_FILE_SUFFIX, POD_FILE + POD_LOG_SUFFIX)
        return g_pod
    except:
        logger.exception("Error while loading pod")
        return None
Exemple #4
0
def get_pod():
    path = "data/bbe.json"
    log_path = "data/bbe.log"
    pod = None

    try:
        pod = Pod.Load(path)
    except:
        pass

    if pod is None:
        pod = Pod()
        pod.path = path
        pod.log_file_path = log_path
        pod.id_lot = 44147
        pod.id_t = 1100256
        pod.radio_address = 0x1f0e89f0
        pod.Save()

    return pod
Exemple #5
0
def pair_pod():
    _verify_auth(request)

    pod = _get_pod()
    if pod.state_progress >= PodProgress.Running:
        pod = Pod()
        _archive_pod()
        _set_pod(pod)

    pdm = _get_pdm()

    req_address = _get_next_pod_address()
    utc_offset = int(request.args.get('utc'))
    pdm.pair_pod(req_address, utc_offset=utc_offset)
    _save_activated_pod_address(req_address)
Exemple #6
0
def new_pod():
    verify_auth(request)

    pod = Pod()

    if request.args.get('id_lot') is not None:
        pod.id_lot = int(request.args.get('id_lot'))
    if request.args.get('id_t') is not None:
        pod.id_t = int(request.args.get('id_t'))
    if request.args.get('radio_address') is not None:
        pod.radio_address = int(request.args.get('radio_address'))
        pod.radio_address2 = int(request.args.get('radio_address'))

    archive_pod()
    pod.Save(POD_FILE + POD_FILE_SUFFIX)
    return pod
Exemple #7
0
def new_pod():
    _verify_auth(request)

    pod = Pod()

    if request.args.get('id_lot') is not None:
        pod.id_lot = int(request.args.get('id_lot'))
    if request.args.get('id_t') is not None:
        pod.id_t = int(request.args.get('id_t'))
    if request.args.get('radio_address') is not None:
        pod.radio_address = int(request.args.get('radio_address'))
    else:
        pod.radio_address = 0

    if pod.radio_address == 0:
        pod.radio_address = _get_pdm_address(45000)

    _archive_pod()
    _set_pod(pod)
Exemple #8
0
def _get_pod():
    global g_pod
    try:
        if g_pod is None:
            if os.path.exists(DATA_PATH + POD_FILE + POD_FILE_SUFFIX):
                g_pod = Pod.Load(DATA_PATH + POD_FILE + POD_FILE_SUFFIX,
                                 DATA_PATH + POD_FILE + POD_DB_SUFFIX)
            else:
                g_pod = Pod()
                g_pod.path = DATA_PATH + POD_FILE + POD_FILE_SUFFIX
                g_pod.path_db = DATA_PATH + POD_FILE + POD_DB_SUFFIX
                g_pod.Save()
        return g_pod
    except:
        logger.exception("Error while loading pod")
        return None
Exemple #9
0
def new_pod():
    try:
        verify_auth(request)

        pod = Pod()

        if request.args.get('lot') is not None:
            pod.lot = int(request.args.get('lot'))
        if request.args.get('tid') is not None:
            pod.tid = int(request.args.get('tid'))
        if request.args.get('address') is not None:
            pod.address = int(request.args.get('address'))

        archive_pod()
        pod.Save(POD_FILE + POD_FILE_SUFFIX)
        return respond_ok({})
    except RestApiException as rae:
        return respond_error(str(rae))
    except Exception:
        logger.exception("Error while creating new pod")
        return respond_error("Other error. Please check log files.")
Exemple #10
0
#!/usr/bin/python3
from podcomm.pod import Pod
from podcomm.pdm import Pdm
from podcomm.packet import Packet
from podcomm.rileylink import RileyLink
import logging
import sys
from podcomm.crc import crc8

logging.basicConfig(level=logging.DEBUG)

pod = Pod()
pod.lot = int(sys.argv[1])
pod.tid = int(sys.argv[2])
pod.lastNonce = None

r = RileyLink("88:6b:0f:44:fc:1b")
print("connecting to RL")
r.connect()
print("initializing")
r.init_radio()
print("ready to listen for pdm")

p = None
while True:
    data = r.get_packet(30000)
    if data is not None and len(data) > 2:
        calc = crc8(data[2:-1])
        if data[-1] == calc:
            p = Packet(0, data[2:-1])
            break
Exemple #11
0
from podcomm.nonce import Nonce
from podcomm.protocol_radio import PdmRadio, RadioPacket, RadioPacketType, TxPower
from podcomm.crc import crc16, crc8
from tests.mock_radio import MockPacketRadio
from podcomm.pod import Pod

import time

pod = None
try:
    pod = pod.Load("/home/ba/newpod2.json", "/home/ba/newpod2.json.log")
except:
    pass

if pod is None:
    pod = Pod()
    pod.path = "/home/ba/newpod2.json"
    pod.log_file_path = "/home/ba/newpod2.json.log"

req_address = 0x1f000015
radio = PdmRadio(0xffffffff)

msg = request_assign_address(req_address)
rsp = radio.send_message_get_message(msg,
                                     ack_address_override=req_address,
                                     tx_power=TxPower.Lowest)
response_parse(rsp, pod)
pod.Save("/home/ba/newpod1.json")

radio.message_sequence = 1
msg = request_setup_pod(pod.id_lot, pod.id_t, pod.radio_address, 2019, 4, 3,
Exemple #12
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--POD-CONFIG", required=True)
    parser.add_argument("--MQTT-SERVER",
                        required=True,
                        default=None,
                        nargs="?")
    parser.add_argument("--MQTT-PORT",
                        required=False,
                        default="1881",
                        nargs="?")
    parser.add_argument("--MQTT-SSL", required=False, default="", nargs="?")
    parser.add_argument("--MQTT-CLIENTID",
                        required=True,
                        default="",
                        nargs="?")
    parser.add_argument("--MQTT-TOPIC", required=True, default="", nargs="?")
    parser.add_argument("--LOG-LEVEL",
                        required=False,
                        default="DEBUG",
                        nargs="?")
    parser.add_argument("--LOG-FILE", required=False, default=None, nargs="?")

    args = parser.parse_args()

    formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
    ch = logging.StreamHandler(sys.stdout)
    ch.setFormatter(formatter)
    ch.setLevel(level=args.LOG_LEVEL)
    logging.basicConfig(level=args.LOG_LEVEL, handlers=[ch])

    if args.LOG_FILE:
        fh = logging.FileHandler(filename=args.LOG_FILE)
        fh.setFormatter(formatter)
        fh.setLevel(level=args.LOG_LEVEL)
        logging.getLogger().addHandler(fh)

    pod = Pod.Load(args.POD_CONFIG)

    mqtt_client = Client(client_id=args.MQTT_CLIENTID,
                         clean_session=False,
                         protocol=MQTTv311,
                         transport="tcp")

    if args.MQTT_SSL != "":
        mqtt_client.tls_set(certfile=None,
                            keyfile=None,
                            cert_reqs=ssl.CERT_REQUIRED,
                            tls_version=ssl.PROTOCOL_TLSv1_2,
                            ciphers=None)
        mqtt_client.tls_insecure_set(True)

    mqtt_client.reconnect_delay_set(min_delay=5, max_delay=120)
    mqtt_client.retry_first_connection = True

    pdm = Pdm(pod)

    processor = Processor(mqtt_client, args.MQTT_TOPIC, pdm)

    processor.start(args.MQTT_SERVER, int(args.MQTT_PORT), 30)

    try:
        while not exit_event.wait(timeout=10):
            pass
    except KeyboardInterrupt:
        pass

    processor.stop()
Exemple #13
0
def archive_pod():
    _verify_auth(request)
    pod = Pod()
    _archive_pod()
    _set_pod(pod)
Exemple #14
0
from podcomm.protocol_common import PdmMessage, PdmRequest, PodMessage, PodResponse
from podcomm.protocol import *
from podcomm.nonce import Nonce
from podcomm.protocol_radio import PdmRadio, RadioPacket, RadioPacketType, TxPower
from podcomm.crc import crc16, crc8
from tests.mock_radio import MockPacketRadio
from podcomm.pod import Pod

import time

pod = Pod()
mock_radio = MockPacketRadio()

req_address = 0x1f000010
radio = PdmRadio(0xffffffff, packet_radio=mock_radio)

rsp = PodMessage()
body = bytearray.fromhex("020700")
body += bytearray.fromhex("020700")
body += bytearray.fromhex("0202")
body += bytearray.fromhex("00001555")  #lot
body += bytearray.fromhex("00012223")  #tid
body += bytearray.fromhex("ff")
body += bytearray.fromhex("1f000010")

rsp.add_part(PodResponse.VersionInfo, body)
mock_radio.add_response(rsp, 0xffffffff, 0xffffffff, False)

msg = request_assign_address(req_address)
rsp = radio.send_message_get_message(msg,
                                     ack_address_override=req_address,
Exemple #15
0
def get_pod():
    return Pod.Load(POD_FILE + POD_FILE_SUFFIX, POD_FILE + POD_LOG_SUFFIX)
Exemple #16
0
from podcomm.protocol_common import PdmMessage, PdmRequest, PodMessage, PodResponse
from podcomm.protocol import *
from podcomm.nonce import Nonce
from podcomm.protocol_radio import PdmRadio, RadioPacket, RadioPacketType
from podcomm.crc import crc16, crc8
from tests.mock_radio import MockPacketRadio
from podcomm.pod import Pod


import time

pod = Pod()
pod.radio_address = 0x1f10fc49
radio = PdmRadio(0x1f10fc49, 0, 0)
pod.id_t = 381741
pod.id_lot = 44425

msg = request_status()
msg = radio.send_message_get_message(msg)
response_parse(msg, pod)
print(pod.__dict__)



Exemple #17
0
#!/usr/bin/python3
from podcomm.pod import Pod
from podcomm.pdm import Pdm, PdmError
import logging
import sys
import time
from decimal import *

logging.basicConfig(level=logging.DEBUG)

pod = Pod.Load(sys.argv[1])
pdm = Pdm(pod)

amount = Decimal(sys.argv[2])

print("\nStarting bolus of %.2f units\n" % (amount))

try:
    pdm.bolus(amount, False)
    print("Bolus started status:")
    pdm.updatePodStatus()
    print(pdm.pod)
except PdmError as ProtocolError:
    pdm.updatePodStatus()

print("\n\nBolusing %.2f units\n\nPress ctrl+c to cancel\n\n" % (amount))

try:
    while True:
        print("Getting interim status")
        pdm.updatePodStatus()
Exemple #18
0
import time


def skip_ack(packet):
    if packet.type == RadioPacketType.ACK:
        return None
    else:
        return packet


path = "data/bbe.json"
log_path = "data/bbe.log"
pod = None
try:
    pod = Pod.Load(path)
except:
    pass

if pod is None:
    pod = Pod()
    pod.path = path
    pod.log_file_path = log_path
    pod.id_lot = 44147
    pod.id_t = 1100256
    pod.radio_address = 0x1f0e89f0
    pod.Save()

mock_radio = MockPacketRadio(send_callback=skip_ack,
                             allow_connect=True,
                             allow_listen=True)
Exemple #19
0
def main():
    pod = Pod(msgHandler, errHandler, 123456, 123456)
    pod.start()
    raw_input()
    pod.stop()