{"id":26647245,"url":"https://github.com/offirmo/traffic-lights-simulator","last_synced_at":"2025-09-11T12:34:09.070Z","repository":{"id":66270995,"uuid":"87284220","full_name":"Offirmo/traffic-lights-simulator","owner":"Offirmo","description":"An exercise of modeling and testing the traffic lights at an intersection","archived":false,"fork":false,"pushed_at":"2017-04-05T12:48:23.000Z","size":82,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T23:51:38.297Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Offirmo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-05T08:15:30.000Z","updated_at":"2017-04-05T08:17:32.000Z","dependencies_parsed_at":"2023-06-13T21:15:34.545Z","dependency_job_id":null,"html_url":"https://github.com/Offirmo/traffic-lights-simulator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Offirmo/traffic-lights-simulator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Offirmo%2Ftraffic-lights-simulator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Offirmo%2Ftraffic-lights-simulator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Offirmo%2Ftraffic-lights-simulator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Offirmo%2Ftraffic-lights-simulator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Offirmo","download_url":"https://codeload.github.com/Offirmo/traffic-lights-simulator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Offirmo%2Ftraffic-lights-simulator/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265538507,"owners_count":23784610,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-24T23:51:28.439Z","updated_at":"2025-07-16T20:34:37.069Z","avatar_url":"https://github.com/Offirmo.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# traffic-lights-simulator\n\nAn exercise of modeling and testing the traffic lights at an intersection\n\nPlease find in this repo:\n- the code of a crude traffic lights controller\n- the corresponding unit tests\n- a simulator\n\nCode is written in TypeScript 2 / ES7 since it's what I'm using the most right now.\n\n## Design\n\nNOTE: Since it changes in every country, let's assume the traffic lights are going in cycles of:\n\u003e RED -\u003e GREEN -\u003e YELLOW -\u003e ...\n\nHere we want to simulate an intersection of 2 roads, North \u003c-\u003e South and East \u003c-\u003e West.\nWithout further instructions, we'll assume that the lights alternance is this trivial one:\n\n1. N\u0026S proceeding, E\u0026W stopped\n1. E\u0026W proceeding, N\u0026S stopped\n\nIt's obvious that we don't want to model the lights separately, because:\n\n1. the N/S lights are paired to the same state, same for E/W\n1. the state of N/S and E/W are also linked, when one is green or yellow, the other MUST be red, and so on.\n\nSo we'd rather model the intersection itself as having 2 traffic flows:\n\n1. N \u003c-\u003e S\n1. E \u003c-\u003e W\n\nproceeding and stopping alternatively.\n\n\n## Usage\n\n### traffic controller\nCode is written in TypeScript 2 / ES7 and exposed as JavaScript ES7 / CommonJS\n\n```bash\nnpm i -S Offirmo/traffic-lights-simulator   \u003c\u003c XXX I didn't publish it to npm to not waste their resources\n```\n\n```javascript\nimport * as TrafficLightsController from 'traffic-lights-simulator'\n\nconst controller = TrafficLightsController.factory({\n   onStateChange: console.log\n})\n```\n\nThe controller immediately starts running indefinitely on invocation, starting with N\u003c-\u003eS green.\nIt can be stopped with `controller.stop()`\n\nTo retrieve how the lights should be displayed for a flow according to a state:\n```javascript\nTrafficLightsController.getTrafficLightStateForFlow(controller.getState(), 'NS') // -\u003e red, yellow, green\nTrafficLightsController.getTrafficLightStateForFlow(controller.getState(), 'EW') // -\u003e red, yellow, green\n\n// or use this shortcut\ncontroller.getTrafficLightStateForFlow('NS') // -\u003e red, yellow, green\ncontroller.getTrafficLightStateForFlow('EW') // -\u003e red, yellow, green\n```\n\nBut this is more convenient to do it on transitions, hence the `onStateChange` callback:\n```javascript\nimport * as TrafficLightsController from 'traffic-lights-simulator'\n\nconst controller = TrafficLightsController.factory({\n   onStateChange: (state) =\u003e {\n      changeNSLightTo(TrafficLightsController.getTrafficLightStateForFlow(controller.getState(), 'NS'))\n      ...\n   }\n})\n```\n\nThe factory takes a bunch of options:\n```javascript\n\nconst controller = TrafficLightsController.factory({\n   // here are the default values\n   SWITCH_PERIOD_S: 5 * 60,  // lights alternance period, in s\n   SWITCH_NOTIFICATION_DURATION_S: 30,  // how much time before a change do we switch to yellow\n   debug: false,  // prints traces on state change\n   onStateChange: (state) =\u003e {} // called on state change\n})\n```\n\n### Simulator\nFrom the cloned repository, using node 7:\n\n```bash\nnpm install\nnpm run simulate  // defaulting to simulating 1800s = 30min\nnpm run simulate 500\n```\nShould give something like that:\n\n![simulator output in console](doc/simulator.png)\n\n\n## Contributing\nFrom the cloned repository, using node 7:\n\n```bash\nnpm install  (do NOT use yarn, sorry)\nnpm run dev\nnpm test\n```\nShould give something like that:\n\n![tests output in console](doc/tests.png)\n\n\n## Possible enhancements\n\nExtension of the model:\n- for safety reasons we should have an initial state and an error state\n- How about having different timings for each \"flows\" ? For example, N/S could be a bigger road than E/W, thus being allowed more time in \"pass\" state\n- how about more flows than just 2, maybe flows than can be allowed in parallel\n- How about linking to the state the pedestrian lights also ?\n  - this is trivial with our model\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foffirmo%2Ftraffic-lights-simulator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foffirmo%2Ftraffic-lights-simulator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foffirmo%2Ftraffic-lights-simulator/lists"}