Exemple #1
0
    def POST(self):
        web.header('Content-Type', 'application/json')
        try:
            status = False
            action = common.getJsonArg("action", "")
            if action == 'form' or action == 'join':
                channel = common.getJsonArg("channel", None)
                power = common.getJsonArg("power", None)
                panid = common.getJsonArg("panid", None)
                #d = logger.Logger("nwk.py")
                #d.log("%s, %s, %s", str(type(channel)), str(type(power)), str(type(panid)))
                if channel is not None and not isinstance(channel, (int)):
                    return json.dumps({"status": -1, "errormsg": "Malformed channel input"})
                if power is not None and not isinstance(power, (int)):
                    return json.dumps({"status": -1, "errormsg": "Malformed power input"})
                if panid is not None and not isinstance(panid, (int)):
                    return json.dumps({"status": -1, "errormsg": "Malformed PAN ID input"})
                if action == 'form':
                    status = smartmeter.smeter.smctrl.nwk_mgr.form_network(radio_channel=channel, radio_power=power, pan_id=panid)
                else:
                    status = smartmeter.smeter.smctrl.nwk_mgr.join_network(radio_channel=channel, radio_power=power, pan_id=panid)

            elif action == 'pjoin':
                duration = common.getJsonArg("duration", None)
                bcast = common.getJsonArg("broadcast", None)
                if not isinstance(duration, (int)):
                    return json.dumps({"status": -1, "errormsg": "Malformed duration input"})
                if not isinstance(bcast, (int)):
                    return json.dumps({"status": -1, "errormsg": "Malformed broadcast input"})
                if bcast > 0:
                    broadcast = True
                else:
                    broadcast = False
                status = smartmeter.smeter.smctrl.nwk_mgr.permit_join(duration=duration, broadcast=broadcast)

            elif action == 'leave':
                status = smartmeter.smeter.smctrl.nwk_mgr.leave_network()

            elif action == 'setexpanid':
                expanid = common.getJsonArg("expanid", "")
                if not isinstance(expanid, str) or len(expanid) != 16 or not all(c in string.hexdigits for c in expanid):
                    return json.dumps({"status": -1, "errormsg": "Malformed input"})
                status = smartmeter.smeter.smctrl.nwk_mgr.set_extended_pan_id(expanid)

            else:
                return json.dumps({"status": -1, "errormsg": "Unsupported action"})

            if not status:
                return json.dumps({"status": -1, "errormsg": "Action failed"})
            return json.dumps({"status": 0})

        except Exception, ex:
            logger.print_trace(ex)
            return json.dumps({"status": -1, "errormsg": "Server error"})
Exemple #2
0
    def POST(self):
        web.header('Content-Type', 'application/json')
        try:
            status = False
            action = common.getJsonArg("action", "")
            if action == 'add':
                kwargs = {}
                # mandatory inputs
                device_class = common.getJsonArg("dev", None)
                if not isinstance(device_class, (int)):
                    return json.dumps({"status": -1, "errormsg": "Malformed device class input"})
                kwargs['dev'] = device_class
                ueg = common.getJsonArg("ueg", None)
                if not isinstance(ueg, (int)):
                    return json.dumps({"status": -1, "errormsg": "Malformed utility enrolment group input"})
                kwargs['ueg'] = ueg
                start_time = common.getJsonArg("start", None)
                if not isinstance(start_time, (int,long)):
                    return json.dumps({"status": -1, "errormsg": "Malformed start time input"})
                kwargs['start_time'] = start_time
                duration = common.getJsonArg("duration", None)
                if not isinstance(duration, (int)):
                    return json.dumps({"status": -1, "errormsg": "Malformed duration input"})
                kwargs['duration'] = duration
                criticality = common.getJsonArg("criticality", None)
                if not isinstance(criticality, (int)):
                    return json.dumps({"status": -1, "errormsg": "Malformed criticality input"})
                kwargs['criticality'] = criticality
                event_control = common.getJsonArg("ectrl", None)
                if not isinstance(event_control, (int)):
                    return json.dumps({"status": -1, "errormsg": "Malformed event control input"})
                kwargs['ectrl'] = event_control
                # optional inputs
                cto = common.getJsonArg("cto", None)
                if cto is not None:
                    if not isinstance(cto, (int)):
                        return json.dumps({"status": -1, "errormsg": "Malformed cooling temp offset input"})
                    else:
                        kwargs['cto'] = cto
                hto = common.getJsonArg("hto", None)
                if hto is not None:
                    if not isinstance(hto, (int)):
                        return json.dumps({"status": -1, "errormsg": "Malformed heating temp offset input"})
                    else:
                        kwargs['hto'] = hto
                ctsp = common.getJsonArg("ctsp", None)
                if ctsp is not None:
                    if not isinstance(ctsp, (int)):
                        return json.dumps({"status": -1, "errormsg": "Malformed cooling temp set point input"})
                    else:
                        kwargs['ctsp'] = ctsp
                htsp = common.getJsonArg("htsp", None)
                if htsp is not None:
                    if not isinstance(htsp, (int)):
                        return json.dumps({"status": -1, "errormsg": "Malformed heating temp set point input"})
                    else:
                        kwargs['htsp'] = htsp
                avgload = common.getJsonArg("avgload", None)
                if avgload is not None:
                    if not isinstance(avgload, (int)):
                        return json.dumps({"status": -1, "errormsg": "Malformed average load input"})
                    else:
                        kwargs['avgload'] = avgload
                dutycycle = common.getJsonArg("dutycycle", None)
                if dutycycle is not None:
                    if not isinstance(dutycycle, (int)):
                        return json.dumps({"status": -1, "errormsg": "Malformed duty cycle input"})
                    else:
                        kwargs['dutycycle'] = dutycycle
                # add event
                #d = logger.Logger("drlc.py")
                #d.log("going to add event")
                status = smartmeter.smeter.smctrl.drlc_mgr.add_event(**kwargs)
                #d.log("status %s", status)

            elif action == 'send':
                event_ids = common.getJsonArg("eids", None)
                if not isinstance(event_ids, list):
                    return json.dumps({"status": -1, "errormsg": "Malformed event ID input"})
                for eid in event_ids:
                    if not isinstance(eid, (int,long)):
                        return json.dumps({"status": -1, "errormsg": "Malformed event ID input"})
                nodes = common.getJsonArg("nodes", None)
                if not isinstance(nodes, list):
                    return json.dumps({"status": -1, "errormsg": "Malformed Node ID input"})
                for n in nodes:
                    if not isinstance(n, (int)):
                        return json.dumps({"status": -1, "errormsg": "Malformed Node ID input"})
                eps = common.getJsonArg("eps", None)
                if not isinstance(eps, list):
                    return json.dumps({"status": -1, "errormsg": "Malformed End Point input"})
                for ep in eps:
                    if not isinstance(ep, (int)):
                        return json.dumps({"status": -1, "errormsg": "Malformed End Point input"})
                if len(nodes) != len(eps):
                    return json.dumps({"status": -1, "errormsg": "Malformed Node ID and End Point input"})

                status = True
                for eid in event_ids:
                    for i in range(len(nodes)):
                        tmp_status = smartmeter.smeter.smctrl.drlc_mgr.send_event(eid, nodes[i], eps[i])
                        if not tmp_status:
                            status = False

            elif action == 'rm':
                event_ids = common.getJsonArg("eids", None)
                if not isinstance(event_ids, list):
                    return json.dumps({"status": -1, "errormsg": "Malformed event ID input"})
                for eid in event_ids:
                    if not isinstance(eid, (int,long)):
                        return json.dumps({"status": -1, "errormsg": "Malformed event ID input"})
                status = True
                for eid in event_ids:
                    tmp_status = smartmeter.smeter.smctrl.drlc_mgr.rm_event(eid)
                    if not tmp_status:
                        status = False

            elif action == 'clear':
                status = smartmeter.smeter.smctrl.drlc_mgr.rm_all_events()

            else:
                return json.dumps({"status": -1, "errormsg": "Unsupported action"})

            if not status:
                return json.dumps({"status": -1, "errormsg": "Action failed"})
            return json.dumps({"status": 0})

        except Exception, ex:
            logger.print_trace(ex)
            return json.dumps({"status": -1, "errormsg": "Server error"})
Exemple #3
0
    def POST(self):
        web.header("Content-Type", "application/json")
        try:
            status = False
            action = common.getJsonArg("action", "")
            if action == "set":
                kwargs = {}
                msg_ctrl = common.getJsonArg("ctrl", None)
                if not isinstance(msg_ctrl, (int)):
                    return json.dumps({"status": -1, "errormsg": "Malformed message control input"})
                kwargs["msg_ctrl"] = msg_ctrl
                start_time = common.getJsonArg("start", None)
                if not isinstance(start_time, (int, long)):
                    return json.dumps({"status": -1, "errormsg": "Malformed start time input"})
                kwargs["start_time"] = start_time
                duration = common.getJsonArg("duration", None)
                if not isinstance(duration, (int)):
                    return json.dumps({"status": -1, "errormsg": "Malformed duration input"})
                kwargs["duration"] = duration
                message_string = common.getJsonArg("message", None)
                # Need to check max string length?
                if not isinstance(message_string, str):
                    return json.dumps({"status": -1, "errormsg": "Malformed message input"})
                kwargs["message_string"] = message_string
                # add message
                # d = logger.Logger("msg.py")
                # d.log("going to add message")
                status = smartmeter.smeter.smctrl.msg_mgr.set_message(**kwargs)
                # d.log("status %s", status)

            elif action == "send":
                nodes = common.getJsonArg("nodes", None)
                if not isinstance(nodes, list):
                    return json.dumps({"status": -1, "errormsg": "Malformed Node ID input"})
                for n in nodes:
                    if not isinstance(n, (int)):
                        return json.dumps({"status": -1, "errormsg": "Malformed Node ID input"})
                eps = common.getJsonArg("eps", None)
                if not isinstance(eps, list):
                    return json.dumps({"status": -1, "errormsg": "Malformed End Point input"})
                for ep in eps:
                    if not isinstance(ep, (int)):
                        return json.dumps({"status": -1, "errormsg": "Malformed End Point input"})
                if len(nodes) != len(eps):
                    return json.dumps({"status": -1, "errormsg": "Malformed Node ID and End Point input"})

                status = True
                for i in range(len(nodes)):
                    tmp_status = smartmeter.smeter.smctrl.msg_mgr.display_message(nodes[i], eps[i])
                    if not tmp_status:
                        status = False

            elif action == "cancel":
                nodes = common.getJsonArg("nodes", None)
                if not isinstance(nodes, list):
                    return json.dumps({"status": -1, "errormsg": "Malformed Node ID input"})
                for n in nodes:
                    if not isinstance(n, (int)):
                        return json.dumps({"status": -1, "errormsg": "Malformed Node ID input"})
                eps = common.getJsonArg("eps", None)
                if not isinstance(eps, list):
                    return json.dumps({"status": -1, "errormsg": "Malformed End Point input"})
                for ep in eps:
                    if not isinstance(ep, (int)):
                        return json.dumps({"status": -1, "errormsg": "Malformed End Point input"})
                if len(nodes) != len(eps):
                    return json.dumps({"status": -1, "errormsg": "Malformed Node ID and End Point input"})

                status = True
                for i in range(len(nodes)):
                    tmp_status = smartmeter.smeter.smctrl.msg_mgr.cancel_message(nodes[i], eps[i])
                    if not tmp_status:
                        status = False

            elif action == "clear":
                status = smartmeter.smeter.smctrl.msg_mgr.rm_message()

            else:
                return json.dumps({"status": -1, "errormsg": "Unsupported action"})

            if not status:
                return json.dumps({"status": -1, "errormsg": "Action failed"})
            return json.dumps({"status": 0})

        except Exception, ex:
            logger.print_trace(ex)
            return json.dumps({"status": -1, "errormsg": "Server error"})