def update_client_set(json_data): data = json.loads(json_data) for entry in data: # client cc, created = Client.objects.get_or_create(id=entry['client_id'], defaults={ 'short_name': entry['short_name'], 'description': entry['description'] }) if created: log.info(("created client: %s") % (entry['short_name'])) else: log.info(("updated client: %s") % (entry['short_name'])) # modbus config if hasattr(obj, 'modbusclient'): cc.modbusclient.ip_address = entry['modbus_ip.ip_address'] cc.modbusclient.port = entry['modbus_ip.port'] cc.modbusclient.protocol = entry['modbus_ip.protocol'] else: ModbusClient(modbus_client=cc, ip_address=entry['modbus_ip.ip_address'], port=entry['modbus_ip.port'], protocol=entry['modbus_ip.protocol'])
def write_data(self, variable_id, value): """ write value to single modbus register or coil """ if not self.variables[variable_id].writeable: return False if self.variables[variable_id].modbusvariable.function_code_read == 3: # write register if 0 <= self.variables[variable_id].modbusvariable.address <= 65535: if self._connect(): if self.variables[variable_id].get_bits_by_class( ) / 16 == 1: # just write the value to one register self.slave.write_register( self.variables[variable_id].modbusvariable.address, int(value), unit=self._unit_id) else: # encode it first self.slave.write_registers( self.variables[variable_id].modbusvariable.address, list(self.variables[variable_id].encode_value( value)), unit=self._unit_id) self._disconnect() return True else: log.info(("device with id: %d is now accessible") % (self.device.pk)) return False else: log.error('Modbus Address %d out of range' % self.variables[variable_id].modbusvariable.address) return False elif self.variables[ variable_id].modbusvariable.function_code_read == 1: # write coil if 0 <= self.variables[variable_id].modbusvariable.address <= 65535: if self._connect(): self.slave.write_coil( self.variables[variable_id].modbusvariable.address, bool(value), unit=self._unit_id) self._disconnect() return True else: log.info(("device with id: %d is now accessible") % (self.device.pk)) return False else: log.error('Modbus Address %d out of range' % self.variables[variable_id].modbusvariable.address) else: log.error( 'wrong type of function code %d' % self.variables[variable_id].modbusvariable.function_code_read) return False
def request_data(self): """ """ if not driver_ok: return None if not self._connect(): if self._device_not_accessible == -1: # log.error("device with id: %d is not accessible" % (self.device.pk)) self._device_not_accessible -= 1 return [] output = [] for register_block in self._variable_config: result = register_block.request_data(self.slave, self._unit_id) if result is None: self._disconnect() self._connect() result = register_block.request_data(self.slave, self._unit_id) if result is not None: for variable_id in register_block.variables: if self.variables[variable_id].update_value( result[variable_id], time()): redorded_data_element = self.variables[ variable_id].create_recorded_data_element() if redorded_data_element is not None: output.append(redorded_data_element) if self.variables[variable_id].accessible < 1: log.info(("variable with id: %d is now accessible") % (variable_id)) self.variables[variable_id].accessible = 1 else: for variable_id in register_block.variables: if self.variables[variable_id].accessible == -1: log.error(("variable with id: %d is not accessible") % (variable_id)) self.variables[variable_id].update_value(None, time()) self.variables[variable_id].accessible -= 1 # reset device not accessible status if self._device_not_accessible <= -1: log.info( ("device with id: %d is now accessible") % (self.device.pk)) if self._device_not_accessible < 1: self._device_not_accessible = 1 self._disconnect() return output
def update_variable_set(json_data): #json_data = file(json_file) #data = json.loads(json_data.read().decode("utf-8-sig")) #json_data.close() data = json.loads(json_data) # deactivate all variables for entry in data: # device cc, ccc = Device.objects.get_or_create(id = entry['device_id'],defaults={'short_name':entry['device_id'],'description':entry['device_id']}) # unit config uc, ucc = Unit.objects.get_or_create(unit = entry['unit'].replace(' ','')) # variable exist obj, created = Variable.objects.get_or_create(id=entry['id'], defaults={\ 'id':entry['id'],\ 'name':entry['variable_name'].replace(' ',''),\ 'description': entry['description'],\ 'device':cc,\ 'active':bool(entry['active']),\ 'writeable':bool(entry['writeable']),\ 'unit':uc,\ 'value_class':entry["value_class"].replace(' ',''),\ 'chart_line_color_id':entry["color_id"],\ 'short_name': entry["short_name"]\ }) if created: log.info(("created variable: %s") %(entry['variable_name'])) else: log.info(("updated variable: %s") %(entry['variable_name'])) obj.name = entry['variable_name'] obj.description = entry['description'] obj.device_id = entry['device_id'] obj.active = bool(entry['active']) obj.writeable = bool(entry['writeable']) obj.unit = uc obj.value_class = entry["value_class"].replace(' ','') obj.chart_line_color_id = entry["color_id"] obj.short_name = entry["short_name"] obj.save() if hasattr(obj,'modbusvariable'): obj.modbusvariable.address = entry["modbus_ip.address"] obj.modbusvariable.function_code_read = entry["modbus_ip.function_code_read"] obj.modbusvariable.save() else: ModbusVariable(modbus_variable=obj,address=entry["modbus_ip.address"],function_code_read=entry["modbus_ip.function_code_read"]).save()
def update_device_set(json_data): data = json.loads(json_data) for entry in data: # device cc, created = Device.objects.get_or_create(id = entry['device_id'],defaults={'short_name':entry['short_name'],'description':entry['description']}) if created: log.info(("created device: %s") %(entry['short_name'])) else: log.info(("updated device: %s") %(entry['short_name'])) # modbus config if hasattr(cc,'modbusdevice'): cc.modbusdevice.ip_address = entry['modbus_ip.ip_address'] cc.modbusdevice.port = entry['modbus_ip.port'] cc.modbusdevice.protocol = entry['modbus_ip.protocol'] else: ModbusDevice(modbus_device=cc,ip_address=entry['modbus_ip.ip_address'],port=entry['modbus_ip.port'],protocol=entry['modbus_ip.protocol'])
def update_HMI(json_data): data = json.loads(json_data) # Chart for chart in data['Chart']: co,created = Chart.objects.get_or_create(id=chart['id'],defaults={'title':chart['title'],'x_axis_label':chart['x_axis_label'],'x_axis_ticks':chart['x_axis_ticks'],'y_axis_label':chart['y_axis_label'],'y_axis_min':chart['y_axis_min'],'y_axis_max':chart['y_axis_max']}) if created: log.info(("created Chart: %s") %(chart['title'])) else: log.info(("updated Chart: %s") %(chart['title'])) co.title = chart['title'] co.x_axis_label = chart['x_axis_label'] co.x_axis_ticks = chart['x_axis_ticks'] co.y_axis_label = chart['y_axis_label'] co.y_axis_min = chart['y_axis_min'] co.y_axis_max = chart['y_axis_max'] co.save() co.variables.clear() co.variables.add(*Variable.objects.filter(pk__in=chart['variables'])) co.save() # Page for page in data['Page']: po,created = Page.objects.get_or_create(id = page['id'],defaults={'link_title':page['link_title'],'title':page['title']}) if created: log.info(("created Page: %s") %(page['link_title'])) else: log.info(("updated Page: %s") %(page['link_title'])) po.title = page['title'] po.link_title = page['link_title'] po.save() po.save() # ControlItem for item in data['ControlItem']: cio,created = ControlItem.objects.get_or_create(id = item['id'], defaults={'label':item['label'],'position':item['position'],'type':item['type'],'variable_id':item['variable']}) if created: log.info(("created ControlItem: %s") %(item['id'])) else: log.info(("updated ControlItem: %s") %(item['id'])) cio.label=item['label'] cio.position=item['position'] cio.type=item['type'] cio.variable_id=item['variable'] cio.save() cio.save() # ControlPanel for item in data['ControlPanel']: cpo,created = ControlPanel.objects.get_or_create(id = item['id'], defaults={'title':item['title']}) if created: log.info(("created ControlPanel: %s") %(item['id'])) else: log.info(("updated ControlPanel: %s") %(item['id'])) cpo.title = item['title'] cpo.save() cpo.items.clear() cpo.items.add(*ControlItem.objects.filter(pk__in=item['items'])) cpo.save() # Widget for widget in data['Widget']: if widget['control_panel'] > 0: wo,created = Widget.objects.get_or_create(id = widget['id'], defaults={'title':widget['title'],'page_id':widget['page'],'col':widget['col'],'row':widget['row'],'size':widget['size'],'control_panel_id':widget['control_panel']}) if created: log.info(("created Widget: %s") %(widget['id'])) else: log.info(("updated Widget: %s") %(widget['id'])) wo.title = widget['title'] wo.page_id = widget['page'] wo.col = widget['col'] wo.row = widget['row'] wo.size = widget['size'] if widget['control_panel'] > 0: wo.control_panel_id = widget['control_panel'] wo.save() wo.save() # SlidingPanelMenu for item in data['SlidingPanelMenu']: spo,created = SlidingPanelMenu.objects.get_or_create(id = item['id'], defaults={'title':item['title'],'position':item['position'],'control_panel_id':item['control_panel']}) if created: log.info(("created SlidingPanelMenu: %s") %(item['id'])) else: log.info(("updated SlidingPanelMenu: %s") %(item['id'])) spo.title = item['title'] spo.position = item['position'] spo.control_panel_id = item['control_panel'] spo.save() spo.save()
def import_xml_config_file(filename): ''' read xml file content and write/update the model data ''' from xml.dom.minidom import parse with open(filename, "r") as file_: xml_doc = parse(file_) doc_node = xml_doc.documentElement doc_version = doc_node.getAttribute('version') objects = doc_node.getElementsByTagName('object') def _parse_field(): if field.hasAttribute('type'): _type = field.getAttribute('type') else: _type = 'string' field_name = field.getAttribute('name') values[field_name] = _cast(field.firstChild.nodeValue,_type) # read all objects _Devices = [] _Variables = [] _Units = [] _Colors = [] _DeviceWriteTask = [] for obj in objects: obj_name = obj.getAttribute('name') fields = obj.getElementsByTagName('field') values = {} if not obj.hasAttribute('id'): continue values['id'] = int(obj.getAttribute('id')) for field in fields: _parse_field() if obj_name.upper() in ['DEVICE']: _Devices.append(values) elif obj_name.upper() in ['VARIABLE']: _Variables.append(values) elif obj_name.upper() in ['UNIT']: _Units.append(values) elif obj_name.upper() in ['COLOR']: _Colors.append(values) elif obj_name.upper() in ['DEVICEWRITETASK']: _DeviceWriteTask.append(values) ## update/import Devices ################################################### for entry in _Devices: # Device (object) cc, created = Device.objects.get_or_create(pk = entry['id'],defaults={'id':entry['id'],'short_name':entry['name'],'description':entry['description'],'protocol':entry['protocol'],'active':entry['active']}) if created: log.info(("created device: %s") %(entry['name'])) else: cc.short_name = entry['name'] cc.description = entry['description'] cc.protocol = entry['protocol'] cc.active = entry['active'] cc.save() log.info(("updated device: %s (%d)") %(entry['name'],entry['id'])) # modbus config if entry.has_key('modbus.protocol') and entry.has_key('modbus.ip_address') and entry.has_key('modbus.port') and entry.has_key('modbus.unit_id'): # get protocol choice id protocol_choices = ModbusDevice._meta.get_field('protocol').choices for prtc in protocol_choices: if entry['modbus.protocol'] == prtc[1]: entry['modbus.protocol'] = prtc[0] if hasattr(cc,'modbusdevice'): cc.modbusdevice.ip_address = entry['modbus.ip_address'] cc.modbusdevice.port = entry['modbus.port'] cc.modbusdevice.unit_id = entry['modbus.unit_id'] cc.modbusdevice.protocol = entry['modbus.protocol'] cc.modbusdevice.save() else: mc = ModbusDevice(modbus_device=cc,ip_address=entry['modbus.ip_address'],port=entry['modbus.port'],protocol=entry['modbus.protocol'],unit_id=entry['modbus.unit_id']) mc.save() # Unit (object) for entry in _Units: # unit config uc, ucc = Unit.objects.get_or_create(pk = entry['id'],defaults={'id':entry['id'],'unit':entry['unit'],'description':entry['description'],'udunit':entry['udunit']}) if not created: uc.unit = entry['unit'] uc.description = entry['description'] uc.udunit = entry['udunit'] uc.save() # Color (object) for entry in _Colors: # unit config cc, ucc = Color.objects.get_or_create(pk = entry['id'],defaults={'id':entry['id'],'name':entry['name'],'R':entry['R'],'G':entry['G'],'B':entry['B']}) if not created: cc.name = entry['name'], cc.R = entry['R'] cc.G = entry['G'] cc.B = entry['B'] cc.save() # Variable (object) for entry in _Variables: vc, created = Variable.objects.get_or_create(pk=entry['id'], defaults={\ 'id':entry['id'],\ 'name':entry['name'],\ 'description': entry['description'],\ 'device_id':entry['device_id'],\ 'active':entry['active'],\ 'writeable':entry['writeable'],\ 'record':entry['record'],\ 'unit_id':entry['unit_id'],\ 'value_class':validate_value_class(entry["value_class"]) # TODO UNIXTIME ?? \ }) if created: log.info(("created variable: %s") %(entry['name'])) else: log.info(("updated variable: %s") %(entry['name'])) vc.name = entry['name'] vc.description = entry['description'] vc.device_id = entry['device_id'] vc.active =entry['active'] vc.writeable = entry['writeable'] vc.record = entry['record'] vc.unit_id = entry['unit_id'] vc.value_class = validate_value_class(entry["value_class"]) vc.save() if entry.has_key("hmi.chart_line_color_id"): vc.chart_line_color_id = entry["hmi.chart_line_color_id"] if entry.has_key("hmi.short_name"): vc.short_name = entry["hmi.short_name"] if entry.has_key("hmi.chart_line_thickness"): vc.chart_line_thickness = entry["hmi.chart_line_thickness"] vc.save() if hasattr(vc,'modbusvariable'): if entry.has_key("modbus.address"): vc.modbusvariable.address = entry["modbus.address"] if entry.has_key("modbus.function_code_read"): vc.modbusvariable.function_code_read = entry["modbus.function_code_read"] vc.modbusvariable.save() else: if entry.has_key("modbus.address") and entry.has_key("modbus.function_code_read"): ModbusVariable(modbus_variable=vc,address=entry["modbus.address"],function_code_read=entry["modbus.function_code_read"]).save() for entry in _DeviceWriteTask: # start if isinstance(entry['start'],basestring): # must be convertet from local datestr to datenum timestamp = time.mktime(datetime.datetime.strptime(entry['start'], "%d-%b-%Y %H:%M:%S").timetuple()) else: # is already datenum timestamp = entry['start'] # verify timestamp if timestamp < time.time(): continue # user user = User.objects.filter(username = entry['user']) if user: user = user.first() else: user = None # variable variable = Variable.objects.filter(name=entry['variable'],active=1,device__active=1) if variable: # check for duplicates dcwt = DeviceWriteTask.objects.filter(variable=variable.first(),value=entry['value'],user=user,start__range=(timestamp-2.5,timestamp+2.5)) if dcwt: continue # write to DB DeviceWriteTask(variable=variable.first(),value=entry['value'],user=user,start=timestamp).save()
def handle(self, *args, **options): if dv[1] >= 8: self.stderr.write( "this command is not supported in Django>=1.8\n please use python manage.py PyScadaDaemonHandler modbus {start | stop} instead\n", ending='') return else: self.stdout.write( "this command is depricated\n please use python manage.py PyScadaDaemonHandler modbus {start | stop} instead\n", ending='') if len(args) != 1: self.stdout.write( "usage: python manage.py PyScadaModbusDaemon start | stop | restart\n", ending='') else: if not os.path.exists(settings.PID_ROOT): os.makedirs(settings.PID_ROOT) mdaemon = MainDaemon( '%s%s' % (settings.PID_ROOT, settings.PYSCADA_MODBUS['pid_file_name'])) if 'start' == args[0]: log.info("try starting dataaquisition daemon") try: pf = file(mdaemon.pidfile, 'r') pid = int(pf.read().strip()) pf.close() except IOError: pid = None if pid: # Check For the existence of a unix pid. try: os.kill(pid, 0) except OSError: # daemon is dead delete file and mark taskprogress as failed tp = BackgroundTask.objects.filter(pid=pid).last() if tp: tp.failed = True tp.save() os.remove(mdaemon.pidfile) mdaemon.start() elif 'stop' == args[0]: log.info("try stopping dataaquisition daemon") try: pf = file(mdaemon.pidfile, 'r') pid = int(pf.read().strip()) pf.close() except IOError: pid = None if pid: wait_count = 0 tp = BackgroundTask.objects.filter(pid=pid).last() if tp: tp.stop_daemon = 1 tp.save() while (wait_count < 10 and not tp.done): tp = BackgroundTask.objects.filter(pid=pid).last() wait_count += 1 sleep(1) mdaemon.stop() log.notice("stopped dataaquisition daemon") elif 'restart' == args[0]: log.info("try restarting data aquisition daemon") try: pf = file(mdaemon.pidfile, 'r') pid = int(pf.read().strip()) pf.close() except IOError: pid = None if pid: wait_count = 0 tp = BackgroundTask.objects.filter(pid=pid).last() if tp: tp.stop_daemon = 1 tp.save() while (wait_count < 10 and not tp.done): tp = BackgroundTask.objects.filter(pid=pid).last() wait_count += 1 sleep(1) mdaemon.stop() mdaemon.start() else: self.stdout.write("Unknown command", ending='')