1#!/usr/bin/env python3
  2
  3"""Raise ONBOARD EVENTS as CHART events."""
  4
  5import os
  6import logging
  7
  8from chart.db import ts
  9from chart.alg import init_algorithm
 10from chart.events.event import Event
 11from chart.alg import JobStatus
 12from chart.products.pus.packetdef import PacketDef
 13
 14logger = logging.getLogger()
 15
 16
 17def raise_ob_event(sid, sensing_start, sensing_stop, datatable, eventraiser):
 18    """Detect PUS Service 5 onboard event report in one Table for the given time range.
 19
 20    Args:
 21        sid (SID): Source ID
 22        start (datetime): Start of time range to process
 23        stop (datetime): End of time range to process
 24        table (str): time-series table containing Service 5 onboard events
 25    """
 26    spid = 0
 27
 28    for row in ts.select(table=datatable,
 29                         fields=('SENSING_TIME', 'SPID', 'SEQUENCE_COUNT', 'PAYLOAD', 'HEX'),
 30                         sid=sid,
 31                         sensing_start=sensing_start,
 32                         sensing_stop=sensing_stop,
 33                         calibrated=False):
 34
 35        # get packet details for each spid
 36        sensing_time = row[0]
 37        spid = row[1]
 38        sequence_count = row[2]
 39        payload = row[3]
 40        hexdata = row[4]
 41        packet_reader = PacketDef.find_one(spid=spid, sid=sid)
 42        severity = 0
 43
 44        # onboard events are all Service 5 Packets
 45        if packet_reader is not None and (packet_reader.service == 5 or \
 46            (packet_reader.service == 1 and packet_reader.subservice in [2, 4, 6, 8])):
 47            # logger.info('Got an onboard event ' + str(sensing_time))
 48
 49            # set severity according to
 50            if packet_reader.service == 5:
 51                if packet_reader.subservice in [1, 2, 3, 4]:
 52                    severity = packet_reader.subservice
 53
 54            elif packet_reader.service == 1:
 55                severity = 3
 56
 57            eventraiser.add(Event(sid=sid,
 58                          event_classname='ONBOARD-EVENTS',
 59                          start_time=sensing_time,
 60                          instance_properties={
 61                                'Name': packet_reader.name,
 62                                'Severity': severity,
 63                                'Description': packet_reader.description,
 64                                'SPID': packet_reader.spid,
 65                                'Type': packet_reader.service,
 66                                'SType': packet_reader.subservice,
 67                                'APID': packet_reader.apid,
 68                                'SSC': sequence_count,
 69                                'Param1': packet_reader.param1,
 70                                'Param2': packet_reader.param2,
 71                                'Param_Data': payload,
 72                                'Hex_Data': hexdata}
 73                            )
 74                        )
 75
 76
 77def event_tables(sid, sensing_start, sensing_stop, eventraiser):
 78    raise_ob_event(sid=sid,
 79                       sensing_start=sensing_start,
 80                       sensing_stop=sensing_stop,
 81                       datatable='TM_STORE',
 82                       eventraiser=eventraiser
 83                       )
 84
 85
 86def dispatch(wo, resultfile, eventraiser):
 87    """We are being run by the dispatcher tool."""
 88    logger.info('Starting ONBOARD_EVENTS')
 89    for job in wo.read_jobs():
 90        logger.info('Begin event from {start} to {stop}'.
 91                             format(start=job['sensing_start'],
 92                                    stop=job['sensing_stop']))
 93
 94        # generate events from Service 5 packets stored in TM table
 95        event_tables(job.sid,
 96                     job.sensing_start,
 97                     job.sensing_stop,
 98                     eventraiser)
 99
100        eventraiser.close()
101
102        resultfile.add_job(job, JobStatus.COMPLETED)
103
104    eventraiser.log_report()
105    resultfile.close()
106    logger.info('All done')
107
108
109def main():
110    """ Command line entry point.
111
112    This program can either be called via 'dispatcher' or standalone."""
113    dispatch(*init_algorithm())
114
115
116if __name__ == '__main__':
117    main()