#!/usr/bin/python
from edge.com import fs
from edge import path_helpers as ph
import ujson
import os


_PIPELINE_SET_FILEPATH = '/etc/edge/setupstore/admin/actions/pipeline.json'

_UI_TYPE_VIDEOSOURCE = 'videosource'

with open(_PIPELINE_SET_FILEPATH, 'r') as json_file:
    pipeline_jnode_set = ujson.load(json_file)

pipelines_to_migrate = filter(lambda x: x['type'] == ph.PTYPE_LPR_RTSP, pipeline_jnode_set)

migrate_list = map(lambda x: x['id'], pipelines_to_migrate)

for pipeline_id in migrate_list:
    ui_jnode_filepath = ph.get_ui_pipeline_jnode_filepath(pipeline_id, _UI_TYPE_VIDEOSOURCE, '0')
    if not os.path.isfile(ui_jnode_filepath):
        continue

    ui_jnode = fs.load_json2(ui_jnode_filepath)

    if not ui_jnode['rw']['videostream'].get('SkipRate', False):
        ui_jnode['rw']['videostream']['SkipRate'] = 1

    fs.write_json_file(ui_jnode, ui_jnode_filepath, pretty=True)

